str_repeat.phpt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. --TEST--
  2. Test str_repeat() function
  3. --INI--
  4. precision=14
  5. --FILE--
  6. <?php
  7. /* Prototype: string str_repeat ( string $input, int $multiplier );
  8. Description: Returns input repeated multiplier times. multiplier has to be
  9. greater than or equal to 0. If the multiplier is set to 0, the function
  10. will return an empty string.
  11. */
  12. echo "*** Testing str_repeat() with possible strings ***";
  13. $variations = array(
  14. 'a',
  15. 'foo',
  16. 'barbazbax',
  17. "\x00",
  18. '\0',
  19. NULL,
  20. TRUE,
  21. 4,
  22. 1.23,
  23. "",
  24. " "
  25. );
  26. /* variations in string and multiplier as an integer */
  27. foreach($variations as $input) {
  28. echo "\n--- str_repeat() of '$input' ---\n" ;
  29. for($n=0; $n<4; $n++) {
  30. echo "-- after repeating $n times is => ";
  31. echo str_repeat($input, $n)."\n";
  32. }
  33. }
  34. /* variations in multiplier as well as string to be repeated. Same varient
  35. values are used as string to be repeated as well as multiplier */
  36. echo "\n\n*** Testing str_repeat() with various strings & multiplier value ***";
  37. foreach ( $variations as $input ) {
  38. echo "\n--- str_repeat() of '$input' ---\n" ;
  39. foreach ( $variations as $multiplier ) {
  40. echo "-- after repeating '$multiplier' times is => ";
  41. var_dump( str_repeat($input, $multiplier) );
  42. }
  43. }
  44. echo "\n*** Testing str_repeat() with complex strings containing
  45. other than 7-bit chars ***\n";
  46. $str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255);
  47. var_dump(str_repeat($str, chr(51))); // ASCII value of '3' given
  48. var_dump(str_repeat($str, 3));
  49. echo "\n\n*** Testing error conditions ***";
  50. var_dump( str_repeat() ); // Zero args
  51. var_dump( str_repeat($input[0]) ); // args < expected
  52. var_dump( str_repeat($input[0], 3, 4) ); // args > expected
  53. var_dump( str_repeat($input[0], -1) ); // Invalid arg for multiplier
  54. echo "Done\n";
  55. ?>
  56. --EXPECTF--
  57. *** Testing str_repeat() with possible strings ***
  58. --- str_repeat() of 'a' ---
  59. -- after repeating 0 times is =>
  60. -- after repeating 1 times is => a
  61. -- after repeating 2 times is => aa
  62. -- after repeating 3 times is => aaa
  63. --- str_repeat() of 'foo' ---
  64. -- after repeating 0 times is =>
  65. -- after repeating 1 times is => foo
  66. -- after repeating 2 times is => foofoo
  67. -- after repeating 3 times is => foofoofoo
  68. --- str_repeat() of 'barbazbax' ---
  69. -- after repeating 0 times is =>
  70. -- after repeating 1 times is => barbazbax
  71. -- after repeating 2 times is => barbazbaxbarbazbax
  72. -- after repeating 3 times is => barbazbaxbarbazbaxbarbazbax
  73. --- str_repeat() of '�' ---
  74. -- after repeating 0 times is =>
  75. -- after repeating 1 times is => �
  76. -- after repeating 2 times is => ��
  77. -- after repeating 3 times is => ���
  78. --- str_repeat() of '\0' ---
  79. -- after repeating 0 times is =>
  80. -- after repeating 1 times is => \0
  81. -- after repeating 2 times is => \0\0
  82. -- after repeating 3 times is => \0\0\0
  83. --- str_repeat() of '' ---
  84. -- after repeating 0 times is =>
  85. -- after repeating 1 times is =>
  86. -- after repeating 2 times is =>
  87. -- after repeating 3 times is =>
  88. --- str_repeat() of '1' ---
  89. -- after repeating 0 times is =>
  90. -- after repeating 1 times is => 1
  91. -- after repeating 2 times is => 11
  92. -- after repeating 3 times is => 111
  93. --- str_repeat() of '4' ---
  94. -- after repeating 0 times is =>
  95. -- after repeating 1 times is => 4
  96. -- after repeating 2 times is => 44
  97. -- after repeating 3 times is => 444
  98. --- str_repeat() of '1.23' ---
  99. -- after repeating 0 times is =>
  100. -- after repeating 1 times is => 1.23
  101. -- after repeating 2 times is => 1.231.23
  102. -- after repeating 3 times is => 1.231.231.23
  103. --- str_repeat() of '' ---
  104. -- after repeating 0 times is =>
  105. -- after repeating 1 times is =>
  106. -- after repeating 2 times is =>
  107. -- after repeating 3 times is =>
  108. --- str_repeat() of ' ' ---
  109. -- after repeating 0 times is =>
  110. -- after repeating 1 times is =>
  111. -- after repeating 2 times is =>
  112. -- after repeating 3 times is =>
  113. *** Testing str_repeat() with various strings & multiplier value ***
  114. --- str_repeat() of 'a' ---
  115. -- after repeating 'a' times is =>
  116. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  117. NULL
  118. -- after repeating 'foo' times is =>
  119. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  120. NULL
  121. -- after repeating 'barbazbax' times is =>
  122. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  123. NULL
  124. -- after repeating '�' times is =>
  125. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  126. NULL
  127. -- after repeating '\0' times is =>
  128. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  129. NULL
  130. -- after repeating '' times is => string(0) ""
  131. -- after repeating '1' times is => string(1) "a"
  132. -- after repeating '4' times is => string(4) "aaaa"
  133. -- after repeating '1.23' times is => string(1) "a"
  134. -- after repeating '' times is =>
  135. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  136. NULL
  137. -- after repeating ' ' times is =>
  138. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  139. NULL
  140. --- str_repeat() of 'foo' ---
  141. -- after repeating 'a' times is =>
  142. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  143. NULL
  144. -- after repeating 'foo' times is =>
  145. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  146. NULL
  147. -- after repeating 'barbazbax' times is =>
  148. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  149. NULL
  150. -- after repeating '�' times is =>
  151. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  152. NULL
  153. -- after repeating '\0' times is =>
  154. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  155. NULL
  156. -- after repeating '' times is => string(0) ""
  157. -- after repeating '1' times is => string(3) "foo"
  158. -- after repeating '4' times is => string(12) "foofoofoofoo"
  159. -- after repeating '1.23' times is => string(3) "foo"
  160. -- after repeating '' times is =>
  161. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  162. NULL
  163. -- after repeating ' ' times is =>
  164. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  165. NULL
  166. --- str_repeat() of 'barbazbax' ---
  167. -- after repeating 'a' times is =>
  168. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  169. NULL
  170. -- after repeating 'foo' times is =>
  171. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  172. NULL
  173. -- after repeating 'barbazbax' times is =>
  174. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  175. NULL
  176. -- after repeating '�' times is =>
  177. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  178. NULL
  179. -- after repeating '\0' times is =>
  180. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  181. NULL
  182. -- after repeating '' times is => string(0) ""
  183. -- after repeating '1' times is => string(9) "barbazbax"
  184. -- after repeating '4' times is => string(36) "barbazbaxbarbazbaxbarbazbaxbarbazbax"
  185. -- after repeating '1.23' times is => string(9) "barbazbax"
  186. -- after repeating '' times is =>
  187. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  188. NULL
  189. -- after repeating ' ' times is =>
  190. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  191. NULL
  192. --- str_repeat() of '�' ---
  193. -- after repeating 'a' times is =>
  194. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  195. NULL
  196. -- after repeating 'foo' times is =>
  197. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  198. NULL
  199. -- after repeating 'barbazbax' times is =>
  200. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  201. NULL
  202. -- after repeating '�' times is =>
  203. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  204. NULL
  205. -- after repeating '\0' times is =>
  206. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  207. NULL
  208. -- after repeating '' times is => string(0) ""
  209. -- after repeating '1' times is => string(1) "�"
  210. -- after repeating '4' times is => string(4) "����"
  211. -- after repeating '1.23' times is => string(1) "�"
  212. -- after repeating '' times is =>
  213. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  214. NULL
  215. -- after repeating ' ' times is =>
  216. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  217. NULL
  218. --- str_repeat() of '\0' ---
  219. -- after repeating 'a' times is =>
  220. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  221. NULL
  222. -- after repeating 'foo' times is =>
  223. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  224. NULL
  225. -- after repeating 'barbazbax' times is =>
  226. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  227. NULL
  228. -- after repeating '�' times is =>
  229. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  230. NULL
  231. -- after repeating '\0' times is =>
  232. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  233. NULL
  234. -- after repeating '' times is => string(0) ""
  235. -- after repeating '1' times is => string(2) "\0"
  236. -- after repeating '4' times is => string(8) "\0\0\0\0"
  237. -- after repeating '1.23' times is => string(2) "\0"
  238. -- after repeating '' times is =>
  239. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  240. NULL
  241. -- after repeating ' ' times is =>
  242. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  243. NULL
  244. --- str_repeat() of '' ---
  245. -- after repeating 'a' times is =>
  246. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  247. NULL
  248. -- after repeating 'foo' times is =>
  249. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  250. NULL
  251. -- after repeating 'barbazbax' times is =>
  252. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  253. NULL
  254. -- after repeating '�' times is =>
  255. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  256. NULL
  257. -- after repeating '\0' times is =>
  258. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  259. NULL
  260. -- after repeating '' times is => string(0) ""
  261. -- after repeating '1' times is => string(0) ""
  262. -- after repeating '4' times is => string(0) ""
  263. -- after repeating '1.23' times is => string(0) ""
  264. -- after repeating '' times is =>
  265. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  266. NULL
  267. -- after repeating ' ' times is =>
  268. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  269. NULL
  270. --- str_repeat() of '1' ---
  271. -- after repeating 'a' times is =>
  272. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  273. NULL
  274. -- after repeating 'foo' times is =>
  275. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  276. NULL
  277. -- after repeating 'barbazbax' times is =>
  278. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  279. NULL
  280. -- after repeating '�' times is =>
  281. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  282. NULL
  283. -- after repeating '\0' times is =>
  284. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  285. NULL
  286. -- after repeating '' times is => string(0) ""
  287. -- after repeating '1' times is => string(1) "1"
  288. -- after repeating '4' times is => string(4) "1111"
  289. -- after repeating '1.23' times is => string(1) "1"
  290. -- after repeating '' times is =>
  291. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  292. NULL
  293. -- after repeating ' ' times is =>
  294. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  295. NULL
  296. --- str_repeat() of '4' ---
  297. -- after repeating 'a' times is =>
  298. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  299. NULL
  300. -- after repeating 'foo' times is =>
  301. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  302. NULL
  303. -- after repeating 'barbazbax' times is =>
  304. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  305. NULL
  306. -- after repeating '�' times is =>
  307. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  308. NULL
  309. -- after repeating '\0' times is =>
  310. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  311. NULL
  312. -- after repeating '' times is => string(0) ""
  313. -- after repeating '1' times is => string(1) "4"
  314. -- after repeating '4' times is => string(4) "4444"
  315. -- after repeating '1.23' times is => string(1) "4"
  316. -- after repeating '' times is =>
  317. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  318. NULL
  319. -- after repeating ' ' times is =>
  320. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  321. NULL
  322. --- str_repeat() of '1.23' ---
  323. -- after repeating 'a' times is =>
  324. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  325. NULL
  326. -- after repeating 'foo' times is =>
  327. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  328. NULL
  329. -- after repeating 'barbazbax' times is =>
  330. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  331. NULL
  332. -- after repeating '�' times is =>
  333. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  334. NULL
  335. -- after repeating '\0' times is =>
  336. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  337. NULL
  338. -- after repeating '' times is => string(0) ""
  339. -- after repeating '1' times is => string(4) "1.23"
  340. -- after repeating '4' times is => string(16) "1.231.231.231.23"
  341. -- after repeating '1.23' times is => string(4) "1.23"
  342. -- after repeating '' times is =>
  343. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  344. NULL
  345. -- after repeating ' ' times is =>
  346. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  347. NULL
  348. --- str_repeat() of '' ---
  349. -- after repeating 'a' times is =>
  350. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  351. NULL
  352. -- after repeating 'foo' times is =>
  353. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  354. NULL
  355. -- after repeating 'barbazbax' times is =>
  356. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  357. NULL
  358. -- after repeating '�' times is =>
  359. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  360. NULL
  361. -- after repeating '\0' times is =>
  362. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  363. NULL
  364. -- after repeating '' times is => string(0) ""
  365. -- after repeating '1' times is => string(0) ""
  366. -- after repeating '4' times is => string(0) ""
  367. -- after repeating '1.23' times is => string(0) ""
  368. -- after repeating '' times is =>
  369. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  370. NULL
  371. -- after repeating ' ' times is =>
  372. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  373. NULL
  374. --- str_repeat() of ' ' ---
  375. -- after repeating 'a' times is =>
  376. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  377. NULL
  378. -- after repeating 'foo' times is =>
  379. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  380. NULL
  381. -- after repeating 'barbazbax' times is =>
  382. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  383. NULL
  384. -- after repeating '�' times is =>
  385. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  386. NULL
  387. -- after repeating '\0' times is =>
  388. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  389. NULL
  390. -- after repeating '' times is => string(0) ""
  391. -- after repeating '1' times is => string(1) " "
  392. -- after repeating '4' times is => string(4) " "
  393. -- after repeating '1.23' times is => string(1) " "
  394. -- after repeating '' times is =>
  395. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  396. NULL
  397. -- after repeating ' ' times is =>
  398. Warning: str_repeat() expects parameter 2 to be long, string given in %s on line %d
  399. NULL
  400. *** Testing str_repeat() with complex strings containing
  401. other than 7-bit chars ***
  402. string(21) "�€�êëþÿ�€�êëþÿ�€�êëþÿ"
  403. string(21) "�€�êëþÿ�€�êëþÿ�€�êëþÿ"
  404. *** Testing error conditions ***
  405. Warning: str_repeat() expects exactly 2 parameters, 0 given in %s on line %d
  406. NULL
  407. Warning: str_repeat() expects exactly 2 parameters, 1 given in %s on line %d
  408. NULL
  409. Warning: str_repeat() expects exactly 2 parameters, 3 given in %s on line %d
  410. NULL
  411. Warning: str_repeat(): Second argument has to be greater than or equal to 0 in %s on line %d
  412. NULL
  413. Done