| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package tests |
| 6 |
| 7 import ( |
| 8 "encoding/binary" |
| 9 "fmt" |
| 10 "math" |
| 11 "strings" |
| 12 |
| 13 "mojo/public/go/system" |
| 14 ) |
| 15 |
| 16 // inputParser parses validation tests input format as described in |
| 17 // |mojo/public/cpp/bindings/tests/validation_test_input_parser.h| |
| 18 type inputParser struct { |
| 19 } |
| 20 |
| 21 type dataItem struct { |
| 22 Type string |
| 23 Value string |
| 24 } |
| 25 |
| 26 type pointerPlaceholder struct { |
| 27 Position int |
| 28 Size int |
| 29 } |
| 30 |
| 31 func (p *inputParser) parseToDataItems(s string) []dataItem { |
| 32 var items []dataItem |
| 33 s = strings.TrimSpace(s) |
| 34 for len(s) > 0 { |
| 35 // Parsing data item type. |
| 36 var itemType string |
| 37 if s[0] == '[' { |
| 38 closeBracket := strings.Index(s, "]") |
| 39 if closeBracket == -1 { |
| 40 panic("unmatched left [") |
| 41 } |
| 42 itemType = strings.TrimSpace(s[1:closeBracket]) |
| 43 s = strings.TrimSpace(s[closeBracket+1:]) |
| 44 } else { |
| 45 itemType = "u1" |
| 46 } |
| 47 |
| 48 // Parsing data item value. |
| 49 itemEnd := strings.IndexAny(s, "[ \t\n\r") |
| 50 if itemEnd == -1 { |
| 51 items = append(items, dataItem{itemType, strings.TrimSpa
ce(s)}) |
| 52 s = "" |
| 53 } else { |
| 54 items = append(items, dataItem{itemType, strings.TrimSpa
ce(s[:itemEnd])}) |
| 55 s = strings.TrimSpace(s[itemEnd:]) |
| 56 } |
| 57 } |
| 58 return items |
| 59 } |
| 60 |
| 61 // Parse parses a validation tests input string that has no comments. |
| 62 // Panics if input has errors. |
| 63 func (p *inputParser) Parse(s string) ([]byte, []system.Handle) { |
| 64 var bytes []byte |
| 65 var handles []system.Handle |
| 66 var buf [8]byte |
| 67 pointers := make(map[string]pointerPlaceholder) |
| 68 for _, item := range p.parseToDataItems(s) { |
| 69 switch item.Type { |
| 70 case "u1": |
| 71 var value uint8 |
| 72 fmt.Sscan(item.Value, &value) |
| 73 bytes = append(bytes, byte(value)) |
| 74 case "u2": |
| 75 var value uint16 |
| 76 fmt.Sscan(item.Value, &value) |
| 77 binary.LittleEndian.PutUint16(buf[:2], value) |
| 78 bytes = append(bytes, buf[:2]...) |
| 79 case "u4": |
| 80 var value uint32 |
| 81 fmt.Sscan(item.Value, &value) |
| 82 binary.LittleEndian.PutUint32(buf[:4], value) |
| 83 bytes = append(bytes, buf[:4]...) |
| 84 case "u8": |
| 85 var value uint64 |
| 86 fmt.Sscan(item.Value, &value) |
| 87 binary.LittleEndian.PutUint64(buf[:8], value) |
| 88 bytes = append(bytes, buf[:8]...) |
| 89 case "s1": |
| 90 var value int8 |
| 91 fmt.Sscan(item.Value, &value) |
| 92 bytes = append(bytes, byte(value)) |
| 93 case "s2": |
| 94 var value int16 |
| 95 fmt.Sscan(item.Value, &value) |
| 96 binary.LittleEndian.PutUint16(buf[:2], uint16(value)) |
| 97 bytes = append(bytes, buf[:2]...) |
| 98 case "s4": |
| 99 var value int32 |
| 100 fmt.Sscan(item.Value, &value) |
| 101 binary.LittleEndian.PutUint32(buf[:4], uint32(value)) |
| 102 bytes = append(bytes, buf[:4]...) |
| 103 case "s8": |
| 104 var value int64 |
| 105 fmt.Sscan(item.Value, &value) |
| 106 binary.LittleEndian.PutUint64(buf[:8], uint64(value)) |
| 107 bytes = append(bytes, buf[:8]...) |
| 108 case "b": |
| 109 var value byte |
| 110 for i := 0; i < 8; i++ { |
| 111 value <<= 1 |
| 112 if item.Value[i] == '1' { |
| 113 value++ |
| 114 } |
| 115 } |
| 116 bytes = append(bytes, value) |
| 117 case "f": |
| 118 var value float32 |
| 119 fmt.Sscan(item.Value, &value) |
| 120 binary.LittleEndian.PutUint32(buf[:4], math.Float32bits(
value)) |
| 121 bytes = append(bytes, buf[:4]...) |
| 122 case "d": |
| 123 var value float64 |
| 124 fmt.Sscan(item.Value, &value) |
| 125 binary.LittleEndian.PutUint64(buf[:8], math.Float64bits(
value)) |
| 126 bytes = append(bytes, buf[:8]...) |
| 127 case "dist4": |
| 128 pointers[item.Value] = pointerPlaceholder{len(bytes), 4} |
| 129 bytes = append(bytes, buf[:4]...) |
| 130 case "dist8": |
| 131 pointers[item.Value] = pointerPlaceholder{len(bytes), 8} |
| 132 bytes = append(bytes, buf[:8]...) |
| 133 case "anchr": |
| 134 placeholder := pointers[item.Value] |
| 135 dist := len(bytes) - placeholder.Position |
| 136 switch placeholder.Size { |
| 137 case 4: |
| 138 binary.LittleEndian.PutUint32(bytes[placeholder.
Position:], uint32(dist)) |
| 139 case 8: |
| 140 binary.LittleEndian.PutUint64(bytes[placeholder.
Position:], uint64(dist)) |
| 141 } |
| 142 delete(pointers, item.Value) |
| 143 case "handles": |
| 144 var value int |
| 145 fmt.Sscan(item.Value, &value) |
| 146 handles = make([]system.Handle, value) |
| 147 for i, _ := range handles { |
| 148 handles[i] = system.GetCore().AcquireNativeHandl
e(0) |
| 149 } |
| 150 default: |
| 151 panic(fmt.Sprintf("unsupported item type: %v", item.Type
)) |
| 152 } |
| 153 } |
| 154 if len(pointers) != 0 { |
| 155 panic(fmt.Sprintf("unmatched pointers: %v", pointers)) |
| 156 } |
| 157 return bytes, handles |
| 158 } |
| OLD | NEW |