git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/validate/numerics_test.go (about) 1 package validate 2 3 import "testing" 4 5 func TestAbs(t *testing.T) { 6 t.Parallel() 7 8 var tests = []struct { 9 param float64 10 expected float64 11 }{ 12 {0, 0}, 13 {-1, 1}, 14 {10, 10}, 15 {3.14, 3.14}, 16 {-96, 96}, 17 {-10e-12, 10e-12}, 18 } 19 for _, test := range tests { 20 actual := Abs(test.param) 21 if actual != test.expected { 22 t.Errorf("Expected Abs(%v) to be %v, got %v", test.param, test.expected, actual) 23 } 24 } 25 } 26 27 func TestSign(t *testing.T) { 28 t.Parallel() 29 30 var tests = []struct { 31 param float64 32 expected float64 33 }{ 34 {0, 0}, 35 {-1, -1}, 36 {10, 1}, 37 {3.14, 1}, 38 {-96, -1}, 39 {-10e-12, -1}, 40 } 41 for _, test := range tests { 42 actual := Sign(test.param) 43 if actual != test.expected { 44 t.Errorf("Expected Sign(%v) to be %v, got %v", test.param, test.expected, actual) 45 } 46 } 47 } 48 49 func TestIsNegative(t *testing.T) { 50 t.Parallel() 51 52 var tests = []struct { 53 param float64 54 expected bool 55 }{ 56 {0, false}, 57 {-1, true}, 58 {10, false}, 59 {3.14, false}, 60 {-96, true}, 61 {-10e-12, true}, 62 } 63 for _, test := range tests { 64 actual := IsNegative(test.param) 65 if actual != test.expected { 66 t.Errorf("Expected IsNegative(%v) to be %v, got %v", test.param, test.expected, actual) 67 } 68 } 69 } 70 71 func TestIsNonNegative(t *testing.T) { 72 t.Parallel() 73 74 var tests = []struct { 75 param float64 76 expected bool 77 }{ 78 {0, true}, 79 {-1, false}, 80 {10, true}, 81 {3.14, true}, 82 {-96, false}, 83 {-10e-12, false}, 84 } 85 for _, test := range tests { 86 actual := IsNonNegative(test.param) 87 if actual != test.expected { 88 t.Errorf("Expected IsNonNegative(%v) to be %v, got %v", test.param, test.expected, actual) 89 } 90 } 91 } 92 93 func TestIsPositive(t *testing.T) { 94 t.Parallel() 95 96 var tests = []struct { 97 param float64 98 expected bool 99 }{ 100 {0, false}, 101 {-1, false}, 102 {10, true}, 103 {3.14, true}, 104 {-96, false}, 105 {-10e-12, false}, 106 } 107 for _, test := range tests { 108 actual := IsPositive(test.param) 109 if actual != test.expected { 110 t.Errorf("Expected IsPositive(%v) to be %v, got %v", test.param, test.expected, actual) 111 } 112 } 113 } 114 115 func TestIsNonPositive(t *testing.T) { 116 t.Parallel() 117 118 var tests = []struct { 119 param float64 120 expected bool 121 }{ 122 {0, true}, 123 {-1, true}, 124 {10, false}, 125 {3.14, false}, 126 {-96, true}, 127 {-10e-12, true}, 128 } 129 for _, test := range tests { 130 actual := IsNonPositive(test.param) 131 if actual != test.expected { 132 t.Errorf("Expected IsNonPositive(%v) to be %v, got %v", test.param, test.expected, actual) 133 } 134 } 135 } 136 137 func TestIsWhole(t *testing.T) { 138 t.Parallel() 139 140 var tests = []struct { 141 param float64 142 expected bool 143 }{ 144 {0, true}, 145 {-1, true}, 146 {10, true}, 147 {3.14, false}, 148 {-96, true}, 149 {-10e-12, false}, 150 } 151 for _, test := range tests { 152 actual := IsWhole(test.param) 153 if actual != test.expected { 154 t.Errorf("Expected IsWhole(%v) to be %v, got %v", test.param, test.expected, actual) 155 } 156 } 157 } 158 159 func TestIsNatural(t *testing.T) { 160 t.Parallel() 161 162 var tests = []struct { 163 param float64 164 expected bool 165 }{ 166 {0, false}, 167 {-1, false}, 168 {10, true}, 169 {3.14, false}, 170 {96, true}, 171 {-10e-12, false}, 172 } 173 for _, test := range tests { 174 actual := IsNatural(test.param) 175 if actual != test.expected { 176 t.Errorf("Expected IsNatural(%v) to be %v, got %v", test.param, test.expected, actual) 177 } 178 } 179 } 180 181 func TestInRangeInt(t *testing.T) { 182 t.Parallel() 183 184 var testAsInts = []struct { 185 param int 186 left int 187 right int 188 expected bool 189 }{ 190 {0, 0, 0, true}, 191 {1, 0, 0, false}, 192 {-1, 0, 0, false}, 193 {0, -1, 1, true}, 194 {0, 0, 1, true}, 195 {0, -1, 0, true}, 196 {0, 0, -1, true}, 197 {0, 10, 5, false}, 198 } 199 for _, test := range testAsInts { 200 actual := InRangeInt(test.param, test.left, test.right) 201 if actual != test.expected { 202 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type int", test.param, test.left, test.right, test.expected, actual) 203 } 204 } 205 206 var testAsInt8s = []struct { 207 param int8 208 left int8 209 right int8 210 expected bool 211 }{ 212 {0, 0, 0, true}, 213 {1, 0, 0, false}, 214 {-1, 0, 0, false}, 215 {0, -1, 1, true}, 216 {0, 0, 1, true}, 217 {0, -1, 0, true}, 218 {0, 0, -1, true}, 219 {0, 10, 5, false}, 220 } 221 for _, test := range testAsInt8s { 222 actual := InRangeInt(test.param, test.left, test.right) 223 if actual != test.expected { 224 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type int8", test.param, test.left, test.right, test.expected, actual) 225 } 226 } 227 228 var testAsInt16s = []struct { 229 param int16 230 left int16 231 right int16 232 expected bool 233 }{ 234 {0, 0, 0, true}, 235 {1, 0, 0, false}, 236 {-1, 0, 0, false}, 237 {0, -1, 1, true}, 238 {0, 0, 1, true}, 239 {0, -1, 0, true}, 240 {0, 0, -1, true}, 241 {0, 10, 5, false}, 242 } 243 for _, test := range testAsInt16s { 244 actual := InRangeInt(test.param, test.left, test.right) 245 if actual != test.expected { 246 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type int16", test.param, test.left, test.right, test.expected, actual) 247 } 248 } 249 250 var testAsInt32s = []struct { 251 param int32 252 left int32 253 right int32 254 expected bool 255 }{ 256 {0, 0, 0, true}, 257 {1, 0, 0, false}, 258 {-1, 0, 0, false}, 259 {0, -1, 1, true}, 260 {0, 0, 1, true}, 261 {0, -1, 0, true}, 262 {0, 0, -1, true}, 263 {0, 10, 5, false}, 264 } 265 for _, test := range testAsInt32s { 266 actual := InRangeInt(test.param, test.left, test.right) 267 if actual != test.expected { 268 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type int32", test.param, test.left, test.right, test.expected, actual) 269 } 270 } 271 272 var testAsInt64s = []struct { 273 param int64 274 left int64 275 right int64 276 expected bool 277 }{ 278 {0, 0, 0, true}, 279 {1, 0, 0, false}, 280 {-1, 0, 0, false}, 281 {0, -1, 1, true}, 282 {0, 0, 1, true}, 283 {0, -1, 0, true}, 284 {0, 0, -1, true}, 285 {0, 10, 5, false}, 286 } 287 for _, test := range testAsInt64s { 288 actual := InRangeInt(test.param, test.left, test.right) 289 if actual != test.expected { 290 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type int64", test.param, test.left, test.right, test.expected, actual) 291 } 292 } 293 294 var testAsUInts = []struct { 295 param uint 296 left uint 297 right uint 298 expected bool 299 }{ 300 {0, 0, 0, true}, 301 {1, 0, 0, false}, 302 {0, 0, 1, true}, 303 {0, 10, 5, false}, 304 } 305 for _, test := range testAsUInts { 306 actual := InRangeInt(test.param, test.left, test.right) 307 if actual != test.expected { 308 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type uint", test.param, test.left, test.right, test.expected, actual) 309 } 310 } 311 312 var testAsUInt8s = []struct { 313 param uint8 314 left uint8 315 right uint8 316 expected bool 317 }{ 318 {0, 0, 0, true}, 319 {1, 0, 0, false}, 320 {0, 0, 1, true}, 321 {0, 10, 5, false}, 322 } 323 for _, test := range testAsUInt8s { 324 actual := InRangeInt(test.param, test.left, test.right) 325 if actual != test.expected { 326 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type uint", test.param, test.left, test.right, test.expected, actual) 327 } 328 } 329 330 var testAsUInt16s = []struct { 331 param uint16 332 left uint16 333 right uint16 334 expected bool 335 }{ 336 {0, 0, 0, true}, 337 {1, 0, 0, false}, 338 {0, 0, 1, true}, 339 {0, 10, 5, false}, 340 } 341 for _, test := range testAsUInt16s { 342 actual := InRangeInt(test.param, test.left, test.right) 343 if actual != test.expected { 344 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type uint", test.param, test.left, test.right, test.expected, actual) 345 } 346 } 347 348 var testAsUInt32s = []struct { 349 param uint32 350 left uint32 351 right uint32 352 expected bool 353 }{ 354 {0, 0, 0, true}, 355 {1, 0, 0, false}, 356 {0, 0, 1, true}, 357 {0, 10, 5, false}, 358 } 359 for _, test := range testAsUInt32s { 360 actual := InRangeInt(test.param, test.left, test.right) 361 if actual != test.expected { 362 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type uint", test.param, test.left, test.right, test.expected, actual) 363 } 364 } 365 366 var testAsUInt64s = []struct { 367 param uint64 368 left uint64 369 right uint64 370 expected bool 371 }{ 372 {0, 0, 0, true}, 373 {1, 0, 0, false}, 374 {0, 0, 1, true}, 375 {0, 10, 5, false}, 376 } 377 for _, test := range testAsUInt64s { 378 actual := InRangeInt(test.param, test.left, test.right) 379 if actual != test.expected { 380 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type uint", test.param, test.left, test.right, test.expected, actual) 381 } 382 } 383 384 var testAsStrings = []struct { 385 param string 386 left string 387 right string 388 expected bool 389 }{ 390 {"0", "0", "0", true}, 391 {"1", "0", "0", false}, 392 {"-1", "0", "0", false}, 393 {"0", "-1", "1", true}, 394 {"0", "0", "1", true}, 395 {"0", "-1", "0", true}, 396 {"0", "0", "-1", true}, 397 {"0", "10", "5", false}, 398 } 399 for _, test := range testAsStrings { 400 actual := InRangeInt(test.param, test.left, test.right) 401 if actual != test.expected { 402 t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type string", test.param, test.left, test.right, test.expected, actual) 403 } 404 } 405 } 406 407 func TestInRangeFloat32(t *testing.T) { 408 t.Parallel() 409 410 var tests = []struct { 411 param float32 412 left float32 413 right float32 414 expected bool 415 }{ 416 {0, 0, 0, true}, 417 {1, 0, 0, false}, 418 {-1, 0, 0, false}, 419 {0, -1, 1, true}, 420 {0, 0, 1, true}, 421 {0, -1, 0, true}, 422 {0, 0, -1, true}, 423 {0, 10, 5, false}, 424 } 425 for _, test := range tests { 426 actual := InRangeFloat32(test.param, test.left, test.right) 427 if actual != test.expected { 428 t.Errorf("Expected InRangeFloat32(%v, %v, %v) to be %v, got %v", test.param, test.left, test.right, test.expected, actual) 429 } 430 } 431 } 432 433 func TestInRangeFloat64(t *testing.T) { 434 t.Parallel() 435 436 var tests = []struct { 437 param float64 438 left float64 439 right float64 440 expected bool 441 }{ 442 {0, 0, 0, true}, 443 {1, 0, 0, false}, 444 {-1, 0, 0, false}, 445 {0, -1, 1, true}, 446 {0, 0, 1, true}, 447 {0, -1, 0, true}, 448 {0, 0, -1, true}, 449 {0, 10, 5, false}, 450 } 451 for _, test := range tests { 452 actual := InRangeFloat64(test.param, test.left, test.right) 453 if actual != test.expected { 454 t.Errorf("Expected InRangeFloat64(%v, %v, %v) to be %v, got %v", test.param, test.left, test.right, test.expected, actual) 455 } 456 } 457 } 458 459 func TestInRange(t *testing.T) { 460 t.Parallel() 461 462 var testsInt = []struct { 463 param int 464 left int 465 right int 466 expected bool 467 }{ 468 {0, 0, 0, true}, 469 {1, 0, 0, false}, 470 {-1, 0, 0, false}, 471 {0, -1, 1, true}, 472 {0, 0, 1, true}, 473 {0, -1, 0, true}, 474 {0, 0, -1, true}, 475 {0, 10, 5, false}, 476 } 477 for _, test := range testsInt { 478 actual := InRange(test.param, test.left, test.right) 479 if actual != test.expected { 480 t.Errorf("Expected InRange(%v, %v, %v) to be %v, got %v", test.param, test.left, test.right, test.expected, actual) 481 } 482 } 483 484 var testsFloat32 = []struct { 485 param float32 486 left float32 487 right float32 488 expected bool 489 }{ 490 {0, 0, 0, true}, 491 {1, 0, 0, false}, 492 {-1, 0, 0, false}, 493 {0, -1, 1, true}, 494 {0, 0, 1, true}, 495 {0, -1, 0, true}, 496 {0, 0, -1, true}, 497 {0, 10, 5, false}, 498 } 499 for _, test := range testsFloat32 { 500 actual := InRange(test.param, test.left, test.right) 501 if actual != test.expected { 502 t.Errorf("Expected InRange(%v, %v, %v) to be %v, got %v", test.param, test.left, test.right, test.expected, actual) 503 } 504 } 505 506 var testsFloat64 = []struct { 507 param float64 508 left float64 509 right float64 510 expected bool 511 }{ 512 {0, 0, 0, true}, 513 {1, 0, 0, false}, 514 {-1, 0, 0, false}, 515 {0, -1, 1, true}, 516 {0, 0, 1, true}, 517 {0, -1, 0, true}, 518 {0, 0, -1, true}, 519 {0, 10, 5, false}, 520 } 521 for _, test := range testsFloat64 { 522 actual := InRange(test.param, test.left, test.right) 523 if actual != test.expected { 524 t.Errorf("Expected InRange(%v, %v, %v) to be %v, got %v", test.param, test.left, test.right, test.expected, actual) 525 } 526 } 527 528 var testsTypeMix = []struct { 529 param int 530 left float64 531 right float64 532 expected bool 533 }{ 534 {0, 0, 0, true}, 535 {1, 0, 0, false}, 536 {-1, 0, 0, false}, 537 {0, -1, 1, true}, 538 {0, 0, 1, true}, 539 {0, -1, 0, true}, 540 {0, 0, -1, true}, 541 {0, 10, 5, false}, 542 } 543 for _, test := range testsTypeMix { 544 actual := InRange(test.param, test.left, test.right) 545 if actual != test.expected { 546 t.Errorf("Expected InRange(%v, %v, %v) to be %v, got %v", test.param, test.left, test.right, test.expected, actual) 547 } 548 } 549 }