|
| 1 | +--TEST-- |
| 2 | +clamp() tests – compare frameless and dynamic code paths |
| 3 | +--INI-- |
| 4 | +precision=14 |
| 5 | +date.timezone=UTC |
| 6 | +--FILE-- |
| 7 | +<?php |
| 8 | + |
| 9 | +function make_clamp_fcc() { |
| 10 | + return clamp(...); |
| 11 | +} |
| 12 | + |
| 13 | +function check_clamp_result($value, $min, $max) { |
| 14 | + $flf = clamp($value, $min, $max); |
| 15 | + $dyn = make_clamp_fcc()($value, $min, $max); |
| 16 | + assert($flf === $dyn || (is_nan($flf) && is_nan($dyn))); |
| 17 | + |
| 18 | + return $flf; |
| 19 | +} |
| 20 | + |
| 21 | +function check_clamp_exception($value, $min, $max) { |
| 22 | + try { |
| 23 | + var_dump(clamp($value, $min, $max)); |
| 24 | + } catch (ValueError $error) { |
| 25 | + echo $error->getMessage(), "\n"; |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + var_dump(make_clamp_fcc()($value, $min, $max)); |
| 30 | + } catch (ValueError $error) { |
| 31 | + echo $error->getMessage(), "\n"; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +var_dump(check_clamp_result(2, 1, 3)); |
| 36 | +var_dump(check_clamp_result(0, 1, 3)); |
| 37 | +var_dump(check_clamp_result(6, 1, 3)); |
| 38 | +var_dump(check_clamp_result(2, 1.3, 3.4)); |
| 39 | +var_dump(check_clamp_result(2.5, 1, 3)); |
| 40 | +var_dump(check_clamp_result(2.5, 1.3, 3.4)); |
| 41 | +var_dump(check_clamp_result(0, 1.3, 3.4)); |
| 42 | +var_dump(check_clamp_result(M_PI, -INF, INF)); |
| 43 | +var_dump(check_clamp_result(NAN, 4, 6)); |
| 44 | +var_dump(check_clamp_result("a", "c", "g")); |
| 45 | +var_dump(check_clamp_result("d", "c", "g")); |
| 46 | +echo check_clamp_result('2025-08-01', '2025-08-15', '2025-09-15'), "\n"; |
| 47 | +echo check_clamp_result('2025-08-20', '2025-08-15', '2025-09-15'), "\n"; |
| 48 | +echo check_clamp_result(new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n"; |
| 49 | +echo check_clamp_result(new \DateTimeImmutable('2025-08-20'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n"; |
| 50 | +var_dump(check_clamp_result(null, -1, 1)); |
| 51 | +var_dump(check_clamp_result(null, 1, 3)); |
| 52 | +var_dump(check_clamp_result(null, -3, -1)); |
| 53 | +var_dump(check_clamp_result(-9999, null, 10)); |
| 54 | +var_dump(check_clamp_result(12, null, 10)); |
| 55 | + |
| 56 | +$a = new \InvalidArgumentException('a'); |
| 57 | +$b = new \RuntimeException('b'); |
| 58 | +$c = new \LogicException('c'); |
| 59 | +echo check_clamp_result($a, $b, $c)::class, "\n"; |
| 60 | +echo check_clamp_result($b, $a, $c)::class, "\n"; |
| 61 | +echo check_clamp_result($c, $a, $b)::class, "\n"; |
| 62 | + |
| 63 | +check_clamp_exception(4, NAN, 6); |
| 64 | +check_clamp_exception(7, 6, NAN); |
| 65 | +check_clamp_exception(1, 3, 2); |
| 66 | +check_clamp_exception(-9999, 5, null); |
| 67 | +check_clamp_exception(12, -5, null); |
| 68 | + |
| 69 | +?> |
| 70 | +--EXPECT-- |
| 71 | +int(2) |
| 72 | +int(1) |
| 73 | +int(3) |
| 74 | +int(2) |
| 75 | +float(2.5) |
| 76 | +float(2.5) |
| 77 | +float(1.3) |
| 78 | +float(3.141592653589793) |
| 79 | +float(NAN) |
| 80 | +string(1) "c" |
| 81 | +string(1) "d" |
| 82 | +2025-08-15 |
| 83 | +2025-08-20 |
| 84 | +2025-08-15 |
| 85 | +2025-08-20 |
| 86 | +int(-1) |
| 87 | +int(1) |
| 88 | +int(-3) |
| 89 | +int(-9999) |
| 90 | +int(10) |
| 91 | +InvalidArgumentException |
| 92 | +RuntimeException |
| 93 | +LogicException |
| 94 | +clamp(): Argument #2 ($min) cannot be NAN |
| 95 | +clamp(): Argument #2 ($min) cannot be NAN |
| 96 | +clamp(): Argument #3 ($max) cannot be NAN |
| 97 | +clamp(): Argument #3 ($max) cannot be NAN |
| 98 | +clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max) |
| 99 | +clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max) |
| 100 | +clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max) |
| 101 | +clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max) |
| 102 | +clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max) |
| 103 | +clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max) |
0 commit comments