value_test.go 2.41 KB
Newer Older
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
5 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16

obscuren's avatar
obscuren committed
17
package common
obscuren's avatar
obscuren committed
18 19

import (
obscuren's avatar
obscuren committed
20
	"math/big"
21 22

	checker "gopkg.in/check.v1"
obscuren's avatar
obscuren committed
23 24
)

25 26 27 28 29
type ValueSuite struct{}

var _ = checker.Suite(&ValueSuite{})

func (s *ValueSuite) TestValueCmp(c *checker.C) {
obscuren's avatar
obscuren committed
30 31
	val1 := NewValue("hello")
	val2 := NewValue("world")
32
	c.Assert(val1.Cmp(val2), checker.Equals, false)
obscuren's avatar
obscuren committed
33 34 35

	val3 := NewValue("hello")
	val4 := NewValue("hello")
36
	c.Assert(val3.Cmp(val4), checker.Equals, true)
obscuren's avatar
obscuren committed
37 38
}

39
func (s *ValueSuite) TestValueTypes(c *checker.C) {
obscuren's avatar
obscuren committed
40 41 42
	str := NewValue("str")
	num := NewValue(1)
	inter := NewValue([]interface{}{1})
obscuren's avatar
obscuren committed
43 44
	byt := NewValue([]byte{1, 2, 3, 4})
	bigInt := NewValue(big.NewInt(10))
obscuren's avatar
obscuren committed
45

46 47
	strExp := "str"
	numExp := uint64(1)
obscuren's avatar
obscuren committed
48 49 50
	interExp := []interface{}{1}
	bytExp := []byte{1, 2, 3, 4}
	bigExp := big.NewInt(10)
51 52 53

	c.Assert(str.Str(), checker.Equals, strExp)
	c.Assert(num.Uint(), checker.Equals, numExp)
54
	c.Assert(NewValue(inter.Val).Cmp(NewValue(interExp)), checker.Equals, true)
55 56
	c.Assert(byt.Bytes(), checker.DeepEquals, bytExp)
	c.Assert(bigInt.BigInt(), checker.DeepEquals, bigExp)
obscuren's avatar
obscuren committed
57
}
58

59
func (s *ValueSuite) TestIterator(c *checker.C) {
60
	value := NewValue([]interface{}{1, 2, 3})
61
	iter := value.NewIterator()
62 63
	values := []uint64{1, 2, 3}
	i := 0
64 65
	for iter.Next() {
		c.Assert(values[i], checker.Equals, iter.Value().Uint())
66 67 68
		i++
	}
}
obscuren's avatar
obscuren committed
69

70 71 72 73 74 75 76
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)
obscuren's avatar
obscuren committed
77

78 79
	c.Assert(data1.DeepCmp(exp1), checker.Equals, true)
	c.Assert(data2.DeepCmp(exp2), checker.Equals, true)
obscuren's avatar
obscuren committed
80
}
obscuren's avatar
obscuren committed
81

82 83 84 85
func (s *ValueSuite) TestString(c *checker.C) {
	data := "10"
	exp := int64(10)
	c.Assert(NewValue(data).Int(), checker.DeepEquals, exp)
obscuren's avatar
obscuren committed
86
}