mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 22:26:42 +00:00
improve some test loop
Signed-off-by: xiekeyang <xiekeyang@huawei.com>
This commit is contained in:
parent
bd0aafb4fd
commit
1a8a447267
1 changed files with 42 additions and 49 deletions
|
|
@ -30,25 +30,21 @@ type TrieEncodingSuite struct{}
|
|||
var _ = checker.Suite(&TrieEncodingSuite{})
|
||||
|
||||
func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) {
|
||||
// even compact encode
|
||||
test1 := []byte{1, 2, 3, 4, 5}
|
||||
res1 := compactEncode(test1)
|
||||
c.Assert(res1, checker.DeepEquals, []byte("\x11\x23\x45"))
|
||||
|
||||
// odd compact encode
|
||||
test2 := []byte{0, 1, 2, 3, 4, 5}
|
||||
res2 := compactEncode(test2)
|
||||
c.Assert(res2, checker.DeepEquals, []byte("\x00\x01\x23\x45"))
|
||||
|
||||
//odd terminated compact encode
|
||||
test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
|
||||
res3 := compactEncode(test3)
|
||||
c.Assert(res3, checker.DeepEquals, []byte("\x20\x0f\x1c\xb8"))
|
||||
|
||||
// even terminated compact encode
|
||||
test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16}
|
||||
res4 := compactEncode(test4)
|
||||
c.Assert(res4, checker.DeepEquals, []byte("\x3f\x1c\xb8"))
|
||||
for _, test := range []struct {
|
||||
hexSlice []byte
|
||||
exp []byte
|
||||
}{
|
||||
// even compact encode
|
||||
{[]byte{1, 2, 3, 4, 5}, []byte("\x11\x23\x45")},
|
||||
// odd compact encode
|
||||
{[]byte{0, 1, 2, 3, 4, 5}, []byte("\x00\x01\x23\x45")},
|
||||
//odd terminated compact encode
|
||||
{[]byte{0, 15, 1, 12, 11, 8 /*term*/, 16}, []byte("\x20\x0f\x1c\xb8")},
|
||||
// even terminated compact encode
|
||||
{[]byte{15, 1, 12, 11, 8 /*term*/, 16}, []byte("\x3f\x1c\xb8")},
|
||||
} {
|
||||
c.Assert(compactEncode(test.hexSlice), checker.DeepEquals, test.exp)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) {
|
||||
|
|
@ -64,39 +60,36 @@ func (s *TrieEncodingSuite) TestCompactHexEncode(c *checker.C) {
|
|||
}
|
||||
|
||||
func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) {
|
||||
// odd compact decode
|
||||
exp := []byte{1, 2, 3, 4, 5}
|
||||
res := compactDecode([]byte("\x11\x23\x45"))
|
||||
c.Assert(res, checker.DeepEquals, exp)
|
||||
|
||||
// even compact decode
|
||||
exp = []byte{0, 1, 2, 3, 4, 5}
|
||||
res = compactDecode([]byte("\x00\x01\x23\x45"))
|
||||
c.Assert(res, checker.DeepEquals, exp)
|
||||
|
||||
// even terminated compact decode
|
||||
exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
|
||||
res = compactDecode([]byte("\x20\x0f\x1c\xb8"))
|
||||
c.Assert(res, checker.DeepEquals, exp)
|
||||
|
||||
// even terminated compact decode
|
||||
exp = []byte{15, 1, 12, 11, 8 /*term*/, 16}
|
||||
res = compactDecode([]byte("\x3f\x1c\xb8"))
|
||||
c.Assert(res, checker.DeepEquals, exp)
|
||||
for _, test := range []struct {
|
||||
str []byte
|
||||
exp []byte
|
||||
}{
|
||||
// odd compact decode
|
||||
{[]byte("\x11\x23\x45"), []byte{1, 2, 3, 4, 5}},
|
||||
// even compact decode
|
||||
{[]byte("\x00\x01\x23\x45"), []byte{0, 1, 2, 3, 4, 5}},
|
||||
// even terminated compact decode
|
||||
{[]byte("\x20\x0f\x1c\xb8"), []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}},
|
||||
// even terminated compact decode
|
||||
{[]byte("\x3f\x1c\xb8"), []byte{15, 1, 12, 11, 8 /*term*/, 16}},
|
||||
} {
|
||||
c.Assert(compactDecode(test.str), checker.DeepEquals, test.exp)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TrieEncodingSuite) TestDecodeCompact(c *checker.C) {
|
||||
exp, _ := hex.DecodeString("012345")
|
||||
res := decodeCompact([]byte{0, 1, 2, 3, 4, 5})
|
||||
c.Assert(res, checker.DeepEquals, exp)
|
||||
|
||||
exp, _ = hex.DecodeString("012345")
|
||||
res = decodeCompact([]byte{0, 1, 2, 3, 4, 5, 16})
|
||||
c.Assert(res, checker.DeepEquals, exp)
|
||||
|
||||
exp, _ = hex.DecodeString("abcdef")
|
||||
res = decodeCompact([]byte{10, 11, 12, 13, 14, 15})
|
||||
c.Assert(res, checker.DeepEquals, exp)
|
||||
for _, test := range []struct {
|
||||
str string
|
||||
key []byte
|
||||
}{
|
||||
{"012345", []byte{0, 1, 2, 3, 4, 5}},
|
||||
{"012345", []byte{0, 1, 2, 3, 4, 5, 16}},
|
||||
{"abcdef", []byte{10, 11, 12, 13, 14, 15}},
|
||||
} {
|
||||
exp, err := hex.DecodeString(test.str)
|
||||
c.Assert(err, checker.Equals, nil)
|
||||
c.Assert(decodeCompact(test.key), checker.DeepEquals, exp)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCompactEncode(b *testing.B) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue