str_repeat.phpt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. --TEST--
  2. Test str_repeat() function
  3. --INI--
  4. precision=14
  5. --FILE--
  6. <?php
  7. echo "*** Testing str_repeat() with possible strings ***";
  8. $variations = array(
  9. 'a',
  10. 'foo',
  11. 'barbazbax',
  12. "\x00",
  13. '\0',
  14. TRUE,
  15. 4,
  16. 1.23,
  17. "",
  18. " "
  19. );
  20. /* variations in string and multiplier as an int */
  21. foreach($variations as $input) {
  22. echo "\n--- str_repeat() of '$input' ---\n" ;
  23. for($n=0; $n<4; $n++) {
  24. echo "-- after repeating $n times is => ";
  25. echo str_repeat($input, $n)."\n";
  26. }
  27. }
  28. echo "\n\n*** Testing error conditions ***\n";
  29. try {
  30. str_repeat($input[0], -1); // Invalid arg for multiplier
  31. } catch (\ValueError $e) {
  32. echo $e->getMessage() . "\n";
  33. }
  34. ?>
  35. --EXPECTF--
  36. *** Testing str_repeat() with possible strings ***
  37. --- str_repeat() of 'a' ---
  38. -- after repeating 0 times is =>
  39. -- after repeating 1 times is => a
  40. -- after repeating 2 times is => aa
  41. -- after repeating 3 times is => aaa
  42. --- str_repeat() of 'foo' ---
  43. -- after repeating 0 times is =>
  44. -- after repeating 1 times is => foo
  45. -- after repeating 2 times is => foofoo
  46. -- after repeating 3 times is => foofoofoo
  47. --- str_repeat() of 'barbazbax' ---
  48. -- after repeating 0 times is =>
  49. -- after repeating 1 times is => barbazbax
  50. -- after repeating 2 times is => barbazbaxbarbazbax
  51. -- after repeating 3 times is => barbazbaxbarbazbaxbarbazbax
  52. --- str_repeat() of '%0' ---
  53. -- after repeating 0 times is =>
  54. -- after repeating 1 times is => %0
  55. -- after repeating 2 times is => %0%0
  56. -- after repeating 3 times is => %0%0%0
  57. --- str_repeat() of '\0' ---
  58. -- after repeating 0 times is =>
  59. -- after repeating 1 times is => \0
  60. -- after repeating 2 times is => \0\0
  61. -- after repeating 3 times is => \0\0\0
  62. --- str_repeat() of '1' ---
  63. -- after repeating 0 times is =>
  64. -- after repeating 1 times is => 1
  65. -- after repeating 2 times is => 11
  66. -- after repeating 3 times is => 111
  67. --- str_repeat() of '4' ---
  68. -- after repeating 0 times is =>
  69. -- after repeating 1 times is => 4
  70. -- after repeating 2 times is => 44
  71. -- after repeating 3 times is => 444
  72. --- str_repeat() of '1.23' ---
  73. -- after repeating 0 times is =>
  74. -- after repeating 1 times is => 1.23
  75. -- after repeating 2 times is => 1.231.23
  76. -- after repeating 3 times is => 1.231.231.23
  77. --- str_repeat() of '' ---
  78. -- after repeating 0 times is =>
  79. -- after repeating 1 times is =>
  80. -- after repeating 2 times is =>
  81. -- after repeating 3 times is =>
  82. --- str_repeat() of ' ' ---
  83. -- after repeating 0 times is =>
  84. -- after repeating 1 times is =>
  85. -- after repeating 2 times is =>
  86. -- after repeating 3 times is =>
  87. *** Testing error conditions ***
  88. str_repeat(): Argument #2 ($times) must be greater than or equal to 0