aboutsummaryrefslogtreecommitdiffstats
path: root/mobile/interface_test.go
blob: 4bd1af47aa1df8b7139fa6b90e244836f2096dee (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
81
82
83
84
85
86
87
88
89
90
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// 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.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package geth

import (
    "fmt"
    "math/big"
    "reflect"
    "testing"

    "github.com/ethereum/go-ethereum/common"
)

func TestInterfaceGetSet(t *testing.T) {
    var tests = []struct {
        method string
        input  interface{}
        expect interface{}
    }{
        {"Bool", true, true},
        {"Bool", false, false},
        {"Bools", &Bools{[]bool{false, true}}, &Bools{[]bool{false, true}}},
        {"String", "go-ethereum", "go-ethereum"},
        {"Strings", &Strings{strs: []string{"hello", "world"}}, &Strings{strs: []string{"hello", "world"}}},
        {"Binary", []byte{0x01, 0x02}, []byte{0x01, 0x02}},
        {"Binaries", &Binaries{[][]byte{{0x01, 0x02}, {0x03, 0x04}}}, &Binaries{[][]byte{{0x01, 0x02}, {0x03, 0x04}}}},
        {"Address", &Address{common.HexToAddress("deadbeef")}, &Address{common.HexToAddress("deadbeef")}},
        {"Addresses", &Addresses{[]common.Address{common.HexToAddress("deadbeef"), common.HexToAddress("cafebabe")}}, &Addresses{[]common.Address{common.HexToAddress("deadbeef"), common.HexToAddress("cafebabe")}}},
        {"Hash", &Hash{common.HexToHash("deadbeef")}, &Hash{common.HexToHash("deadbeef")}},
        {"Hashes", &Hashes{[]common.Hash{common.HexToHash("deadbeef"), common.HexToHash("cafebabe")}}, &Hashes{[]common.Hash{common.HexToHash("deadbeef"), common.HexToHash("cafebabe")}}},
        {"Int8", int8(1), int8(1)},
        {"Int16", int16(1), int16(1)},
        {"Int32", int32(1), int32(1)},
        {"Int64", int64(1), int64(1)},
        {"Int8s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
        {"Int16s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
        {"Int32s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
        {"Int64s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
        {"Uint8", NewBigInt(1), NewBigInt(1)},
        {"Uint16", NewBigInt(1), NewBigInt(1)},
        {"Uint32", NewBigInt(1), NewBigInt(1)},
        {"Uint64", NewBigInt(1), NewBigInt(1)},
        {"Uint8s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
        {"Uint16s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
        {"Uint32s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
        {"Uint64s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
        {"BigInt", NewBigInt(1), NewBigInt(1)},
        {"BigInts", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
    }

    args := NewInterfaces(len(tests))

    callFn := func(receiver interface{}, method string, arg interface{}) interface{} {
        rval := reflect.ValueOf(receiver)
        rval.MethodByName(fmt.Sprintf("Set%s", method)).Call([]reflect.Value{reflect.ValueOf(arg)})
        res := rval.MethodByName(fmt.Sprintf("Get%s", method)).Call(nil)
        if len(res) > 0 {
            return res[0].Interface()
        }
        return nil
    }

    for index, c := range tests {
        // In theory the change of iface shouldn't effect the args value
        iface, _ := args.Get(index)
        result := callFn(iface, c.method, c.input)
        if !reflect.DeepEqual(result, c.expect) {
            t.Errorf("Interface get/set mismatch, want %v, got %v", c.expect, result)
        }
        // Check whether the underlying value in args is still zero
        iface, _ = args.Get(index)
        if iface.object != nil {
            t.Error("Get operation is not write safe")
        }
    }
}