common_test.go 2.48 KB
Newer Older
obscuren's avatar
obscuren committed
1
package common
obscuren's avatar
obscuren committed
2 3

import (
4
	"bytes"
obscuren's avatar
obscuren committed
5
	"math/big"
6
	"os"
7
	"testing"
8 9

	checker "gopkg.in/check.v1"
obscuren's avatar
obscuren committed
10 11
)

12
type CommonSuite struct{}
13

14 15 16 17 18
var _ = checker.Suite(&CommonSuite{})

func (s *CommonSuite) TestOS(c *checker.C) {
	expwin := (os.PathSeparator == '\\' && os.PathListSeparator == ';')
	res := IsWindows()
19

20 21 22 23
	if !expwin {
		c.Assert(res, checker.Equals, expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator))
	} else {
		c.Assert(res, checker.Not(checker.Equals), expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator))
24 25 26
	}
}

27 28
func (s *CommonSuite) TestWindonziePath(c *checker.C) {
	iswindowspath := os.PathSeparator == '\\'
29 30
	path := "/opt/eth/test/file.ext"
	res := WindonizePath(path)
31
	ressep := string(res[0])
32

33 34 35 36
	if !iswindowspath {
		c.Assert(ressep, checker.Equals, "/")
	} else {
		c.Assert(ressep, checker.Not(checker.Equals), "/")
37 38 39
	}
}

40
func (s *CommonSuite) TestCommon(c *checker.C) {
41 42
	douglas := CurrencyToString(BigPow(10, 43))
	einstein := CurrencyToString(BigPow(10, 22))
obscuren's avatar
obscuren committed
43
	ether := CurrencyToString(BigPow(10, 19))
Taylor Gerring's avatar
Taylor Gerring committed
44
	finney := CurrencyToString(BigPow(10, 16))
obscuren's avatar
obscuren committed
45
	szabo := CurrencyToString(BigPow(10, 13))
46 47 48
	shannon := CurrencyToString(BigPow(10, 10))
	babbage := CurrencyToString(BigPow(10, 7))
	ada := CurrencyToString(BigPow(10, 4))
obscuren's avatar
obscuren committed
49 50
	wei := CurrencyToString(big.NewInt(10))

51 52 53 54 55 56 57 58 59
	c.Assert(douglas, checker.Equals, "10 Douglas")
	c.Assert(einstein, checker.Equals, "10 Einstein")
	c.Assert(ether, checker.Equals, "10 Ether")
	c.Assert(finney, checker.Equals, "10 Finney")
	c.Assert(szabo, checker.Equals, "10 Szabo")
	c.Assert(shannon, checker.Equals, "10 Shannon")
	c.Assert(babbage, checker.Equals, "10 Babbage")
	c.Assert(ada, checker.Equals, "10 Ada")
	c.Assert(wei, checker.Equals, "10 Wei")
obscuren's avatar
obscuren committed
60
}
61

62
func (s *CommonSuite) TestLarge(c *checker.C) {
63 64 65 66
	douglaslarge := CurrencyToString(BigPow(100000000, 43))
	adalarge := CurrencyToString(BigPow(100000000, 4))
	weilarge := CurrencyToString(big.NewInt(100000000))

67 68 69
	c.Assert(douglaslarge, checker.Equals, "10000E298 Douglas")
	c.Assert(adalarge, checker.Equals, "10000E7 Einstein")
	c.Assert(weilarge, checker.Equals, "100 Babbage")
70
}
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

//fromHex
func TestFromHex(t *testing.T) {
	input := "0x01"
	expected := []byte{1}
	result := FromHex(input)
	if bytes.Compare(expected, result) != 0 {
		t.Errorf("Expected % x got % x", expected, result)
	}
}

func TestFromHexOddLength(t *testing.T) {
	input := "0x1"
	expected := []byte{1}
	result := FromHex(input)
	if bytes.Compare(expected, result) != 0 {
		t.Errorf("Expected % x got % x", expected, result)
	}
}