github.com/bobyang007/helper@v1.1.3/reflecth/op-binary_test.go (about)

     1  package reflecth
     2  
     3  import "go/token"
     4  
     5  //replacer:ignore
     6  //go:generate go run $GOPATH/src/github.com/apaxa-go/generator/replacer/main.go -- $GOFILE
     7  
     8  import (
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  type testBinaryOpElement struct {
    14  	x   interface{}
    15  	op  token.Token
    16  	y   interface{}
    17  	r   interface{}
    18  	err bool
    19  }
    20  
    21  //replacer:replace
    22  //replacer:old int64	Int64
    23  //replacer:new int	Int
    24  //replacer:new int8	Int8
    25  //replacer:new int16	Int16
    26  //replacer:new int32	Int32
    27  //replacer:new uint	Uint
    28  //replacer:new uint8	Uint8
    29  //replacer:new uint16	Uint16
    30  //replacer:new uint32	Uint32
    31  //replacer:new uint64	Uint64
    32  
    33  var testsBinaryOpInt64 = []testBinaryOpElement{
    34  	{int64(2), token.ADD, int64(1), int64(3), false},
    35  	{int64(2), token.SUB, int64(1), int64(1), false},
    36  	{int64(2), token.MUL, int64(3), int64(6), false},
    37  	{int64(11), token.QUO, int64(3), int64(3), false},
    38  	{int64(8), token.REM, int64(3), int64(2), false},
    39  	{int64(6), token.AND, int64(3), int64(2), false},
    40  	{int64(6), token.OR, int64(3), int64(7), false},
    41  	{int64(6), token.XOR, int64(3), int64(5), false},
    42  	{int64(6), token.AND_NOT, int64(3), int64(6) &^ int64(3), false},
    43  	{int64(6), token.EQL, int64(3), nil, true},
    44  }
    45  
    46  //replacer:replace
    47  //replacer:old float32		Float32
    48  //replacer:new float64		Float64
    49  
    50  var testsBinaryOpFloat32 = []testBinaryOpElement{
    51  	{float32(3.4), token.ADD, float32(1.3), float32(4.7), false},
    52  	{float32(3.4), token.SUB, float32(1.2), float32(2.2), false},
    53  	{float32(1.2), token.MUL, float32(0.3), float32(0.36), false},
    54  	{float32(3), token.QUO, float32(1.5), float32(2), false},
    55  	{float32(3.4), token.EQL, float32(17), nil, true},
    56  }
    57  
    58  //replacer:replace
    59  //replacer:old complex64	Complex64
    60  //replacer:new complex128	Complex128
    61  
    62  var testsBinaryOpComplex64 = []testBinaryOpElement{
    63  	{complex64(3.4 + 2i), token.ADD, complex64(1.3 - 1i), complex64(4.7 + 1i), false},
    64  	{complex64(3.4 + 2i), token.SUB, complex64(1.2 + 1i), complex64(2.2 + 1i), false},
    65  	{complex64(1 + 3i), token.MUL, complex64(5 + 6i), complex64(-13 + 21i), false},
    66  	{complex64(3i), token.QUO, complex64(1.5), complex64(2i), false},
    67  	{complex64(3.4 + 2.1i), token.EQL, complex64(17 - 0.1i), nil, true},
    68  }
    69  
    70  //replacer:ignore
    71  
    72  func TestBinaryOp(t *testing.T) {
    73  	tests := []testBinaryOpElement{
    74  		// Bool
    75  		{true, token.LAND, false, false, false},
    76  		{true, token.LOR, false, true, false},
    77  		{true, token.ADD, false, nil, true},
    78  		// String
    79  		{"s1", token.ADD, "s2", "s1s2", false},
    80  		{"s1", token.SUB, "s2", nil, true},
    81  		// Negative
    82  		{1, token.ADD, "s1", nil, true},
    83  		{struct{ a int }{1}, token.SUB, struct{ a int }{2}, nil, true},
    84  	}
    85  
    86  	tests = append(tests, testsBinaryOpInt...)
    87  	tests = append(tests, testsBinaryOpInt8...)
    88  	tests = append(tests, testsBinaryOpInt16...)
    89  	tests = append(tests, testsBinaryOpInt32...)
    90  	tests = append(tests, testsBinaryOpInt64...)
    91  	tests = append(tests, testsBinaryOpUint...)
    92  	tests = append(tests, testsBinaryOpUint8...)
    93  	tests = append(tests, testsBinaryOpUint16...)
    94  	tests = append(tests, testsBinaryOpUint32...)
    95  	tests = append(tests, testsBinaryOpUint64...)
    96  	tests = append(tests, testsBinaryOpFloat32...)
    97  	tests = append(tests, testsBinaryOpFloat64...)
    98  	tests = append(tests, testsBinaryOpComplex64...)
    99  	tests = append(tests, testsBinaryOpComplex128...)
   100  
   101  	for _, test := range tests {
   102  		r, err := BinaryOp(reflect.ValueOf(test.x), test.op, reflect.ValueOf(test.y))
   103  		if err != nil != test.err || (!test.err && (r.Interface() != test.r)) {
   104  			t.Errorf("%v %v %v: expect %v %v, got %v %v", test.x, test.op, test.y, test.r, test.err, r, err)
   105  		}
   106  	}
   107  }