improve some test loop

Signed-off-by: xiekeyang <xiekeyang@huawei.com>
This commit is contained in:
xiekeyang 2016-11-16 17:58:59 +08:00
parent bd0aafb4fd
commit 1a8a447267

View file

@ -30,25 +30,21 @@ type TrieEncodingSuite struct{}
var _ = checker.Suite(&TrieEncodingSuite{}) var _ = checker.Suite(&TrieEncodingSuite{})
func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) { func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) {
for _, test := range []struct {
hexSlice []byte
exp []byte
}{
// even compact encode // even compact encode
test1 := []byte{1, 2, 3, 4, 5} {[]byte{1, 2, 3, 4, 5}, []byte("\x11\x23\x45")},
res1 := compactEncode(test1)
c.Assert(res1, checker.DeepEquals, []byte("\x11\x23\x45"))
// odd compact encode // odd compact encode
test2 := []byte{0, 1, 2, 3, 4, 5} {[]byte{0, 1, 2, 3, 4, 5}, []byte("\x00\x01\x23\x45")},
res2 := compactEncode(test2)
c.Assert(res2, checker.DeepEquals, []byte("\x00\x01\x23\x45"))
//odd terminated compact encode //odd terminated compact encode
test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} {[]byte{0, 15, 1, 12, 11, 8 /*term*/, 16}, []byte("\x20\x0f\x1c\xb8")},
res3 := compactEncode(test3)
c.Assert(res3, checker.DeepEquals, []byte("\x20\x0f\x1c\xb8"))
// even terminated compact encode // even terminated compact encode
test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16} {[]byte{15, 1, 12, 11, 8 /*term*/, 16}, []byte("\x3f\x1c\xb8")},
res4 := compactEncode(test4) } {
c.Assert(res4, checker.DeepEquals, []byte("\x3f\x1c\xb8")) c.Assert(compactEncode(test.hexSlice), checker.DeepEquals, test.exp)
}
} }
func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) { 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) { func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) {
for _, test := range []struct {
str []byte
exp []byte
}{
// odd compact decode // odd compact decode
exp := []byte{1, 2, 3, 4, 5} {[]byte("\x11\x23\x45"), []byte{1, 2, 3, 4, 5}},
res := compactDecode([]byte("\x11\x23\x45"))
c.Assert(res, checker.DeepEquals, exp)
// even compact decode // even compact decode
exp = []byte{0, 1, 2, 3, 4, 5} {[]byte("\x00\x01\x23\x45"), []byte{0, 1, 2, 3, 4, 5}},
res = compactDecode([]byte("\x00\x01\x23\x45"))
c.Assert(res, checker.DeepEquals, exp)
// even terminated compact decode // even terminated compact decode
exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} {[]byte("\x20\x0f\x1c\xb8"), []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 // even terminated compact decode
exp = []byte{15, 1, 12, 11, 8 /*term*/, 16} {[]byte("\x3f\x1c\xb8"), []byte{15, 1, 12, 11, 8 /*term*/, 16}},
res = compactDecode([]byte("\x3f\x1c\xb8")) } {
c.Assert(res, checker.DeepEquals, exp) c.Assert(compactDecode(test.str), checker.DeepEquals, test.exp)
}
} }
func (s *TrieEncodingSuite) TestDecodeCompact(c *checker.C) { func (s *TrieEncodingSuite) TestDecodeCompact(c *checker.C) {
exp, _ := hex.DecodeString("012345") for _, test := range []struct {
res := decodeCompact([]byte{0, 1, 2, 3, 4, 5}) str string
c.Assert(res, checker.DeepEquals, exp) key []byte
}{
exp, _ = hex.DecodeString("012345") {"012345", []byte{0, 1, 2, 3, 4, 5}},
res = decodeCompact([]byte{0, 1, 2, 3, 4, 5, 16}) {"012345", []byte{0, 1, 2, 3, 4, 5, 16}},
c.Assert(res, checker.DeepEquals, exp) {"abcdef", []byte{10, 11, 12, 13, 14, 15}},
} {
exp, _ = hex.DecodeString("abcdef") exp, err := hex.DecodeString(test.str)
res = decodeCompact([]byte{10, 11, 12, 13, 14, 15}) c.Assert(err, checker.Equals, nil)
c.Assert(res, checker.DeepEquals, exp) c.Assert(decodeCompact(test.key), checker.DeepEquals, exp)
}
} }
func BenchmarkCompactEncode(b *testing.B) { func BenchmarkCompactEncode(b *testing.B) {