From e22e8b7feffedced7fa1f8809b1828a224ccf774 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 4 Nov 2014 16:39:08 +0100 Subject: Environment tests --- ethutil/common_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'ethutil') diff --git a/ethutil/common_test.go b/ethutil/common_test.go index 2667eaf3a..e6e2d1e8b 100644 --- a/ethutil/common_test.go +++ b/ethutil/common_test.go @@ -2,9 +2,36 @@ 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) { ether := CurrencyToString(BigPow(10, 19)) finney := CurrencyToString(BigPow(10, 16)) -- cgit From 123282e0454179c8728394753501e0de93ac9b96 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 4 Nov 2014 16:41:03 +0100 Subject: Update variable name to match unit name --- ethutil/common_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'ethutil') diff --git a/ethutil/common_test.go b/ethutil/common_test.go index e6e2d1e8b..819ead7cb 100644 --- a/ethutil/common_test.go +++ b/ethutil/common_test.go @@ -36,9 +36,9 @@ func TestCommon(t *testing.T) { 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 ether != "10 Ether" { @@ -53,16 +53,16 @@ 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" { -- cgit From e7d9bcd36b5809b649dc7f8e6092d5edaa0d155f Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 4 Nov 2014 16:42:28 +0100 Subject: Added Douglas and Einstein cases --- ethutil/common_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'ethutil') diff --git a/ethutil/common_test.go b/ethutil/common_test.go index 819ead7cb..791ff0a25 100644 --- a/ethutil/common_test.go +++ b/ethutil/common_test.go @@ -33,6 +33,8 @@ func TestWindonziePath(t *testing.T) { } 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)) @@ -41,6 +43,14 @@ func TestCommon(t *testing.T) { 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) } -- cgit From b96a59eb28654ba5bc915bfaa21f511fdaa443df Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 4 Nov 2014 16:45:54 +0100 Subject: Added tests for extra large values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test ā€œEā€ scientific notation formatting --- ethutil/common.go | 1 + ethutil/common_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) (limited to 'ethutil') 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 791ff0a25..056676765 100644 --- a/ethutil/common_test.go +++ b/ethutil/common_test.go @@ -79,3 +79,21 @@ func TestCommon(t *testing.T) { 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) + } +} -- cgit From 92299b7c2449940c4b98f1aebcd53076780a7704 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 11:23:30 +0100 Subject: New test coverage for ethutil/path.go --- ethutil/path_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 ethutil/path_test.go (limited to 'ethutil') 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) + } + +} -- cgit From e76c58d175dd5b50deb2d5fb4582adae4cd3f7ef Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 13:23:50 +0100 Subject: New test coverage for ethutil/big.go --- ethutil/big_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 ethutil/big_test.go (limited to 'ethutil') diff --git a/ethutil/big_test.go b/ethutil/big_test.go new file mode 100644 index 000000000..b26e58b5c --- /dev/null +++ b/ethutil/big_test.go @@ -0,0 +1,63 @@ +package ethutil + +import ( + "fmt" + "testing" +) + +func TestMisc(t *testing.T) { + a := Big("10") + b := Big("57896044618658097711785492504343953926634992332820282019728792003956564819968") + c := []byte{1, 2, 3, 4} + fmt.Println(b) + z := BitTest(a, 1) + fmt.Println(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) + z := BigToBytes(c, 16) + fmt.Println(y) + fmt.Println(z) +} -- cgit From 94b0ce84da875bac3847aec73479f1bcd680c1ed Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 13:35:43 +0100 Subject: Cleanup big_test.go --- ethutil/big_test.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'ethutil') diff --git a/ethutil/big_test.go b/ethutil/big_test.go index b26e58b5c..bf3c96c6d 100644 --- a/ethutil/big_test.go +++ b/ethutil/big_test.go @@ -1,7 +1,7 @@ package ethutil import ( - "fmt" + "bytes" "testing" ) @@ -9,9 +9,11 @@ func TestMisc(t *testing.T) { a := Big("10") b := Big("57896044618658097711785492504343953926634992332820282019728792003956564819968") c := []byte{1, 2, 3, 4} - fmt.Println(b) z := BitTest(a, 1) - fmt.Println(z) + + if z != true { + t.Error("Expected true got", z) + } U256(a) S256(a) @@ -57,7 +59,15 @@ func TestBigCopy(t *testing.T) { b := BigCopy(a) c := Big("1000000000000") y := BigToBytes(b, 16) + ybytes := []byte{0, 10} z := BigToBytes(c, 16) - fmt.Println(y) - fmt.Println(z) + 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) + } } -- cgit From 4f009290847dea29e7ea050244418b8b14c3aa61 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 17:04:24 +0100 Subject: Added byte padding tests --- ethutil/bytes_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index 381efe7a2..27b31c0c8 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -12,3 +12,63 @@ func TestParseData(t *testing.T) { t.Error("Error parsing data") } } + +func TestLeftPadBytes(t *testing.T) { + val := []byte{1, 2, 3, 4} + exp := []byte{0, 0, 0, 0, 1, 2, 3, 4} + + resstd := LeftPadBytes(val, 8) + if bytes.Compare(resstd, exp) != 0 { + t.Errorf("Expected % x Got % x", exp, resstd) + } + + resshrt := LeftPadBytes(val, 2) + if bytes.Compare(resshrt, val) != 0 { + t.Errorf("Expected % x Got % x", exp, resshrt) + } +} + +func TestRightPadBytes(t *testing.T) { + val := []byte{1, 2, 3, 4} + exp := []byte{1, 2, 3, 4, 0, 0, 0, 0} + + resstd := RightPadBytes(val, 8) + if bytes.Compare(resstd, exp) != 0 { + t.Errorf("Expected % x Got % x", exp, resstd) + } + + resshrt := RightPadBytes(val, 2) + if bytes.Compare(resshrt, val) != 0 { + t.Errorf("Expected % x Got % x", exp, resshrt) + } +} + +func TestLeftPadString(t *testing.T) { + val := "test" + + resstd := LeftPadString(val, 8) + + if resstd != "\x30\x30\x30\x30"+val { + t.Errorf("Expected % x Got % x", val, resstd) + } + + resshrt := LeftPadString(val, 2) + + if resshrt != val { + t.Errorf("Expected % x Got % x", val, resshrt) + } +} + +func TestRightPadString(t *testing.T) { + val := "test" + + resstd := RightPadString(val, 8) + if resstd != val+"\x30\x30\x30\x30" { + t.Errorf("Expected % x Got % x", val, resstd) + } + + resshrt := RightPadString(val, 2) + if resshrt != val { + t.Errorf("Expected % x Got % x", val, resshrt) + } +} -- cgit From cb32f52b9cdc0f44c7c25f956b9de8c62e24f275 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 17:11:40 +0100 Subject: added test for parsing bytes --- ethutil/bytes_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index 27b31c0c8..cbc38da77 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -func TestParseData(t *testing.T) { +func TestParseDataString(t *testing.T) { data := ParseData("hello", "world", "0x0106") exp := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000" if bytes.Compare(data, Hex2Bytes(exp)) != 0 { @@ -13,6 +13,16 @@ func TestParseData(t *testing.T) { } } +func TestParseDataBytes(t *testing.T) { + data := []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} + + res := ParseData(data) + if bytes.Compare(res, exp) != 0 { + t.Errorf("Expected %x got %x", exp, res) + } +} + func TestLeftPadBytes(t *testing.T) { val := []byte{1, 2, 3, 4} exp := []byte{0, 0, 0, 0, 1, 2, 3, 4} -- cgit From 92b30cc452d71a84927529b50ac9aa40eb0d550e Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 17:39:26 +0100 Subject: add tests for ReadVarInt --- ethutil/bytes_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index cbc38da77..051f924d4 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -82,3 +82,36 @@ func TestRightPadString(t *testing.T) { t.Errorf("Expected % x Got % x", val, resshrt) } } + +func TestReadVarInt(t *testing.T) { + 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) + + if res8 != exp8 { + t.Errorf("Expected %d | Got %d", exp8, res8) + } + + 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) + } +} -- cgit From ada684e05458cce26a97b245cd96a8fd72523daa Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 17:44:28 +0100 Subject: added test for BinaryLength --- ethutil/bytes_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index 051f924d4..2a106d585 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -115,3 +115,22 @@ func TestReadVarInt(t *testing.T) { t.Errorf("Expected %d | Got %d", exp1, res1) } } + +func TestBinaryLength(t *testing.T) { + data1 := 0 + data2 := 920987656789 + + exp1 := 0 + exp2 := 5 + + res1 := BinaryLength(data1) + res2 := BinaryLength(data2) + + if res1 != exp1 { + t.Errorf("Expected %d got %d", exp1, res1) + } + + if res2 != exp2 { + t.Errorf("Expected %d got %d", exp2, res2) + } +} -- cgit From 834f8a1dc8b609199f84dcd59a885e77d8f770ba Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 17:48:23 +0100 Subject: added test for CopyBytes --- ethutil/bytes_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index 2a106d585..361df4b9d 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -134,3 +134,12 @@ func TestBinaryLength(t *testing.T) { t.Errorf("Expected %d got %d", exp2, res2) } } + +func TestCopyBytes(t *testing.T) { + data1 := []byte{1, 2, 3, 4} + exp1 := []byte{1, 2, 3, 4} + res1 := CopyBytes(data1) + if bytes.Compare(res1, exp1) != 0 { + t.Errorf("Expected % x got % x", exp1, res1) + } +} -- cgit From b100546c9dd82a085d4b984275082a61fe3f3fe2 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 18:02:20 +0100 Subject: add test for Bytes.String() --- ethutil/bytes_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index 361df4b9d..82167f054 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -5,6 +5,17 @@ import ( "testing" ) +func TestByteString(t *testing.T) { + var data Bytes + data = []byte{102, 111, 111} + exp := "foo" + res := data.String() + + if res != exp { + t.Errorf("Expected %s got %s", exp, res) + } +} + func TestParseDataString(t *testing.T) { data := ParseData("hello", "world", "0x0106") exp := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000" -- cgit From 4e15adac6d46acb5b8173050618a1ffac525e528 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 21:59:22 +0100 Subject: Remove fmt dependency --- ethutil/value_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'ethutil') diff --git a/ethutil/value_test.go b/ethutil/value_test.go index 5452a0790..54c8d9fd6 100644 --- a/ethutil/value_test.go +++ b/ethutil/value_test.go @@ -2,7 +2,6 @@ package ethutil import ( "bytes" - "fmt" "math/big" "testing" ) @@ -81,6 +80,10 @@ func TestMath(t *testing.T) { } func TestString(t *testing.T) { - a := NewValue("10") - fmt.Println("VALUE WITH STRING:", a.Int()) + data := "10" + exp := int64(10) + res := NewValue(data).Int() + if res != exp { + t.Errorf("Exprected %d Got res", exp, res) + } } -- cgit From ab6b9c44aa97a18e37dcd9214eadbdfaa38a4b22 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 22:00:01 +0100 Subject: Added test for IsHex --- ethutil/bytes_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index 82167f054..ee69b38b5 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -16,6 +16,23 @@ func TestByteString(t *testing.T) { } } + +func TestIsHex(t *testing.T) { + data1 := "a9e67e" + exp1 := false + res1 := IsHex(data1) + if exp1 != res1 { + t.Errorf("Expected % x Got % x", exp1, res1) + } + + data2 := "0xa9e67e00" + exp2 := true + res2 := IsHex(data2) + if exp2 != res2 { + t.Errorf("Expected % x Got % x", exp2, res2) + } +} + func TestParseDataString(t *testing.T) { data := ParseData("hello", "world", "0x0106") exp := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000" -- cgit From 8f94f731a55440341eb02d890fd4eca0fbc32544 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 22:01:09 +0100 Subject: Reorder tests to match source order --- ethutil/bytes_test.go | 122 +++++++++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 61 deletions(-) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index ee69b38b5..64483350c 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -17,6 +17,67 @@ func TestByteString(t *testing.T) { } +func TestReadVarInt(t *testing.T) { + 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) + + if res8 != exp8 { + t.Errorf("Expected %d | Got %d", exp8, res8) + } + + 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) { + data1 := 0 + data2 := 920987656789 + + exp1 := 0 + exp2 := 5 + + res1 := BinaryLength(data1) + res2 := BinaryLength(data2) + + if res1 != exp1 { + t.Errorf("Expected %d got %d", exp1, res1) + } + + if res2 != exp2 { + t.Errorf("Expected %d got %d", exp2, res2) + } +} + +func TestCopyBytes(t *testing.T) { + data1 := []byte{1, 2, 3, 4} + exp1 := []byte{1, 2, 3, 4} + res1 := CopyBytes(data1) + if bytes.Compare(res1, exp1) != 0 { + t.Errorf("Expected % x got % x", exp1, res1) + } +} + func TestIsHex(t *testing.T) { data1 := "a9e67e" exp1 := false @@ -110,64 +171,3 @@ func TestRightPadString(t *testing.T) { t.Errorf("Expected % x Got % x", val, resshrt) } } - -func TestReadVarInt(t *testing.T) { - 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) - - if res8 != exp8 { - t.Errorf("Expected %d | Got %d", exp8, res8) - } - - 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) { - data1 := 0 - data2 := 920987656789 - - exp1 := 0 - exp2 := 5 - - res1 := BinaryLength(data1) - res2 := BinaryLength(data2) - - if res1 != exp1 { - t.Errorf("Expected %d got %d", exp1, res1) - } - - if res2 != exp2 { - t.Errorf("Expected %d got %d", exp2, res2) - } -} - -func TestCopyBytes(t *testing.T) { - data1 := []byte{1, 2, 3, 4} - exp1 := []byte{1, 2, 3, 4} - res1 := CopyBytes(data1) - if bytes.Compare(res1, exp1) != 0 { - t.Errorf("Expected % x got % x", exp1, res1) - } -} -- cgit From be96da179a50735ef0ed72d66c5672d89c1a5b66 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 22:01:41 +0100 Subject: Added tests for FormatData --- ethutil/bytes_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index 64483350c..08c44eabb 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -127,6 +127,39 @@ func TestLeftPadBytes(t *testing.T) { } } +func TestFormatData(t *testing.T) { + 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) + + if bytes.Compare(res1, exp1) != 0 { + t.Errorf("Expected % x Got % x", exp1, res1) + } + + 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) { val := []byte{1, 2, 3, 4} exp := []byte{1, 2, 3, 4, 0, 0, 0, 0} -- cgit From a1d62abca4b07f35f7f80702c9f58bb40e703504 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 22:16:44 +0100 Subject: Restructure StorageSize string test --- ethutil/size_test.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'ethutil') diff --git a/ethutil/size_test.go b/ethutil/size_test.go index 82aa1c653..9127521cb 100644 --- a/ethutil/size_test.go +++ b/ethutil/size_test.go @@ -1,12 +1,31 @@ package ethutil import ( - "fmt" "testing" ) -func TestSize(t *testing.T) { - fmt.Println(StorageSize(2381273)) - fmt.Println(StorageSize(2192)) - fmt.Println(StorageSize(12)) +func TestStorageSizeString(t *testing.T) { + data1 := 2381273 + data2 := 2192 + data3 := 12 + + exp1 := "2.38 mB" + exp2 := "2.19 kB" + exp3 := "12.00 B" + + res1 := StorageSize(data1).String() + res2 := StorageSize(data2).String() + res3 := StorageSize(data3).String() + + if res1 != exp1 { + t.Errorf("Expected %s got %s", exp1, res1) + } + + if res2 != exp2 { + t.Errorf("Expected %s got %s", exp2, res2) + } + + if res3 != exp3 { + t.Errorf("Expected %s got %s", exp3, res3) + } } -- cgit From 48a3f0986ccc3caf6405604e3cfe6a3b4de5726c Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 5 Nov 2014 22:21:28 +0100 Subject: Add coverage for rand --- ethutil/rand_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 ethutil/rand_test.go (limited to 'ethutil') diff --git a/ethutil/rand_test.go b/ethutil/rand_test.go new file mode 100644 index 000000000..cc64c73e5 --- /dev/null +++ b/ethutil/rand_test.go @@ -0,0 +1,9 @@ +package ethutil + +import ( + "testing" +) + +func TestRandomUint64(t *testing.T) { + RandomUint64() +} -- cgit From 0a3a148ed4d2fbe29ec7a62f5b15a25f908d7f23 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 6 Nov 2014 12:35:46 +0100 Subject: Added more byte tests --- ethutil/bytes_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index 08c44eabb..c7e696b55 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -16,6 +16,43 @@ func TestByteString(t *testing.T) { } } +/* +func TestDeleteFromByteSlice(t *testing.T) { + data := []byte{1, 2, 3, 4} + slice := []byte{1, 2, 3, 4} + exp := []byte{1, 4} + res := DeleteFromByteSlice(data, slice) + if bytes.Compare(res, exp) != 0 { + t.Errorf("Expected % x Got % x", exp, res) + } +} + +func TestNumberToBytes(t *testing.T) { + data := int(1) + exp := []byte{0, 0, 0, 0, 0, 0, 0, 1} + // TODO this fails. why? + res := NumberToBytes(data, 16) + if bytes.Compare(res, exp) != 0 { + t.Errorf("Expected % x Got % x", exp, res) + } +} +*/ + +func TestBytesToNumber(t *testing.T) { + datasmall := []byte{0, 1} + datalarge := []byte{1, 2, 3} + expsmall := uint64(0) + explarge := uint64(0) + // TODO this fails. why? + ressmall := BytesToNumber(datasmall) + reslarge := BytesToNumber(datalarge) + if ressmall != expsmall { + t.Errorf("Expected %d Got %d", expsmall, ressmall) + } + if reslarge != explarge { + t.Errorf("Expected %d Got %d", explarge, reslarge) + } +} func TestReadVarInt(t *testing.T) { data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8} -- cgit From cd94b5ffb3c29d72d87c6cd7e439868e11e24b8d Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 11 Nov 2014 15:04:11 +0100 Subject: Convert value_test to use gocheck --- ethutil/value_test.go | 85 +++++++++++++++++++++------------------------------ 1 file changed, 34 insertions(+), 51 deletions(-) (limited to 'ethutil') diff --git a/ethutil/value_test.go b/ethutil/value_test.go index 54c8d9fd6..1c62d3e20 100644 --- a/ethutil/value_test.go +++ b/ethutil/value_test.go @@ -1,89 +1,72 @@ package ethutil import ( - "bytes" + checker "gopkg.in/check.v1" "math/big" "testing" ) -func TestValueCmp(t *testing.T) { +func Test(t *testing.T) { checker.TestingT(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) +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) - if !a.DeepCmp(NewValue(3)) { - t.Error("Expected 3, got", a) - } - - 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) { +func (s *ValueSuite) TestString(c *checker.C) { data := "10" exp := int64(10) - res := NewValue(data).Int() - if res != exp { - t.Errorf("Exprected %d Got res", exp, res) - } + c.Assert(NewValue(data).Int(), checker.DeepEquals, exp) } -- cgit From d9ccbf04dbdcda527c00e561aa0cc97ffde08f86 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 11 Nov 2014 19:05:23 +0100 Subject: Move test bootstrap to main_test.go --- ethutil/main_test.go | 8 ++++++++ ethutil/size_test.go | 8 ++++++-- ethutil/value_test.go | 3 --- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 ethutil/main_test.go (limited to 'ethutil') 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/size_test.go b/ethutil/size_test.go index 9127521cb..098e41cd4 100644 --- a/ethutil/size_test.go +++ b/ethutil/size_test.go @@ -1,10 +1,14 @@ package ethutil import ( - "testing" + checker "gopkg.in/check.v1" ) -func TestStorageSizeString(t *testing.T) { +type SizeSuite struct{} + +var _ = checker.Suite(&SizeSuite{}) + +func (s *SizeSuite) TestStorageSizeString(c *checker.C) { data1 := 2381273 data2 := 2192 data3 := 12 diff --git a/ethutil/value_test.go b/ethutil/value_test.go index 1c62d3e20..7c58d3b56 100644 --- a/ethutil/value_test.go +++ b/ethutil/value_test.go @@ -3,11 +3,8 @@ package ethutil import ( checker "gopkg.in/check.v1" "math/big" - "testing" ) -func Test(t *testing.T) { checker.TestingT(t) } - type ValueSuite struct{} var _ = checker.Suite(&ValueSuite{}) -- cgit From bfd1fe977e1a4f921fce6b4ca522b024df0b2733 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 11 Nov 2014 19:05:35 +0100 Subject: Update test style to checker --- ethutil/size_test.go | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'ethutil') diff --git a/ethutil/size_test.go b/ethutil/size_test.go index 098e41cd4..e0f28abc5 100644 --- a/ethutil/size_test.go +++ b/ethutil/size_test.go @@ -17,19 +17,7 @@ func (s *SizeSuite) TestStorageSizeString(c *checker.C) { exp2 := "2.19 kB" exp3 := "12.00 B" - res1 := StorageSize(data1).String() - res2 := StorageSize(data2).String() - res3 := StorageSize(data3).String() - - if res1 != exp1 { - t.Errorf("Expected %s got %s", exp1, res1) - } - - if res2 != exp2 { - t.Errorf("Expected %s got %s", exp2, res2) - } - - if res3 != exp3 { - t.Errorf("Expected %s got %s", exp3, res3) - } + c.Assert(StorageSize(data1).String(), checker.Equals, exp1) + c.Assert(StorageSize(data2).String(), checker.Equals, exp2) + c.Assert(StorageSize(data3).String(), checker.Equals, exp3) } -- cgit From c24d143c9e08f9782d7ad063868ea7c647db2a03 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 11 Nov 2014 19:18:52 +0100 Subject: Convert rand_test to checker --- ethutil/rand_test.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'ethutil') diff --git a/ethutil/rand_test.go b/ethutil/rand_test.go index cc64c73e5..c12698538 100644 --- a/ethutil/rand_test.go +++ b/ethutil/rand_test.go @@ -1,9 +1,17 @@ package ethutil import ( - "testing" + checker "gopkg.in/check.v1" ) -func TestRandomUint64(t *testing.T) { - RandomUint64() +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) } -- cgit From cff0d93105956d716d32535af64ad3154f1628ae Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 11 Nov 2014 19:38:23 +0100 Subject: Converts bytes_test to checker --- ethutil/bytes_test.go | 181 ++++++++++++++++++-------------------------------- 1 file changed, 65 insertions(+), 116 deletions(-) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index c7e696b55..f334c4f57 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -1,23 +1,24 @@ package ethutil import ( - "bytes" - "testing" + checker "gopkg.in/check.v1" ) -func TestByteString(t *testing.T) { +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() - if res != exp { - t.Errorf("Expected %s got %s", exp, res) - } + c.Assert(res, checker.Equals, exp) } /* -func TestDeleteFromByteSlice(t *testing.T) { +func (s *BytesSuite) TestDeleteFromByteSlice(c *checker.C) { data := []byte{1, 2, 3, 4} slice := []byte{1, 2, 3, 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) exp := []byte{0, 0, 0, 0, 0, 0, 0, 1} // 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} datalarge := []byte{1, 2, 3} expsmall := uint64(0) @@ -46,15 +47,13 @@ func TestBytesToNumber(t *testing.T) { // TODO this fails. why? ressmall := BytesToNumber(datasmall) reslarge := BytesToNumber(datalarge) - if ressmall != expsmall { - t.Errorf("Expected %d Got %d", expsmall, ressmall) - } - if reslarge != explarge { - t.Errorf("Expected %d Got %d", explarge, reslarge) - } + + c.Assert(ressmall, checker.DeepEquals, expsmall) + c.Assert(reslarge, checker.DeepEquals, explarge) + } -func TestReadVarInt(t *testing.T) { +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} @@ -70,24 +69,13 @@ func TestReadVarInt(t *testing.T) { res2 := ReadVarInt(data2) res1 := ReadVarInt(data1) - if res8 != exp8 { - t.Errorf("Expected %d | Got %d", exp8, res8) - } - - 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) - } + 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 TestBinaryLength(t *testing.T) { +func (s *BytesSuite) TestBinaryLength(c *checker.C) { data1 := 0 data2 := 920987656789 @@ -97,80 +85,64 @@ func TestBinaryLength(t *testing.T) { res1 := BinaryLength(data1) res2 := BinaryLength(data2) - if res1 != exp1 { - t.Errorf("Expected %d got %d", exp1, res1) - } - - if res2 != exp2 { - t.Errorf("Expected %d got %d", exp2, res2) - } + c.Assert(res1, checker.Equals, exp1) + c.Assert(res2, checker.Equals, exp2) } -func TestCopyBytes(t *testing.T) { +func (s *BytesSuite) TestCopyBytes(c *checker.C) { data1 := []byte{1, 2, 3, 4} exp1 := []byte{1, 2, 3, 4} res1 := CopyBytes(data1) - if bytes.Compare(res1, exp1) != 0 { - t.Errorf("Expected % x got % x", exp1, res1) - } + c.Assert(res1, checker.DeepEquals, exp1) } -func TestIsHex(t *testing.T) { +func (s *BytesSuite) TestIsHex(c *checker.C) { data1 := "a9e67e" exp1 := false res1 := IsHex(data1) - if exp1 != res1 { - t.Errorf("Expected % x Got % x", exp1, res1) - } + c.Assert(res1, checker.DeepEquals, exp1) data2 := "0xa9e67e00" exp2 := true res2 := IsHex(data2) - if exp2 != res2 { - t.Errorf("Expected % x Got % x", exp2, res2) - } + c.Assert(res2, checker.DeepEquals, exp2) + } -func TestParseDataString(t *testing.T) { - data := ParseData("hello", "world", "0x0106") - exp := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000" - if bytes.Compare(data, Hex2Bytes(exp)) != 0 { - t.Error("Error parsing data") - } +func (s *BytesSuite) TestParseDataString(c *checker.C) { + res1 := ParseData("hello", "world", "0x0106") + data := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000" + exp1 := Hex2Bytes(data) + c.Assert(res1, checker.DeepEquals, exp1) } -func TestParseDataBytes(t *testing.T) { - data := []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} +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) - res := ParseData(data) - if bytes.Compare(res, exp) != 0 { - t.Errorf("Expected %x got %x", exp, res) - } } -func TestLeftPadBytes(t *testing.T) { - val := []byte{1, 2, 3, 4} - exp := []byte{0, 0, 0, 0, 1, 2, 3, 4} +func (s *BytesSuite) TestLeftPadBytes(c *checker.C) { + val1 := []byte{1, 2, 3, 4} + exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4} - resstd := LeftPadBytes(val, 8) - if bytes.Compare(resstd, exp) != 0 { - t.Errorf("Expected % x Got % x", exp, resstd) - } + res1 := LeftPadBytes(val1, 8) + res2 := LeftPadBytes(val1, 2) - resshrt := LeftPadBytes(val, 2) - if bytes.Compare(resshrt, val) != 0 { - t.Errorf("Expected % x Got % x", exp, resshrt) - } + c.Assert(res1, checker.DeepEquals, exp1) + c.Assert(res2, checker.DeepEquals, val1) } -func TestFormatData(t *testing.T) { +func (s *BytesSuite) TestFormatData(c *checker.C) { data1 := "" data2 := "0xa9e67e00" data3 := "a9e67e" 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} 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} @@ -180,64 +152,41 @@ func TestFormatData(t *testing.T) { res3 := FormatData(data3) res4 := FormatData(data4) - if bytes.Compare(res1, exp1) != 0 { - t.Errorf("Expected % x Got % x", exp1, res1) - } - - 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) - } + c.Assert(res1, checker.IsNil) + c.Assert(res2, checker.DeepEquals, exp2) + c.Assert(res3, checker.DeepEquals, exp3) + c.Assert(res4, checker.DeepEquals, exp4) } -func TestRightPadBytes(t *testing.T) { +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) - if bytes.Compare(resstd, exp) != 0 { - t.Errorf("Expected % x Got % x", exp, resstd) - } - 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" + exp := "\x30\x30\x30\x30" + val resstd := LeftPadString(val, 8) - - if resstd != "\x30\x30\x30\x30"+val { - t.Errorf("Expected % x Got % x", val, resstd) - } - resshrt := LeftPadString(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) } -func TestRightPadString(t *testing.T) { +func (s *BytesSuite) TestRightPadString(c *checker.C) { val := "test" + exp := val + "\x30\x30\x30\x30" resstd := RightPadString(val, 8) - if resstd != val+"\x30\x30\x30\x30" { - t.Errorf("Expected % x Got % x", val, resstd) - } - 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) } -- cgit From 0d1cdd26d657f6559b52332e207e1cc9e7db7c67 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 11 Nov 2014 20:03:50 +0100 Subject: Update TestNumberToBytes --- ethutil/bytes_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index f334c4f57..358df9f83 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -28,16 +28,17 @@ func (s *BytesSuite) TestDeleteFromByteSlice(c *checker.C) { } } +*/ func (s *BytesSuite) TestNumberToBytes(c *checker.C) { - data := int(1) - exp := []byte{0, 0, 0, 0, 0, 0, 0, 1} - // TODO this fails. why? - res := NumberToBytes(data, 16) - if bytes.Compare(res, exp) != 0 { - t.Errorf("Expected % x Got % x", exp, res) - } + // 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{0, 1} -- cgit From 12e8404f8fb0409c6db2a48ff4cc52c7aa81f72d Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 11 Nov 2014 20:04:03 +0100 Subject: Fix TestBytestoNumber --- ethutil/bytes_test.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index 358df9f83..d65336631 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -41,16 +41,17 @@ func (s *BytesSuite) TestNumberToBytes(c *checker.C) { } func (s *BytesSuite) TestBytesToNumber(c *checker.C) { - datasmall := []byte{0, 1} - datalarge := []byte{1, 2, 3} - expsmall := uint64(0) - explarge := uint64(0) - // TODO this fails. why? + 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.DeepEquals, expsmall) - c.Assert(reslarge, checker.DeepEquals, explarge) + c.Assert(ressmall, checker.Equals, expsmall) + c.Assert(reslarge, checker.Equals, explarge) } -- cgit From 5c5df21e3d8768481c2bc1b6e9475099590e10be Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 11 Nov 2014 20:05:27 +0100 Subject: Update TestDeleteFromByteSlice Still broken --- ethutil/bytes_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'ethutil') diff --git a/ethutil/bytes_test.go b/ethutil/bytes_test.go index d65336631..179a8c7ef 100644 --- a/ethutil/bytes_test.go +++ b/ethutil/bytes_test.go @@ -23,9 +23,8 @@ func (s *BytesSuite) TestDeleteFromByteSlice(c *checker.C) { slice := []byte{1, 2, 3, 4} exp := []byte{1, 4} res := DeleteFromByteSlice(data, slice) - if bytes.Compare(res, exp) != 0 { - t.Errorf("Expected % x Got % x", exp, res) - } + + c.Assert(res, checker.DeepEquals, exp) } */ -- cgit