Skip to content

Commit a6a79c9

Browse files
committed
[skip ci] W.I.P. generate tests comparing compile time to runtime
1 parent b00c43d commit a6a79c9

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
--TEST--
2+
Test binary operands exposing the same behavior at compile as at run time
3+
--INI--
4+
memory_limit=256M
5+
--SKIPIF--
6+
<?php
7+
//if (getenv("SKIP_SLOW_TESTS")) die('skip slow test');
8+
?>
9+
--FILE--
10+
<?php
11+
12+
$containers = [
13+
null,
14+
false,
15+
true,
16+
'10',
17+
'25.5',
18+
'string',
19+
[],
20+
STDERR,
21+
new stdClass(),
22+
];
23+
24+
$offsets = [
25+
null,
26+
false,
27+
true,
28+
4,
29+
5.5,
30+
6.0,
31+
//PHP_INT_MAX,
32+
//PHP_INT_MIN,
33+
PHP_INT_MAX * 2,
34+
PHP_INT_MIN * 2,
35+
INF,
36+
NAN,
37+
'string',
38+
'7',
39+
'8.5',
40+
'9.0',
41+
'2e3',
42+
'20a',
43+
' 20',
44+
'20 ',
45+
//"9179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368",
46+
//"-9179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368",
47+
"0x14",
48+
(string) PHP_INT_MAX * 2,
49+
(string) PHP_INT_MIN * 2,
50+
];
51+
52+
function makeContainer($container) {
53+
if (is_resource($container)) {
54+
return "STDERR";
55+
}
56+
if ($container instanceof stdClass) {
57+
return "new stdClass()";
58+
}
59+
return var_export($container, true);
60+
}
61+
function makeOffset($offset) {
62+
if ($offset === PHP_INT_MIN) {
63+
return "PHP_INT_MIN";
64+
}
65+
if ($offset === PHP_INT_MAX) {
66+
return "PHP_INT_MAX";
67+
}
68+
return var_export($offset, true);
69+
}
70+
71+
function makeTestFile($container, $offset) {
72+
$offset_p = makeOffset($offset);
73+
$container_p = makeContainer($container);
74+
$fileContent = <<<test
75+
<?php
76+
77+
\$container = $container_p;
78+
79+
// Read before write
80+
try {
81+
echo "Read before write:\\n";
82+
var_dump(\$container[$offset_p]);
83+
} catch (\Throwable \$e) {
84+
echo \$e->getMessage(), "\\n";
85+
}
86+
// Write
87+
try {
88+
echo "Write:\\n";
89+
\$container[$offset_p] = 'v';
90+
} catch (\Throwable \$e) {
91+
echo \$e->getMessage(), "\\n";
92+
}
93+
// Read
94+
try {
95+
echo "Read:\\n";
96+
var_dump(\$container[$offset_p]);
97+
} catch (\Throwable \$e) {
98+
echo \$e->getMessage(), "\\n";
99+
}
100+
// Read-Write
101+
try {
102+
echo "Read-Write:\\n";
103+
\$container[$offset_p] .= 'append';
104+
} catch (\Throwable \$e) {
105+
echo \$e->getMessage(), "\\n";
106+
}
107+
// Is
108+
try {
109+
echo "isset():\\n";
110+
var_dump(isset(\$container[$offset_p]));
111+
} catch (\Throwable \$e) {
112+
echo \$e->getMessage(), "\\n";
113+
}
114+
try {
115+
echo "empty():\\n";
116+
var_dump(empty(\$container[$offset_p]));
117+
} catch (\Throwable \$e) {
118+
echo \$e->getMessage(), "\\n";
119+
}
120+
try {
121+
echo "Coalesce():\\n";
122+
var_dump(\$container[$offset_p] ?? 'default');
123+
} catch (\Throwable \$e) {
124+
echo \$e->getMessage(), "\\n";
125+
}
126+
test;
127+
return $fileContent;
128+
}
129+
130+
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'compare_binary_offsets_temp.php';
131+
132+
$failures = [];
133+
134+
ob_start();
135+
foreach ($containers as $container_orig) {
136+
foreach ($offsets as $offset) {
137+
$error = makeContainer($container_orig) . '[' . makeOffset($offset) . '] has different outputs' . "\n";
138+
file_put_contents($filename, makeTestFile($container_orig, $offset));
139+
140+
include $filename;
141+
$constOutput = ob_get_contents();
142+
ob_clean();
143+
$constOutput = str_replace(
144+
[$filename],
145+
['%s'],
146+
$constOutput
147+
);
148+
149+
$dimension = $offset;
150+
$container = $container_orig;
151+
$var_filename = __DIR__ . DIRECTORY_SEPARATOR . 'test_variable_offsets.inc';
152+
include $var_filename;
153+
$varOutput = ob_get_contents();
154+
ob_clean();
155+
$varOutput = str_replace(
156+
[$var_filename],
157+
['%s'],
158+
$varOutput
159+
);
160+
161+
if ($constOutput !== $varOutput) {
162+
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'debug_const.txt', $constOutput);
163+
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'debug_var.txt', $varOutput);
164+
$failures[] = $error;
165+
}
166+
}
167+
}
168+
ob_end_clean();
169+
170+
if ($failures !== []) {
171+
echo implode($failures);
172+
}
173+
174+
?>
175+
--CLEAN--
176+
<?php
177+
$fl = __DIR__ . DIRECTORY_SEPARATOR . 'compare_binary_operands_temp.php';
178+
@unlink($fl);
179+
?>
180+
--EXPECT--
181+
Failed: 0

Zend/tests/offsets/test_variable_offsets.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
//$container var declaration in const generated file
4+
35
// Read before write
46
try {
57
echo "Read before write:\n";

0 commit comments

Comments
 (0)