<?php
function consistsOfTheSameValues(array $A, array $B, $strict = false, $assumeEqual = false)
{
// check size of both arrays
if (count($A) !== count($B)) {
return false;
}
// optional pre-test that increases performance for most unequal arrays, but is worse for equal arrays
if (! $assumeEqual) {
foreach ($A as $a) {
// check that expected value exists in the array
if (! in_array($a, $B, $strict)) {
return false;
}
}
}
foreach ($A as $a) {
// check that expected value occurs the same amount of times in both arrays
if (count(array_keys($A, $a, $strict)) !== count(array_keys($B, $a, $strict))) {
return false;
}
}
return true;
}
// A unit testing framework in a tweet. https://gist.github.com/mathiasverraes/9046427
function it($m,$p){echo ($p?'✔︎':'✘')." It $m\n"; if(!$p){$GLOBALS['f']=1;}}function done(){if(@$GLOBALS['f'])die(1);}
// create two big arrays
$big1 = [];
for ($i = 0; $i < 999; $i++) { $big1[] = rand(); }
$big2 = $big1;
$big1[] = 0;
$big2[] = 1;
foreach ([false, true] as $ae) {
foreach ([true, false] as $st) {
echo '### ' . ($st ? 'strict (===)' : 'loose (==)' ) . ' - ' . ($ae ? 'skip' : 'use') . " in_array\n";
// positive tests
it('consists of the same values',
consistsOfTheSameValues([1], [1], $st, $ae) === true
);
it('consists of the same values',
consistsOfTheSameValues([1, 1], [1, 1], $st, $ae) === true
);
it('consists of the same values',
consistsOfTheSameValues(['1', 1], ['1', 1], $st, $ae) === true
);
it('consists of the same values',
consistsOfTheSameValues(['1', 1], [1, '1'], $st, $ae) === true
);
it('consists of the same values',
consistsOfTheSameValues([1, '1'], ['1', 1], $st, $ae) === true
);
it('consists of the same values',
consistsOfTheSameValues([1, '1'], [1, '1'], $st, $ae) === true
);
it('consists of the same values with key',
consistsOfTheSameValues(['x' => 1], ['x' => 1], $st, $ae) === true
);
it('consists of the same values with key',
consistsOfTheSameValues(['x' => 1], ['y' => 1], $st, $ae) === true
);
it('consists of the same values with key',
consistsOfTheSameValues(['y' => 1], ['x' => 1], $st, $ae) === true
);
it('consists of the same values with key',
consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 1, 'y' => 1], $st, $ae) === true
);
it('consists of the same values with key',
consistsOfTheSameValues(['y' => 1, 'x' => 1], ['x' => 1, 'y' => 1], $st, $ae) === true
);
it('consists of the same values with key',
consistsOfTheSameValues(['x' => 1, 'y' => 1], ['y' => 1, 'x' => 1], $st, $ae) === true
);
it('consists of the same values with key',
consistsOfTheSameValues(['y' => 1, 'x' => 1], ['y' => 1, 'x' => 1], $st, $ae) === true
);
it('consists of the same values with key',
consistsOfTheSameValues(['x' => 2, 'y' => 1], ['x' => 1, 'y' => 2], $st, $ae) === true
);
it('consists of the same values for big1 and big1',
consistsOfTheSameValues($big1, $big1, $st, $ae) === true
);
it('consists of the same values for big2 and big2',
consistsOfTheSameValues($big2, $big2, $st, $ae) === true
);
// negative tests
it('does not consist of the same values',
consistsOfTheSameValues([1], [2], $st, $ae) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1], [1, 1], $st, $ae) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1, 1], [1], $st, $ae) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1], [1, 2], $st, $ae) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1], [2, 1], $st, $ae) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1, 2], [1], $st, $ae) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([2, 1], [1], $st, $ae) === false
);
it('does not consist of the same values with key',
consistsOfTheSameValues(['x' => 1], ['x' => 2], $st, $ae) === false
);
it('does not consist of the same values with key',
consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 1, 'y' => 2], $st, $ae) === false
);
it('does not consist of the same values with key',
consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 2, 'y' => 1], $st, $ae) === false
);
it('does not consist of the same values with key',
consistsOfTheSameValues(['x' => 2, 'y' => 1], ['x' => 1, 'y' => 1], $st, $ae) === false
);
it('does not consist of the same values for big1 and big2',
consistsOfTheSameValues($big1, $big2, $st, $ae) === false
);
it('does not consist of the same values for big2 and big1',
consistsOfTheSameValues($big2, $big1, $st, $ae) === false
);
// test strictness
$consists_of = ($st ? 'does not consist of the same strict values' : 'consists of the same loose values');
it($consists_of,
consistsOfTheSameValues(['1'], [1], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues([1], ['1'], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues(['1', 1], [1, 1], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues([1, '1'], [1, 1], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues([1, 1], ['1', 1], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues([1, 1], [1, '1'], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues(['1', '1'], [1, 1], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues(['1', '1'], ['1', 1], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues(['1', '1'], [1, '1'], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues([1, 1], ['1', '1'], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues(['1', 1], ['1', '1'], $st, $ae) !== $st
);
it($consists_of,
consistsOfTheSameValues([1, '1'], ['1', '1'], $st, $ae) !== $st
);
}
}
preferences:
26.13 ms | 404 KiB | 5 Q