- add some basic tests for array diff and intersection functions

This commit is contained in:
Robert Nicholson 2008-01-25 00:13:49 +00:00
parent 39b585001a
commit 3b8256dce4
13 changed files with 466 additions and 0 deletions

View file

@ -0,0 +1,19 @@
--TEST--
Test array_diff_key() : basic functionality
--FILE--
<?php
/*
* proto array array_diff_key(array arr1, array arr2 [, array ...])
* Function is implemented in ext/standard/array.c
*/
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_diff_key($array1, $array2));
?>
--EXPECT--
array(2) {
["red"]=>
int(2)
["purple"]=>
int(4)
}

View file

@ -0,0 +1,89 @@
--TEST--
array_diff_key() : type variations
--FILE--
<?php
/*
* proto array array_diff_key(array arr1, array arr2 [, array ...])
* Function is implemented in ext/standard/array.c
*/
/*
* Testing how array_diff_key treats keys that are numbers, floating point numbers or strings.
*/
$arr1 = array(1 => 'a', 2 => 'b', 3 => 'c', 'key1' => 'value');
$arr2 = array(1.00 => 'a', 2.00 => 'b', 3.00 => 'c', 'key2' => 'value');
$arr3 = array('1' => 'a', '2' => 'b', '3' => 'c', 'key3' => 'value');
$arr4 = array('1.00' => 'a', '2.00' => 'b', '3.00' => 'c', 'key4' => 'value'); //$arr4 looks different to the other three arrays.
print "Result of comparing integers and floating point value:\n"; //1 and 1.00 are treated as the same thing
print_r(array_diff_key($arr1, $arr2));
print_r(array_diff_key($arr2, $arr1));
print "Result of comparing integers and strings containing an integers:\n"; //1 and the string 1 are treated as the same thing
print_r(array_diff_key($arr1, $arr3));
print_r(array_diff_key($arr3, $arr1));
print "Result of comparing integers and strings containing floating points:\n"; //1 and the string 1.00 are not treated as the same thing
print_r(array_diff_key($arr1, $arr4));
print_r(array_diff_key($arr4, $arr1));
print "Result of comparing floating points and strings containing integers:\n";
print_r(array_diff_key($arr2, $arr3)); //1.00 and the string 1 are treated as the same thing
print_r(array_diff_key($arr3, $arr2));
print "Result of comparing strings containing integers and strings containing floating points:\n"; //the strings 1 and 1.00 are not treated as the same thing.
print_r(array_diff_key($arr3, $arr4));
print_r(array_diff_key($arr4, $arr3));
?>
--EXPECTF--
Result of comparing integers and floating point value:
Array
(
[key1] => value
)
Array
(
[key2] => value
)
Result of comparing integers and strings containing an integers:
Array
(
[key1] => value
)
Array
(
[key3] => value
)
Result of comparing integers and strings containing floating points:
Array
(
[1] => a
[2] => b
[3] => c
[key1] => value
)
Array
(
[1.00] => a
[2.00] => b
[3.00] => c
[key4] => value
)
Result of comparing floating points and strings containing integers:
Array
(
[key2] => value
)
Array
(
[key3] => value
)
Result of comparing strings containing integers and strings containing floating points:
Array
(
[1] => a
[2] => b
[3] => c
[key3] => value
)
Array
(
[1.00] => a
[2.00] => b
[3.00] => c
[key4] => value
)

View file

@ -0,0 +1,28 @@
--TEST--
array_diff_uassoc(): Basic test
--FILE--
<?php
/*
* array array_diff_uassoc ( array $array1, array $array2 [, array $..., callback $key_compare_func] )
* Function is implemented in ext/standard/array.c
*/
function key_compare_func($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
var_dump($result);
?>
--EXPECT--
array(3) {
["b"]=>
string(5) "brown"
["c"]=>
string(4) "blue"
[0]=>
string(3) "red"
}

View file

@ -0,0 +1,24 @@
--TEST--
array_diff_ukey() : Basic test.
--FILE--
<?php
/*
* proto array array_diff_ukey ( array $array1, array $array2 [, array $ ..., callback $key_compare_func] )
* Function is implemented in ext/standard/array.c
*/
function key_compare_func($key1, $key2) {
if ($key1 == $key2) return 0;
else if ($key1 > $key2) return 1;
else return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_diff_ukey($array1, $array2, 'key_compare_func'));
?>
--EXPECT--
array(2) {
["red"]=>
int(2)
["purple"]=>
int(4)
}

View file

@ -0,0 +1,19 @@
--TEST--
array_intersect_key(): Basic Test
--FILE--
<?php
/*
* proto array array_intersect_key(array arr1, array arr2 [, array ...])
* Function is implemented in ext/standard/array.c
*/
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_key($array1, $array2));
?>
--EXPECT--
array(2) {
["blue"]=>
int(1)
["green"]=>
int(3)
}

View file

@ -0,0 +1,24 @@
--TEST--
array_intersect_uassoc(): Basic test
--FILE--
<?php
/*
* array array_intersect_uassoc ( array $array1, array $array2 [, array $..., callback $key_compare_func] )
* Function is implemented in ext/standard/array.c
*/
function key_compare_func($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_intersect_uassoc($array1, $array2, "key_compare_func");
var_dump($result);
?>
--EXPECT--
array(1) {
["a"]=>
string(5) "green"
}

View file

@ -0,0 +1,24 @@
--TEST--
array_intersect_ukey(): Basic test.
--FILE--
<?php
/*
* proto array array_intersect_ukey ( array $array1, array $array2 [, array $ ..., callback $key_compare_func] )
* Function is implemented in ext/standard/array.c
*/
function key_compare_func($key1, $key2) {
if ($key1 == $key2) return 0;
else if ($key1 > $key2) return 1;
else return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));
?>
--EXPECT--
array(2) {
["blue"]=>
int(1)
["green"]=>
int(3)
}

View file

@ -0,0 +1,41 @@
--TEST--
array_udiff_assoc(): Test return type and value for expected input
--FILE--
<?php
/*
* proto array array_udiff_assoc ( array $array1, array $array2 [, array $ ..., callback $data_compare_func] )
* Function is implemented in ext/standard/array.c
*/
class cr {
private $priv_member;
function cr($val) {
$this->priv_member = $val;
}
static function comp_func_cr($a, $b) {
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member) ? 1 : -1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1 => new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1 => new cr(4), 2 => new cr(-15),);
$result = array_udiff_assoc($a, $b, array("cr", "comp_func_cr"));
var_dump($result);
?>
--EXPECTF--
array(3) {
["0.1"]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(9)
}
["0.5"]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(12)
}
[0]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(23)
}
}

View file

@ -0,0 +1,36 @@
--TEST--
array_udiff():Test return type and value for expected input
--FILE--
<?php
/*
* proto array array_udiff ( array $array1, array $array2 [, array $ ..., callback $data_compare_func] )
* Function is implemented in ext/standard/array.c
*/
class cr {
private $priv_member;
function cr($val) {
$this->priv_member = $val;
}
static function comp_func_cr($a, $b) {
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member) ? 1 : -1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1 => new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1 => new cr(4), 2 => new cr(-15),);
$result = array_udiff($a, $b, array("cr", "comp_func_cr"));
var_dump($result);
?>
--EXPECTF--
array(2) {
["0.5"]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(12)
}
[0]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(23)
}
}

View file

@ -0,0 +1,45 @@
--TEST--
array_udiff_uassoc(): Test return type and value for expected input
--FILE--
<?php
/*
* proto array array_udiff_assoc ( array $array1, array $array2 [, array $ ..., callback $data_compare_func] )
* Function is implemented in ext/standard/array.c
*/
class cr {
private $priv_member;
function cr($val) {
$this->priv_member = $val;
}
static function comp_func_cr($a, $b) {
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member) ? 1 : -1;
}
static function comp_func_key($a, $b) {
if ($a === $b) return 0;
return ($a > $b) ? 1 : -1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1 => new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1 => new cr(4), 2 => new cr(-15),);
$result = array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key"));
var_dump($result);
?>
--EXPECTF--
array(3) {
["0.1"]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(9)
}
["0.5"]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(12)
}
[0]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(23)
}
}

View file

@ -0,0 +1,36 @@
--TEST--
array_uintersect_assoc(): Test return type and value for expected input
--FILE--
<?php
/*
* proto array array_uintersect_assoc ( array $array1, array $array2 [, array $ ..., callback $data_compare_func] )
* Function is implemented in ext/standard/array.c
*/
class cr {
private $priv_member;
function cr($val) {
$this->priv_member = $val;
}
static function comp_func_cr($a, $b) {
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member) ? 1 : -1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1 => new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1 => new cr(4), 2 => new cr(-15),);
$result = array_uintersect_assoc($a, $b, array("cr", "comp_func_cr"));
var_dump($result);
?>
--EXPECTF--
array(2) {
[1]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(4)
}
[2]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(-15)
}
}

View file

@ -0,0 +1,41 @@
--TEST--
array_uintersect(): Test return type and value for expected input
--FILE--
<?php
/*
* proto array array_uintersect ( array $array1, array $array2 [, array $ ..., callback $data_compare_func] )
* Function is implemented in ext/standard/array.c
*/
class cr {
private $priv_member;
function cr($val) {
$this->priv_member = $val;
}
static function comp_func_cr($a, $b) {
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member) ? 1 : -1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1 => new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1 => new cr(4), 2 => new cr(-15),);
$result = array_uintersect($a, $b, array("cr", "comp_func_cr"));
var_dump($result);
?>
--EXPECTF--
array(3) {
["0.1"]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(9)
}
[1]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(4)
}
[2]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(-15)
}
}

View file

@ -0,0 +1,40 @@
--TEST--
array_uintersect_uassoc(): Test return type and value for expected input
--FILE--
<?php
/*
* proto array array_uintersect_assoc ( array $array1, array $array2 [, array $ ..., callback $data_compare_func] )
* Function is implemented in ext/standard/array.c
*/
class cr {
private $priv_member;
function cr($val) {
$this->priv_member = $val;
}
static function comp_func_cr($a, $b) {
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member) ? 1 : -1;
}
static function comp_func_key($a, $b) {
if ($a === $b) return 0;
return ($a > $b) ? 1 : -1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1 => new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1 => new cr(4), 2 => new cr(-15),);
$result = array_uintersect_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key"));
var_dump($result);
?>
--EXPECTF--
array(2) {
[1]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(4)
}
[2]=>
object(cr)#%d (1) {
["priv_member":"cr":private]=>
int(-15)
}
}