encoding_test.go 3.79 KB
Newer Older
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
5 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16

obscuren's avatar
obscuren committed
17
package trie
obscuren's avatar
obscuren committed
18 19

import (
20 21 22
	"encoding/hex"
	"testing"

23
	checker "gopkg.in/check.v1"
obscuren's avatar
obscuren committed
24 25
)

Felix Lange's avatar
Felix Lange committed
26
func TestEncoding(t *testing.T) { checker.TestingT(t) }
27

28 29 30 31 32 33
type TrieEncodingSuite struct{}

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

func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) {
	// even compact encode
34
	test1 := []byte{1, 2, 3, 4, 5}
Felix Lange's avatar
Felix Lange committed
35
	res1 := compactEncode(test1)
36
	c.Assert(res1, checker.DeepEquals, []byte("\x11\x23\x45"))
obscuren's avatar
obscuren committed
37

38
	// odd compact encode
39
	test2 := []byte{0, 1, 2, 3, 4, 5}
Felix Lange's avatar
Felix Lange committed
40
	res2 := compactEncode(test2)
41
	c.Assert(res2, checker.DeepEquals, []byte("\x00\x01\x23\x45"))
obscuren's avatar
obscuren committed
42

43
	//odd terminated compact encode
44
	test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
Felix Lange's avatar
Felix Lange committed
45
	res3 := compactEncode(test3)
46
	c.Assert(res3, checker.DeepEquals, []byte("\x20\x0f\x1c\xb8"))
obscuren's avatar
obscuren committed
47

48
	// even terminated compact encode
49
	test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16}
Felix Lange's avatar
Felix Lange committed
50
	res4 := compactEncode(test4)
51
	c.Assert(res4, checker.DeepEquals, []byte("\x3f\x1c\xb8"))
obscuren's avatar
obscuren committed
52 53
}

54
func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) {
55
	exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
Felix Lange's avatar
Felix Lange committed
56
	res := compactHexDecode([]byte("verb"))
57
	c.Assert(res, checker.DeepEquals, exp)
obscuren's avatar
obscuren committed
58
}
Joey Zhou's avatar
Joey Zhou committed
59

60 61 62 63 64 65
func (s *TrieEncodingSuite) TestCompactHexEncode(c *checker.C) {
	exp := []byte("verb")
	res := compactHexEncode([]byte{7, 6, 6, 5, 7, 2, 6, 2, 16})
	c.Assert(res, checker.DeepEquals, exp)
}

66 67
func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) {
	// odd compact decode
68
	exp := []byte{1, 2, 3, 4, 5}
Felix Lange's avatar
Felix Lange committed
69
	res := compactDecode([]byte("\x11\x23\x45"))
70
	c.Assert(res, checker.DeepEquals, exp)
Joey Zhou's avatar
Joey Zhou committed
71

72
	// even compact decode
73
	exp = []byte{0, 1, 2, 3, 4, 5}
Felix Lange's avatar
Felix Lange committed
74
	res = compactDecode([]byte("\x00\x01\x23\x45"))
75
	c.Assert(res, checker.DeepEquals, exp)
Joey Zhou's avatar
Joey Zhou committed
76

77
	// even terminated compact decode
78
	exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
Felix Lange's avatar
Felix Lange committed
79
	res = compactDecode([]byte("\x20\x0f\x1c\xb8"))
80
	c.Assert(res, checker.DeepEquals, exp)
Joey Zhou's avatar
Joey Zhou committed
81

82
	// even terminated compact decode
83
	exp = []byte{15, 1, 12, 11, 8 /*term*/, 16}
Felix Lange's avatar
Felix Lange committed
84
	res = compactDecode([]byte("\x3f\x1c\xb8"))
85
	c.Assert(res, checker.DeepEquals, exp)
zelig's avatar
zelig committed
86
}
87 88 89

func (s *TrieEncodingSuite) TestDecodeCompact(c *checker.C) {
	exp, _ := hex.DecodeString("012345")
Felix Lange's avatar
Felix Lange committed
90
	res := decodeCompact([]byte{0, 1, 2, 3, 4, 5})
91 92 93
	c.Assert(res, checker.DeepEquals, exp)

	exp, _ = hex.DecodeString("012345")
Felix Lange's avatar
Felix Lange committed
94
	res = decodeCompact([]byte{0, 1, 2, 3, 4, 5, 16})
95 96 97
	c.Assert(res, checker.DeepEquals, exp)

	exp, _ = hex.DecodeString("abcdef")
Felix Lange's avatar
Felix Lange committed
98
	res = decodeCompact([]byte{10, 11, 12, 13, 14, 15})
99 100 101 102 103 104 105
	c.Assert(res, checker.DeepEquals, exp)
}

func BenchmarkCompactEncode(b *testing.B) {

	testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
	for i := 0; i < b.N; i++ {
Felix Lange's avatar
Felix Lange committed
106
		compactEncode(testBytes)
107 108 109 110 111 112
	}
}

func BenchmarkCompactDecode(b *testing.B) {
	testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
	for i := 0; i < b.N; i++ {
Felix Lange's avatar
Felix Lange committed
113
		compactDecode(testBytes)
114 115 116 117 118 119
	}
}

func BenchmarkCompactHexDecode(b *testing.B) {
	testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
	for i := 0; i < b.N; i++ {
Felix Lange's avatar
Felix Lange committed
120
		compactHexDecode(testBytes)
121 122 123 124 125 126
	}
}

func BenchmarkDecodeCompact(b *testing.B) {
	testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
	for i := 0; i < b.N; i++ {
Felix Lange's avatar
Felix Lange committed
127
		decodeCompact(testBytes)
128 129
	}
}