github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekamath/min_max_test.go (about)

     1  package ekamath_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/qioalice/ekago/v3/ekaext"
     7  	"github.com/qioalice/ekago/v3/ekamath"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestMinMax(t *testing.T) {
    13  
    14  	for _, tc := range []struct {
    15  		A, B, Exp int32
    16  		IsMin     bool
    17  	}{
    18  		{1, 1, 1, true},
    19  		{-1, -1, -1, true},
    20  		{1, 1, 1, false},
    21  		{-1, -1, -1, false},
    22  		{1, 2, 1, true},
    23  		{2, 1, 1, true},
    24  		{1, 2, 2, false},
    25  		{2, 1, 2, false},
    26  		{-1, -1, -1, true},
    27  		{-1, -1, -1, false},
    28  		{-1, -2, -2, true},
    29  		{-2, -1, -2, true},
    30  		{-1, -2, -1, false},
    31  		{-2, -1, -1, false},
    32  		{-1, 1, -1, true},
    33  		{1, -1, -1, true},
    34  		{-1, 1, 1, false},
    35  		{1, -1, 1, false},
    36  	} {
    37  		var a, b = tc.A, tc.B
    38  		var got = ekaext.If(tc.IsMin, ekamath.Min(a, b), ekamath.Max(a, b))
    39  
    40  		require.EqualValuesf(t, tc.Exp, got, "IsMin: %t, A: %d, B: %d", tc.IsMin, a, b)
    41  	}
    42  }
    43  
    44  func TestClamp(t *testing.T) {
    45  
    46  	for _, tc := range []struct {
    47  		A, B, V, Exp int32
    48  	}{
    49  		{-2, 2, 1, 1},
    50  		{-2, 2, 3, 2},
    51  		{-2, 2, -3, -2},
    52  		{2, -2, 1, 1},
    53  		{2, -2, 3, 2},
    54  		{2, -2, -3, -2},
    55  		{1, 5, 4, 4},
    56  		{3, 5, 10, 5},
    57  		{5, 3, 1, 3},
    58  	} {
    59  		var got = ekamath.Clamp(tc.V, tc.A, tc.B)
    60  		require.EqualValuesf(t, tc.Exp, got, "A: %d, B: %d, V: %d", tc.A, tc.B, tc.V)
    61  	}
    62  }