Skip to content

Commit 2b80f92

Browse files
committed
Create tests triggering both frameless and dynamic variants
1 parent 8616967 commit 2b80f92

File tree

1 file changed

+56
-50
lines changed

1 file changed

+56
-50
lines changed

ext/standard/tests/math/clamp.phpt

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,69 @@
22
clamp() tests
33
--INI--
44
precision=14
5-
date.timezone = UTC
5+
date.timezone=UTC
66
--FILE--
77
<?php
88

9-
var_dump(clamp(2, 1, 3));
10-
var_dump(clamp(0, 1, 3));
11-
var_dump(clamp(6, 1, 3));
12-
var_dump(clamp(2, 1.3, 3.4));
13-
var_dump(clamp(2.5, 1, 3));
14-
var_dump(clamp(2.5, 1.3, 3.4));
15-
var_dump(clamp(0, 1.3, 3.4));
16-
var_dump(clamp(M_PI, -INF, INF));
17-
var_dump(clamp(NAN, 4, 6));
18-
var_dump(clamp("a", "c", "g"));
19-
var_dump(clamp("d", "c", "g"));
20-
echo clamp('2025-08-01', '2025-08-15', '2025-09-15'), "\n";
21-
echo clamp('2025-08-20', '2025-08-15', '2025-09-15'), "\n";
22-
echo clamp(new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n";
23-
echo clamp(new \DateTimeImmutable('2025-08-20'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n";
24-
var_dump(clamp(null, -1, 1));
25-
var_dump(clamp(null, 1, 3));
26-
var_dump(clamp(null, -3, -1));
27-
var_dump(clamp(-9999, null, 10));
28-
var_dump(clamp(12, null, 10));
9+
function make_clamp_fcc() {
10+
return clamp(...);
11+
}
2912

30-
$a = new \InvalidArgumentException('a');
31-
$b = new \RuntimeException('b');
32-
$c = new \LogicException('c');
33-
echo clamp($a, $b, $c)::class, "\n";
34-
echo clamp($b, $a, $c)::class, "\n";
35-
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)));
3617

37-
try {
38-
var_dump(clamp(4, NAN, 6));
39-
} catch (ValueError $error) {
40-
echo $error->getMessage(), "\n";
18+
return $flf;
4119
}
4220

43-
try {
44-
var_dump(clamp(7, 6, NAN));
45-
} catch (ValueError $error) {
46-
echo $error->getMessage(), "\n";
47-
}
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+
}
4827

49-
try {
50-
var_dump(clamp(1, 3, 2));
51-
} catch (ValueError $error) {
52-
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+
}
5333
}
5434

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));
5555

56-
try {
57-
var_dump(clamp(-9999, 5, null));
58-
} catch (ValueError $error) {
59-
echo $error->getMessage(), "\n";
60-
}
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";
6162

62-
try {
63-
var_dump(clamp(12, -5, null));
64-
} catch (ValueError $error) {
65-
echo $error->getMessage(), "\n";
66-
}
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);
6768

6869
?>
6970
--EXPECT--
@@ -91,7 +92,12 @@ InvalidArgumentException
9192
RuntimeException
9293
LogicException
9394
clamp(): Argument #2 ($min) cannot be NAN
95+
clamp(): Argument #2 ($min) cannot be NAN
96+
clamp(): Argument #3 ($max) cannot be NAN
9497
clamp(): Argument #3 ($max) cannot be NAN
9598
clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)
9699
clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)
97100
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

Comments
 (0)