aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/value_test.go
blob: 710cbd8876039b3335f163742f2232a838c78cea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package ethutil

import (
    "bytes"
    "math/big"
    "testing"
)

func TestValueCmp(t *testing.T) {
    val1 := NewValue("hello")
    val2 := NewValue("world")
    if val1.Cmp(val2) {
        t.Error("Expected values not to be equal")
    }

    val3 := NewValue("hello")
    val4 := NewValue("hello")
    if !val3.Cmp(val4) {
        t.Error("Expected values to be equal")
    }
}

func TestValueTypes(t *testing.T) {
    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())
    }

    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())
    }
}

func TestIterator(t *testing.T) {
    value := NewValue([]interface{}{1, 2, 3})
    it := 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())
        }
        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)
    }

    a = NewValue(2)
    a.Sub(1).Sub(1)
    if !a.DeepCmp(NewValue(0)) {
        t.Error("Expected 0, got", a)
    }
}