|
17 | 17 | package trie |
18 | 18 |
|
19 | 19 | import ( |
20 | | - "encoding/hex" |
| 20 | + "bytes" |
21 | 21 | "testing" |
22 | | - |
23 | | - checker "gopkg.in/check.v1" |
24 | 22 | ) |
25 | 23 |
|
26 | | -func TestEncoding(t *testing.T) { checker.TestingT(t) } |
27 | | - |
28 | | -type TrieEncodingSuite struct{} |
29 | | - |
30 | | -var _ = checker.Suite(&TrieEncodingSuite{}) |
31 | | - |
32 | | -func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) { |
33 | | - // even compact encode |
34 | | - test1 := []byte{1, 2, 3, 4, 5} |
35 | | - res1 := compactEncode(test1) |
36 | | - c.Assert(res1, checker.DeepEquals, []byte("\x11\x23\x45")) |
37 | | - |
38 | | - // odd compact encode |
39 | | - test2 := []byte{0, 1, 2, 3, 4, 5} |
40 | | - res2 := compactEncode(test2) |
41 | | - c.Assert(res2, checker.DeepEquals, []byte("\x00\x01\x23\x45")) |
42 | | - |
43 | | - //odd terminated compact encode |
44 | | - test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} |
45 | | - res3 := compactEncode(test3) |
46 | | - c.Assert(res3, checker.DeepEquals, []byte("\x20\x0f\x1c\xb8")) |
47 | | - |
48 | | - // even terminated compact encode |
49 | | - test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16} |
50 | | - res4 := compactEncode(test4) |
51 | | - c.Assert(res4, checker.DeepEquals, []byte("\x3f\x1c\xb8")) |
52 | | -} |
53 | | - |
54 | | -func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) { |
55 | | - exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} |
56 | | - res := compactHexDecode([]byte("verb")) |
57 | | - c.Assert(res, checker.DeepEquals, exp) |
58 | | -} |
59 | | - |
60 | | -func (s *TrieEncodingSuite) TestCompactHexEncode(c *checker.C) { |
61 | | - exp := []byte("verb") |
62 | | - res := compactHexEncode([]byte{7, 6, 6, 5, 7, 2, 6, 2, 16}) |
63 | | - c.Assert(res, checker.DeepEquals, exp) |
64 | | -} |
65 | | - |
66 | | -func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) { |
67 | | - // odd compact decode |
68 | | - exp := []byte{1, 2, 3, 4, 5} |
69 | | - res := compactDecode([]byte("\x11\x23\x45")) |
70 | | - c.Assert(res, checker.DeepEquals, exp) |
71 | | - |
72 | | - // even compact decode |
73 | | - exp = []byte{0, 1, 2, 3, 4, 5} |
74 | | - res = compactDecode([]byte("\x00\x01\x23\x45")) |
75 | | - c.Assert(res, checker.DeepEquals, exp) |
76 | | - |
77 | | - // even terminated compact decode |
78 | | - exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} |
79 | | - res = compactDecode([]byte("\x20\x0f\x1c\xb8")) |
80 | | - c.Assert(res, checker.DeepEquals, exp) |
81 | | - |
82 | | - // even terminated compact decode |
83 | | - exp = []byte{15, 1, 12, 11, 8 /*term*/, 16} |
84 | | - res = compactDecode([]byte("\x3f\x1c\xb8")) |
85 | | - c.Assert(res, checker.DeepEquals, exp) |
| 24 | +func TestHexCompact(t *testing.T) { |
| 25 | + tests := []struct{ hex, compact []byte }{ |
| 26 | + // empty keys, with and without terminator. |
| 27 | + {hex: []byte{}, compact: []byte{0x00}}, |
| 28 | + {hex: []byte{16}, compact: []byte{0x20}}, |
| 29 | + // odd length, no terminator |
| 30 | + {hex: []byte{1, 2, 3, 4, 5}, compact: []byte{0x11, 0x23, 0x45}}, |
| 31 | + // even length, no terminator |
| 32 | + {hex: []byte{0, 1, 2, 3, 4, 5}, compact: []byte{0x00, 0x01, 0x23, 0x45}}, |
| 33 | + // odd length, terminator |
| 34 | + {hex: []byte{15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x3f, 0x1c, 0xb8}}, |
| 35 | + // even length, terminator |
| 36 | + {hex: []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}, compact: []byte{0x20, 0x0f, 0x1c, 0xb8}}, |
| 37 | + } |
| 38 | + for _, test := range tests { |
| 39 | + if c := hexToCompact(test.hex); !bytes.Equal(c, test.compact) { |
| 40 | + t.Errorf("hexToCompact(%x) -> %x, want %x", test.hex, c, test.compact) |
| 41 | + } |
| 42 | + if h := compactToHex(test.compact); !bytes.Equal(h, test.hex) { |
| 43 | + t.Errorf("compactToHex(%x) -> %x, want %x", test.compact, h, test.hex) |
| 44 | + } |
| 45 | + } |
86 | 46 | } |
87 | 47 |
|
88 | | -func (s *TrieEncodingSuite) TestDecodeCompact(c *checker.C) { |
89 | | - exp, _ := hex.DecodeString("012345") |
90 | | - res := decodeCompact([]byte{0, 1, 2, 3, 4, 5}) |
91 | | - c.Assert(res, checker.DeepEquals, exp) |
92 | | - |
93 | | - exp, _ = hex.DecodeString("012345") |
94 | | - res = decodeCompact([]byte{0, 1, 2, 3, 4, 5, 16}) |
95 | | - c.Assert(res, checker.DeepEquals, exp) |
96 | | - |
97 | | - exp, _ = hex.DecodeString("abcdef") |
98 | | - res = decodeCompact([]byte{10, 11, 12, 13, 14, 15}) |
99 | | - c.Assert(res, checker.DeepEquals, exp) |
| 48 | +func TestHexKeybytes(t *testing.T) { |
| 49 | + tests := []struct{ key, hexIn, hexOut []byte }{ |
| 50 | + {key: []byte{}, hexIn: []byte{16}, hexOut: []byte{16}}, |
| 51 | + {key: []byte{}, hexIn: []byte{}, hexOut: []byte{16}}, |
| 52 | + { |
| 53 | + key: []byte{0x12, 0x34, 0x56}, |
| 54 | + hexIn: []byte{1, 2, 3, 4, 5, 6, 16}, |
| 55 | + hexOut: []byte{1, 2, 3, 4, 5, 6, 16}, |
| 56 | + }, |
| 57 | + { |
| 58 | + key: []byte{0x12, 0x34, 0x5}, |
| 59 | + hexIn: []byte{1, 2, 3, 4, 0, 5, 16}, |
| 60 | + hexOut: []byte{1, 2, 3, 4, 0, 5, 16}, |
| 61 | + }, |
| 62 | + { |
| 63 | + key: []byte{0x12, 0x34, 0x56}, |
| 64 | + hexIn: []byte{1, 2, 3, 4, 5, 6}, |
| 65 | + hexOut: []byte{1, 2, 3, 4, 5, 6, 16}, |
| 66 | + }, |
| 67 | + } |
| 68 | + for _, test := range tests { |
| 69 | + if h := keybytesToHex(test.key); !bytes.Equal(h, test.hexOut) { |
| 70 | + t.Errorf("keybytesToHex(%x) -> %x, want %x", test.key, h, test.hexOut) |
| 71 | + } |
| 72 | + if k := hexToKeybytes(test.hexIn); !bytes.Equal(k, test.key) { |
| 73 | + t.Errorf("hexToKeybytes(%x) -> %x, want %x", test.hexIn, k, test.key) |
| 74 | + } |
| 75 | + } |
100 | 76 | } |
101 | 77 |
|
102 | | -func BenchmarkCompactEncode(b *testing.B) { |
103 | | - |
104 | | - testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} |
| 78 | +func BenchmarkHexToCompact(b *testing.B) { |
| 79 | + testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/} |
105 | 80 | for i := 0; i < b.N; i++ { |
106 | | - compactEncode(testBytes) |
| 81 | + hexToCompact(testBytes) |
107 | 82 | } |
108 | 83 | } |
109 | 84 |
|
110 | | -func BenchmarkCompactDecode(b *testing.B) { |
111 | | - testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} |
| 85 | +func BenchmarkCompactToHex(b *testing.B) { |
| 86 | + testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/} |
112 | 87 | for i := 0; i < b.N; i++ { |
113 | | - compactDecode(testBytes) |
| 88 | + compactToHex(testBytes) |
114 | 89 | } |
115 | 90 | } |
116 | 91 |
|
117 | | -func BenchmarkCompactHexDecode(b *testing.B) { |
| 92 | +func BenchmarkKeybytesToHex(b *testing.B) { |
118 | 93 | testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} |
119 | 94 | for i := 0; i < b.N; i++ { |
120 | | - compactHexDecode(testBytes) |
| 95 | + keybytesToHex(testBytes) |
121 | 96 | } |
122 | 97 | } |
123 | 98 |
|
124 | | -func BenchmarkDecodeCompact(b *testing.B) { |
| 99 | +func BenchmarkHexToKeybytes(b *testing.B) { |
125 | 100 | testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} |
126 | 101 | for i := 0; i < b.N; i++ { |
127 | | - decodeCompact(testBytes) |
| 102 | + hexToKeybytes(testBytes) |
128 | 103 | } |
129 | 104 | } |
0 commit comments