Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d27e1e1

Browse files
committed
core: Add new test for coercions from NAN
1 parent f389ade commit d27e1e1

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
--TEST--
2+
NAN coerced to other types
3+
--FILE--
4+
<?php
5+
6+
$inputs = [
7+
0,
8+
null,
9+
false,
10+
true,
11+
"",
12+
[],
13+
NAN,
14+
];
15+
16+
$nan = fdiv(0, 0);
17+
var_dump($nan);
18+
foreach ($inputs as $right) {
19+
echo 'Using ';
20+
var_export($right);
21+
echo ' as right op', PHP_EOL;
22+
var_dump($nan == $right);
23+
var_dump($nan != $right);
24+
var_dump($nan === $right);
25+
var_dump($nan !== $right);
26+
var_dump($nan < $right);
27+
var_dump($nan <= $right);
28+
var_dump($nan > $right);
29+
var_dump($nan >= $right);
30+
var_dump($nan <=> $right);
31+
}
32+
33+
?>
34+
--EXPECT--
35+
float(NAN)
36+
Using 0 as right op
37+
bool(false)
38+
bool(true)
39+
bool(false)
40+
bool(true)
41+
bool(false)
42+
bool(false)
43+
bool(false)
44+
bool(false)
45+
int(1)
46+
Using NULL as right op
47+
bool(false)
48+
bool(true)
49+
bool(false)
50+
bool(true)
51+
bool(false)
52+
bool(false)
53+
bool(true)
54+
bool(true)
55+
int(1)
56+
Using false as right op
57+
bool(false)
58+
bool(true)
59+
bool(false)
60+
bool(true)
61+
bool(false)
62+
bool(false)
63+
bool(true)
64+
bool(true)
65+
int(1)
66+
Using true as right op
67+
bool(true)
68+
bool(false)
69+
bool(false)
70+
bool(true)
71+
bool(false)
72+
bool(true)
73+
bool(false)
74+
bool(true)
75+
int(0)
76+
Using '' as right op
77+
bool(false)
78+
bool(true)
79+
bool(false)
80+
bool(true)
81+
bool(false)
82+
bool(false)
83+
bool(false)
84+
bool(false)
85+
int(1)
86+
Using array (
87+
) as right op
88+
bool(false)
89+
bool(true)
90+
bool(false)
91+
bool(true)
92+
bool(true)
93+
bool(true)
94+
bool(false)
95+
bool(false)
96+
int(-1)
97+
Using NAN as right op
98+
bool(false)
99+
bool(true)
100+
bool(false)
101+
bool(true)
102+
bool(false)
103+
bool(false)
104+
bool(false)
105+
bool(false)
106+
int(1)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--TEST--
2+
NAN coerced to other types
3+
--FILE--
4+
<?php
5+
6+
$nan = fdiv(0, 0);
7+
var_dump($nan);
8+
9+
function implicit_to_bool(bool $v) {
10+
var_dump($v);
11+
}
12+
function implicit_to_string(string $v) {
13+
var_dump($v);
14+
}
15+
16+
implicit_to_bool($nan);
17+
implicit_to_string($nan);
18+
19+
var_dump((int) $nan);
20+
var_dump((bool) $nan);
21+
var_dump((string) $nan);
22+
var_dump((array) $nan);
23+
var_dump((object) $nan);
24+
25+
$types = [
26+
'null',
27+
'bool',
28+
'int',
29+
'string',
30+
'array',
31+
'object',
32+
];
33+
34+
foreach ($types as $type) {
35+
$nan = fdiv(0, 0);
36+
settype($nan, $type);
37+
var_dump($nan);
38+
}
39+
40+
?>
41+
--EXPECTF--
42+
float(NAN)
43+
bool(true)
44+
string(3) "NAN"
45+
46+
Warning: The float NAN is not representable as an int, cast occurred in %s on line %d
47+
int(0)
48+
bool(true)
49+
string(3) "NAN"
50+
array(1) {
51+
[0]=>
52+
float(NAN)
53+
}
54+
object(stdClass)#%d (1) {
55+
["scalar"]=>
56+
float(NAN)
57+
}
58+
NULL
59+
bool(true)
60+
61+
Warning: The float NAN is not representable as an int, cast occurred in %s on line %d
62+
int(0)
63+
string(3) "NAN"
64+
array(1) {
65+
[0]=>
66+
float(NAN)
67+
}
68+
object(stdClass)#%d (1) {
69+
["scalar"]=>
70+
float(NAN)
71+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Const NAN with bool cast should emit 1 warning
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
--EXTENSIONS--
8+
opcache
9+
--FILE--
10+
<?php
11+
echo (bool)NAN;
12+
?>
13+
--EXPECTF--
14+
Warning: unexpected NAN value was coerced to bool in %s on line 2
15+
1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /