|
2 | 2 | clamp() tests – non‑frameless code path |
3 | 3 | --INI-- |
4 | 4 | precision=14 |
5 | | -date.timezone = UTC |
6 | | -opcache.enable=1 |
7 | | -opcache.enable_cli=1 |
8 | | -opcache.jit=0 |
9 | | -opcache.jit_buffer_size=0 |
| 5 | +date.timezone=UTC |
10 | 6 | --FILE-- |
11 | 7 | <?php |
12 | 8 |
|
13 | | -var_dump(clamp(2, 1, 3)); |
14 | | -var_dump(clamp(0, 1, 3)); |
15 | | -var_dump(clamp(6, 1, 3)); |
16 | | -var_dump(clamp(2, 1.3, 3.4)); |
17 | | -var_dump(clamp(2.5, 1, 3)); |
18 | | -var_dump(clamp(2.5, 1.3, 3.4)); |
19 | | -var_dump(clamp(0, 1.3, 3.4)); |
20 | | -var_dump(clamp(M_PI, -INF, INF)); |
21 | | -var_dump(clamp(NAN, 4, 6)); |
22 | | -var_dump(clamp("a", "c", "g")); |
23 | | -var_dump(clamp("d", "c", "g")); |
24 | | -echo clamp('2025-08-01', '2025-08-15', '2025-09-15'), "\n"; |
25 | | -echo clamp('2025-08-20', '2025-08-15', '2025-09-15'), "\n"; |
26 | | -echo clamp(new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n"; |
27 | | -echo clamp(new \DateTimeImmutable('2025-08-20'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n"; |
28 | | -var_dump(clamp(null, -1, 1)); |
29 | | -var_dump(clamp(null, 1, 3)); |
30 | | -var_dump(clamp(null, -3, -1)); |
31 | | -var_dump(clamp(-9999, null, 10)); |
32 | | -var_dump(clamp(12, null, 10)); |
| 9 | +function make_clamp_fcc() { |
| 10 | + return clamp(...); |
| 11 | +} |
33 | 12 |
|
34 | | -$a = new \InvalidArgumentException('a'); |
35 | | -$b = new \RuntimeException('b'); |
36 | | -$c = new \LogicException('c'); |
37 | | -echo clamp($a, $b, $c)::class, "\n"; |
38 | | -echo clamp($b, $a, $c)::class, "\n"; |
39 | | -echo clamp($c, $a, $b)::class, "\n"; |
| 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))); |
40 | 17 |
|
41 | | -try { |
42 | | - var_dump(clamp(4, NAN, 6)); |
43 | | -} catch (ValueError $error) { |
44 | | - echo $error->getMessage(), "\n"; |
| 18 | + return $flf; |
45 | 19 | } |
46 | 20 |
|
47 | | -try { |
48 | | - var_dump(clamp(7, 6, NAN)); |
49 | | -} catch (ValueError $error) { |
50 | | - echo $error->getMessage(), "\n"; |
51 | | -} |
| 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 | + } |
52 | 27 |
|
53 | | -try { |
54 | | - var_dump(clamp(1, 3, 2)); |
55 | | -} catch (ValueError $error) { |
56 | | - echo $error->getMessage(), "\n"; |
| 28 | + try { |
| 29 | + var_dump(make_clamp_fcc()($value, $min, $max)); |
| 30 | + } catch (ValueError $error) { |
| 31 | + echo $error->getMessage(), "\n"; |
| 32 | + } |
57 | 33 | } |
58 | 34 |
|
59 | | -try { |
60 | | - var_dump(clamp(-9999, 5, null)); |
61 | | -} catch (ValueError $error) { |
62 | | - echo $error->getMessage(), "\n"; |
63 | | -} |
| 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)); |
64 | 55 |
|
65 | | -try { |
66 | | - var_dump(clamp(12, -5, null)); |
67 | | -} catch (ValueError $error) { |
68 | | - echo $error->getMessage(), "\n"; |
69 | | -} |
| 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); |
70 | 68 |
|
71 | 69 | ?> |
72 | 70 | --EXPECT-- |
@@ -94,7 +92,12 @@ InvalidArgumentException |
94 | 92 | RuntimeException |
95 | 93 | LogicException |
96 | 94 | clamp(): Argument #2 ($min) cannot be NAN |
| 95 | +clamp(): Argument #2 ($min) cannot be NAN |
97 | 96 | clamp(): Argument #3 ($max) cannot be NAN |
| 97 | +clamp(): Argument #2 ($min) 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) |
98 | 101 | clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max) |
99 | 102 | clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max) |
100 | 103 | clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max) |
0 commit comments