PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/zend/bad/ext/standard/tests/array/each.php

http://github.com/facebook/hiphop-php
PHP | 101 lines | 73 code | 17 blank | 11 comment | 3 complexity | 216cead69d1bc2576eba1e1dc155fc54 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. <?php
  2. /* Return the current key and value pair from an array
  3. and advance the array cursor */
  4. echo "*** Testing each() : basic functionality ***\n";
  5. $arrays = array (
  6. array(0),
  7. array(1),
  8. array(-1),
  9. array(1, 2, 3, 4, 5),
  10. array(-1, -2, -3, 6, 7, 8),
  11. array("a", "ab", "abc", "abcd"),
  12. array("1" => "one", "2" => "two", "3" => "three", "4" => "four"),
  13. array("one" => 1, "two" => 2, 3 => "three", 4 => 4, "" => 5,
  14. " " => 6, "\x00" => "\x000", "\0" => "\0", "" => "",
  15. TRUE => TRUE, FALSE => FALSE, NULL => NULL
  16. ),
  17. array("1.5" => "one.5", "-2.0" => "negative2.0"),
  18. array(-5 => "negative5", -.05 => "negative.05")
  19. );
  20. /* loop through to check working of each() on different arrays */
  21. $i = 0;
  22. while( list( $key, $sub_array) = each($arrays) ) {
  23. echo "-- Iteration $i --\n";
  24. $c = count ($sub_array);
  25. $c++; // increment by one to create the situation
  26. // of accessing beyond array size
  27. while ( $c ) {
  28. var_dump( each($sub_array) );
  29. $c --;
  30. }
  31. /* assignment of an array to another variable resets the internal
  32. pointer of the array. check this and ensure that each returns
  33. the first element after the assignment */
  34. $new_array = $sub_array;
  35. var_dump( each($sub_array) );
  36. echo "\n";
  37. $i++;
  38. }
  39. echo "\n*** Testing each() : possible variations ***\n";
  40. echo "-- Testing each() with reset() function --\n";
  41. /* reset the $arrays and use each to get the first element */
  42. var_dump( reset($arrays) );
  43. var_dump( each($arrays) ); // first element
  44. list($key, $sub_array) = each($arrays); // now second element
  45. var_dump( each($sub_array) );
  46. echo "-- Testing each() with resources --\n";
  47. $fp = fopen(__FILE__, "r");
  48. $dfp = opendir(".");
  49. $resources = array (
  50. "file" => $fp,
  51. "dir" => $dfp
  52. );
  53. for( $i = 0; $i < count($resources); $i++) {
  54. var_dump( each($resources) );
  55. }
  56. echo "-- Testing each with objects --\n";
  57. class each_class {
  58. private $var_private = 100;
  59. protected $var_protected = "string";
  60. public $var_public = array(0, 1, TRUE, NULL);
  61. }
  62. $each_obj = new each_class();
  63. for( $i = 0; $i <= 2; $i++ ) {
  64. var_dump( each($each_obj) );
  65. }
  66. echo "-- Testing each() with null array --\n";
  67. $null_array = array();
  68. var_dump( each($null_array) );
  69. echo "\n*** Testing error conditions ***\n";
  70. /* unexpected number of arguments */
  71. var_dump( each() ); // args = 0
  72. var_dump( each($null_array, $null_array) ); // args > expected
  73. /* unexpected argument type */
  74. $var=1;
  75. $str ="string";
  76. $fl = "15.5";
  77. var_dump( each($var) );
  78. var_dump( each($str) );
  79. var_dump( each($fl) );
  80. // close resourses used
  81. fclose($fp);
  82. closedir($dfp);
  83. echo "Done\n";
  84. ?>