PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/good/tests/strings/001.php

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