Skip to content

Commit e465211

Browse files
committed
Add test to check polyfill behaves as str_increment()
1 parent 379fe18 commit e465211

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
--TEST--
2+
Verifying that the str_increment() polyfill behaves the same
3+
--FILE--
4+
<?php
5+
6+
function polyfill(string $s): string {
7+
if (is_numeric($s)) {
8+
$offset = stripos($s, 'e');
9+
if ($offset !== false) {
10+
/* Using increment operator would cast the string to float
11+
* Therefore we manually increment it to convert it to an "f"/"F" that doesn't get affected */
12+
$c = $s[$offset];
13+
$c++;
14+
$s[$offset] = $c;
15+
$s++;
16+
$s[$offset] = match ($s[$offset]) {
17+
'f' => 'e',
18+
'F' => 'E',
19+
'g' => 'f',
20+
'G' => 'F',
21+
};
22+
return $s;
23+
}
24+
}
25+
return ++$s;
26+
}
27+
28+
$strictlyAlphaNumeric = [
29+
"Az",
30+
"aZ",
31+
"A9",
32+
"a9",
33+
// Carrying values until the beginning of the string
34+
"Zz",
35+
"zZ",
36+
"9z",
37+
"9Z",
38+
// string interpretable as a number in scientific notation
39+
"5e6",
40+
"5E6",
41+
"5e9",
42+
"5E9",
43+
// Interned strings
44+
"d",
45+
"D",
46+
"4",
47+
];
48+
49+
foreach ($strictlyAlphaNumeric as $s) {
50+
if (str_increment($s) !== polyfill($s)) {
51+
var_dump("Error:", str_increment($s), polyfill($s));
52+
}
53+
}
54+
55+
echo "DONE";
56+
57+
?>
58+
--EXPECT--
59+
DONE

0 commit comments

Comments
 (0)