github.com/searKing/golang/go@v1.2.74/exp/math/all_test.go (about) 1 // Copyright 2022 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 math_test 6 7 import ( 8 "fmt" 9 "testing" 10 11 math_ "github.com/searKing/golang/go/exp/math" 12 ) 13 14 var vf = []int{ 15 1, 16 2, 17 3, 18 } 19 var ceil = []int{ 20 1, 21 4, 22 6, 23 } 24 var floor = []int{ 25 1, 26 -2, 27 -3, 28 } 29 30 var fdim = []int{ 31 1, 32 2, 33 3, 34 } 35 36 func TestDim(t *testing.T) { 37 for i := 0; i < len(vf); i++ { 38 if f := math_.Dim(vf[i], 0); fdim[i] != f { 39 t.Errorf("Dim(%d, %d) = %d, want %d", vf[i], 0, f, fdim[i]) 40 } 41 } 42 } 43 44 func TestMax(t *testing.T) { 45 for i := 0; i < len(vf); i++ { 46 if f := math_.Max(vf[i], ceil[i]); ceil[i] != f { 47 t.Errorf("Max(%d, %d) = %d, want %d", vf[i], ceil[i], f, ceil[i]) 48 } 49 } 50 } 51 52 func TestMin(t *testing.T) { 53 for i := 0; i < len(vf); i++ { 54 if f := math_.Min(vf[i], floor[i]); floor[i] != f { 55 t.Errorf("Min(%d, %d) = %d, want %d", vf[i], floor[i], f, floor[i]) 56 } 57 } 58 } 59 60 func TestClamp(t *testing.T) { 61 tests := []struct { 62 v, lo, hi int 63 want int 64 }{ 65 {0, 0, 0, 0}, 66 {-1, 0, 1, 0}, 67 {-1, 1, 1, 1}, 68 {-1, -1, 1, -1}, 69 {1, 1, -1, 1}, 70 {1, -1, -1, -1}, 71 } 72 for _, tt := range tests { 73 t.Run(fmt.Sprintf("math_.Clamp(%v, %v, %v)", tt.v, tt.lo, tt.hi), func(t *testing.T) { 74 { 75 got := math_.Clamp(tt.v, tt.lo, tt.hi) 76 if got != tt.want { 77 t.Errorf("math_.Clamp(%v, %v, %v) = %v, want %v", tt.v, tt.lo, tt.hi, got, tt.want) 78 } 79 } 80 }) 81 } 82 } 83 84 func TestRem(t *testing.T) { 85 tests := []struct { 86 x, y int 87 want int 88 }{ 89 //{0, 0, 0}, // panics for y == 0 (division by zero). 90 {-3, 3, 0}, 91 {-2, 3, 1}, 92 {-1, 3, 2}, 93 {0, 3, 0}, 94 {1, 3, 1}, 95 {2, 3, 2}, 96 {3, 3, 0}, 97 98 {-3, -3, 0}, 99 {-2, -3, -2}, 100 {-1, -3, -1}, 101 {0, -3, 0}, 102 {1, -3, -2}, 103 {2, -3, -1}, 104 {3, -3, 0}, 105 } 106 for _, tt := range tests { 107 t.Run(fmt.Sprintf("math_.RingRem(%v, %v)", tt.x, tt.y), func(t *testing.T) { 108 { 109 got := math_.RingRem(tt.x, tt.y) 110 if got != tt.want { 111 t.Errorf("math_.RingRem(%v, %v) = %v, want %v", tt.x, tt.y, got, tt.want) 112 } 113 } 114 }) 115 } 116 }