PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/good/ext/standard/tests/strings/substr.php

http://github.com/facebook/hiphop-php
PHP | 78 lines | 41 code | 19 blank | 18 comment | 7 complexity | 228191b2da86b3bb88a7afb9ff9a82cc 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. /* Prototype: string substr( string str, int start[, int length] )
  3. * Description: Returns the portion of string specified by the start and length parameters.
  4. */
  5. <<__EntryPoint>> function main(): void {
  6. $strings_array = varray[ "", '12345', "abcdef", "123abc", "_123abc"];
  7. /* Testing for error conditions */
  8. echo "*** Testing for error conditions ***\n";
  9. /* Zero Argument */
  10. try { var_dump( substr() ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
  11. /* NULL as Argument */
  12. try { var_dump( substr(NULL) ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
  13. /* Single Argument */
  14. try { var_dump( substr("abcde") ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
  15. /* Scalar Argument */
  16. try { var_dump( substr(12345) ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
  17. /* more than valid number of arguments ( valid are 2 or 3 ) */
  18. try { var_dump( substr("abcde", 2, 3, 4) ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
  19. $counter = 1;
  20. foreach ($strings_array as $str) {
  21. /* variations with two arguments */
  22. /* start values >, < and = 0 */
  23. echo ("\n--- Iteration ".$counter." ---\n");
  24. echo ("\n-- Variations for two arguments --\n");
  25. var_dump ( substr($str, 1) );
  26. var_dump ( substr($str, 0) );
  27. var_dump ( substr($str, -2) );
  28. /* variations with three arguments */
  29. /* start value variations with length values */
  30. echo ("\n-- Variations for three arguments --\n");
  31. var_dump ( substr($str, 1, 3) );
  32. var_dump ( substr($str, 1, 0) );
  33. var_dump ( substr($str, 1, -3) );
  34. var_dump ( substr($str, 0, 3) );
  35. var_dump ( substr($str, 0, 0) );
  36. var_dump ( substr($str, 0, -3) );
  37. var_dump ( substr($str, -2, 3) );
  38. var_dump ( substr($str, -2, 0 ) );
  39. var_dump ( substr($str, -2, -3) );
  40. $counter++;
  41. }
  42. /* variation of start and length to point to same element */
  43. echo ("\n*** Testing for variations of start and length to point to same element ***\n");
  44. var_dump (substr("abcde" , 2, -2) );
  45. var_dump (substr("abcde" , -3, -2) );
  46. /* Testing to return empty string when start denotes the position beyond the truncation (set by negative length) */
  47. echo ("\n*** Testing for start > truncation ***\n");
  48. var_dump (substr("abcdef" , 4, -4) );
  49. /* String with null character */
  50. echo ("\n*** Testing for string with null characters ***\n");
  51. var_dump (substr("abc\x0xy\x0z" ,2) );
  52. /* String with international characters */
  53. echo ("\n*** Testing for string with international characters ***\n");
  54. var_dump (substr('\xIñtërnâtiônàlizætiøn',3) );
  55. /* start <0 && -start > length */
  56. echo "\n*** Start before the first char ***\n";
  57. var_dump (substr("abcd" , -8) );
  58. echo"\nDone";
  59. }