github.com/core-coin/go-core/v2@v2.1.9/mobile/interface_test.go (about)

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