encoding_test.go 1.6 KB
Newer Older
obscuren's avatar
obscuren committed
1
package trie
obscuren's avatar
obscuren committed
2 3

import (
4
	checker "gopkg.in/check.v1"
obscuren's avatar
obscuren committed
5 6
)

7 8 9 10 11 12
type TrieEncodingSuite struct{}

var _ = checker.Suite(&TrieEncodingSuite{})

func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) {
	// even compact encode
13
	test1 := []byte{1, 2, 3, 4, 5}
14 15
	res1 := CompactEncode(test1)
	c.Assert(res1, checker.Equals, "\x11\x23\x45")
obscuren's avatar
obscuren committed
16

17
	// odd compact encode
18
	test2 := []byte{0, 1, 2, 3, 4, 5}
19 20
	res2 := CompactEncode(test2)
	c.Assert(res2, checker.Equals, "\x00\x01\x23\x45")
obscuren's avatar
obscuren committed
21

22
	//odd terminated compact encode
23
	test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
24 25
	res3 := CompactEncode(test3)
	c.Assert(res3, checker.Equals, "\x20\x0f\x1c\xb8")
obscuren's avatar
obscuren committed
26

27
	// even terminated compact encode
28
	test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16}
29 30
	res4 := CompactEncode(test4)
	c.Assert(res4, checker.Equals, "\x3f\x1c\xb8")
obscuren's avatar
obscuren committed
31 32
}

33
func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) {
34
	exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
obscuren's avatar
obscuren committed
35
	res := CompactHexDecode("verb")
36
	c.Assert(res, checker.DeepEquals, exp)
obscuren's avatar
obscuren committed
37
}
Joey Zhou's avatar
Joey Zhou committed
38

39 40
func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) {
	// odd compact decode
41
	exp := []byte{1, 2, 3, 4, 5}
Joey Zhou's avatar
Joey Zhou committed
42
	res := CompactDecode("\x11\x23\x45")
43
	c.Assert(res, checker.DeepEquals, exp)
Joey Zhou's avatar
Joey Zhou committed
44

45
	// even compact decode
46
	exp = []byte{0, 1, 2, 3, 4, 5}
Joey Zhou's avatar
Joey Zhou committed
47
	res = CompactDecode("\x00\x01\x23\x45")
48
	c.Assert(res, checker.DeepEquals, exp)
Joey Zhou's avatar
Joey Zhou committed
49

50
	// even terminated compact decode
51
	exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
Joey Zhou's avatar
Joey Zhou committed
52
	res = CompactDecode("\x20\x0f\x1c\xb8")
53
	c.Assert(res, checker.DeepEquals, exp)
Joey Zhou's avatar
Joey Zhou committed
54

55
	// even terminated compact decode
56
	exp = []byte{15, 1, 12, 11, 8 /*term*/, 16}
Joey Zhou's avatar
Joey Zhou committed
57
	res = CompactDecode("\x3f\x1c\xb8")
58
	c.Assert(res, checker.DeepEquals, exp)
zelig's avatar
zelig committed
59
}