github.com/seeker-insurance/kit@v0.0.13/umath/umath_test.go (about) 1 package umath 2 3 import ( 4 "math/rand" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestMin(t *testing.T) { 11 assert.Equal(t, uint(3), Min(5, 3)) 12 } 13 14 func TestSum(t *testing.T) { 15 assert.Equal(t, uint(10), Sum(2, 5, 3)) 16 } 17 18 func TestRange(t *testing.T) { 19 assert.Equal(t, []uint{0, 1, 2, 3, 4}, Range(0, 5, 1)) 20 assert.Equal(t, []uint{5, 3, 1}, Range(5, 0, -2)) 21 assert.Nil(t, Range(1, 0, 1)) 22 } 23 24 func TestProduct(t *testing.T) { 25 assert.Equal(t, uint(30), Product(5, 2, 3)) 26 } 27 28 func TestMax(t *testing.T) { 29 assert.Equal(t, uint(10), Max(2, 10, 5)) 30 } 31 32 func TestClamp(t *testing.T) { 33 const low, high uint = 1, 500 34 assert.Equal(t, low, Clamp(0, low, high)) 35 assert.Equal(t, uint(2), Clamp(2, low, high)) 36 assert.Equal(t, high, Clamp(5000, low, high)) 37 } 38 39 func naivePow(base, exp uint) uint { 40 result := uint(1) 41 for ; exp > 0; exp-- { 42 result *= base 43 } 44 return result 45 } 46 47 func TestPow(t *testing.T) { 48 49 t.Run("random small pairs", func(t *testing.T) { 50 for i := 0; i < 50; i++ { 51 base := uint(rand.Intn(2 << 20)) 52 exp := uint(rand.Intn(2 << 16)) 53 if got, want := Pow(base, exp), naivePow(base, exp); got != want { 54 t.Errorf("Pow got %v, but want %v", got, want) 55 } 56 } 57 }) 58 }