1717package trie
1818
1919import (
20+ "encoding/hex"
21+ "testing"
22+
2023 checker "gopkg.in/check.v1"
2124)
2225
26+ func Test (t * testing.T ) { checker .TestingT (t ) }
27+
2328type TrieEncodingSuite struct {}
2429
2530var _ = checker .Suite (& TrieEncodingSuite {})
@@ -28,22 +33,22 @@ func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) {
2833 // even compact encode
2934 test1 := []byte {1 , 2 , 3 , 4 , 5 }
3035 res1 := CompactEncode (test1 )
31- c .Assert (res1 , checker .Equals , "\x11 \x23 \x45 " )
36+ c .Assert (res1 , checker .DeepEquals , [] byte ( "\x11 \x23 \x45 " ) )
3237
3338 // odd compact encode
3439 test2 := []byte {0 , 1 , 2 , 3 , 4 , 5 }
3540 res2 := CompactEncode (test2 )
36- c .Assert (res2 , checker .Equals , "\x00 \x01 \x23 \x45 " )
41+ c .Assert (res2 , checker .DeepEquals , [] byte ( "\x00 \x01 \x23 \x45 " ) )
3742
3843 //odd terminated compact encode
3944 test3 := []byte {0 , 15 , 1 , 12 , 11 , 8 /*term*/ , 16 }
4045 res3 := CompactEncode (test3 )
41- c .Assert (res3 , checker .Equals , "\x20 \x0f \x1c \xb8 " )
46+ c .Assert (res3 , checker .DeepEquals , [] byte ( "\x20 \x0f \x1c \xb8 " ) )
4247
4348 // even terminated compact encode
4449 test4 := []byte {15 , 1 , 12 , 11 , 8 /*term*/ , 16 }
4550 res4 := CompactEncode (test4 )
46- c .Assert (res4 , checker .Equals , "\x3f \x1c \xb8 " )
51+ c .Assert (res4 , checker .DeepEquals , [] byte ( "\x3f \x1c \xb8 " ) )
4752}
4853
4954func (s * TrieEncodingSuite ) TestCompactHexDecode (c * checker.C ) {
@@ -73,3 +78,48 @@ func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) {
7378 res = CompactDecode ([]byte ("\x3f \x1c \xb8 " ))
7479 c .Assert (res , checker .DeepEquals , exp )
7580}
81+
82+ func (s * TrieEncodingSuite ) TestDecodeCompact (c * checker.C ) {
83+ exp , _ := hex .DecodeString ("012345" )
84+ res := DecodeCompact ([]byte {0 , 1 , 2 , 3 , 4 , 5 })
85+ c .Assert (res , checker .DeepEquals , exp )
86+
87+ exp , _ = hex .DecodeString ("012345" )
88+ res = DecodeCompact ([]byte {0 , 1 , 2 , 3 , 4 , 5 , 16 })
89+ c .Assert (res , checker .DeepEquals , exp )
90+
91+ exp , _ = hex .DecodeString ("abcdef" )
92+ res = DecodeCompact ([]byte {10 , 11 , 12 , 13 , 14 , 15 })
93+ c .Assert (res , checker .DeepEquals , exp )
94+ }
95+
96+ func BenchmarkCompactEncode (b * testing.B ) {
97+
98+ testBytes := []byte {0 , 15 , 1 , 12 , 11 , 8 /*term*/ , 16 }
99+ for i := 0 ; i < b .N ; i ++ {
100+ CompactEncode (testBytes )
101+ }
102+ }
103+
104+ func BenchmarkCompactDecode (b * testing.B ) {
105+ testBytes := []byte {0 , 15 , 1 , 12 , 11 , 8 /*term*/ , 16 }
106+ for i := 0 ; i < b .N ; i ++ {
107+ CompactDecode (testBytes )
108+ }
109+ }
110+
111+ func BenchmarkCompactHexDecode (b * testing.B ) {
112+ testBytes := []byte {7 , 6 , 6 , 5 , 7 , 2 , 6 , 2 , 16 }
113+ for i := 0 ; i < b .N ; i ++ {
114+ CompactHexDecode (testBytes )
115+ }
116+
117+ }
118+
119+ func BenchmarkDecodeCompact (b * testing.B ) {
120+ testBytes := []byte {7 , 6 , 6 , 5 , 7 , 2 , 6 , 2 , 16 }
121+ for i := 0 ; i < b .N ; i ++ {
122+ DecodeCompact (testBytes )
123+ }
124+
125+ }
0 commit comments