Converts bytes_test to checker

This commit is contained in:
Taylor Gerring 2014-11-11 19:38:23 +01:00
parent c24d143c9e
commit cff0d93105

View File

@ -1,23 +1,24 @@
package ethutil package ethutil
import ( import (
"bytes" checker "gopkg.in/check.v1"
"testing"
) )
func TestByteString(t *testing.T) { type BytesSuite struct{}
var _ = checker.Suite(&BytesSuite{})
func (s *BytesSuite) TestByteString(c *checker.C) {
var data Bytes var data Bytes
data = []byte{102, 111, 111} data = []byte{102, 111, 111}
exp := "foo" exp := "foo"
res := data.String() res := data.String()
if res != exp { c.Assert(res, checker.Equals, exp)
t.Errorf("Expected %s got %s", exp, res)
}
} }
/* /*
func TestDeleteFromByteSlice(t *testing.T) { func (s *BytesSuite) TestDeleteFromByteSlice(c *checker.C) {
data := []byte{1, 2, 3, 4} data := []byte{1, 2, 3, 4}
slice := []byte{1, 2, 3, 4} slice := []byte{1, 2, 3, 4}
exp := []byte{1, 4} exp := []byte{1, 4}
@ -27,7 +28,7 @@ func TestDeleteFromByteSlice(t *testing.T) {
} }
} }
func TestNumberToBytes(t *testing.T) { func (s *BytesSuite) TestNumberToBytes(c *checker.C) {
data := int(1) data := int(1)
exp := []byte{0, 0, 0, 0, 0, 0, 0, 1} exp := []byte{0, 0, 0, 0, 0, 0, 0, 1}
// TODO this fails. why? // TODO this fails. why?
@ -38,7 +39,7 @@ func TestNumberToBytes(t *testing.T) {
} }
*/ */
func TestBytesToNumber(t *testing.T) { func (s *BytesSuite) TestBytesToNumber(c *checker.C) {
datasmall := []byte{0, 1} datasmall := []byte{0, 1}
datalarge := []byte{1, 2, 3} datalarge := []byte{1, 2, 3}
expsmall := uint64(0) expsmall := uint64(0)
@ -46,15 +47,13 @@ func TestBytesToNumber(t *testing.T) {
// TODO this fails. why? // TODO this fails. why?
ressmall := BytesToNumber(datasmall) ressmall := BytesToNumber(datasmall)
reslarge := BytesToNumber(datalarge) reslarge := BytesToNumber(datalarge)
if ressmall != expsmall {
t.Errorf("Expected %d Got %d", expsmall, ressmall) c.Assert(ressmall, checker.DeepEquals, expsmall)
} c.Assert(reslarge, checker.DeepEquals, explarge)
if reslarge != explarge {
t.Errorf("Expected %d Got %d", explarge, reslarge)
}
} }
func TestReadVarInt(t *testing.T) { func (s *BytesSuite) TestReadVarInt(c *checker.C) {
data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8} data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8}
data4 := []byte{1, 2, 3, 4} data4 := []byte{1, 2, 3, 4}
data2 := []byte{1, 2} data2 := []byte{1, 2}
@ -70,24 +69,13 @@ func TestReadVarInt(t *testing.T) {
res2 := ReadVarInt(data2) res2 := ReadVarInt(data2)
res1 := ReadVarInt(data1) res1 := ReadVarInt(data1)
if res8 != exp8 { c.Assert(res8, checker.Equals, exp8)
t.Errorf("Expected %d | Got %d", exp8, res8) c.Assert(res4, checker.Equals, exp4)
} c.Assert(res2, checker.Equals, exp2)
c.Assert(res1, checker.Equals, exp1)
if res4 != exp4 {
t.Errorf("Expected %d | Got %d", exp4, res4)
}
if res2 != exp2 {
t.Errorf("Expected %d | Got %d", exp2, res2)
}
if res1 != exp1 {
t.Errorf("Expected %d | Got %d", exp1, res1)
}
} }
func TestBinaryLength(t *testing.T) { func (s *BytesSuite) TestBinaryLength(c *checker.C) {
data1 := 0 data1 := 0
data2 := 920987656789 data2 := 920987656789
@ -97,80 +85,64 @@ func TestBinaryLength(t *testing.T) {
res1 := BinaryLength(data1) res1 := BinaryLength(data1)
res2 := BinaryLength(data2) res2 := BinaryLength(data2)
if res1 != exp1 { c.Assert(res1, checker.Equals, exp1)
t.Errorf("Expected %d got %d", exp1, res1) c.Assert(res2, checker.Equals, exp2)
}
if res2 != exp2 {
t.Errorf("Expected %d got %d", exp2, res2)
}
} }
func TestCopyBytes(t *testing.T) { func (s *BytesSuite) TestCopyBytes(c *checker.C) {
data1 := []byte{1, 2, 3, 4} data1 := []byte{1, 2, 3, 4}
exp1 := []byte{1, 2, 3, 4} exp1 := []byte{1, 2, 3, 4}
res1 := CopyBytes(data1) res1 := CopyBytes(data1)
if bytes.Compare(res1, exp1) != 0 { c.Assert(res1, checker.DeepEquals, exp1)
t.Errorf("Expected % x got % x", exp1, res1)
}
} }
func TestIsHex(t *testing.T) { func (s *BytesSuite) TestIsHex(c *checker.C) {
data1 := "a9e67e" data1 := "a9e67e"
exp1 := false exp1 := false
res1 := IsHex(data1) res1 := IsHex(data1)
if exp1 != res1 { c.Assert(res1, checker.DeepEquals, exp1)
t.Errorf("Expected % x Got % x", exp1, res1)
}
data2 := "0xa9e67e00" data2 := "0xa9e67e00"
exp2 := true exp2 := true
res2 := IsHex(data2) res2 := IsHex(data2)
if exp2 != res2 { c.Assert(res2, checker.DeepEquals, exp2)
t.Errorf("Expected % x Got % x", exp2, res2)
}
} }
func TestParseDataString(t *testing.T) { func (s *BytesSuite) TestParseDataString(c *checker.C) {
data := ParseData("hello", "world", "0x0106") res1 := ParseData("hello", "world", "0x0106")
exp := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000" data := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000"
if bytes.Compare(data, Hex2Bytes(exp)) != 0 { exp1 := Hex2Bytes(data)
t.Error("Error parsing data") c.Assert(res1, checker.DeepEquals, exp1)
}
} }
func TestParseDataBytes(t *testing.T) { func (s *BytesSuite) TestParseDataBytes(c *checker.C) {
data := []byte{232, 212, 165, 16, 0} data1 := []byte{232, 212, 165, 16, 0}
exp := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 232, 212, 165, 16, 0} exp1 := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 232, 212, 165, 16, 0}
res1 := ParseData(data1)
c.Assert(res1, checker.DeepEquals, exp1)
res := ParseData(data)
if bytes.Compare(res, exp) != 0 {
t.Errorf("Expected %x got %x", exp, res)
}
} }
func TestLeftPadBytes(t *testing.T) { func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
val := []byte{1, 2, 3, 4} val1 := []byte{1, 2, 3, 4}
exp := []byte{0, 0, 0, 0, 1, 2, 3, 4} exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
resstd := LeftPadBytes(val, 8) res1 := LeftPadBytes(val1, 8)
if bytes.Compare(resstd, exp) != 0 { res2 := LeftPadBytes(val1, 2)
t.Errorf("Expected % x Got % x", exp, resstd)
}
resshrt := LeftPadBytes(val, 2) c.Assert(res1, checker.DeepEquals, exp1)
if bytes.Compare(resshrt, val) != 0 { c.Assert(res2, checker.DeepEquals, val1)
t.Errorf("Expected % x Got % x", exp, resshrt)
}
} }
func TestFormatData(t *testing.T) { func (s *BytesSuite) TestFormatData(c *checker.C) {
data1 := "" data1 := ""
data2 := "0xa9e67e00" data2 := "0xa9e67e00"
data3 := "a9e67e" data3 := "a9e67e"
data4 := "\"a9e67e00\"" data4 := "\"a9e67e00\""
exp1 := []byte{} // exp1 := []byte{}
exp2 := []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0xa9, 0xe6, 0x7e, 00} exp2 := []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0xa9, 0xe6, 0x7e, 00}
exp3 := []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00} exp3 := []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00}
exp4 := []byte{0x61, 0x39, 0x65, 0x36, 0x37, 0x65, 0x30, 0x30, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00} exp4 := []byte{0x61, 0x39, 0x65, 0x36, 0x37, 0x65, 0x30, 0x30, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00}
@ -180,64 +152,41 @@ func TestFormatData(t *testing.T) {
res3 := FormatData(data3) res3 := FormatData(data3)
res4 := FormatData(data4) res4 := FormatData(data4)
if bytes.Compare(res1, exp1) != 0 { c.Assert(res1, checker.IsNil)
t.Errorf("Expected % x Got % x", exp1, res1) c.Assert(res2, checker.DeepEquals, exp2)
} c.Assert(res3, checker.DeepEquals, exp3)
c.Assert(res4, checker.DeepEquals, exp4)
if bytes.Compare(res2, exp2) != 0 {
t.Errorf("Expected % x Got % x", exp2, res2)
}
if bytes.Compare(res3, exp3) != 0 {
t.Errorf("Expected % x Got % x", exp3, res3)
}
if bytes.Compare(res4, exp4) != 0 {
t.Errorf("Expected % x Got % x", exp4, res4)
}
} }
func TestRightPadBytes(t *testing.T) { func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
val := []byte{1, 2, 3, 4} val := []byte{1, 2, 3, 4}
exp := []byte{1, 2, 3, 4, 0, 0, 0, 0} exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}
resstd := RightPadBytes(val, 8) resstd := RightPadBytes(val, 8)
if bytes.Compare(resstd, exp) != 0 {
t.Errorf("Expected % x Got % x", exp, resstd)
}
resshrt := RightPadBytes(val, 2) resshrt := RightPadBytes(val, 2)
if bytes.Compare(resshrt, val) != 0 {
t.Errorf("Expected % x Got % x", exp, resshrt) c.Assert(resstd, checker.DeepEquals, exp)
} c.Assert(resshrt, checker.DeepEquals, val)
} }
func TestLeftPadString(t *testing.T) { func (s *BytesSuite) TestLeftPadString(c *checker.C) {
val := "test" val := "test"
exp := "\x30\x30\x30\x30" + val
resstd := LeftPadString(val, 8) resstd := LeftPadString(val, 8)
if resstd != "\x30\x30\x30\x30"+val {
t.Errorf("Expected % x Got % x", val, resstd)
}
resshrt := LeftPadString(val, 2) resshrt := LeftPadString(val, 2)
if resshrt != val { c.Assert(resstd, checker.Equals, exp)
t.Errorf("Expected % x Got % x", val, resshrt) c.Assert(resshrt, checker.Equals, val)
}
} }
func TestRightPadString(t *testing.T) { func (s *BytesSuite) TestRightPadString(c *checker.C) {
val := "test" val := "test"
exp := val + "\x30\x30\x30\x30"
resstd := RightPadString(val, 8) resstd := RightPadString(val, 8)
if resstd != val+"\x30\x30\x30\x30" {
t.Errorf("Expected % x Got % x", val, resstd)
}
resshrt := RightPadString(val, 2) resshrt := RightPadString(val, 2)
if resshrt != val {
t.Errorf("Expected % x Got % x", val, resshrt) c.Assert(resstd, checker.Equals, exp)
} c.Assert(resshrt, checker.Equals, val)
} }