diff options
-rw-r--r-- | ethutil/big_test.go | 73 | ||||
-rw-r--r-- | ethutil/bytes_test.go | 195 | ||||
-rw-r--r-- | ethutil/common.go | 1 | ||||
-rw-r--r-- | ethutil/common_test.go | 73 | ||||
-rw-r--r-- | ethutil/main_test.go | 8 | ||||
-rw-r--r-- | ethutil/path_test.go | 51 | ||||
-rw-r--r-- | ethutil/rand_test.go | 17 | ||||
-rw-r--r-- | ethutil/size_test.go | 23 | ||||
-rw-r--r-- | ethutil/value_test.go | 85 | ||||
-rw-r--r-- | state/main_test.go | 8 | ||||
-rw-r--r-- | state/state_test.go | 67 | ||||
-rw-r--r-- | trie/encoding_test.go | 67 | ||||
-rw-r--r-- | trie/main_test.go | 8 | ||||
-rw-r--r-- | trie/trie_test.go | 277 | ||||
-rw-r--r-- | vm/vm_test.go | 14 |
15 files changed, 661 insertions, 306 deletions
diff --git a/ethutil/big_test.go b/ethutil/big_test.go new file mode 100644 index 000000000..bf3c96c6d --- /dev/null +++ b/ethutil/big_test.go @@ -0,0 +1,73 @@ +package ethutil + +import ( + "bytes" + "testing" +) + +func TestMisc(t *testing.T) { + a := Big("10") + b := Big("57896044618658097711785492504343953926634992332820282019728792003956564819968") + c := []byte{1, 2, 3, 4} + z := BitTest(a, 1) + + if z != true { + t.Error("Expected true got", z) + } + + U256(a) + S256(a) + + U256(b) + S256(b) + + BigD(c) +} + +func TestBigMax(t *testing.T) { + a := Big("10") + b := Big("5") + + max1 := BigMax(a, b) + if max1 != a { + t.Errorf("Expected %d got %d", a, max1) + } + + max2 := BigMax(b, a) + if max2 != a { + t.Errorf("Expected %d got %d", a, max2) + } +} + +func TestBigMin(t *testing.T) { + a := Big("10") + b := Big("5") + + min1 := BigMin(a, b) + if min1 != b { + t.Errorf("Expected %d got %d", b, min1) + } + + min2 := BigMin(b, a) + if min2 != b { + t.Errorf("Expected %d got %d", b, min2) + } +} + +func TestBigCopy(t *testing.T) { + a := Big("10") + b := BigCopy(a) + c := Big("1000000000000") + y := BigToBytes(b, 16) + ybytes := []byte{0, 10} + z := BigToBytes(c, 16) + zbytes := []byte{232, 212, 165, 16, 0} + + if bytes.Compare(y, ybytes) != 0 { + t.Error("Got", ybytes) + } + + if bytes.Compare(z, zbytes) != 0 { + t.Error("Got", zbytes) + } +} diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index 381efe7a2..179a8c7ef 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -1,14 +1,193 @@ package ethutil import ( - "bytes" - "testing" + checker "gopkg.in/check.v1" ) -func TestParseData(t *testing.T) { - data := ParseData("hello", "world", "0x0106") - exp := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000" - if bytes.Compare(data, Hex2Bytes(exp)) != 0 { - t.Error("Error parsing data") - } +type BytesSuite struct{} + +var _ = checker.Suite(&BytesSuite{}) + +func (s *BytesSuite) TestByteString(c *checker.C) { + var data Bytes + data = []byte{102, 111, 111} + exp := "foo" + res := data.String() + + c.Assert(res, checker.Equals, exp) +} + +/* +func (s *BytesSuite) TestDeleteFromByteSlice(c *checker.C) { + data := []byte{1, 2, 3, 4} + slice := []byte{1, 2, 3, 4} + exp := []byte{1, 4} + res := DeleteFromByteSlice(data, slice) + + c.Assert(res, checker.DeepEquals, exp) +} + +*/ +func (s *BytesSuite) TestNumberToBytes(c *checker.C) { + // data1 := int(1) + // res1 := NumberToBytes(data1, 16) + // c.Check(res1, checker.Panics) + + var data2 float64 = 3.141592653 + exp2 := []byte{0xe9, 0x38} + res2 := NumberToBytes(data2, 16) + c.Assert(res2, checker.DeepEquals, exp2) +} + +func (s *BytesSuite) TestBytesToNumber(c *checker.C) { + datasmall := []byte{0xe9, 0x38, 0xe9, 0x38} + datalarge := []byte{0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38} + + var expsmall uint64 = 0xe938e938 + var explarge uint64 = 0x0 + + ressmall := BytesToNumber(datasmall) + reslarge := BytesToNumber(datalarge) + + c.Assert(ressmall, checker.Equals, expsmall) + c.Assert(reslarge, checker.Equals, explarge) + +} + +func (s *BytesSuite) TestReadVarInt(c *checker.C) { + data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8} + data4 := []byte{1, 2, 3, 4} + data2 := []byte{1, 2} + data1 := []byte{1} + + exp8 := uint64(72623859790382856) + exp4 := uint64(16909060) + exp2 := uint64(258) + exp1 := uint64(1) + + res8 := ReadVarInt(data8) + res4 := ReadVarInt(data4) + res2 := ReadVarInt(data2) + res1 := ReadVarInt(data1) + + c.Assert(res8, checker.Equals, exp8) + c.Assert(res4, checker.Equals, exp4) + c.Assert(res2, checker.Equals, exp2) + c.Assert(res1, checker.Equals, exp1) +} + +func (s *BytesSuite) TestBinaryLength(c *checker.C) { + data1 := 0 + data2 := 920987656789 + + exp1 := 0 + exp2 := 5 + + res1 := BinaryLength(data1) + res2 := BinaryLength(data2) + + c.Assert(res1, checker.Equals, exp1) + c.Assert(res2, checker.Equals, exp2) +} + +func (s *BytesSuite) TestCopyBytes(c *checker.C) { + data1 := []byte{1, 2, 3, 4} + exp1 := []byte{1, 2, 3, 4} + res1 := CopyBytes(data1) + c.Assert(res1, checker.DeepEquals, exp1) +} + +func (s *BytesSuite) TestIsHex(c *checker.C) { + data1 := "a9e67e" + exp1 := false + res1 := IsHex(data1) + c.Assert(res1, checker.DeepEquals, exp1) + + data2 := "0xa9e67e00" + exp2 := true + res2 := IsHex(data2) + c.Assert(res2, checker.DeepEquals, exp2) + +} + +func (s *BytesSuite) TestParseDataString(c *checker.C) { + res1 := ParseData("hello", "world", "0x0106") + data := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000" + exp1 := Hex2Bytes(data) + c.Assert(res1, checker.DeepEquals, exp1) +} + +func (s *BytesSuite) TestParseDataBytes(c *checker.C) { + data1 := []byte{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) + +} + +func (s *BytesSuite) TestLeftPadBytes(c *checker.C) { + val1 := []byte{1, 2, 3, 4} + exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4} + + res1 := LeftPadBytes(val1, 8) + res2 := LeftPadBytes(val1, 2) + + c.Assert(res1, checker.DeepEquals, exp1) + c.Assert(res2, checker.DeepEquals, val1) +} + +func (s *BytesSuite) TestFormatData(c *checker.C) { + data1 := "" + data2 := "0xa9e67e00" + data3 := "a9e67e" + data4 := "\"a9e67e00\"" + + // 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} + 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} + + res1 := FormatData(data1) + res2 := FormatData(data2) + res3 := FormatData(data3) + res4 := FormatData(data4) + + c.Assert(res1, checker.IsNil) + c.Assert(res2, checker.DeepEquals, exp2) + c.Assert(res3, checker.DeepEquals, exp3) + c.Assert(res4, checker.DeepEquals, exp4) +} + +func (s *BytesSuite) TestRightPadBytes(c *checker.C) { + val := []byte{1, 2, 3, 4} + exp := []byte{1, 2, 3, 4, 0, 0, 0, 0} + + resstd := RightPadBytes(val, 8) + resshrt := RightPadBytes(val, 2) + + c.Assert(resstd, checker.DeepEquals, exp) + c.Assert(resshrt, checker.DeepEquals, val) +} + +func (s *BytesSuite) TestLeftPadString(c *checker.C) { + val := "test" + exp := "\x30\x30\x30\x30" + val + + resstd := LeftPadString(val, 8) + resshrt := LeftPadString(val, 2) + + c.Assert(resstd, checker.Equals, exp) + c.Assert(resshrt, checker.Equals, val) +} + +func (s *BytesSuite) TestRightPadString(c *checker.C) { + val := "test" + exp := val + "\x30\x30\x30\x30" + + resstd := RightPadString(val, 8) + resshrt := RightPadString(val, 2) + + c.Assert(resstd, checker.Equals, exp) + c.Assert(resshrt, checker.Equals, val) } diff --git a/ethutil/common.go b/ethutil/common.go index e60f237cf..0a29cac6c 100644 --- a/ethutil/common.go +++ b/ethutil/common.go @@ -66,6 +66,7 @@ func CurrencyToString(num *big.Int) string { denom = "Ada" } + // TODO add comment clarifying expected behavior if len(fin.String()) > 5 { return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom) } diff --git a/ethutil/common_test.go b/ethutil/common_test.go index 2667eaf3a..056676765 100644 --- a/ethutil/common_test.go +++ b/ethutil/common_test.go @@ -2,18 +2,55 @@ package ethutil import ( "math/big" + "os" "testing" ) +func TestOS(t *testing.T) { + res := IsWindows() + + if res && (os.PathSeparator != '\\' || os.PathListSeparator != ';') { + t.Error("IsWindows is", res, "but path is", os.PathSeparator) + } + + if !res && (os.PathSeparator == '\\' && os.PathListSeparator == ';') { + t.Error("IsWindows is", res, "but path is", os.PathSeparator) + } +} + +func TestWindonziePath(t *testing.T) { + path := "/opt/eth/test/file.ext" + res := WindonizePath(path) + iswindowspath := os.PathSeparator == '\\' + + if !iswindowspath && string(res[0]) != "/" { + t.Error("Got", res) + } + + if iswindowspath && string(res[0]) == "/" { + t.Error("Got", res) + } +} + func TestCommon(t *testing.T) { + douglas := CurrencyToString(BigPow(10, 43)) + einstein := CurrencyToString(BigPow(10, 22)) ether := CurrencyToString(BigPow(10, 19)) finney := CurrencyToString(BigPow(10, 16)) szabo := CurrencyToString(BigPow(10, 13)) - vito := CurrencyToString(BigPow(10, 10)) - turing := CurrencyToString(BigPow(10, 7)) - eins := CurrencyToString(BigPow(10, 4)) + shannon := CurrencyToString(BigPow(10, 10)) + babbage := CurrencyToString(BigPow(10, 7)) + ada := CurrencyToString(BigPow(10, 4)) wei := CurrencyToString(big.NewInt(10)) + if douglas != "10 Douglas" { + t.Error("Got", douglas) + } + + if einstein != "10 Einstein" { + t.Error("Got", einstein) + } + if ether != "10 Ether" { t.Error("Got", ether) } @@ -26,19 +63,37 @@ func TestCommon(t *testing.T) { t.Error("Got", szabo) } - if vito != "10 Shannon" { - t.Error("Got", vito) + if shannon != "10 Shannon" { + t.Error("Got", shannon) } - if turing != "10 Babbage" { - t.Error("Got", turing) + if babbage != "10 Babbage" { + t.Error("Got", babbage) } - if eins != "10 Ada" { - t.Error("Got", eins) + if ada != "10 Ada" { + t.Error("Got", ada) } if wei != "10 Wei" { t.Error("Got", wei) } } + +func TestLarge(t *testing.T) { + douglaslarge := CurrencyToString(BigPow(100000000, 43)) + adalarge := CurrencyToString(BigPow(100000000, 4)) + weilarge := CurrencyToString(big.NewInt(100000000)) + + if douglaslarge != "10000E298 Douglas" { + t.Error("Got", douglaslarge) + } + + if adalarge != "10000E7 Einstein" { + t.Error("Got", adalarge) + } + + if weilarge != "100 Babbage" { + t.Error("Got", weilarge) + } +} diff --git a/ethutil/main_test.go b/ethutil/main_test.go new file mode 100644 index 000000000..94f34677d --- /dev/null +++ b/ethutil/main_test.go @@ -0,0 +1,8 @@ +package ethutil + +import ( + checker "gopkg.in/check.v1" + "testing" +) + +func Test(t *testing.T) { checker.TestingT(t) } diff --git a/ethutil/path_test.go b/ethutil/path_test.go new file mode 100644 index 000000000..908c94ee7 --- /dev/null +++ b/ethutil/path_test.go @@ -0,0 +1,51 @@ +package ethutil + +import ( + // "os" + "testing" +) + +func TestGoodFile(t *testing.T) { + goodpath := "~/goethereumtest.pass" + path := ExpandHomePath(goodpath) + contentstring := "3.14159265358979323846" + + err := WriteFile(path, []byte(contentstring)) + if err != nil { + t.Error("Could not write file") + } + + if !FileExist(path) { + t.Error("File not found at", path) + } + + v, err := ReadAllFile(path) + if err != nil { + t.Error("Could not read file", path) + } + if v != contentstring { + t.Error("Expected", contentstring, "Got", v) + } + +} + +func TestBadFile(t *testing.T) { + badpath := "/this/path/should/not/exist/goethereumtest.fail" + path := ExpandHomePath(badpath) + contentstring := "3.14159265358979323846" + + err := WriteFile(path, []byte(contentstring)) + if err == nil { + t.Error("Wrote file, but should not be able to", path) + } + + if FileExist(path) { + t.Error("Found file, but should not be able to", path) + } + + v, err := ReadAllFile(path) + if err == nil { + t.Error("Read file, but should not be able to", v) + } + +} diff --git a/ethutil/rand_test.go b/ethutil/rand_test.go new file mode 100644 index 000000000..c12698538 --- /dev/null +++ b/ethutil/rand_test.go @@ -0,0 +1,17 @@ +package ethutil + +import ( + checker "gopkg.in/check.v1" +) + +type RandomSuite struct{} + +var _ = checker.Suite(&RandomSuite{}) + +func (s *RandomSuite) TestRandomUint64(c *checker.C) { + res1, _ := RandomUint64() + res2, _ := RandomUint64() + c.Assert(res1, checker.NotNil) + c.Assert(res2, checker.NotNil) + c.Assert(res1, checker.Not(checker.Equals), res2) +} diff --git a/ethutil/size_test.go b/ethutil/size_test.go index 82aa1c653..e0f28abc5 100644 --- a/ethutil/size_test.go +++ b/ethutil/size_test.go @@ -1,12 +1,23 @@ package ethutil import ( - "fmt" - "testing" + checker "gopkg.in/check.v1" ) -func TestSize(t *testing.T) { - fmt.Println(StorageSize(2381273)) - fmt.Println(StorageSize(2192)) - fmt.Println(StorageSize(12)) +type SizeSuite struct{} + +var _ = checker.Suite(&SizeSuite{}) + +func (s *SizeSuite) TestStorageSizeString(c *checker.C) { + data1 := 2381273 + data2 := 2192 + data3 := 12 + + exp1 := "2.38 mB" + exp2 := "2.19 kB" + exp3 := "12.00 B" + + c.Assert(StorageSize(data1).String(), checker.Equals, exp1) + c.Assert(StorageSize(data2).String(), checker.Equals, exp2) + c.Assert(StorageSize(data3).String(), checker.Equals, exp3) } diff --git a/ethutil/value_test.go b/ethutil/value_test.go index 5452a0790..7c58d3b56 100644 --- a/ethutil/value_test.go +++ b/ethutil/value_test.go @@ -1,86 +1,69 @@ package ethutil import ( - "bytes" - "fmt" + checker "gopkg.in/check.v1" "math/big" - "testing" ) -func TestValueCmp(t *testing.T) { +type ValueSuite struct{} + +var _ = checker.Suite(&ValueSuite{}) + +func (s *ValueSuite) TestValueCmp(c *checker.C) { val1 := NewValue("hello") val2 := NewValue("world") - if val1.Cmp(val2) { - t.Error("Expected values not to be equal") - } + c.Assert(val1.Cmp(val2), checker.Equals, false) val3 := NewValue("hello") val4 := NewValue("hello") - if !val3.Cmp(val4) { - t.Error("Expected values to be equal") - } + c.Assert(val3.Cmp(val4), checker.Equals, true) } -func TestValueTypes(t *testing.T) { +func (s *ValueSuite) TestValueTypes(c *checker.C) { str := NewValue("str") num := NewValue(1) inter := NewValue([]interface{}{1}) byt := NewValue([]byte{1, 2, 3, 4}) bigInt := NewValue(big.NewInt(10)) - if str.Str() != "str" { - t.Errorf("expected Str to return 'str', got %s", str.Str()) - } - - if num.Uint() != 1 { - t.Errorf("expected Uint to return '1', got %d", num.Uint()) - } - + strExp := "str" + numExp := uint64(1) interExp := []interface{}{1} - if !NewValue(inter.Interface()).Cmp(NewValue(interExp)) { - t.Errorf("expected Interface to return '%v', got %v", interExp, num.Interface()) - } - bytExp := []byte{1, 2, 3, 4} - if bytes.Compare(byt.Bytes(), bytExp) != 0 { - t.Errorf("expected Bytes to return '%v', got %v", bytExp, byt.Bytes()) - } - bigExp := big.NewInt(10) - if bigInt.BigInt().Cmp(bigExp) != 0 { - t.Errorf("expected BigInt to return '%v', got %v", bigExp, bigInt.BigInt()) - } + + c.Assert(str.Str(), checker.Equals, strExp) + c.Assert(num.Uint(), checker.Equals, numExp) + c.Assert(NewValue(inter.Interface()).Cmp(NewValue(interExp)), checker.Equals, true) + c.Assert(byt.Bytes(), checker.DeepEquals, bytExp) + c.Assert(bigInt.BigInt(), checker.DeepEquals, bigExp) } -func TestIterator(t *testing.T) { +func (s *ValueSuite) TestIterator(c *checker.C) { value := NewValue([]interface{}{1, 2, 3}) - it := value.NewIterator() + iter := value.NewIterator() values := []uint64{1, 2, 3} i := 0 - for it.Next() { - if values[i] != it.Value().Uint() { - t.Errorf("Expected %d, got %d", values[i], it.Value().Uint()) - } + for iter.Next() { + c.Assert(values[i], checker.Equals, iter.Value().Uint()) i++ } } -func TestMath(t *testing.T) { - a := NewValue(1) - a.Add(1).Add(1) - - if !a.DeepCmp(NewValue(3)) { - t.Error("Expected 3, got", a) - } +func (s *ValueSuite) TestMath(c *checker.C) { + data1 := NewValue(1) + data1.Add(1).Add(1) + exp1 := NewValue(3) + data2 := NewValue(2) + data2.Sub(1).Sub(1) + exp2 := NewValue(0) - a = NewValue(2) - a.Sub(1).Sub(1) - if !a.DeepCmp(NewValue(0)) { - t.Error("Expected 0, got", a) - } + c.Assert(data1.DeepCmp(exp1), checker.Equals, true) + c.Assert(data2.DeepCmp(exp2), checker.Equals, true) } -func TestString(t *testing.T) { - a := NewValue("10") - fmt.Println("VALUE WITH STRING:", a.Int()) +func (s *ValueSuite) TestString(c *checker.C) { + data := "10" + exp := int64(10) + c.Assert(NewValue(data).Int(), checker.DeepEquals, exp) } diff --git a/state/main_test.go b/state/main_test.go new file mode 100644 index 000000000..973a7c373 --- /dev/null +++ b/state/main_test.go @@ -0,0 +1,8 @@ +package state + +import ( + checker "gopkg.in/check.v1" + "testing" +) + +func Test(t *testing.T) { checker.TestingT(t) } diff --git a/state/state_test.go b/state/state_test.go index 737815e90..825d21fcc 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -1,36 +1,61 @@ package state import ( - "testing" + checker "gopkg.in/check.v1" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/trie" ) -var ZeroHash256 = make([]byte, 32) - -func TestSnapshot(t *testing.T) { - db, _ := ethdb.NewMemDatabase() - ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "") - ethutil.Config.Db = db - - state := New(trie.New(db, "")) - - stateObject := state.GetOrNewStateObject([]byte("aa")) +type StateSuite struct { + state *State +} - stateObject.SetStorage(ethutil.Big("0"), ethutil.NewValue(42)) +var _ = checker.Suite(&StateSuite{}) - snapshot := state.Copy() +// var ZeroHash256 = make([]byte, 32) - stateObject = state.GetStateObject([]byte("aa")) - stateObject.SetStorage(ethutil.Big("0"), ethutil.NewValue(43)) +func (s *StateSuite) TestDump(c *checker.C) { + key := []byte{0x01} + value := "foo" + node := []interface{}{key, value} + s.state.Trie.Put(node) + dump := s.state.Dump() + c.Assert(dump, checker.NotNil) +} - state.Set(snapshot) +func (s *StateSuite) SetUpTest(c *checker.C) { + db, _ := ethdb.NewMemDatabase() + ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "") + ethutil.Config.Db = db + s.state = New(trie.New(db, "")) +} - stateObject = state.GetStateObject([]byte("aa")) - res := stateObject.GetStorage(ethutil.Big("0")) - if !res.Cmp(ethutil.NewValue(42)) { - t.Error("Expected storage 0 to be 42", res) - } +func (s *StateSuite) TestSnapshot(c *checker.C) { + stateobjaddr := []byte("aa") + storageaddr := ethutil.Big("0") + data1 := ethutil.NewValue(42) + data2 := ethutil.NewValue(43) + + // get state object + stateObject := s.state.GetOrNewStateObject(stateobjaddr) + // set inital state object value + stateObject.SetStorage(storageaddr, data1) + // get snapshot of current state + snapshot := s.state.Copy() + + // get state object. is this strictly necessary? + stateObject = s.state.GetStateObject(stateobjaddr) + // set new state object value + stateObject.SetStorage(storageaddr, data2) + // restore snapshot + s.state.Set(snapshot) + + // get state object + stateObject = s.state.GetStateObject(stateobjaddr) + // get state storage value + res := stateObject.GetStorage(storageaddr) + + c.Assert(data1, checker.DeepEquals, res) } diff --git a/trie/encoding_test.go b/trie/encoding_test.go index c9bc113c9..193c898f3 100644 --- a/trie/encoding_test.go +++ b/trie/encoding_test.go @@ -1,68 +1,59 @@ package trie import ( - "bytes" - "fmt" - "testing" + checker "gopkg.in/check.v1" ) -func TestCompactEncode(t *testing.T) { +type TrieEncodingSuite struct{} + +var _ = checker.Suite(&TrieEncodingSuite{}) + +func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) { + // even compact encode test1 := []byte{1, 2, 3, 4, 5} - if res := CompactEncode(test1); res != "\x11\x23\x45" { - t.Error(fmt.Sprintf("even compact encode failed. Got: %q", res)) - } + res1 := CompactEncode(test1) + c.Assert(res1, checker.Equals, "\x11\x23\x45") + // odd compact encode test2 := []byte{0, 1, 2, 3, 4, 5} - if res := CompactEncode(test2); res != "\x00\x01\x23\x45" { - t.Error(fmt.Sprintf("odd compact encode failed. Got: %q", res)) - } + res2 := CompactEncode(test2) + c.Assert(res2, checker.Equals, "\x00\x01\x23\x45") + //odd terminated compact encode test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} - if res := CompactEncode(test3); res != "\x20\x0f\x1c\xb8" { - t.Error(fmt.Sprintf("odd terminated compact encode failed. Got: %q", res)) - } + res3 := CompactEncode(test3) + c.Assert(res3, checker.Equals, "\x20\x0f\x1c\xb8") + // even terminated compact encode test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16} - if res := CompactEncode(test4); res != "\x3f\x1c\xb8" { - t.Error(fmt.Sprintf("even terminated compact encode failed. Got: %q", res)) - } + res4 := CompactEncode(test4) + c.Assert(res4, checker.Equals, "\x3f\x1c\xb8") } -func TestCompactHexDecode(t *testing.T) { +func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) { exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} res := CompactHexDecode("verb") - - if !bytes.Equal(res, exp) { - t.Error("Error compact hex decode. Expected", exp, "got", res) - } + c.Assert(res, checker.DeepEquals, exp) } -func TestCompactDecode(t *testing.T) { +func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) { + // odd compact decode exp := []byte{1, 2, 3, 4, 5} res := CompactDecode("\x11\x23\x45") + c.Assert(res, checker.DeepEquals, exp) - if !bytes.Equal(res, exp) { - t.Error("odd compact decode. Expected", exp, "got", res) - } - + // even compact decode exp = []byte{0, 1, 2, 3, 4, 5} res = CompactDecode("\x00\x01\x23\x45") + c.Assert(res, checker.DeepEquals, exp) - if !bytes.Equal(res, exp) { - t.Error("even compact decode. Expected", exp, "got", res) - } - + // even terminated compact decode exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} res = CompactDecode("\x20\x0f\x1c\xb8") + c.Assert(res, checker.DeepEquals, exp) - if !bytes.Equal(res, exp) { - t.Error("even terminated compact decode. Expected", exp, "got", res) - } - + // even terminated compact decode exp = []byte{15, 1, 12, 11, 8 /*term*/, 16} res = CompactDecode("\x3f\x1c\xb8") - - if !bytes.Equal(res, exp) { - t.Error("even terminated compact decode. Expected", exp, "got", res) - } + c.Assert(res, checker.DeepEquals, exp) } diff --git a/trie/main_test.go b/trie/main_test.go new file mode 100644 index 000000000..f131b83de --- /dev/null +++ b/trie/main_test.go @@ -0,0 +1,8 @@ +package trie + +import ( + checker "gopkg.in/check.v1" + "testing" +) + +func Test(t *testing.T) { checker.TestingT(t) } diff --git a/trie/trie_test.go b/trie/trie_test.go index 4c7e621dc..98d02c83e 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -1,15 +1,13 @@ package trie import ( - "bytes" "encoding/hex" "encoding/json" "fmt" + checker "gopkg.in/check.v1" "io/ioutil" "math/rand" "net/http" - "reflect" - "testing" "time" "github.com/ethereum/go-ethereum/ethutil" @@ -17,6 +15,11 @@ import ( const LONG_WORD = "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ" +type TrieSuite struct { + db *MemDatabase + trie *Trie +} + type MemDatabase struct { db map[string][]byte } @@ -44,142 +47,97 @@ func NewTrie() (*MemDatabase, *Trie) { return db, New(db, "") } -func TestTrieSync(t *testing.T) { - db, trie := NewTrie() - - trie.Update("dog", LONG_WORD) - if len(db.db) != 0 { - t.Error("Expected no data in database") - } - - trie.Sync() - if len(db.db) == 0 { - t.Error("Expected data to be persisted") - } +func (s *TrieSuite) SetUpTest(c *checker.C) { + s.db, s.trie = NewTrie() } -func TestTrieDirtyTracking(t *testing.T) { - _, trie := NewTrie() - trie.Update("dog", LONG_WORD) - if !trie.cache.IsDirty { - t.Error("Expected trie to be dirty") - } +func (s *TrieSuite) TestTrieSync(c *checker.C) { + s.trie.Update("dog", LONG_WORD) + c.Assert(s.db.db, checker.HasLen, 0, checker.Commentf("Expected no data in database")) + s.trie.Sync() + c.Assert(s.db.db, checker.HasLen, 3) +} - trie.Sync() - if trie.cache.IsDirty { - t.Error("Expected trie not to be dirty") - } +func (s *TrieSuite) TestTrieDirtyTracking(c *checker.C) { + s.trie.Update("dog", LONG_WORD) + c.Assert(s.trie.cache.IsDirty, checker.Equals, true, checker.Commentf("Expected no data in database")) - trie.Update("test", LONG_WORD) - trie.cache.Undo() - if trie.cache.IsDirty { - t.Error("Expected trie not to be dirty") - } + s.trie.Sync() + c.Assert(s.trie.cache.IsDirty, checker.Equals, false, checker.Commentf("Expected trie to be dirty")) + s.trie.Update("test", LONG_WORD) + s.trie.cache.Undo() + c.Assert(s.trie.cache.IsDirty, checker.Equals, false) } -func TestTrieReset(t *testing.T) { - _, trie := NewTrie() - - trie.Update("cat", LONG_WORD) - if len(trie.cache.nodes) == 0 { - t.Error("Expected cached nodes") - } +func (s *TrieSuite) TestTrieReset(c *checker.C) { + s.trie.Update("cat", LONG_WORD) + c.Assert(s.trie.cache.nodes, checker.HasLen, 1, checker.Commentf("Expected cached nodes")) - trie.cache.Undo() - - if len(trie.cache.nodes) != 0 { - t.Error("Expected no nodes after undo", len(trie.cache.nodes)) - } + s.trie.cache.Undo() + c.Assert(s.trie.cache.nodes, checker.HasLen, 0, checker.Commentf("Expected no nodes after undo")) } -func TestTrieGet(t *testing.T) { - _, trie := NewTrie() - - trie.Update("cat", LONG_WORD) - x := trie.Get("cat") - if x != LONG_WORD { - t.Error("expected %s, got %s", LONG_WORD, x) - } +func (s *TrieSuite) TestTrieGet(c *checker.C) { + s.trie.Update("cat", LONG_WORD) + x := s.trie.Get("cat") + c.Assert(x, checker.DeepEquals, LONG_WORD) } -func TestTrieUpdating(t *testing.T) { - _, trie := NewTrie() - trie.Update("cat", LONG_WORD) - trie.Update("cat", LONG_WORD+"1") - x := trie.Get("cat") - if x != LONG_WORD+"1" { - t.Error("expected %S, got %s", LONG_WORD+"1", x) - } +func (s *TrieSuite) TestTrieUpdating(c *checker.C) { + s.trie.Update("cat", LONG_WORD) + s.trie.Update("cat", LONG_WORD+"1") + x := s.trie.Get("cat") + c.Assert(x, checker.DeepEquals, LONG_WORD+"1") } -func TestTrieCmp(t *testing.T) { +func (s *TrieSuite) TestTrieCmp(c *checker.C) { _, trie1 := NewTrie() _, trie2 := NewTrie() trie1.Update("doge", LONG_WORD) trie2.Update("doge", LONG_WORD) - if !trie1.Cmp(trie2) { - t.Error("Expected tries to be equal") - } + c.Assert(trie1, checker.DeepEquals, trie2) trie1.Update("dog", LONG_WORD) trie2.Update("cat", LONG_WORD) - if trie1.Cmp(trie2) { - t.Errorf("Expected tries not to be equal %x %x", trie1.Root, trie2.Root) - } + c.Assert(trie1, checker.Not(checker.DeepEquals), trie2) } -func TestTrieDelete(t *testing.T) { - t.Skip() - _, trie := NewTrie() - trie.Update("cat", LONG_WORD) - exp := trie.Root - trie.Update("dog", LONG_WORD) - trie.Delete("dog") - if !reflect.DeepEqual(exp, trie.Root) { - t.Errorf("Expected tries to be equal %x : %x", exp, trie.Root) - } - - trie.Update("dog", LONG_WORD) - exp = trie.Root - trie.Update("dude", LONG_WORD) - trie.Delete("dude") - if !reflect.DeepEqual(exp, trie.Root) { - t.Errorf("Expected tries to be equal %x : %x", exp, trie.Root) - } +func (s *TrieSuite) TestTrieDelete(c *checker.C) { + s.trie.Update("cat", LONG_WORD) + exp := s.trie.Root + s.trie.Update("dog", LONG_WORD) + s.trie.Delete("dog") + c.Assert(s.trie.Root, checker.DeepEquals, exp) + + s.trie.Update("dog", LONG_WORD) + exp = s.trie.Root + s.trie.Update("dude", LONG_WORD) + s.trie.Delete("dude") + c.Assert(s.trie.Root, checker.DeepEquals, exp) } -func TestTrieDeleteWithValue(t *testing.T) { - t.Skip() - _, trie := NewTrie() - trie.Update("c", LONG_WORD) - exp := trie.Root - trie.Update("ca", LONG_WORD) - trie.Update("cat", LONG_WORD) - trie.Delete("ca") - trie.Delete("cat") - if !reflect.DeepEqual(exp, trie.Root) { - t.Errorf("Expected tries to be equal %x : %x", exp, trie.Root) - } - +func (s *TrieSuite) TestTrieDeleteWithValue(c *checker.C) { + s.trie.Update("c", LONG_WORD) + exp := s.trie.Root + s.trie.Update("ca", LONG_WORD) + s.trie.Update("cat", LONG_WORD) + s.trie.Delete("ca") + s.trie.Delete("cat") + c.Assert(s.trie.Root, checker.DeepEquals, exp) } -func TestTriePurge(t *testing.T) { - _, trie := NewTrie() - trie.Update("c", LONG_WORD) - trie.Update("ca", LONG_WORD) - trie.Update("cat", LONG_WORD) - - lenBefore := len(trie.cache.nodes) - it := trie.NewIterator() - if num := it.Purge(); num != 3 { - t.Errorf("Expected purge to return 3, got %d", num) - } +func (s *TrieSuite) TestTriePurge(c *checker.C) { + s.trie.Update("c", LONG_WORD) + s.trie.Update("ca", LONG_WORD) + s.trie.Update("cat", LONG_WORD) - if lenBefore == len(trie.cache.nodes) { - t.Errorf("Expected cached nodes to be deleted") - } + lenBefore := len(s.trie.cache.nodes) + it := s.trie.NewIterator() + num := it.Purge() + c.Assert(num, checker.Equals, 3) + c.Assert(len(s.trie.cache.nodes), checker.Equals, lenBefore) } func h(str string) string { @@ -201,23 +159,23 @@ func get(in string) (out string) { return } -type Test struct { +type TrieTest struct { Name string In map[string]string Root string } -func CreateTest(name string, data []byte) (Test, error) { - t := Test{Name: name} +func CreateTest(name string, data []byte) (TrieTest, error) { + t := TrieTest{Name: name} err := json.Unmarshal(data, &t) if err != nil { - return Test{}, fmt.Errorf("%v", err) + return TrieTest{}, fmt.Errorf("%v", err) } return t, nil } -func CreateTests(uri string, cb func(Test)) map[string]Test { +func CreateTests(uri string, cb func(TrieTest)) map[string]TrieTest { resp, err := http.Get(uri) if err != nil { panic(err) @@ -232,7 +190,7 @@ func CreateTests(uri string, cb func(Test)) map[string]Test { panic(err) } - tests := make(map[string]Test) + tests := make(map[string]TrieTest) for name, testData := range objmap { test, err := CreateTest(name, *testData) if err != nil { @@ -276,7 +234,7 @@ func RandomData() [][]string { const MaxTest = 1000 // This test insert data in random order and seeks to find indifferences between the different tries -func TestRegression(t *testing.T) { +func (s *TrieSuite) TestRegression(c *checker.C) { rand.Seed(time.Now().Unix()) roots := make(map[string]int) @@ -292,34 +250,33 @@ func TestRegression(t *testing.T) { roots[string(trie.Root.([]byte))] += 1 } - if len(roots) > 1 { - for root, num := range roots { - t.Errorf("%x => %d\n", root, num) - } - } + c.Assert(len(roots) <= 1, checker.Equals, true) + // if len(roots) > 1 { + // for root, num := range roots { + // t.Errorf("%x => %d\n", root, num) + // } + // } } -func TestDelete(t *testing.T) { - _, trie := NewTrie() - - trie.Update("a", "jeffreytestlongstring") - trie.Update("aa", "otherstring") - trie.Update("aaa", "othermorestring") - trie.Update("aabbbbccc", "hithere") - trie.Update("abbcccdd", "hstanoehutnaheoustnh") - trie.Update("rnthaoeuabbcccdd", "hstanoehutnaheoustnh") - trie.Update("rneuabbcccdd", "hstanoehutnaheoustnh") - trie.Update("rneuabboeusntahoeucccdd", "hstanoehutnaheoustnh") - trie.Update("rnxabboeusntahoeucccdd", "hstanoehutnaheoustnh") - trie.Delete("aaboaestnuhbccc") - trie.Delete("a") - trie.Update("a", "nthaonethaosentuh") - trie.Update("c", "shtaosntehua") - trie.Delete("a") - trie.Update("aaaa", "testmegood") +func (s *TrieSuite) TestDelete(c *checker.C) { + s.trie.Update("a", "jeffreytestlongstring") + s.trie.Update("aa", "otherstring") + s.trie.Update("aaa", "othermorestring") + s.trie.Update("aabbbbccc", "hithere") + s.trie.Update("abbcccdd", "hstanoehutnaheoustnh") + s.trie.Update("rnthaoeuabbcccdd", "hstanoehutnaheoustnh") + s.trie.Update("rneuabbcccdd", "hstanoehutnaheoustnh") + s.trie.Update("rneuabboeusntahoeucccdd", "hstanoehutnaheoustnh") + s.trie.Update("rnxabboeusntahoeucccdd", "hstanoehutnaheoustnh") + s.trie.Delete("aaboaestnuhbccc") + s.trie.Delete("a") + s.trie.Update("a", "nthaonethaosentuh") + s.trie.Update("c", "shtaosntehua") + s.trie.Delete("a") + s.trie.Update("aaaa", "testmegood") _, t2 := NewTrie() - trie.NewIterator().Each(func(key string, v *ethutil.Value) { + s.trie.NewIterator().Each(func(key string, v *ethutil.Value) { if key == "aaaa" { t2.Update(key, v.Str()) } else { @@ -327,27 +284,22 @@ func TestDelete(t *testing.T) { } }) - a := ethutil.NewValue(trie.Root).Bytes() + a := ethutil.NewValue(s.trie.Root).Bytes() b := ethutil.NewValue(t2.Root).Bytes() - if bytes.Compare(a, b) != 0 { - t.Errorf("Expected %x and %x to be equal", a, b) - } + c.Assert(a, checker.DeepEquals, b) } -func TestTerminator(t *testing.T) { +func (s *TrieSuite) TestTerminator(c *checker.C) { key := CompactDecode("hello") - if !HasTerm(key) { - t.Errorf("Expected %v to have a terminator", key) - } + c.Assert(HasTerm(key), checker.Equals, true, checker.Commentf("Expected %v to have a terminator", key)) } -func TestIt(t *testing.T) { - _, trie := NewTrie() - trie.Update("cat", "cat") - trie.Update("doge", "doge") - trie.Update("wallace", "wallace") - it := trie.Iterator() +func (s *TrieSuite) TestIt(c *checker.C) { + s.trie.Update("cat", "cat") + s.trie.Update("doge", "doge") + s.trie.Update("wallace", "wallace") + it := s.trie.Iterator() inputs := []struct { In, Out string @@ -363,23 +315,16 @@ func TestIt(t *testing.T) { for _, test := range inputs { res := string(it.Next(test.In)) - if res != test.Out { - t.Errorf(test.In, "failed. Got", res, "Expected", test.Out) - } + c.Assert(res, checker.Equals, test.Out) } } -func TestBeginsWith(t *testing.T) { +func (s *TrieSuite) TestBeginsWith(c *checker.C) { a := CompactDecode("hello") b := CompactDecode("hel") - if BeginsWith(a, b) { - t.Errorf("Expected %x to begin with %x", a, b) - } - - if BeginsWith(b, a) { - t.Errorf("Expected %x not to begin with %x", b, a) - } + c.Assert(BeginsWith(a, b), checker.Equals, false) + c.Assert(BeginsWith(b, a), checker.Equals, true) } func TestItems(t *testing.T) { diff --git a/vm/vm_test.go b/vm/vm_test.go index 948dd835f..ed05cddfb 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -14,7 +14,7 @@ import ( "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/trie" - "github.com/obscuren/mutan" + // "github.com/obscuren/mutan" ) type TestEnv struct{} @@ -67,9 +67,9 @@ func setup(level logger.LogLevel, typ Type) (*Closure, VirtualMachine) { var big9 = ethutil.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000009") func TestDebugVm(t *testing.T) { - if mutan.Version < "0.6" { - t.Skip("skipping for mutan version", mutan.Version, " < 0.6") - } + // if mutan.Version < "0.6" { + // t.Skip("skipping for mutan version", mutan.Version, " < 0.6") + // } closure, vm := setup(logger.DebugLevel, DebugVmTy) ret, _, e := closure.Call(vm, nil) @@ -82,9 +82,9 @@ func TestDebugVm(t *testing.T) { } func TestVm(t *testing.T) { - if mutan.Version < "0.6" { - t.Skip("skipping for mutan version", mutan.Version, " < 0.6") - } + // if mutan.Version < "0.6" { + // t.Skip("skipping for mutan version", mutan.Version, " < 0.6") + // } closure, vm := setup(logger.DebugLevel, StandardVmTy) ret, _, e := closure.Call(vm, nil) |