Skip to content

Commit 4f07c5d

Browse files
committed
Add tests for null and not-comparable cases
1 parent c165218 commit 4f07c5d

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

ext/standard/math.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ PHP_FUNCTION(clamp)
410410
RETURN_THROWS();
411411
}
412412

413-
if (zend_compare(zmin, zmax) > 0) {
413+
if (zend_compare(zmax, zmin) == -1) {
414414
zend_argument_value_error(2, "must be smaller than or equal to argument #3 ($max)");
415415
RETURN_THROWS();
416416
}
@@ -445,7 +445,7 @@ ZEND_FRAMELESS_FUNCTION(clamp, 3)
445445
RETURN_THROWS();
446446
}
447447

448-
if (zend_compare(zmin, zmax) > 0) {
448+
if (zend_compare(zmax, zmin) == -1) {
449449
zend_argument_value_error(2, "must be smaller than or equal to argument #3 ($max)");
450450
RETURN_THROWS();
451451
}

ext/standard/tests/math/clamp.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ echo clamp('2025-08-01', '2025-08-15', '2025-09-15'), "\n";
2121
echo clamp('2025-08-20', '2025-08-15', '2025-09-15'), "\n";
2222
echo clamp(new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'), "\n";
2323
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));
29+
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";
2436

2537
try {
2638
var_dump(clamp(4, NAN, 6));
@@ -39,6 +51,20 @@ try {
3951
} catch (ValueError $error) {
4052
echo $error->getMessage(), "\n";
4153
}
54+
55+
56+
try {
57+
var_dump(clamp(-9999, 5, null));
58+
} catch (ValueError $error) {
59+
echo $error->getMessage(), "\n";
60+
}
61+
62+
try {
63+
var_dump(clamp(12, -5, null));
64+
} catch (ValueError $error) {
65+
echo $error->getMessage(), "\n";
66+
}
67+
4268
?>
4369
--EXPECT--
4470
int(2)
@@ -56,6 +82,16 @@ string(1) "d"
5682
2025-08-20
5783
2025-08-15
5884
2025-08-20
85+
int(-1)
86+
int(1)
87+
int(-3)
88+
int(-9999)
89+
int(10)
90+
InvalidArgumentException
91+
RuntimeException
92+
LogicException
5993
clamp(): Argument #2 ($min) cannot be NAN
6094
clamp(): Argument #3 ($max) cannot be NAN
6195
clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)
96+
clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)
97+
clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)

0 commit comments

Comments
 (0)