Skip to content

Commit 2a9fc7b

Browse files
committed
Merge branch 'develop' of https://github.com/tgerring/go-ethereum
1 parent 56aa240 commit 2a9fc7b

15 files changed

Lines changed: 661 additions & 306 deletions

‎ethutil/big_test.go‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package ethutil
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
)
7+
8+
func TestMisc(t *testing.T) {
9+
a := Big("10")
10+
b := Big("57896044618658097711785492504343953926634992332820282019728792003956564819968")
11+
c := []byte{1, 2, 3, 4}
12+
z := BitTest(a, 1)
13+
14+
if z != true {
15+
t.Error("Expected true got", z)
16+
}
17+
18+
U256(a)
19+
S256(a)
20+
21+
U256(b)
22+
S256(b)
23+
24+
BigD(c)
25+
}
26+
27+
func TestBigMax(t *testing.T) {
28+
a := Big("10")
29+
b := Big("5")
30+
31+
max1 := BigMax(a, b)
32+
if max1 != a {
33+
t.Errorf("Expected %d got %d", a, max1)
34+
}
35+
36+
max2 := BigMax(b, a)
37+
if max2 != a {
38+
t.Errorf("Expected %d got %d", a, max2)
39+
}
40+
}
41+
42+
func TestBigMin(t *testing.T) {
43+
a := Big("10")
44+
b := Big("5")
45+
46+
min1 := BigMin(a, b)
47+
if min1 != b {
48+
t.Errorf("Expected %d got %d", b, min1)
49+
}
50+
51+
min2 := BigMin(b, a)
52+
if min2 != b {
53+
t.Errorf("Expected %d got %d", b, min2)
54+
}
55+
}
56+
57+
func TestBigCopy(t *testing.T) {
58+
a := Big("10")
59+
b := BigCopy(a)
60+
c := Big("1000000000000")
61+
y := BigToBytes(b, 16)
62+
ybytes := []byte{0, 10}
63+
z := BigToBytes(c, 16)
64+
zbytes := []byte{232, 212, 165, 16, 0}
65+
66+
if bytes.Compare(y, ybytes) != 0 {
67+
t.Error("Got", ybytes)
68+
}
69+
70+
if bytes.Compare(z, zbytes) != 0 {
71+
t.Error("Got", zbytes)
72+
}
73+
}

‎ethutil/bytes_test.go‎

Lines changed: 187 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,193 @@
11
package ethutil
22

33
import (
4-
"bytes"
5-
"testing"
4+
checker "gopkg.in/check.v1"
65
)
76

8-
func TestParseData(t *testing.T) {
9-
data := ParseData("hello", "world", "0x0106")
10-
exp := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000"
11-
if bytes.Compare(data, Hex2Bytes(exp)) != 0 {
12-
t.Error("Error parsing data")
13-
}
7+
type BytesSuite struct{}
8+
9+
var _ = checker.Suite(&BytesSuite{})
10+
11+
func (s *BytesSuite) TestByteString(c *checker.C) {
12+
var data Bytes
13+
data = []byte{102, 111, 111}
14+
exp := "foo"
15+
res := data.String()
16+
17+
c.Assert(res, checker.Equals, exp)
18+
}
19+
20+
/*
21+
func (s *BytesSuite) TestDeleteFromByteSlice(c *checker.C) {
22+
data := []byte{1, 2, 3, 4}
23+
slice := []byte{1, 2, 3, 4}
24+
exp := []byte{1, 4}
25+
res := DeleteFromByteSlice(data, slice)
26+
27+
c.Assert(res, checker.DeepEquals, exp)
28+
}
29+
30+
*/
31+
func (s *BytesSuite) TestNumberToBytes(c *checker.C) {
32+
// data1 := int(1)
33+
// res1 := NumberToBytes(data1, 16)
34+
// c.Check(res1, checker.Panics)
35+
36+
var data2 float64 = 3.141592653
37+
exp2 := []byte{0xe9, 0x38}
38+
res2 := NumberToBytes(data2, 16)
39+
c.Assert(res2, checker.DeepEquals, exp2)
40+
}
41+
42+
func (s *BytesSuite) TestBytesToNumber(c *checker.C) {
43+
datasmall := []byte{0xe9, 0x38, 0xe9, 0x38}
44+
datalarge := []byte{0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38}
45+
46+
var expsmall uint64 = 0xe938e938
47+
var explarge uint64 = 0x0
48+
49+
ressmall := BytesToNumber(datasmall)
50+
reslarge := BytesToNumber(datalarge)
51+
52+
c.Assert(ressmall, checker.Equals, expsmall)
53+
c.Assert(reslarge, checker.Equals, explarge)
54+
55+
}
56+
57+
func (s *BytesSuite) TestReadVarInt(c *checker.C) {
58+
data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8}
59+
data4 := []byte{1, 2, 3, 4}
60+
data2 := []byte{1, 2}
61+
data1 := []byte{1}
62+
63+
exp8 := uint64(72623859790382856)
64+
exp4 := uint64(16909060)
65+
exp2 := uint64(258)
66+
exp1 := uint64(1)
67+
68+
res8 := ReadVarInt(data8)
69+
res4 := ReadVarInt(data4)
70+
res2 := ReadVarInt(data2)
71+
res1 := ReadVarInt(data1)
72+
73+
c.Assert(res8, checker.Equals, exp8)
74+
c.Assert(res4, checker.Equals, exp4)
75+
c.Assert(res2, checker.Equals, exp2)
76+
c.Assert(res1, checker.Equals, exp1)
77+
}
78+
79+
func (s *BytesSuite) TestBinaryLength(c *checker.C) {
80+
data1 := 0
81+
data2 := 920987656789
82+
83+
exp1 := 0
84+
exp2 := 5
85+
86+
res1 := BinaryLength(data1)
87+
res2 := BinaryLength(data2)
88+
89+
c.Assert(res1, checker.Equals, exp1)
90+
c.Assert(res2, checker.Equals, exp2)
91+
}
92+
93+
func (s *BytesSuite) TestCopyBytes(c *checker.C) {
94+
data1 := []byte{1, 2, 3, 4}
95+
exp1 := []byte{1, 2, 3, 4}
96+
res1 := CopyBytes(data1)
97+
c.Assert(res1, checker.DeepEquals, exp1)
98+
}
99+
100+
func (s *BytesSuite) TestIsHex(c *checker.C) {
101+
data1 := "a9e67e"
102+
exp1 := false
103+
res1 := IsHex(data1)
104+
c.Assert(res1, checker.DeepEquals, exp1)
105+
106+
data2 := "0xa9e67e00"
107+
exp2 := true
108+
res2 := IsHex(data2)
109+
c.Assert(res2, checker.DeepEquals, exp2)
110+
111+
}
112+
113+
func (s *BytesSuite) TestParseDataString(c *checker.C) {
114+
res1 := ParseData("hello", "world", "0x0106")
115+
data := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000"
116+
exp1 := Hex2Bytes(data)
117+
c.Assert(res1, checker.DeepEquals, exp1)
118+
}
119+
120+
func (s *BytesSuite) TestParseDataBytes(c *checker.C) {
121+
data1 := []byte{232, 212, 165, 16, 0}
122+
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}
123+
124+
res1 := ParseData(data1)
125+
c.Assert(res1, checker.DeepEquals, exp1)
126+
127+
}
128+
129+
func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
130+
val1 := []byte{1, 2, 3, 4}
131+
exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
132+
133+
res1 := LeftPadBytes(val1, 8)
134+
res2 := LeftPadBytes(val1, 2)
135+
136+
c.Assert(res1, checker.DeepEquals, exp1)
137+
c.Assert(res2, checker.DeepEquals, val1)
138+
}
139+
140+
func (s *BytesSuite) TestFormatData(c *checker.C) {
141+
data1 := ""
142+
data2 := "0xa9e67e00"
143+
data3 := "a9e67e"
144+
data4 := "\"a9e67e00\""
145+
146+
// exp1 := []byte{}
147+
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}
148+
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}
149+
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}
150+
151+
res1 := FormatData(data1)
152+
res2 := FormatData(data2)
153+
res3 := FormatData(data3)
154+
res4 := FormatData(data4)
155+
156+
c.Assert(res1, checker.IsNil)
157+
c.Assert(res2, checker.DeepEquals, exp2)
158+
c.Assert(res3, checker.DeepEquals, exp3)
159+
c.Assert(res4, checker.DeepEquals, exp4)
160+
}
161+
162+
func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
163+
val := []byte{1, 2, 3, 4}
164+
exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}
165+
166+
resstd := RightPadBytes(val, 8)
167+
resshrt := RightPadBytes(val, 2)
168+
169+
c.Assert(resstd, checker.DeepEquals, exp)
170+
c.Assert(resshrt, checker.DeepEquals, val)
171+
}
172+
173+
func (s *BytesSuite) TestLeftPadString(c *checker.C) {
174+
val := "test"
175+
exp := "\x30\x30\x30\x30" + val
176+
177+
resstd := LeftPadString(val, 8)
178+
resshrt := LeftPadString(val, 2)
179+
180+
c.Assert(resstd, checker.Equals, exp)
181+
c.Assert(resshrt, checker.Equals, val)
182+
}
183+
184+
func (s *BytesSuite) TestRightPadString(c *checker.C) {
185+
val := "test"
186+
exp := val + "\x30\x30\x30\x30"
187+
188+
resstd := RightPadString(val, 8)
189+
resshrt := RightPadString(val, 2)
190+
191+
c.Assert(resstd, checker.Equals, exp)
192+
c.Assert(resshrt, checker.Equals, val)
14193
}

‎ethutil/common.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func CurrencyToString(num *big.Int) string {
6666
denom = "Ada"
6767
}
6868

69+
// TODO add comment clarifying expected behavior
6970
if len(fin.String()) > 5 {
7071
return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
7172
}

‎ethutil/common_test.go‎

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,55 @@ package ethutil
22

33
import (
44
"math/big"
5+
"os"
56
"testing"
67
)
78

9+
func TestOS(t *testing.T) {
10+
res := IsWindows()
11+
12+
if res && (os.PathSeparator != '\\' || os.PathListSeparator != ';') {
13+
t.Error("IsWindows is", res, "but path is", os.PathSeparator)
14+
}
15+
16+
if !res && (os.PathSeparator == '\\' && os.PathListSeparator == ';') {
17+
t.Error("IsWindows is", res, "but path is", os.PathSeparator)
18+
}
19+
}
20+
21+
func TestWindonziePath(t *testing.T) {
22+
path := "/opt/eth/test/file.ext"
23+
res := WindonizePath(path)
24+
iswindowspath := os.PathSeparator == '\\'
25+
26+
if !iswindowspath && string(res[0]) != "/" {
27+
t.Error("Got", res)
28+
}
29+
30+
if iswindowspath && string(res[0]) == "/" {
31+
t.Error("Got", res)
32+
}
33+
}
34+
835
func TestCommon(t *testing.T) {
36+
douglas := CurrencyToString(BigPow(10, 43))
37+
einstein := CurrencyToString(BigPow(10, 22))
938
ether := CurrencyToString(BigPow(10, 19))
1039
finney := CurrencyToString(BigPow(10, 16))
1140
szabo := CurrencyToString(BigPow(10, 13))
12-
vito := CurrencyToString(BigPow(10, 10))
13-
turing := CurrencyToString(BigPow(10, 7))
14-
eins := CurrencyToString(BigPow(10, 4))
41+
shannon := CurrencyToString(BigPow(10, 10))
42+
babbage := CurrencyToString(BigPow(10, 7))
43+
ada := CurrencyToString(BigPow(10, 4))
1544
wei := CurrencyToString(big.NewInt(10))
1645

46+
if douglas != "10 Douglas" {
47+
t.Error("Got", douglas)
48+
}
49+
50+
if einstein != "10 Einstein" {
51+
t.Error("Got", einstein)
52+
}
53+
1754
if ether != "10 Ether" {
1855
t.Error("Got", ether)
1956
}
@@ -26,19 +63,37 @@ func TestCommon(t *testing.T) {
2663
t.Error("Got", szabo)
2764
}
2865

29-
if vito != "10 Shannon" {
30-
t.Error("Got", vito)
66+
if shannon != "10 Shannon" {
67+
t.Error("Got", shannon)
3168
}
3269

33-
if turing != "10 Babbage" {
34-
t.Error("Got", turing)
70+
if babbage != "10 Babbage" {
71+
t.Error("Got", babbage)
3572
}
3673

37-
if eins != "10 Ada" {
38-
t.Error("Got", eins)
74+
if ada != "10 Ada" {
75+
t.Error("Got", ada)
3976
}
4077

4178
if wei != "10 Wei" {
4279
t.Error("Got", wei)
4380
}
4481
}
82+
83+
func TestLarge(t *testing.T) {
84+
douglaslarge := CurrencyToString(BigPow(100000000, 43))
85+
adalarge := CurrencyToString(BigPow(100000000, 4))
86+
weilarge := CurrencyToString(big.NewInt(100000000))
87+
88+
if douglaslarge != "10000E298 Douglas" {
89+
t.Error("Got", douglaslarge)
90+
}
91+
92+
if adalarge != "10000E7 Einstein" {
93+
t.Error("Got", adalarge)
94+
}
95+
96+
if weilarge != "100 Babbage" {
97+
t.Error("Got", weilarge)
98+
}
99+
}

‎ethutil/main_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ethutil
2+
3+
import (
4+
checker "gopkg.in/check.v1"
5+
"testing"
6+
)
7+
8+
func Test(t *testing.T) { checker.TestingT(t) }

0 commit comments

Comments
 (0)