bitbucket.org/ai69/amoy@v0.2.3/math_test.go (about) 1 package amoy 2 3 import ( 4 "fmt" 5 "math" 6 "testing" 7 ) 8 9 func TestFractionCeil(t *testing.T) { 10 tests := []struct { 11 name string 12 a int 13 b int 14 want int 15 }{ 16 {"divided by zero", 2, 0, MinInt}, 17 {"1/1", 1, 1, 1}, 18 {"2/2", 2, 2, 1}, 19 {"2/1", 2, 1, 2}, 20 {"1/2", 1, 2, 1}, 21 {"-1/2", -1, 2, 0}, 22 {"1/1000000000", 1, 1000000000, 1}, 23 {"999999999/1000000000", 999999999, 1000000000, 1}, 24 {"1000000001/1000000000", 1000000001, 1000000000, 2}, 25 } 26 for _, tt := range tests { 27 t.Run(tt.name, func(t *testing.T) { 28 if got := FractionCeil(tt.a, tt.b); got != tt.want { 29 t.Errorf("FractionCeil(%d, %d) = %v, want %v", tt.a, tt.b, got, tt.want) 30 } 31 }) 32 } 33 } 34 35 func TestFractionFloor(t *testing.T) { 36 tests := []struct { 37 name string 38 a int 39 b int 40 want int 41 }{ 42 {"divided by zero", 2, 0, MinInt}, 43 {"1/1", 1, 1, 1}, 44 {"2/2", 2, 2, 1}, 45 {"2/1", 2, 1, 2}, 46 {"1/2", 1, 2, 0}, 47 {"-1/2", -1, 2, -1}, 48 {"1/1000000000", 1, 1000000000, 0}, 49 {"999999999/1000000000", 999999999, 1000000000, 0}, 50 {"1000000001/1000000000", 1000000001, 1000000000, 1}, 51 } 52 for _, tt := range tests { 53 t.Run(tt.name, func(t *testing.T) { 54 if got := FractionFloor(tt.a, tt.b); got != tt.want { 55 t.Errorf("FractionFloor(%d, %d) = %v, want %v", tt.a, tt.b, got, tt.want) 56 } 57 }) 58 } 59 } 60 61 func TestFractionTrunc(t *testing.T) { 62 tests := []struct { 63 name string 64 a int 65 b int 66 want int 67 }{ 68 {"divided by zero", 2, 0, MinInt}, 69 {"1/1", 1, 1, 1}, 70 {"2/2", 2, 2, 1}, 71 {"2/1", 2, 1, 2}, 72 {"1/2", 1, 2, 0}, 73 {"-1/2", -1, 2, 0}, 74 {"1/1000000000", 1, 1000000000, 0}, 75 {"999999999/1000000000", 999999999, 1000000000, 0}, 76 {"1000000001/1000000000", 1000000001, 1000000000, 1}, 77 } 78 for _, tt := range tests { 79 t.Run(tt.name, func(t *testing.T) { 80 if got := FractionTrunc(tt.a, tt.b); got != tt.want { 81 t.Errorf("FractionTrunc(%d, %d) = %v, want %v", tt.a, tt.b, got, tt.want) 82 } 83 }) 84 } 85 } 86 87 func TestFractionRound(t *testing.T) { 88 tests := []struct { 89 name string 90 a int 91 b int 92 want int 93 }{ 94 {"divided by zero", 2, 0, MinInt}, 95 {"1/1", 1, 1, 1}, 96 {"2/2", 2, 2, 1}, 97 {"2/1", 2, 1, 2}, 98 {"1/2", 1, 2, 1}, 99 {"-1/2", -1, 2, -1}, 100 {"1/1000000000", 1, 1000000000, 0}, 101 {"999999999/1000000000", 999999999, 1000000000, 1}, 102 {"1000000001/1000000000", 1000000001, 1000000000, 1}, 103 } 104 for _, tt := range tests { 105 t.Run(tt.name, func(t *testing.T) { 106 if got := FractionRound(tt.a, tt.b); got != tt.want { 107 t.Errorf("FractionRound(%d, %d) = %v, want %v", tt.a, tt.b, got, tt.want) 108 } 109 }) 110 } 111 } 112 113 func TestFractionFractional(t *testing.T) { 114 tests := []struct { 115 name string 116 a int 117 b int 118 want float64 119 }{ 120 {"divided by zero", 2, 0, math.NaN()}, 121 {"1/1", 1, 1, 0}, 122 {"2/2", 2, 2, 0}, 123 {"2/1", 2, 1, 0}, 124 {"1/2", 1, 2, 0.5}, 125 {"-1/2", -1, 2, -0.5}, 126 {"1/1000000000", 1, 1000000000, 1e-9}, 127 {"999999999/1000000000", 999999999, 1000000000, 0.999999999}, 128 {"1000000001/1000000000", 1000000001, 1000000000, 1e-9}, 129 } 130 for _, tt := range tests { 131 t.Run(tt.name, func(t *testing.T) { 132 if got := FractionFractional(tt.a, tt.b); math.Abs(got-tt.want) > 1e-10 { 133 t.Errorf("FractionFractional(%d, %d) = %v, want %v", tt.a, tt.b, got, tt.want) 134 } 135 }) 136 } 137 } 138 139 func TestFractional(t *testing.T) { 140 tests := []struct { 141 name string 142 f float64 143 want float64 144 }{ 145 {"zero", 0, 0}, 146 {"one", 1, 0}, 147 {"dot one", 2.1, 0.1}, 148 {"pi", math.Pi, math.Pi - 3}, 149 {"e", math.E, math.E - 2}, 150 } 151 for _, tt := range tests { 152 t.Run(tt.name, func(t *testing.T) { 153 if got := Fractional(tt.f); math.Abs(got-tt.want) > 1e-10 { 154 t.Errorf("Fractional(%v) = %v, want %v", tt.f, got, tt.want) 155 } 156 }) 157 } 158 } 159 160 func TestCountDigit(t *testing.T) { 161 tests := []struct { 162 num int 163 want int 164 }{ 165 {0, 1}, 166 {1, 1}, 167 {-1, 1}, 168 {9, 1}, 169 {10, 2}, 170 {99, 2}, 171 {100, 3}, 172 {-100, 3}, 173 {100000000000, 12}, 174 {5392592365935746, 16}, 175 } 176 for _, tt := range tests { 177 name := fmt.Sprintf("Count(%d)", tt.num) 178 t.Run(name, func(t *testing.T) { 179 if got := CountDigit(tt.num); got != tt.want { 180 t.Errorf("CountDigit(%v) = %v, want %v", tt.num, got, tt.want) 181 } 182 }) 183 } 184 } 185 186 func TestRoundToFixed(t *testing.T) { 187 tests := []struct { 188 num float64 189 precision int 190 want float64 191 }{ 192 {1.23456789, -1, 1.23456789}, 193 {1.23456789, 0, 1}, 194 {1.23456789, 1, 1.2}, 195 {1.23456789, 2, 1.23}, 196 {1.23456789, 3, 1.235}, 197 {1.23456789, 4, 1.2346}, 198 {1.23456789, 5, 1.23457}, 199 {1.23456789, 6, 1.234568}, 200 {1.23456789, 7, 1.2345679}, 201 {1.23456789, 8, 1.23456789}, 202 {1.23456789, 12, 1.23456789}, 203 {1.23456789, 13, 1.23456789}, 204 {0.01, 1, 0}, 205 {0.01, 2, 0.01}, 206 } 207 for _, tt := range tests { 208 name := fmt.Sprintf("(%f,%d)", tt.num, tt.precision) 209 t.Run(name, func(t *testing.T) { 210 if got := RoundToFixed(tt.num, tt.precision); math.Abs(got-tt.want) > 1e-10 { 211 t.Errorf("RoundToFixed(%f, %d) = %v, want %v", tt.num, tt.precision, got, tt.want) 212 } 213 }) 214 } 215 } 216 217 func TestPercentageStr(t *testing.T) { 218 tests := []struct { 219 name string 220 a int 221 b int 222 want string 223 }{ 224 {"Zero numerator", 0, 100, "0%"}, 225 {"Zero denominator", 100, 0, "0%"}, 226 {"Both zero", 0, 0, "0%"}, 227 {"Both one", 1, 1, "100%"}, 228 {"1/2", 1, 2, "50%"}, 229 {"1/3", 1, 3, "33.33%"}, 230 {"1/4", 1, 4, "25%"}, 231 {"123867/4678912", 123867, 4678912, "2.64%"}, 232 {"1/100000", 1, 100000, "0%"}, 233 {"1/10000", 1, 10000, "0.01%"}, 234 {"9999/10000", 9999, 10000, "99.99%"}, 235 {"10/1", 10, 1, "1000%"}, 236 {"-1/4", -1, 4, "-25%"}, 237 } 238 for _, tt := range tests { 239 t.Run(tt.name, func(t *testing.T) { 240 if got := PercentageStr(tt.a, tt.b); got != tt.want { 241 t.Errorf("PercentageStr(%d, %d) = %v, want %v", tt.a, tt.b, got, tt.want) 242 } 243 }) 244 } 245 }