]> granicus.if.org Git - php/commitdiff
New str_replace() tests. All tested on Windows, Linux and Linux 64
authorandy wharmby <wharmby@php.net>
Mon, 17 Aug 2009 22:41:30 +0000 (22:41 +0000)
committerandy wharmby <wharmby@php.net>
Mon, 17 Aug 2009 22:41:30 +0000 (22:41 +0000)
ext/standard/tests/strings/str_replace_basic.phpt [new file with mode: 0644]
ext/standard/tests/strings/str_replace_error.phpt [new file with mode: 0644]
ext/standard/tests/strings/str_replace_variation1.phpt [new file with mode: 0644]
ext/standard/tests/strings/str_replace_variation2.phpt [new file with mode: 0644]
ext/standard/tests/strings/str_replace_variation3.phpt [new file with mode: 0644]

diff --git a/ext/standard/tests/strings/str_replace_basic.phpt b/ext/standard/tests/strings/str_replace_basic.phpt
new file mode 100644 (file)
index 0000000..10bf542
--- /dev/null
@@ -0,0 +1,49 @@
+--TEST--
+Test str_replace() function basic function
+--INI--
+precision=14
+--FILE--
+<?php
+/* 
+  Prototype: mixed str_replace(mixed $search, mixed $replace, 
+                               mixed $subject [, int &$count]);
+  Description: Replace all occurrences of the search string with 
+               the replacement string
+*/
+
+echo "\n*** Testing str_replace() on basic operations ***\n";
+
+var_dump( str_replace("", "", "") );
+
+var_dump( str_replace("e", "b", "test") );
+
+var_dump( str_replace("", "", "", $count) );
+var_dump( $count );
+
+var_dump( str_replace("q", "q", "q", $count) );
+var_dump( $count );
+
+var_dump( str_replace("long string here", "", "", $count) );
+var_dump( $count );
+
+$fp = fopen( __FILE__, "r" );
+$fp_copy = $fp; 
+var_dump( str_replace($fp_copy, $fp_copy, $fp_copy, $fp_copy) );
+var_dump( $fp_copy );
+fclose($fp);
+
+?>
+===DONE===
+--EXPECTF--    
+*** Testing str_replace() on basic operations ***
+string(0) ""
+string(4) "tbst"
+string(0) ""
+int(0)
+string(1) "q"
+int(1)
+string(0) ""
+int(0)
+string(%d) "Resource id #%d"
+int(1)
+===DONE===
diff --git a/ext/standard/tests/strings/str_replace_error.phpt b/ext/standard/tests/strings/str_replace_error.phpt
new file mode 100644 (file)
index 0000000..9fafc57
--- /dev/null
@@ -0,0 +1,41 @@
+--TEST--
+Test str_replace() function error conditions
+--INI--
+precision=14
+--FILE--
+<?php
+/* 
+  Prototype: mixed str_replace(mixed $search, mixed $replace, 
+                               mixed $subject [, int &$count]);
+  Description: Replace all occurrences of the search string with 
+               the replacement string
+*/
+
+
+echo "\n*** Testing str_replace error conditions ***";
+/* Invalid arguments */
+var_dump( str_replace() );
+var_dump( str_replace("") );
+var_dump( str_replace(NULL) );
+var_dump( str_replace(1, 2) );
+var_dump( str_replace(1,2,3,$var,5) );
+
+?>
+===DONE===
+--EXPECTF--    
+*** Testing str_replace error conditions ***
+Warning: str_replace() expects at least 3 parameters, 0 given in %sstr_replace_error.php on line 12
+NULL
+
+Warning: str_replace() expects at least 3 parameters, 1 given in %sstr_replace_error.php on line 13
+NULL
+
+Warning: str_replace() expects at least 3 parameters, 1 given in %sstr_replace_error.php on line 14
+NULL
+
+Warning: str_replace() expects at least 3 parameters, 2 given in %sstr_replace_error.php on line 15
+NULL
+
+Warning: str_replace() expects at most 4 parameters, 5 given in %sstr_replace_error.php on line 16
+NULL
+===DONE===
\ No newline at end of file
diff --git a/ext/standard/tests/strings/str_replace_variation1.phpt b/ext/standard/tests/strings/str_replace_variation1.phpt
new file mode 100644 (file)
index 0000000..279fb22
--- /dev/null
@@ -0,0 +1,392 @@
+--TEST--
+Test str_replace() function - test search values
+--INI--
+precision=14
+--FILE--
+<?php
+/* 
+  Prototype: mixed str_replace(mixed $search, mixed $replace, 
+                               mixed $subject [, int &$count]);
+  Description: Replace all occurrences of the search string with 
+               the replacement string
+*/
+
+
+echo "\n*** Testing str_replace() with various search values ***";
+$search_arr = array( TRUE, FALSE, 1, 0, -1, "1", "0", "-1",  NULL, 
+                     array(), "php", "");
+
+$i = 0;
+/* loop through to replace the matched elements in the array */
+foreach( $search_arr as $value ) {
+  echo "\n-- Iteration $i --\n";
+  /* replace the string in array */
+  var_dump( str_replace($value, "FOUND", $search_arr, $count) ); 
+  var_dump( $count );
+  $i++;
+}
+
+?>
+===DONE===
+--EXPECT--     
+*** Testing str_replace() with various search values ***
+-- Iteration 0 --
+array(12) {
+  [0]=>
+  string(5) "FOUND"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(5) "FOUND"
+  [3]=>
+  string(1) "0"
+  [4]=>
+  string(6) "-FOUND"
+  [5]=>
+  string(5) "FOUND"
+  [6]=>
+  string(1) "0"
+  [7]=>
+  string(6) "-FOUND"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(5)
+
+-- Iteration 1 --
+array(12) {
+  [0]=>
+  string(1) "1"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(1) "1"
+  [3]=>
+  string(1) "0"
+  [4]=>
+  string(2) "-1"
+  [5]=>
+  string(1) "1"
+  [6]=>
+  string(1) "0"
+  [7]=>
+  string(2) "-1"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(0)
+
+-- Iteration 2 --
+array(12) {
+  [0]=>
+  string(5) "FOUND"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(5) "FOUND"
+  [3]=>
+  string(1) "0"
+  [4]=>
+  string(6) "-FOUND"
+  [5]=>
+  string(5) "FOUND"
+  [6]=>
+  string(1) "0"
+  [7]=>
+  string(6) "-FOUND"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(5)
+
+-- Iteration 3 --
+array(12) {
+  [0]=>
+  string(1) "1"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(1) "1"
+  [3]=>
+  string(5) "FOUND"
+  [4]=>
+  string(2) "-1"
+  [5]=>
+  string(1) "1"
+  [6]=>
+  string(5) "FOUND"
+  [7]=>
+  string(2) "-1"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(2)
+
+-- Iteration 4 --
+array(12) {
+  [0]=>
+  string(1) "1"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(1) "1"
+  [3]=>
+  string(1) "0"
+  [4]=>
+  string(5) "FOUND"
+  [5]=>
+  string(1) "1"
+  [6]=>
+  string(1) "0"
+  [7]=>
+  string(5) "FOUND"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(2)
+
+-- Iteration 5 --
+array(12) {
+  [0]=>
+  string(5) "FOUND"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(5) "FOUND"
+  [3]=>
+  string(1) "0"
+  [4]=>
+  string(6) "-FOUND"
+  [5]=>
+  string(5) "FOUND"
+  [6]=>
+  string(1) "0"
+  [7]=>
+  string(6) "-FOUND"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(5)
+
+-- Iteration 6 --
+array(12) {
+  [0]=>
+  string(1) "1"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(1) "1"
+  [3]=>
+  string(5) "FOUND"
+  [4]=>
+  string(2) "-1"
+  [5]=>
+  string(1) "1"
+  [6]=>
+  string(5) "FOUND"
+  [7]=>
+  string(2) "-1"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(2)
+
+-- Iteration 7 --
+array(12) {
+  [0]=>
+  string(1) "1"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(1) "1"
+  [3]=>
+  string(1) "0"
+  [4]=>
+  string(5) "FOUND"
+  [5]=>
+  string(1) "1"
+  [6]=>
+  string(1) "0"
+  [7]=>
+  string(5) "FOUND"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(2)
+
+-- Iteration 8 --
+array(12) {
+  [0]=>
+  string(1) "1"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(1) "1"
+  [3]=>
+  string(1) "0"
+  [4]=>
+  string(2) "-1"
+  [5]=>
+  string(1) "1"
+  [6]=>
+  string(1) "0"
+  [7]=>
+  string(2) "-1"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(0)
+
+-- Iteration 9 --
+array(12) {
+  [0]=>
+  string(1) "1"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(1) "1"
+  [3]=>
+  string(1) "0"
+  [4]=>
+  string(2) "-1"
+  [5]=>
+  string(1) "1"
+  [6]=>
+  string(1) "0"
+  [7]=>
+  string(2) "-1"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(0)
+
+-- Iteration 10 --
+array(12) {
+  [0]=>
+  string(1) "1"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(1) "1"
+  [3]=>
+  string(1) "0"
+  [4]=>
+  string(2) "-1"
+  [5]=>
+  string(1) "1"
+  [6]=>
+  string(1) "0"
+  [7]=>
+  string(2) "-1"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(5) "FOUND"
+  [11]=>
+  string(0) ""
+}
+int(1)
+
+-- Iteration 11 --
+array(12) {
+  [0]=>
+  string(1) "1"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(1) "1"
+  [3]=>
+  string(1) "0"
+  [4]=>
+  string(2) "-1"
+  [5]=>
+  string(1) "1"
+  [6]=>
+  string(1) "0"
+  [7]=>
+  string(2) "-1"
+  [8]=>
+  string(0) ""
+  [9]=>
+  array(0) {
+  }
+  [10]=>
+  string(3) "php"
+  [11]=>
+  string(0) ""
+}
+int(0)
+===DONE===
diff --git a/ext/standard/tests/strings/str_replace_variation2.phpt b/ext/standard/tests/strings/str_replace_variation2.phpt
new file mode 100644 (file)
index 0000000..5534341
Binary files /dev/null and b/ext/standard/tests/strings/str_replace_variation2.phpt differ
diff --git a/ext/standard/tests/strings/str_replace_variation3.phpt b/ext/standard/tests/strings/str_replace_variation3.phpt
new file mode 100644 (file)
index 0000000..9b19153
--- /dev/null
@@ -0,0 +1,227 @@
+--TEST--
+Test str_replace() function
+--INI--
+precision=14
+--FILE--
+<?php
+/* 
+  Prototype: mixed str_replace(mixed $search, mixed $replace, 
+                               mixed $subject [, int &$count]);
+  Description: Replace all occurrences of the search string with 
+               the replacement string
+*/
+
+
+echo "\n*** Testing Miscelleneous input data ***\n";
+/*  If replace has fewer values than search, then an empty 
+    string is used for the rest of replacement values */
+var_dump( str_replace(array("a", "a", "b"), 
+                     array("q", "q"), 
+                     "aaabb", $count
+                    )
+       );
+var_dump($count);
+var_dump( str_replace(array("a", "a", "b"), 
+                      array("q", "q"), 
+                      array("aaa", "bbb", "ccc"), 
+                      $count
+                     )
+        );
+var_dump($count);
+
+
+echo "\n-- Testing objects --\n";
+/* we get "Catchable fatal error: saying Object of class could not be converted
+        to string" by default, when an object is passed instead of string:
+The error can be  avoided by chosing the __toString magix method as follows: */
+
+class subject 
+{
+  function __toString() {
+    return "Hello, world";
+  }
+}
+$obj_subject = new subject;
+
+class search 
+{
+  function __toString() {
+    return "Hello, world";
+  }
+}
+$obj_search = new search;
+
+class replace 
+{
+  function __toString() {
+    return "Hello, world";
+  }
+}
+$obj_replace = new replace;
+
+var_dump(str_replace("$obj_search", "$obj_replace", "$obj_subject", $count));
+var_dump($count);
+
+
+echo "\n-- Testing arrays --\n";
+var_dump(str_replace(array("a", "a", "b"), "multi", "aaa", $count));
+var_dump($count);
+
+var_dump(str_replace( array("a", "a", "b"),
+                      array("q", "q", "c"), 
+                      "aaa", $count
+                    )
+);
+var_dump($count);
+
+var_dump(str_replace( array("a", "a", "b"),
+                      array("q", "q", "c"), 
+                      array("aaa", "bbb"), 
+                      $count
+                    )
+);
+var_dump($count);
+
+var_dump(str_replace("a", array("q", "q", "c"), array("aaa"), $count));
+var_dump($count);
+
+var_dump(str_replace("a", 1, array("aaa", "bbb"), $count));
+var_dump($count);
+
+var_dump(str_replace(1, 3, array("aaa1", "2bbb"), $count));
+var_dump($count);
+
+
+echo "\n-- Testing Resources --\n";
+$resource1 = fopen( __FILE__, "r" );
+$resource2 = opendir( "." );
+var_dump(str_replace("stream", "FOUND", $resource1, $count)); 
+var_dump($count);
+var_dump(str_replace("stream", "FOUND", $resource2, $count));
+var_dump($count);
+
+
+echo "\n-- Testing a longer and heredoc string --\n";
+$string = <<<EOD
+abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
+abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
+abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
+abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
+abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
+abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
+abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
+@#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&*
+abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
+EOD;
+
+var_dump( str_replace("abcdef", "FOUND", $string, $count) );
+var_dump( $count );
+
+echo "\n-- Testing a heredoc null string --\n";
+$str = <<<EOD
+EOD;
+var_dump( str_replace("", "FOUND", $str, $count) );
+var_dump( $count );
+
+
+echo "\n-- Testing simple and complex syntax strings --\n";
+$str = 'world';
+
+/* Simple syntax */
+var_dump( str_replace("world", "FOUND", "$str") );
+var_dump( str_replace("world'S", "FOUND", "$str'S") );
+var_dump( str_replace("worldS", "FOUND", "$strS") );
+
+/* String with curly braces, complex syntax */
+var_dump( str_replace("worldS", "FOUND", "${str}S") );
+var_dump( str_replace("worldS", "FOUND", "{$str}S") );
+
+
+fclose($resource1);
+closedir($resource2);
+
+?>
+===DONE===
+--EXPECTF--    
+*** Testing Miscelleneous input data ***
+string(3) "qqq"
+int(5)
+array(3) {
+  [0]=>
+  string(3) "qqq"
+  [1]=>
+  string(0) ""
+  [2]=>
+  string(3) "ccc"
+}
+int(6)
+
+-- Testing objects --
+string(12) "Hello, world"
+int(1)
+
+-- Testing arrays --
+string(15) "multimultimulti"
+int(3)
+string(3) "qqq"
+int(3)
+array(2) {
+  [0]=>
+  string(3) "qqq"
+  [1]=>
+  string(3) "ccc"
+}
+int(6)
+
+Notice: Array to string conversion in %s on line %d
+array(1) {
+  [0]=>
+  string(15) "ArrayArrayArray"
+}
+int(3)
+array(2) {
+  [0]=>
+  string(3) "111"
+  [1]=>
+  string(3) "bbb"
+}
+int(3)
+array(2) {
+  [0]=>
+  string(4) "aaa3"
+  [1]=>
+  string(4) "2bbb"
+}
+int(1)
+
+-- Testing Resources --
+string(%d) "Resource id #%d"
+int(0)
+string(%d) "Resource id #%d"
+int(0)
+
+-- Testing a longer and heredoc string --
+string(623) "FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
+FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
+FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
+FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
+FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
+FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
+FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
+@#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&*
+FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789"
+int(16)
+
+-- Testing a heredoc null string --
+string(0) ""
+int(0)
+
+-- Testing simple and complex syntax strings --
+string(5) "FOUND"
+string(5) "FOUND"
+
+Notice: Undefined variable: strS in %s on line %d
+string(0) ""
+string(5) "FOUND"
+string(5) "FOUND"
+===DONE===