github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/mobile/interface_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package geth 19 20 import ( 21 "fmt" 22 "math/big" 23 "reflect" 24 "testing" 25 26 "github.com/AigarNetwork/aigar/common" 27 ) 28 29 func TestInterfaceGetSet(t *testing.T) { 30 var tests = []struct { 31 method string 32 input interface{} 33 expect interface{} 34 }{ 35 {"Bool", true, true}, 36 {"Bool", false, false}, 37 {"Bools", &Bools{[]bool{false, true}}, &Bools{[]bool{false, true}}}, 38 {"String", "go-ethereum", "go-ethereum"}, 39 {"Strings", &Strings{strs: []string{"hello", "world"}}, &Strings{strs: []string{"hello", "world"}}}, 40 {"Binary", []byte{0x01, 0x02}, []byte{0x01, 0x02}}, 41 {"Binaries", &Binaries{[][]byte{{0x01, 0x02}, {0x03, 0x04}}}, &Binaries{[][]byte{{0x01, 0x02}, {0x03, 0x04}}}}, 42 {"Address", &Address{common.HexToAddress("deadbeef")}, &Address{common.HexToAddress("deadbeef")}}, 43 {"Addresses", &Addresses{[]common.Address{common.HexToAddress("deadbeef"), common.HexToAddress("cafebabe")}}, &Addresses{[]common.Address{common.HexToAddress("deadbeef"), common.HexToAddress("cafebabe")}}}, 44 {"Hash", &Hash{common.HexToHash("deadbeef")}, &Hash{common.HexToHash("deadbeef")}}, 45 {"Hashes", &Hashes{[]common.Hash{common.HexToHash("deadbeef"), common.HexToHash("cafebabe")}}, &Hashes{[]common.Hash{common.HexToHash("deadbeef"), common.HexToHash("cafebabe")}}}, 46 {"Int8", int8(1), int8(1)}, 47 {"Int16", int16(1), int16(1)}, 48 {"Int32", int32(1), int32(1)}, 49 {"Int64", int64(1), int64(1)}, 50 {"Int8s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, 51 {"Int16s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, 52 {"Int32s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, 53 {"Int64s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, 54 {"Uint8", NewBigInt(1), NewBigInt(1)}, 55 {"Uint16", NewBigInt(1), NewBigInt(1)}, 56 {"Uint32", NewBigInt(1), NewBigInt(1)}, 57 {"Uint64", NewBigInt(1), NewBigInt(1)}, 58 {"Uint8s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, 59 {"Uint16s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, 60 {"Uint32s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, 61 {"Uint64s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, 62 {"BigInt", NewBigInt(1), NewBigInt(1)}, 63 {"BigInts", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}}, 64 } 65 66 args := NewInterfaces(len(tests)) 67 68 callFn := func(receiver interface{}, method string, arg interface{}) interface{} { 69 rval := reflect.ValueOf(receiver) 70 rval.MethodByName(fmt.Sprintf("Set%s", method)).Call([]reflect.Value{reflect.ValueOf(arg)}) 71 res := rval.MethodByName(fmt.Sprintf("Get%s", method)).Call(nil) 72 if len(res) > 0 { 73 return res[0].Interface() 74 } 75 return nil 76 } 77 78 for index, c := range tests { 79 // In theory the change of iface shouldn't effect the args value 80 iface, _ := args.Get(index) 81 result := callFn(iface, c.method, c.input) 82 if !reflect.DeepEqual(result, c.expect) { 83 t.Errorf("Interface get/set mismatch, want %v, got %v", c.expect, result) 84 } 85 // Check whether the underlying value in args is still zero 86 iface, _ = args.Get(index) 87 if iface.object != nil { 88 t.Error("Get operation is not write safe") 89 } 90 } 91 }