Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increased code coverage #182

Merged
merged 37 commits into from
Nov 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e22e8b7
Environment tests
tgerring Nov 4, 2014
123282e
Update variable name to match unit name
tgerring Nov 4, 2014
e7d9bcd
Added Douglas and Einstein cases
tgerring Nov 4, 2014
b96a59e
Added tests for extra large values
tgerring Nov 4, 2014
92299b7
New test coverage for ethutil/path.go
tgerring Nov 5, 2014
e76c58d
New test coverage for ethutil/big.go
tgerring Nov 5, 2014
94b0ce8
Cleanup big_test.go
tgerring Nov 5, 2014
4f00929
Added byte padding tests
tgerring Nov 5, 2014
cb32f52
added test for parsing bytes
tgerring Nov 5, 2014
92b30cc
add tests for ReadVarInt
tgerring Nov 5, 2014
ada684e
added test for BinaryLength
tgerring Nov 5, 2014
834f8a1
added test for CopyBytes
tgerring Nov 5, 2014
b100546
add test for Bytes.String()
tgerring Nov 5, 2014
4e15ada
Remove fmt dependency
tgerring Nov 5, 2014
ab6b9c4
Added test for IsHex
tgerring Nov 5, 2014
8f94f73
Reorder tests to match source order
tgerring Nov 5, 2014
be96da1
Added tests for FormatData
tgerring Nov 5, 2014
a1d62ab
Restructure StorageSize string test
tgerring Nov 5, 2014
48a3f09
Add coverage for rand
tgerring Nov 5, 2014
0a3a148
Added more byte tests
tgerring Nov 6, 2014
8f3a03c
Update state tests to use gocheck
tgerring Nov 11, 2014
3c619ba
Add verbose comments to TestSnapshot
tgerring Nov 11, 2014
cd94b5f
Convert value_test to use gocheck
tgerring Nov 11, 2014
d9ccbf0
Move test bootstrap to main_test.go
tgerring Nov 11, 2014
bfd1fe9
Update test style to checker
tgerring Nov 11, 2014
c24d143
Convert rand_test to checker
tgerring Nov 11, 2014
cff0d93
Converts bytes_test to checker
tgerring Nov 11, 2014
0d1cdd2
Update TestNumberToBytes
tgerring Nov 11, 2014
12e8404
Fix TestBytestoNumber
tgerring Nov 11, 2014
5c5df21
Update TestDeleteFromByteSlice
tgerring Nov 11, 2014
1d866b5
Merge pull request #1 from tgerring/tests
tgerring Nov 11, 2014
00878e5
Convert trie tests to gocheck
tgerring Nov 12, 2014
6eacc8e
eth-go -> go-ethereum
tgerring Nov 12, 2014
bd9bd4a
Reorg state tests
tgerring Nov 12, 2014
fa59db7
Add initial state/TestDump test
tgerring Nov 12, 2014
461324a
Remove references to mutan
tgerring Nov 12, 2014
313cfba
convert trie encoding tests to checker
tgerring Nov 13, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chain/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/vm"
"github.com/ethgo.old/ethutil"
)

func Disassemble(script []byte) (asm []string) {
Expand Down
73 changes: 73 additions & 0 deletions ethutil/big_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
195 changes: 187 additions & 8 deletions ethutil/bytes_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions ethutil/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
73 changes: 64 additions & 9 deletions ethutil/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
}
Loading