Skip to content

Commit 5119597

Browse files
committed
Add dedicated test
1 parent 46aa276 commit 5119597

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
--TEST--
2+
String to number comparison
3+
--FILE--
4+
<?php
5+
6+
// This tests the examples from the RFC.
7+
8+
function format($val) {
9+
if (is_float($val)) {
10+
if (is_nan($val)) return "NAN";
11+
if ($val == INF) return "INF";
12+
if ($val == -INF) return "-INF";
13+
}
14+
return json_encode($val);
15+
}
16+
17+
function compare_eq($val1, $val2) {
18+
echo format($val1), " == ", format($val2), ": ", format($val1 == $val2), "\n";
19+
}
20+
21+
function compare_3way($val1, $val2) {
22+
echo format($val1), " <=> ", format($val2), ": ", format($val1 <=> $val2), "\n";
23+
}
24+
25+
compare_eq(42, "000042");
26+
compare_eq(42, "42.0");
27+
compare_eq(42.0, "+42.0E0");
28+
compare_eq(0, "0e214987142012");
29+
echo "\n";
30+
31+
compare_eq("42", "000042");
32+
compare_eq("42", "42.0");
33+
compare_eq("42.0", "+42.0E0");
34+
compare_eq("0", "0e214987142012");
35+
echo "\n";
36+
37+
compare_eq(42, " 42");
38+
compare_eq(42, "42 ");
39+
compare_eq(42, "42abc");
40+
compare_eq(42, "abc42");
41+
compare_eq( 0, "abc42");
42+
echo "\n";
43+
44+
compare_eq(INF, "INF");
45+
compare_eq(-INF, "-INF");
46+
compare_eq(NAN, "NAN");
47+
compare_eq(INF, "1e1000");
48+
compare_eq(-INF, "-1e1000");
49+
echo "\n";
50+
51+
$float = 1.75;
52+
53+
echo "precision=14:\n";
54+
ini_set('precision', 14);
55+
compare_3way($float, "1.75abc");
56+
compare_3way((string) $float, "1.75abc");
57+
58+
echo "precision=0:\n";
59+
ini_set('precision', 0);
60+
compare_3way($float, "1.75abc");
61+
compare_3way((string) $float, "1.75abc");
62+
63+
?>
64+
--EXPECT--
65+
42 == "000042": true
66+
42 == "42.0": true
67+
42 == "+42.0E0": true
68+
0 == "0e214987142012": true
69+
70+
"42" == "000042": true
71+
"42" == "42.0": true
72+
"42.0" == "+42.0E0": true
73+
"0" == "0e214987142012": true
74+
75+
42 == " 42": true
76+
42 == "42 ": false
77+
42 == "42abc": false
78+
42 == "abc42": false
79+
0 == "abc42": false
80+
81+
INF == "INF": true
82+
-INF == "-INF": true
83+
NAN == "NAN": false
84+
INF == "1e1000": true
85+
-INF == "-1e1000": true
86+
87+
precision=14:
88+
1.75 <=> "1.75abc": -1
89+
"1.75" <=> "1.75abc": -1
90+
precision=0:
91+
1.75 <=> "1.75abc": 1
92+
"2" <=> "1.75abc": 1

0 commit comments

Comments
 (0)