mirror of https://github.com/ethereum/go-ethereum
commit
1d866b5e57
@ -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) |
||||
} |
||||
} |
@ -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) |
||||
} |
||||
|
@ -0,0 +1,8 @@ |
||||
package ethutil |
||||
|
||||
import ( |
||||
checker "gopkg.in/check.v1" |
||||
"testing" |
||||
) |
||||
|
||||
func Test(t *testing.T) { checker.TestingT(t) } |
@ -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) |
||||
} |
||||
|
||||
} |
@ -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) |
||||
} |
@ -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) |
||||
} |
||||
|
@ -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) |
||||
} |
||||
|
Loading…
Reference in new issue