| 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 "bytes" |
| 9 "encoding/binary" |
| 10 "math" |
| 11 "testing" |
| 12 ) |
| 13 |
| 14 func verifyInputParser(t *testing.T, s string, expected []byte, handlesCount int
) { |
| 15 parser := &inputParser{} |
| 16 b, h := parser.Parse(s) |
| 17 if !bytes.Equal(b, expected) { |
| 18 t.Fatalf("unexpected byte slice after parsing %v: expected %v, g
ot %v", s, expected, b) |
| 19 } |
| 20 if len(h) != handlesCount { |
| 21 t.Fatalf("unexpected handles count after parsing %v: expected %v
, got %v", s, handlesCount, len(h)) |
| 22 } |
| 23 } |
| 24 |
| 25 func TestInputParser(t *testing.T) { |
| 26 var buf []byte |
| 27 buf = make([]byte, 1+2+4+8+1+1) |
| 28 buf[0] = 0x10 |
| 29 binary.LittleEndian.PutUint16(buf[1:], 65535) |
| 30 binary.LittleEndian.PutUint32(buf[3:], 65536) |
| 31 binary.LittleEndian.PutUint64(buf[7:], 0xFFFFFFFFFFFFFFFF) |
| 32 buf[15] = 0 |
| 33 buf[16] = 0xFF |
| 34 verifyInputParser(t, "[u1]0x10 [u2]65535 [u4]65536 [u8]0xFFFFFFFFFFFFFFF
F 0 0Xff", buf, 0) |
| 35 |
| 36 buf = make([]byte, 8+1+2+4) |
| 37 binary.LittleEndian.PutUint64(buf[0:], math.MaxUint64-0x800+1) |
| 38 buf[8] = math.MaxUint8 - 128 + 1 |
| 39 binary.LittleEndian.PutUint16(buf[9:], 0) |
| 40 binary.LittleEndian.PutUint32(buf[11:], math.MaxUint32-40+1) |
| 41 verifyInputParser(t, "[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40", buf, 0) |
| 42 |
| 43 buf = make([]byte, 1+1+1) |
| 44 buf[0] = 11 |
| 45 buf[1] = 0x80 |
| 46 buf[2] = 0 |
| 47 verifyInputParser(t, "[b]00001011 [b]10000000 \r [b]00000000", buf, 0) |
| 48 |
| 49 buf = make([]byte, 4+8) |
| 50 binary.LittleEndian.PutUint32(buf[0:], math.Float32bits(+.3e9)) |
| 51 binary.LittleEndian.PutUint64(buf[4:], math.Float64bits(-10.03)) |
| 52 verifyInputParser(t, "[f]+.3e9 [d]-10.03", buf, 0) |
| 53 |
| 54 buf = make([]byte, 4+1+8+1) |
| 55 binary.LittleEndian.PutUint32(buf[0:], 14) |
| 56 buf[4] = 0 |
| 57 binary.LittleEndian.PutUint64(buf[5:], 9) |
| 58 buf[13] = 0 |
| 59 verifyInputParser(t, "[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar",
buf, 0) |
| 60 |
| 61 buf = make([]byte, 8) |
| 62 binary.LittleEndian.PutUint64(buf[0:], 2) |
| 63 verifyInputParser(t, "[handles]50 [u8]2", buf, 50) |
| 64 } |
| OLD | NEW |