]> granicus.if.org Git - php/commitdiff
- add some basic tests for array diff and intersection functions
authorRobert Nicholson <nicholsr@php.net>
Fri, 25 Jan 2008 00:13:49 +0000 (00:13 +0000)
committerRobert Nicholson <nicholsr@php.net>
Fri, 25 Jan 2008 00:13:49 +0000 (00:13 +0000)
13 files changed:
ext/standard/tests/array/array_diff_key_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_diff_key_variation1.phpt [new file with mode: 0644]
ext/standard/tests/array/array_diff_uassoc_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_diff_ukey_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_intersect_key_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_intersect_uassoc_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_intersect_ukey_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_udiff_assoc_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_udiff_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_udiff_uassoc_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_uintersect_assoc_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_uintersect_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_uintersect_uassoc_basic.phpt [new file with mode: 0644]

diff --git a/ext/standard/tests/array/array_diff_key_basic.phpt b/ext/standard/tests/array/array_diff_key_basic.phpt
new file mode 100644 (file)
index 0000000..6f6fcb9
--- /dev/null
@@ -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)
+}
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_diff_key_variation1.phpt b/ext/standard/tests/array/array_diff_key_variation1.phpt
new file mode 100644 (file)
index 0000000..00da7af
--- /dev/null
@@ -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
+)
diff --git a/ext/standard/tests/array/array_diff_uassoc_basic.phpt b/ext/standard/tests/array/array_diff_uassoc_basic.phpt
new file mode 100644 (file)
index 0000000..6a96be6
--- /dev/null
@@ -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"
+}
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_diff_ukey_basic.phpt b/ext/standard/tests/array/array_diff_ukey_basic.phpt
new file mode 100644 (file)
index 0000000..7ac309a
--- /dev/null
@@ -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)
+}
diff --git a/ext/standard/tests/array/array_intersect_key_basic.phpt b/ext/standard/tests/array/array_intersect_key_basic.phpt
new file mode 100644 (file)
index 0000000..fc6e177
--- /dev/null
@@ -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)
+}
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_intersect_uassoc_basic.phpt b/ext/standard/tests/array/array_intersect_uassoc_basic.phpt
new file mode 100644 (file)
index 0000000..1a2d57e
--- /dev/null
@@ -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"
+}
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_intersect_ukey_basic.phpt b/ext/standard/tests/array/array_intersect_ukey_basic.phpt
new file mode 100644 (file)
index 0000000..db21b9b
--- /dev/null
@@ -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)
+}
diff --git a/ext/standard/tests/array/array_udiff_assoc_basic.phpt b/ext/standard/tests/array/array_udiff_assoc_basic.phpt
new file mode 100644 (file)
index 0000000..769bafb
--- /dev/null
@@ -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)
+  }
+}
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_udiff_basic.phpt b/ext/standard/tests/array/array_udiff_basic.phpt
new file mode 100644 (file)
index 0000000..3da1b60
--- /dev/null
@@ -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)
+  }
+}
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_udiff_uassoc_basic.phpt b/ext/standard/tests/array/array_udiff_uassoc_basic.phpt
new file mode 100644 (file)
index 0000000..6095696
--- /dev/null
@@ -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)
+  }
+}
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_uintersect_assoc_basic.phpt b/ext/standard/tests/array/array_uintersect_assoc_basic.phpt
new file mode 100644 (file)
index 0000000..7e9fff7
--- /dev/null
@@ -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)
+  }
+}
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_uintersect_basic.phpt b/ext/standard/tests/array/array_uintersect_basic.phpt
new file mode 100644 (file)
index 0000000..8d4b803
--- /dev/null
@@ -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)
+  }
+}
\ No newline at end of file
diff --git a/ext/standard/tests/array/array_uintersect_uassoc_basic.phpt b/ext/standard/tests/array/array_uintersect_uassoc_basic.phpt
new file mode 100644 (file)
index 0000000..a474bc7
--- /dev/null
@@ -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)
+  }
+}
\ No newline at end of file