github.com/searKing/golang/go@v1.2.117/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, 0, -1, 0}, 70 {1, 1, -1, 1}, 71 {1, -1, -1, -1}, 72 } 73 for _, tt := range tests { 74 t.Run(fmt.Sprintf("math_.Clamp(%v, %v, %v)", tt.v, tt.lo, tt.hi), func(t *testing.T) { 75 { 76 got := math_.Clamp(tt.v, tt.lo, tt.hi) 77 if got != tt.want { 78 t.Errorf("math_.Clamp(%v, %v, %v) = %v, want %v", tt.v, tt.lo, tt.hi, got, tt.want) 79 } 80 } 81 }) 82 } 83 } 84 85 func TestRem(t *testing.T) { 86 tests := []struct { 87 x, y int 88 want int 89 }{ 90 //{0, 0, 0}, // panics for y == 0 (division by zero). 91 {-3, 3, 0}, 92 {-2, 3, 1}, 93 {-1, 3, 2}, 94 {0, 3, 0}, 95 {1, 3, 1}, 96 {2, 3, 2}, 97 {3, 3, 0}, 98 99 {-3, -3, 0}, 100 {-2, -3, -2}, 101 {-1, -3, -1}, 102 {0, -3, 0}, 103 {1, -3, -2}, 104 {2, -3, -1}, 105 {3, -3, 0}, 106 } 107 for _, tt := range tests { 108 t.Run(fmt.Sprintf("math_.RingRem(%v, %v)", tt.x, tt.y), func(t *testing.T) { 109 { 110 got := math_.RingRem(tt.x, tt.y) 111 if got != tt.want { 112 t.Errorf("math_.RingRem(%v, %v) = %v, want %v", tt.x, tt.y, got, tt.want) 113 } 114 } 115 }) 116 } 117 }