PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/bad/ext/standard/tests/strings/strcasecmp.php

http://github.com/facebook/hiphop-php
PHP | 143 lines | 100 code | 29 blank | 14 comment | 2 complexity | 75676e8cc1aaa295209d55b8b496b91c MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. /* Compares two strings in case-insensitive manner */
  3. echo "#### Basic and Possible operations ####";
  4. /* creating an array of strings to be compared */
  5. $arrays = array(
  6. array("a", 'A', chr(128), chr(255), chr(256)),
  7. array("acc", "Acc", 'aC', "acCc", 'acd', "?acc", 'Acc!', "$!acc", ";acc"),
  8. array("1", "0", 0, "-1", -1, NULL, null, "", TRUE, true, FALSE, "string"),
  9. array(10.5, 1.5, 9.5, 11.5, 100.5, 10.5E1, -10.5, 10, 0.5)
  10. );
  11. /* loop through to go each and every element in an array
  12. and comparing the elements with one and other */
  13. foreach($arrays as $str1_arr){
  14. echo "\n*** comparing the strings in an \n";
  15. print_r($str1_arr);
  16. for ($i=0; $i<count($str1_arr); $i++){
  17. echo "\nIteration $i\n";
  18. for($j=0; $j<count($str1_arr); $j++){
  19. echo "- strcasecmp of '$str1_arr[$i]' and '$str1_arr[$j]' is => ";
  20. var_dump(strcasecmp($str1_arr[$i], $str1_arr[$j]));
  21. }
  22. }
  23. }
  24. echo "\n#### Testing Miscelleneous inputs ####\n";
  25. echo "--- Testing objects ---\n";
  26. /* we get "Catchable fatal error: saying Object of class could not be converted
  27. to string" by default when an object is passed instead of string.
  28. The error can be avoided by choosing the __toString magix method as follows: */
  29. class string1 {
  30. function __toString() {
  31. return "Hello, world";
  32. }
  33. }
  34. $obj_string1 = new string1;
  35. class string2 {
  36. function __toString() {
  37. return "hello, world\0";
  38. }
  39. }
  40. $obj_string2 = new string2;
  41. var_dump(strcasecmp("$obj_string1", "$obj_string2"));
  42. echo "\n--- Testing arrays ---\n";
  43. $str_arr = array("hello", "?world", "!$%**()%**[][[[&@#~!");
  44. var_dump(strcasecmp("hello?world,!$%**()%**[][[[&@#~!", $str_arr));
  45. var_dump(strcasecmp("hello?world,!$%**()%**[][[[&@#~!", "$str_arr[1]"));
  46. var_dump(strcasecmp("hello?world,!$%**()%**[][[[&@#~!", "$str_arr[2]"));
  47. echo "\n--- Testing Resources ---\n";
  48. $filename1 = "dummy.txt";
  49. $filename2 = "dummy1.txt";
  50. $file1 = fopen($filename1, "w"); // creating new file
  51. $file2 = fopen($filename2, "w"); // creating new file
  52. /* getting resource type for file handle */
  53. $string1 = get_resource_type($file1);
  54. $string2 = get_resource_type($file2);
  55. $string3 = (int)get_resource_type($file2);
  56. /* string1 and string2 of same "stream" type */
  57. var_dump(strcasecmp($string1, $string2)); // int(0)
  58. /* string1 is of "stream" type & string3 is of "int" type */
  59. var_dump(strcasecmp($string1, $string3)); // int(1)
  60. fclose($file1); // closing the file "dummy.txt"
  61. fclose($file2); // closing the file "dummy1.txt"
  62. unlink("$filename1"); // deletes "dummy.txt"
  63. unlink("$filename2"); // deletes "dummy1.txt"
  64. echo "\n--- Testing a longer and heredoc string ---\n";
  65. $string = <<<EOD
  66. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  67. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  68. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  69. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  70. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  71. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  72. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  73. @#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&*
  74. abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
  75. EOD;
  76. var_dump(strcasecmp($string, $string));
  77. var_dump(strcasecmp($string, "xyz0123456789"));
  78. var_dump(strcasecmp($string, "&&&"));
  79. echo "\n--- Testing a heredoc null string ---\n";
  80. $str = <<<EOD
  81. EOD;
  82. var_dump(strcasecmp($str, "\0"));
  83. var_dump(strcasecmp($str, NULL));
  84. var_dump(strcasecmp($str, "0"));
  85. echo "\n--- Testing simple and complex syntax strings ---\n";
  86. $str = 'world';
  87. /* Simple syntax */
  88. var_dump(strcasecmp("Hello, world", "$str"));
  89. var_dump(strcasecmp("Hello, world'S", "$str'S"));
  90. var_dump(strcasecmp("Hello, worldS", "$strS"));
  91. /* String with curly braces, complex syntax */
  92. var_dump(strcasecmp("Hello, worldS", "${str}S"));
  93. var_dump(strcasecmp("Hello, worldS", "{$str}S"));
  94. echo "\n--- Testing binary safe and binary chars ---\n";
  95. var_dump(strcasecmp("Hello\0world", "Hello"));
  96. var_dump(strcasecmp("Hello\0world", "Helloworld"));
  97. var_dump(strcasecmp("\x0", "\0"));
  98. var_dump(strcasecmp("\000", "\0"));
  99. var_dump(strcasecmp("\x00", ""));
  100. var_dump(strcasecmp("\x00", NULL));
  101. var_dump(strcasecmp("\000", NULL));
  102. echo "\n--- Comparing long float values ---\n";
  103. /* Here two different outputs, which depends on the rounding value
  104. before converting to string. Here Precision = 12 */
  105. var_dump(strcasecmp(10.55555555555555555555555555, 10.5555555556)); // int(0)
  106. var_dump(strcasecmp(10.55555555555555555555555555, 10.555555556)); // int(-1)
  107. var_dump(strcasecmp(10.55555555595555555555555555, 10.555555556)); // int(0)
  108. echo "\n#### checking error conditions ####";
  109. strcasecmp();
  110. strcasecmp("");
  111. strcasecmp("HI");
  112. strcasecmp("Hi", "Hello", "World");
  113. echo "Done\n";
  114. ?>