Skip to content

Commit

Permalink
Update and add 32-bit tests of strict_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
TysonAndre committed Oct 22, 2022
1 parent 29be006 commit d71499e
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@
<file name="misc/strict_hash_array_references.phpt" role="test" />
<file name="misc/strict_hash_array_references_32bit.phpt" role="test" />
<file name="misc/strict_hash_cyclic_recursion_fallback.phpt" role="test" />
<file name="misc/strict_hash_cyclic_recursion_fallback_32bit.phpt" role="test" />
<file name="misc/strict_hash_recursion.phpt" role="test" />
<file name="misc/strict_hash_recursion_32bit.phpt" role="test" />
<file name="misc/strict_hash_typed_property.phpt" role="test" />
Expand Down
14 changes: 7 additions & 7 deletions tests/misc/strict_hash_array_recursion_32bit.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ array(1) {
[0]=>
*RECURSION*
}
int(1357152471)
int(944359473)
int(-665309858)
int(-351226279)
array(1) {
[0]=>
*RECURSION*
}
int(1357152471)
int(944359473)
int(-665309858)
int(-351226279)
Recursive arrays hashed in a predictable way
array(2) {
[0]=>
Expand All @@ -58,7 +58,7 @@ array(2) {
int(123)
}
}
int(-2005905482)
int(-2005905482)
int(-1313776964)
int(-1313776964)
Test deeper recursion level
int(-2086164413)
int(-1313776964)
112 changes: 112 additions & 0 deletions tests/misc/strict_hash_cyclic_recursion_fallback_32bit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
--TEST--
Test strict_hash() handling of cyclic recursion edge cases
--SKIPIF--
<?php if (PHP_INT_SIZE > 4) echo "skip 32-bit only\n"; ?>
--FILE--
<?php

$x = [];
$x[1] = &$x;
$y = [1 => $x];

var_dump($x === $y); // true, so we expect the strict_hash to be the same. Only hash the key 1 but don't hash the top level array

// Expected: same hashes are computed
// Observed: Different hashes in Teds 1.2.6
var_dump(Teds\strict_hash($x), Teds\strict_hash($y));
var_dump($x);
echo "Test hash skips hashing sub-arrays if array contains cyclic arrays anywhere\n";
$x[2] = [];
$x[1] = &$x;
var_dump(Teds\strict_hash($x));
$x[2][] = 1;
var_dump(Teds\strict_hash($x));
var_dump($x);
$y[2] = [1];
$y[1] = &$x;
echo "x, y values:\n";
var_dump($x);
var_dump($y);
echo "x === y: ";
var_export($x === $y);
echo "\n";
var_dump(Teds\strict_hash($x));
var_dump(Teds\strict_hash($y));
$other = [];
$other[1] = &$other;
var_dump(new Teds\StrictHashSet([$x, $y, null, &$other, $other, [1 => null]]));
?>
--EXPECT--
bool(true)
int(-1313776964)
int(-1313776964)
array(1) {
[1]=>
*RECURSION*
}
Test hash skips hashing sub-arrays if array contains cyclic arrays anywhere
int(1650701688)
int(1650701688)
array(2) {
[1]=>
*RECURSION*
[2]=>
array(1) {
[0]=>
int(1)
}
}
x, y values:
array(2) {
[1]=>
*RECURSION*
[2]=>
array(1) {
[0]=>
int(1)
}
}
array(2) {
[1]=>
&array(2) {
[1]=>
*RECURSION*
[2]=>
array(1) {
[0]=>
int(1)
}
}
[2]=>
array(1) {
[0]=>
int(1)
}
}
x === y: true
int(1650701688)
int(1650701688)
object(Teds\StrictHashSet)#1 (4) {
[0]=>
array(2) {
[1]=>
*RECURSION*
[2]=>
array(1) {
[0]=>
int(1)
}
}
[1]=>
NULL
[2]=>
array(1) {
[1]=>
*RECURSION*
}
[3]=>
array(1) {
[1]=>
NULL
}
}

0 comments on commit d71499e

Please sign in to comment.