]> granicus.if.org Git - php/commitdiff
New testcases for array_flip() function
authorRaghubansh Kumar <kraghuba@php.net>
Mon, 15 Oct 2007 06:34:35 +0000 (06:34 +0000)
committerRaghubansh Kumar <kraghuba@php.net>
Mon, 15 Oct 2007 06:34:35 +0000 (06:34 +0000)
ext/standard/tests/array/array_flip_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_flip_error.phpt [new file with mode: 0644]
ext/standard/tests/array/array_flip_variation1.phpt [new file with mode: 0644]
ext/standard/tests/array/array_flip_variation2.phpt [new file with mode: 0644]
ext/standard/tests/array/array_flip_variation3.phpt [new file with mode: 0644]
ext/standard/tests/array/array_flip_variation4.phpt [new file with mode: 0644]
ext/standard/tests/array/array_flip_variation5.phpt [new file with mode: 0644]

diff --git a/ext/standard/tests/array/array_flip_basic.phpt b/ext/standard/tests/array/array_flip_basic.phpt
new file mode 100644 (file)
index 0000000..08a63fb
--- /dev/null
@@ -0,0 +1,71 @@
+--TEST--
+Test array_flip() function : basic functionality 
+--FILE--
+<?php
+/* Prototype  : array array_flip(array $input)
+ * Description: Return array with key <-> value flipped 
+ * Source code: ext/standard/array.c
+*/
+
+echo "*** Testing array_flip() : basic functionality ***\n";
+
+// array with default keys - numeric values
+$input = array(1, 2);   
+var_dump( array_flip($input) );
+
+// array with default keys - string values
+$input = array('value1', "value2");
+var_dump( array_flip($input) );
+
+// associative arrays - key as string
+$input = array('key1' => 1, "key2" => 2); 
+var_dump( array_flip($input) );
+
+// associative arrays - key as numeric
+$input = array(1 => 'one', 2 => "two");
+var_dump( array_flip($input) );
+
+// combination of associative and non-associative array
+$input = array(1 => 'one','two', 3 => 'three', 4, "five" => 5);
+var_dump( array_flip($input) );
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_flip() : basic functionality ***
+array(2) {
+  [1]=>
+  int(0)
+  [2]=>
+  int(1)
+}
+array(2) {
+  ["value1"]=>
+  int(0)
+  ["value2"]=>
+  int(1)
+}
+array(2) {
+  [1]=>
+  string(4) "key1"
+  [2]=>
+  string(4) "key2"
+}
+array(2) {
+  ["one"]=>
+  int(1)
+  ["two"]=>
+  int(2)
+}
+array(5) {
+  ["one"]=>
+  int(1)
+  ["two"]=>
+  int(2)
+  ["three"]=>
+  int(3)
+  [4]=>
+  int(4)
+  [5]=>
+  string(4) "five"
+}
+Done
diff --git a/ext/standard/tests/array/array_flip_error.phpt b/ext/standard/tests/array/array_flip_error.phpt
new file mode 100644 (file)
index 0000000..64339d7
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+Test array_flip() function : error conditions 
+--FILE--
+<?php
+/* Prototype  : array array_flip(array $input)
+ * Description: Return array with key <-> value flipped 
+ * Source code: ext/standard/array.c
+*/
+
+echo "*** Testing array_flip() : error conditions ***\n";
+
+// Zero arguments
+echo "-- Testing array_flip() function with Zero arguments --\n";
+var_dump( array_flip() );
+
+//one more than the expected number of arguments
+echo "-- Testing array_flip() function with more than expected no. of arguments --\n";
+$input = array(1 => 'one', 2 => 'two');
+$extra_arg = 10;
+var_dump( array_flip($input, $extra_arg) );
+
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_flip() : error conditions ***
+-- Testing array_flip() function with Zero arguments --
+
+Warning: Wrong parameter count for array_flip() in %s on line %d
+NULL
+-- Testing array_flip() function with more than expected no. of arguments --
+
+Warning: Wrong parameter count for array_flip() in %s on line %d
+NULL
+Done
diff --git a/ext/standard/tests/array/array_flip_variation1.phpt b/ext/standard/tests/array/array_flip_variation1.phpt
new file mode 100644 (file)
index 0000000..8042803
--- /dev/null
@@ -0,0 +1,178 @@
+--TEST--
+Test array_flip() function : usage variations - unexpected values for 'input' argument
+--FILE--
+<?php
+/* Prototype  : array array_flip(array $input)
+ * Description: Return array with key <-> value flipped 
+ * Source code: ext/standard/array.c
+*/
+
+echo "*** Testing array_flip() : usage variations - unexpected values for 'input' ***\n";
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+//class definition for object variable
+class MyClass
+{
+  public function __toString()
+  {
+     return 'object';
+  }
+}
+
+//resource variable
+$fp = fopen(__FILE__,'r');
+    
+//array of values for 'input' argument
+$values = array(
+          // int data
+  /*1*/   0,
+          1,
+          12345,
+          -2345,
+
+          // float data
+  /*5*/   10.5,
+          -10.5,
+          10.5e10,
+          10.6E-10,
+          .5,
+
+          // null data
+  /*10*/  NULL,
+          null,
+
+          // boolean data
+  /*12*/  true,
+          false,
+          TRUE,
+  /*15*/  FALSE,
+
+          // empty data
+          "",
+          '',
+
+          // string data
+          "string",
+          'string',
+
+          // object data
+  /*20*/  new MyClass(),
+
+          // undefined data
+          @$undefined_var,
+
+          // unset data
+          @$unset_var,
+
+          //resource data
+  /*23*/  $fp
+);
+
+// loop through each element of $values for 'input' argument
+for($count = 0; $count < count($values); $count++) {
+  echo "-- Iteration ".($count + 1). " --\n";
+  var_dump( array_flip($values[$count]) );
+};
+
+//closing resource
+fclose($fp);
+
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_flip() : usage variations - unexpected values for 'input' ***
+-- Iteration 1 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 2 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 3 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 4 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 5 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 6 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 7 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 8 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 9 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 10 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 11 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 12 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 13 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 14 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 15 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 16 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 17 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 18 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 19 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 20 --
+array(0) {
+}
+-- Iteration 21 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 22 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+-- Iteration 23 --
+
+Warning: array_flip(): The argument should be an array in %s on line %d
+bool(false)
+Done
diff --git a/ext/standard/tests/array/array_flip_variation2.phpt b/ext/standard/tests/array/array_flip_variation2.phpt
new file mode 100644 (file)
index 0000000..bd82330
Binary files /dev/null and b/ext/standard/tests/array/array_flip_variation2.phpt differ
diff --git a/ext/standard/tests/array/array_flip_variation3.phpt b/ext/standard/tests/array/array_flip_variation3.phpt
new file mode 100644 (file)
index 0000000..376bdeb
Binary files /dev/null and b/ext/standard/tests/array/array_flip_variation3.phpt differ
diff --git a/ext/standard/tests/array/array_flip_variation4.phpt b/ext/standard/tests/array/array_flip_variation4.phpt
new file mode 100644 (file)
index 0000000..44d670f
--- /dev/null
@@ -0,0 +1,90 @@
+--TEST--
+Test array_flip() function : usage variations - 'input' argument with different invalid values for keys
+--FILE--
+<?php
+/* Prototype  : array array_flip(array $input)
+ * Description: Return array with key <-> value flipped 
+ * Source code: ext/standard/array.c
+*/
+
+/*
+* Trying different invalid values for 'input' array argument
+*/
+
+echo "*** Testing array_flip() : different invalid values in 'input' array argument ***\n";
+
+// class definition for object data
+class MyClass
+{
+   public function __toString()
+   {
+     return 'object';
+   }
+}
+$obj = new MyClass();
+
+// resource data
+$fp = fopen(__FILE__, 'r');
+
+$input = array(
+  // float values
+  'float_value1' => 1.2,
+  'float_value2' => 0.5,
+  'float_value3' => 3.4E3,
+  'float_value4' => 5.6E-6,
+
+  // bool values
+  'bool_value1' => true,
+  'bool_value2' => false,
+  'bool_value3' => TRUE,
+  'bool_value4' => FALSE,
+  
+  // null values
+  'null_value1' => null,
+
+  // array value
+  'array_value' => array(1),
+
+  // object value
+  'obj_value' => $obj,
+
+  // resource value
+  'resource_value' => $fp,
+);
+  
+var_dump( array_flip($input) );
+
+// closing resource
+fclose($fp);
+
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_flip() : different invalid values in 'input' array argument ***
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+
+Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
+array(0) {
+}
+Done
diff --git a/ext/standard/tests/array/array_flip_variation5.phpt b/ext/standard/tests/array/array_flip_variation5.phpt
new file mode 100644 (file)
index 0000000..29222a1
--- /dev/null
@@ -0,0 +1,82 @@
+--TEST--
+Test array_flip() function : usage variations - 'input' argument with repeatitive keys and values
+--FILE--
+<?php
+/* Prototype  : array array_flip(array $input)
+ * Description: Return array with key <-> value flipped 
+ * Source code: ext/standard/array.c
+*/
+
+/*
+* Using different types of repeatitive keys as well as values for 'input' array
+*/
+
+echo "*** Testing array_flip() : 'input' array with repeatitive keys/values ***\n";
+
+// array with numeric key repeatition
+$input = array(1 => 'value', 2 => 'VALUE', 1 => "VaLuE", 3.4 => 4, 3.4 => 5);
+var_dump( array_flip($input) );
+
+// array with string key repeatition
+$input = array("key" => 1, "two" => 'TWO', 'three' => 3, 'key' => "FOUR");
+var_dump( array_flip($input) );
+
+// array with bool key repeatition
+$input = array(true => 1, false => 0, TRUE => -1);
+var_dump( array_flip($input) );
+
+// array with null key repeatition
+$input = array(null => "Hello", NULL => 0);
+var_dump( array_flip($input) );
+
+// array with numeric value repeatition
+$input = array('one' => 1, 'two' => 2, 3 => 1, "index" => 1);
+var_dump( array_flip($input) );
+
+//array with string value repeatition
+$input = array('key1' => "value1", "key2" => '2', 'key3' => 'value1');
+var_dump( array_flip($input) );
+
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_flip() : 'input' array with repeatitive keys/values ***
+array(3) {
+  ["VaLuE"]=>
+  int(1)
+  ["VALUE"]=>
+  int(2)
+  [5]=>
+  int(3)
+}
+array(3) {
+  ["FOUR"]=>
+  string(3) "key"
+  ["TWO"]=>
+  string(3) "two"
+  [3]=>
+  string(5) "three"
+}
+array(2) {
+  [-1]=>
+  int(1)
+  [0]=>
+  int(0)
+}
+array(1) {
+  [0]=>
+  string(0) ""
+}
+array(2) {
+  [1]=>
+  string(5) "index"
+  [2]=>
+  string(3) "two"
+}
+array(2) {
+  ["value1"]=>
+  string(4) "key3"
+  [2]=>
+  string(4) "key2"
+}
+Done