009.phpt 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. --TEST--
  2. Test key(), current(), next() & reset() functions
  3. --FILE--
  4. <?php
  5. /* Prototype & Usage:
  6. mixed key ( array &$array ) -> returns the index element of the current array position
  7. mixed current ( array &$array ) -> returns the current element in the array
  8. mixed next ( array &$array ) -> similar to current() but advances the internal pointer to next element
  9. mixed reset ( array &$array ) -> Reset the internal pointer to first element
  10. */
  11. $basic_arrays = array (
  12. array(0), // array with element as 0
  13. array(1), // array with single element
  14. array(1,2, 3, -1, -2, -3), // array of integers
  15. array(1.1, 2.2, 3.3, -1.1, -2.2, -3.3), // array of floats
  16. array('a', 'b', 'c', "ab", "ac", "ad"), // string array
  17. array("a" => "apple", "b" => "book", "c" => "cook"), // associative array
  18. array('d' => 'drink', 'p' => 'port', 's' => 'set'), // another associative array
  19. array(1 => 'One', 2 => 'two', 3 => "three") // associative array with key as integers
  20. );
  21. $varient_arrays = array (
  22. array(), // empty array
  23. array(""), // array with null string
  24. array(NULL),// array with NULL
  25. array(null),// array with null
  26. array(NULL, true, null, "", 1), // mixed array
  27. array(-1.5 => "test", -2 => "rest", 2.5 => "two",
  28. "" => "string", 0 => "zero", "" => "" ) // mixed array
  29. );
  30. echo "*** Testing basic operations ***\n";
  31. $loop_count = 1;
  32. foreach ($basic_arrays as $sub_array ) {
  33. echo "-- Iteration $loop_count --\n";
  34. $loop_count++;
  35. $c = count ($sub_array);
  36. $c++; // increment by one to create the situation of accessing beyond array size
  37. while ( $c ) {
  38. var_dump( current($sub_array)); // current element
  39. var_dump( key($sub_array) ); // key of the current element
  40. var_dump( next($sub_array) ); // move to next element
  41. $c --;
  42. }
  43. var_dump( reset($sub_array) ); // reset the internal pointer to first element
  44. var_dump( key($sub_array) ); // access the array after reset
  45. var_dump( $sub_array ); // dump the array to see that its intact
  46. echo "\n";
  47. }
  48. echo "\n*** Testing possible variations ***\n";
  49. $loop_count = 1;
  50. foreach ($varient_arrays as $sub_array ) {
  51. echo "-- Iteration $loop_count --\n";
  52. $loop_count++;
  53. $c = count ($sub_array);
  54. $c++; // increment by one to create the situation of accessing beyond array size
  55. while ( $c ) {
  56. var_dump( current($sub_array)); // current element
  57. var_dump( key($sub_array) ); // key of the current element
  58. var_dump( next($sub_array) ); // move to next element
  59. $c --;
  60. }
  61. var_dump( reset($sub_array) ); // reset the internal pointer to first element
  62. var_dump( key($sub_array) ); // access the array after reset
  63. var_dump( $sub_array ); // dump the array to see that its intact
  64. echo "\n";
  65. }
  66. /*test these functions on array which is already unset */
  67. echo "\n-- Testing variation: when array is unset --\n";
  68. $unset_array = array (1);
  69. unset($unset_array);
  70. var_dump( current($unset_array) );
  71. var_dump( key($unset_array) );
  72. var_dump( next($unset_array) );
  73. var_dump( reset($unset_array) );
  74. echo "\n*** Testing error conditions ***\n";
  75. //Zero argument, expected 1 argument
  76. var_dump( key() );
  77. var_dump( current() );
  78. var_dump( reset() );
  79. var_dump( next() );
  80. // args more than expected, expected 1 argument
  81. $temp_array = array(1);
  82. var_dump( key($temp_array, $temp_array) );
  83. var_dump( current($temp_array, $temp_array) );
  84. var_dump( reset($temp_array, $temp_array) );
  85. var_dump( next($temp_array, $temp_array) );
  86. // invalid args type, valid argument: array
  87. $int_var = 1;
  88. $float_var = 1.5;
  89. $string = "string";
  90. var_dump( key($int_var) );
  91. var_dump( key($float_var) );
  92. var_dump( key($string) );
  93. var_dump( current($int_var) );
  94. var_dump( current($float_var) );
  95. var_dump( current($string) );
  96. var_dump( next($int_var) );
  97. var_dump( next($float_var) );
  98. var_dump( next($string) );
  99. var_dump( reset($int_var) );
  100. var_dump( reset($float_var) );
  101. var_dump( reset($string) );
  102. echo "Done\n";
  103. ?>
  104. --EXPECTF--
  105. *** Testing basic operations ***
  106. -- Iteration 1 --
  107. int(0)
  108. int(0)
  109. bool(false)
  110. bool(false)
  111. NULL
  112. bool(false)
  113. int(0)
  114. int(0)
  115. array(1) {
  116. [0]=>
  117. int(0)
  118. }
  119. -- Iteration 2 --
  120. int(1)
  121. int(0)
  122. bool(false)
  123. bool(false)
  124. NULL
  125. bool(false)
  126. int(1)
  127. int(0)
  128. array(1) {
  129. [0]=>
  130. int(1)
  131. }
  132. -- Iteration 3 --
  133. int(1)
  134. int(0)
  135. int(2)
  136. int(2)
  137. int(1)
  138. int(3)
  139. int(3)
  140. int(2)
  141. int(-1)
  142. int(-1)
  143. int(3)
  144. int(-2)
  145. int(-2)
  146. int(4)
  147. int(-3)
  148. int(-3)
  149. int(5)
  150. bool(false)
  151. bool(false)
  152. NULL
  153. bool(false)
  154. int(1)
  155. int(0)
  156. array(6) {
  157. [0]=>
  158. int(1)
  159. [1]=>
  160. int(2)
  161. [2]=>
  162. int(3)
  163. [3]=>
  164. int(-1)
  165. [4]=>
  166. int(-2)
  167. [5]=>
  168. int(-3)
  169. }
  170. -- Iteration 4 --
  171. float(1.1)
  172. int(0)
  173. float(2.2)
  174. float(2.2)
  175. int(1)
  176. float(3.3)
  177. float(3.3)
  178. int(2)
  179. float(-1.1)
  180. float(-1.1)
  181. int(3)
  182. float(-2.2)
  183. float(-2.2)
  184. int(4)
  185. float(-3.3)
  186. float(-3.3)
  187. int(5)
  188. bool(false)
  189. bool(false)
  190. NULL
  191. bool(false)
  192. float(1.1)
  193. int(0)
  194. array(6) {
  195. [0]=>
  196. float(1.1)
  197. [1]=>
  198. float(2.2)
  199. [2]=>
  200. float(3.3)
  201. [3]=>
  202. float(-1.1)
  203. [4]=>
  204. float(-2.2)
  205. [5]=>
  206. float(-3.3)
  207. }
  208. -- Iteration 5 --
  209. string(1) "a"
  210. int(0)
  211. string(1) "b"
  212. string(1) "b"
  213. int(1)
  214. string(1) "c"
  215. string(1) "c"
  216. int(2)
  217. string(2) "ab"
  218. string(2) "ab"
  219. int(3)
  220. string(2) "ac"
  221. string(2) "ac"
  222. int(4)
  223. string(2) "ad"
  224. string(2) "ad"
  225. int(5)
  226. bool(false)
  227. bool(false)
  228. NULL
  229. bool(false)
  230. string(1) "a"
  231. int(0)
  232. array(6) {
  233. [0]=>
  234. string(1) "a"
  235. [1]=>
  236. string(1) "b"
  237. [2]=>
  238. string(1) "c"
  239. [3]=>
  240. string(2) "ab"
  241. [4]=>
  242. string(2) "ac"
  243. [5]=>
  244. string(2) "ad"
  245. }
  246. -- Iteration 6 --
  247. string(5) "apple"
  248. string(1) "a"
  249. string(4) "book"
  250. string(4) "book"
  251. string(1) "b"
  252. string(4) "cook"
  253. string(4) "cook"
  254. string(1) "c"
  255. bool(false)
  256. bool(false)
  257. NULL
  258. bool(false)
  259. string(5) "apple"
  260. string(1) "a"
  261. array(3) {
  262. ["a"]=>
  263. string(5) "apple"
  264. ["b"]=>
  265. string(4) "book"
  266. ["c"]=>
  267. string(4) "cook"
  268. }
  269. -- Iteration 7 --
  270. string(5) "drink"
  271. string(1) "d"
  272. string(4) "port"
  273. string(4) "port"
  274. string(1) "p"
  275. string(3) "set"
  276. string(3) "set"
  277. string(1) "s"
  278. bool(false)
  279. bool(false)
  280. NULL
  281. bool(false)
  282. string(5) "drink"
  283. string(1) "d"
  284. array(3) {
  285. ["d"]=>
  286. string(5) "drink"
  287. ["p"]=>
  288. string(4) "port"
  289. ["s"]=>
  290. string(3) "set"
  291. }
  292. -- Iteration 8 --
  293. string(3) "One"
  294. int(1)
  295. string(3) "two"
  296. string(3) "two"
  297. int(2)
  298. string(5) "three"
  299. string(5) "three"
  300. int(3)
  301. bool(false)
  302. bool(false)
  303. NULL
  304. bool(false)
  305. string(3) "One"
  306. int(1)
  307. array(3) {
  308. [1]=>
  309. string(3) "One"
  310. [2]=>
  311. string(3) "two"
  312. [3]=>
  313. string(5) "three"
  314. }
  315. *** Testing possible variations ***
  316. -- Iteration 1 --
  317. bool(false)
  318. NULL
  319. bool(false)
  320. bool(false)
  321. NULL
  322. array(0) {
  323. }
  324. -- Iteration 2 --
  325. string(0) ""
  326. int(0)
  327. bool(false)
  328. bool(false)
  329. NULL
  330. bool(false)
  331. string(0) ""
  332. int(0)
  333. array(1) {
  334. [0]=>
  335. string(0) ""
  336. }
  337. -- Iteration 3 --
  338. NULL
  339. int(0)
  340. bool(false)
  341. bool(false)
  342. NULL
  343. bool(false)
  344. NULL
  345. int(0)
  346. array(1) {
  347. [0]=>
  348. NULL
  349. }
  350. -- Iteration 4 --
  351. NULL
  352. int(0)
  353. bool(false)
  354. bool(false)
  355. NULL
  356. bool(false)
  357. NULL
  358. int(0)
  359. array(1) {
  360. [0]=>
  361. NULL
  362. }
  363. -- Iteration 5 --
  364. NULL
  365. int(0)
  366. bool(true)
  367. bool(true)
  368. int(1)
  369. NULL
  370. NULL
  371. int(2)
  372. string(0) ""
  373. string(0) ""
  374. int(3)
  375. int(1)
  376. int(1)
  377. int(4)
  378. bool(false)
  379. bool(false)
  380. NULL
  381. bool(false)
  382. NULL
  383. int(0)
  384. array(5) {
  385. [0]=>
  386. NULL
  387. [1]=>
  388. bool(true)
  389. [2]=>
  390. NULL
  391. [3]=>
  392. string(0) ""
  393. [4]=>
  394. int(1)
  395. }
  396. -- Iteration 6 --
  397. string(4) "test"
  398. int(-1)
  399. string(4) "rest"
  400. string(4) "rest"
  401. int(-2)
  402. string(3) "two"
  403. string(3) "two"
  404. int(2)
  405. string(0) ""
  406. string(0) ""
  407. string(0) ""
  408. string(4) "zero"
  409. string(4) "zero"
  410. int(0)
  411. bool(false)
  412. bool(false)
  413. NULL
  414. bool(false)
  415. string(4) "test"
  416. int(-1)
  417. array(5) {
  418. [-1]=>
  419. string(4) "test"
  420. [-2]=>
  421. string(4) "rest"
  422. [2]=>
  423. string(3) "two"
  424. [""]=>
  425. string(0) ""
  426. [0]=>
  427. string(4) "zero"
  428. }
  429. -- Testing variation: when array is unset --
  430. Warning: current() expects parameter 1 to be array, null given in %s on line %d
  431. NULL
  432. Warning: key() expects parameter 1 to be array, null given in %s on line %d
  433. NULL
  434. Warning: next() expects parameter 1 to be array, null given in %s on line %d
  435. NULL
  436. Warning: reset() expects parameter 1 to be array, null given in %s on line %d
  437. NULL
  438. *** Testing error conditions ***
  439. Warning: key() expects exactly 1 parameter, 0 given in %s on line %d
  440. NULL
  441. Warning: current() expects exactly 1 parameter, 0 given in %s on line %d
  442. NULL
  443. Warning: reset() expects exactly 1 parameter, 0 given in %s on line %d
  444. NULL
  445. Warning: next() expects exactly 1 parameter, 0 given in %s on line %d
  446. NULL
  447. Warning: key() expects exactly 1 parameter, 2 given in %s on line %d
  448. NULL
  449. Warning: current() expects exactly 1 parameter, 2 given in %s on line %d
  450. NULL
  451. Warning: reset() expects exactly 1 parameter, 2 given in %s on line %d
  452. NULL
  453. Warning: next() expects exactly 1 parameter, 2 given in %s on line %d
  454. NULL
  455. Warning: key() expects parameter 1 to be array, integer given in %s on line %d
  456. NULL
  457. Warning: key() expects parameter 1 to be array, double given in %s on line %d
  458. NULL
  459. Warning: key() expects parameter 1 to be array, string given in %s on line %d
  460. NULL
  461. Warning: current() expects parameter 1 to be array, integer given in %s on line %d
  462. NULL
  463. Warning: current() expects parameter 1 to be array, double given in %s on line %d
  464. NULL
  465. Warning: current() expects parameter 1 to be array, string given in %s on line %d
  466. NULL
  467. Warning: next() expects parameter 1 to be array, integer given in %s on line %d
  468. NULL
  469. Warning: next() expects parameter 1 to be array, double given in %s on line %d
  470. NULL
  471. Warning: next() expects parameter 1 to be array, string given in %s on line %d
  472. NULL
  473. Warning: reset() expects parameter 1 to be array, integer given in %s on line %d
  474. NULL
  475. Warning: reset() expects parameter 1 to be array, double given in %s on line %d
  476. NULL
  477. Warning: reset() expects parameter 1 to be array, string given in %s on line %d
  478. NULL
  479. Done