github.com/seeker-insurance/kit@v0.0.13/imath/imath_test.go (about)

     1  package imath
     2  
     3  import (
     4  	"math"
     5  	"math/rand"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestClamp(t *testing.T) {
    12  	assert.Equal(t, 4, Clamp(5, 0, 4))
    13  	assert.Equal(t, 2, Clamp(-1, 2, 5))
    14  	assert.Equal(t, 0, Clamp(0, -1, 1))
    15  }
    16  func TestMin(t *testing.T) {
    17  	type args struct {
    18  		x int
    19  		a []int
    20  	}
    21  	tests := []struct {
    22  		name string
    23  		args args
    24  		want int
    25  	}{
    26  		{
    27  			"ok",
    28  			args{x: 5, a: []int{2, 6, -1}},
    29  			-1,
    30  		},
    31  		{"default",
    32  			args{x: 5},
    33  			5,
    34  		},
    35  	}
    36  	for _, tt := range tests {
    37  		t.Run(tt.name, func(t *testing.T) {
    38  			if got := Min(tt.args.x, tt.args.a...); got != tt.want {
    39  				t.Errorf("Min() = %v, want %v", got, tt.want)
    40  			}
    41  		})
    42  	}
    43  }
    44  
    45  func TestMax(t *testing.T) {
    46  	type args struct {
    47  		x int
    48  		a []int
    49  	}
    50  	tests := []struct {
    51  		name string
    52  		args args
    53  		want int
    54  	}{
    55  		{
    56  			"ok",
    57  			args{x: 5, a: []int{2, 6, -1}},
    58  			6,
    59  		},
    60  		{"default",
    61  			args{x: 5},
    62  			5,
    63  		},
    64  	}
    65  	for _, tt := range tests {
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			if got := Max(tt.args.x, tt.args.a...); got != tt.want {
    68  				t.Errorf("Max() = %v, want %v", got, tt.want)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  type pair struct {
    75  	a, b int
    76  }
    77  
    78  func TestAbs(t *testing.T) {
    79  	t.Run("normal", func(t *testing.T) {
    80  		type pair struct {
    81  			a, b int
    82  		}
    83  		pairs := []pair{
    84  			{-1, 1},
    85  			{-0, 0},
    86  			{0, 0},
    87  			{1, 1},
    88  			{2, 2},
    89  			{-2, 2},
    90  			{500, 500},
    91  			{-500, 500},
    92  		}
    93  		for _, p := range pairs {
    94  			if A, B := Abs(p.a), p.b; A != B {
    95  				t.Errorf("Abs(%d) == %d: should be  %d", p.a, A, B)
    96  			}
    97  		}
    98  	})
    99  }
   100  
   101  func TestPow(t *testing.T) {
   102  	t.Run("random small pairs", func(t *testing.T) {
   103  		for i := 0; i < 50; i++ {
   104  			base := rand.Intn(2<<20) * RandSign()
   105  			exp := rand.Intn(2<<16) * RandSign()
   106  			if got, want := Pow(base, exp), naivePow(base, exp); got != want {
   107  				t.Errorf("Pow got %v, but want %v", got, want)
   108  			}
   109  		}
   110  	})
   111  }
   112  
   113  //TODO - test is broken
   114  /*
   115  func TestPowMod(t *testing.T) {
   116  
   117  	t.Run("random small triads", func(t *testing.T) {
   118  		for i := 0; i < 50; i++ {
   119  			base := rand.Intn(2<<20) * RandSign()
   120  			exp := rand.Intn(2 << 16)
   121  			mod := rand.Intn(2<<31) * RandSign()
   122  			if got, want := PowMod(base, exp, mod), naivePowMod(base, exp, mod); got != want {
   123  				t.Errorf("Pow(%d, %d, %d) got %v, but want %v", base, exp, mod, got, want)
   124  			}
   125  		}
   126  	})
   127  
   128  }
   129  */
   130  
   131  func TestSign(t *testing.T) {
   132  	assert := assert.New(t)
   133  	assert.Equal(-1, Sign(-5), "sign -5 == -1")
   134  	assert.Equal(0, Sign(0), "sign 0 == 0")
   135  	assert.Equal(1, Sign(math.MaxInt32), "sign(math.MaxInt32) == 1")
   136  }
   137  
   138  func TestRange(t *testing.T) {
   139  	want := []int{0, 1, 2, 3, 4}
   140  	assert.Equal(t, want, Range(0, 5, 1))
   141  
   142  	want = []int{2, 4, 6, 8}
   143  	assert.Equal(t, want, Range(2, 10, 2))
   144  
   145  	want = []int{-3, -6, -9}
   146  	assert.Equal(t, want, Range(-3, -10, -3))
   147  }
   148  
   149  func TestRandSign(t *testing.T) {
   150  
   151  }