Skip to content

Commit 014ef55

Browse files
committed
Generate tests comparing compile time to runtime offset behaviour
1 parent 99bb8a7 commit 014ef55

File tree

2 files changed

+198
-0
lines changed

2 files changed

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

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)