PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/php_tests/strings/001.php

https://bitbucket.org/asuhan/happy/
PHP | 190 lines | 172 code | 18 blank | 0 comment | 62 complexity | 701b9e14c0dfc3a424af19b7a33d09ee MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. error_reporting(0);
  3. echo "Testing strtok: ";
  4. $str = "testing 1/2\\3";
  5. $tok1 = strtok($str, " ");
  6. $tok2 = strtok("/");
  7. $tok3 = strtok("\\");
  8. $tok4 = strtok(".");
  9. if ($tok1 != "testing") {
  10. echo("failed 1\n");
  11. } elseif ($tok2 != "1") {
  12. echo("failed 2\n");
  13. } elseif ($tok3 != "2") {
  14. echo("failed 3\n");
  15. } elseif ($tok4 != "3") {
  16. echo("failed 4\n");
  17. } else {
  18. echo("passed\n");
  19. }
  20. echo "Testing strstr: ";
  21. $test = "This is a test";
  22. $found1 = strstr($test, 32);
  23. $found2 = strstr($test, "a ");
  24. if ($found1 != " is a test") {
  25. echo("failed 1\n");
  26. } elseif ($found2 != "a test") {
  27. echo("failed 2\n");
  28. } else {
  29. echo("passed\n");
  30. }
  31. echo "Testing strrchr: ";
  32. $test = "fola fola blakken";
  33. $found1 = strrchr($test, "b");
  34. $found2 = strrchr($test, 102);
  35. if ($found1 != "blakken") {
  36. echo("failed 1\n");
  37. } elseif ($found2 != "fola blakken") {
  38. echo("failed 2\n");
  39. }
  40. else {
  41. echo("passed\n");
  42. }
  43. echo "Testing strtoupper: ";
  44. $test = "abCdEfg";
  45. $upper = strtoupper($test);
  46. if ($upper == "ABCDEFG") {
  47. echo("passed\n");
  48. } else {
  49. echo("failed!\n");
  50. }
  51. echo "Testing strtolower: ";
  52. $test = "ABcDeFG";
  53. $lower = strtolower($test);
  54. if ($lower == "abcdefg") {
  55. echo("passed\n");
  56. } else {
  57. echo("failed!\n");
  58. }
  59. echo "Testing substr: ";
  60. $tests = $ok = 0;
  61. $string = "string12345";
  62. $tests++; if (substr($string, 2, 10) == "ring12345") { $ok++; }
  63. $tests++; if (substr($string, 4, 7) == "ng12345") { $ok++; }
  64. $tests++; if (substr($string, 4) == "ng12345") { $ok++; }
  65. $tests++; if (substr($string, 10, 2) == "5") { $ok++; }
  66. $tests++; if (substr($string, 6, 0) == "") { $ok++; }
  67. $tests++; if (substr($string, -2, 2) == "45") { $ok++; }
  68. $tests++; if (substr($string, 1, -1) == "tring1234") { $ok++; }
  69. $tests++; if (substr($string, -1, -2) == "") { $ok++; }
  70. $tests++; if (substr($string, -3, -2) == "3") { $ok++; }
  71. if ($tests == $ok) {
  72. echo("passed\n");
  73. } else {
  74. echo("failed!\n");
  75. }
  76. $raw = ' !"#$%&\'()*+,-./0123456789:;<=>?'
  77. . '@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'
  78. . '`abcdefghijklmnopqrstuvwxyz{|}~'
  79. . "\0";
  80. echo "Testing rawurlencode: ";
  81. $encoded = rawurlencode($raw);
  82. $correct = '%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F'
  83. . '%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_'
  84. . '%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~'
  85. . '%00';
  86. if ($encoded == $correct) {
  87. echo("passed\n");
  88. } else {
  89. echo("failed!\n");
  90. }
  91. echo "Testing rawurldecode: ";
  92. $decoded = rawurldecode($correct);
  93. if ($decoded == $raw) {
  94. echo("passed\n");
  95. } else {
  96. echo("failed!\n");
  97. }
  98. echo "Testing urlencode: ";
  99. $encoded = urlencode($raw);
  100. $correct = '+%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F'
  101. . '%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_'
  102. . '%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E'
  103. . '%00';
  104. if ($encoded == $correct) {
  105. echo("passed\n");
  106. } else {
  107. echo("failed!\n");
  108. }
  109. echo "Testing urldecode: ";
  110. $decoded = urldecode($correct);
  111. if ($decoded == $raw) {
  112. echo("passed\n");
  113. } else {
  114. echo("failed!\n");
  115. }
  116. echo "Testing quotemeta: ";
  117. $raw = "a.\\+*?" . chr(91) . "^" . chr(93) . "b\$c";
  118. $quoted = quotemeta($raw);
  119. if ($quoted == "a\\.\\\\\\+\\*\\?\\[\\^\\]b\\\$c") {
  120. echo("passed\n");
  121. } else {
  122. echo("failed!\n");
  123. }
  124. echo "Testing ufirst: ";
  125. $str = "fahrvergnuegen";
  126. $uc = ucfirst($str);
  127. if ($uc == "Fahrvergnuegen") {
  128. echo("passed\n");
  129. } else {
  130. echo("failed!\n");
  131. }
  132. echo "Testing strtr: ";
  133. $str = "test abcdefgh";
  134. $tr = strtr($str, "def", "456");
  135. if ($tr == "t5st abc456gh") {
  136. echo("passed\n");
  137. } else {
  138. echo("failed!\n");
  139. }
  140. echo "Testing addslashes: ";
  141. $str = "\"\\'";
  142. $as = addslashes($str);
  143. if ($as == "\\\"\\\\\\'") {
  144. echo("passed\n");
  145. } else {
  146. echo("failed!\n");
  147. }
  148. echo "Testing stripslashes: ";
  149. $str = "\$\\'";
  150. $ss = stripslashes($str);
  151. if ($ss == "\$'") {
  152. echo("passed\n");
  153. } else {
  154. echo("failed!\n");
  155. }
  156. echo "Testing uniqid: ";
  157. $str = "prefix";
  158. $ui1 = uniqid($str);
  159. $ui2 = uniqid($str);
  160. $len = strncasecmp(PHP_OS, 'CYGWIN', 6) ? 19 : 29;
  161. if (strlen($ui1) == strlen($ui2) && strlen($ui1) == $len && $ui1 != $ui2) {
  162. echo("passed\n");
  163. } else {
  164. echo("failed!\n");
  165. }
  166. ?>