github.com/searKing/golang/go@v1.2.74/util/function/binary/binary_operator_test.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package binary_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/searKing/golang/go/util"
    11  	"github.com/searKing/golang/go/util/function"
    12  	"github.com/searKing/golang/go/util/function/binary"
    13  )
    14  
    15  type MinByTestInput struct {
    16  	compare func(t interface{}, u interface{}) int
    17  	afters  []func(t interface{}) interface{}
    18  	t       interface{}
    19  	u       interface{}
    20  }
    21  type MinByTest struct {
    22  	input  MinByTestInput
    23  	output interface{}
    24  }
    25  
    26  var minByTests = []MinByTest{
    27  	{
    28  		input: MinByTestInput{
    29  			compare: func(t interface{}, u interface{}) int {
    30  				return t.(int) - u.(int)
    31  			},
    32  			t: 1,
    33  			u: 2,
    34  		},
    35  		output: 1,
    36  	},
    37  	{
    38  		input: MinByTestInput{
    39  			compare: func(t interface{}, u interface{}) int {
    40  				return t.(int) - u.(int)
    41  			},
    42  			afters: []func(t interface{}) interface{}{func(t interface{}) interface{} {
    43  				return 0 - t.(int)
    44  			}, func(t interface{}) interface{} {
    45  				return t.(int) * 3
    46  			}},
    47  			t: 1,
    48  			u: 2,
    49  		},
    50  		output: -3,
    51  	},
    52  }
    53  
    54  func TestMinBy(t *testing.T) {
    55  	for n, test := range minByTests {
    56  		var bi = binary.MinBy(util.ComparatorFunc(test.input.compare))
    57  		for _, after := range test.input.afters {
    58  			bi = bi.AndThen(function.FunctionFunc(after))
    59  		}
    60  		got := bi.Apply(test.input.t, test.input.u)
    61  		if got != test.output {
    62  			t.Errorf("#%d: %v: got %v runs; expected %v", n, test.input, got, test.output)
    63  		}
    64  	}
    65  }
    66  
    67  type MaxByTestInput struct {
    68  	compare func(t interface{}, u interface{}) int
    69  	afters  []func(t interface{}) interface{}
    70  	t       interface{}
    71  	u       interface{}
    72  }
    73  type MaxByTest struct {
    74  	input  MaxByTestInput
    75  	output interface{}
    76  }
    77  
    78  var maxByTests = []MaxByTest{
    79  	{
    80  		input: MaxByTestInput{
    81  			compare: func(t interface{}, u interface{}) int {
    82  				return t.(int) - u.(int)
    83  			},
    84  			t: 1,
    85  			u: 2,
    86  		},
    87  		output: 2,
    88  	},
    89  	{
    90  		input: MaxByTestInput{
    91  			compare: func(t interface{}, u interface{}) int {
    92  				return t.(int) - u.(int)
    93  			},
    94  			afters: []func(t interface{}) interface{}{func(t interface{}) interface{} {
    95  				return 0 - t.(int)
    96  			}, func(t interface{}) interface{} {
    97  				return t.(int) * 3
    98  			}},
    99  			t: 1,
   100  			u: 2,
   101  		},
   102  		output: -6,
   103  	},
   104  }
   105  
   106  func TestMaxBy(t *testing.T) {
   107  	for n, test := range maxByTests {
   108  		var bi = binary.MaxBy(util.ComparatorFunc(test.input.compare))
   109  		for _, after := range test.input.afters {
   110  			bi = bi.AndThen(function.FunctionFunc(after))
   111  		}
   112  		got := bi.Apply(test.input.t, test.input.u)
   113  		if got != test.output {
   114  			t.Errorf("#%d: %v: got %v runs; expected %v", n, test.input, got, test.output)
   115  		}
   116  	}
   117  }