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

/hphp/test/zend/bad/ext/standard/tests/general_functions/intval.php

http://github.com/facebook/hiphop-php
PHP | 152 lines | 108 code | 25 blank | 19 comment | 0 complexity | 48f9c1e6f0a47bde43763501e65b7211 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. /* Prototype: int intval( mixed $var [.int $base] );
  3. * Description: Returns the integer value of var, using the specified base for the conversion(the default is base 10).
  4. */
  5. echo "*** Testing intval() with valid integer values ***\n";
  6. // different valid integer vlaues
  7. $valid_ints = array(
  8. '0',
  9. '1',
  10. '-1',
  11. '-2147483648', // max negative integer value
  12. '-2147483647',
  13. 2147483647, // max positive integer value
  14. 2147483640,
  15. 0x123B, // integer as hexadecimal
  16. '0x12ab',
  17. '0Xfff',
  18. '0XFA',
  19. -0x80000000, // max negative integer as hexadecimal
  20. '0x7fffffff', // max postive integer as hexadecimal
  21. 0x7FFFFFFF, // max postive integer as hexadecimal
  22. '0123', // integer as octal
  23. 01912, // should be quivalent to octal 1
  24. -020000000000, // max negative integer as octal
  25. 017777777777, // max positive integer as octal
  26. );
  27. /* loop to check that intval() recognizes different
  28. integer values, expected output:integer value in decimal notation for valid integer */
  29. echo "\n***Output with default base value ie 10 ***\n";
  30. foreach ($valid_ints as $value ) {
  31. var_dump( intval($value) );
  32. }
  33. echo "\n***Output with base value of 10( explicitly passed as argument) ***\n";
  34. foreach ($valid_ints as $value ) {
  35. var_dump( intval($value, 10) );
  36. }
  37. echo "\n***Output with base value of 16 ***\n";
  38. foreach ($valid_ints as $value ) {
  39. var_dump( intval($value, 16) );
  40. }
  41. echo "\n***Output with base value of 8 ***\n";
  42. foreach ($valid_ints as $value ) {
  43. var_dump( intval($value, 8) );
  44. }
  45. echo "\n*** Testing intval() on non integer types ***\n";
  46. // get a resource type variable
  47. $fp = fopen (__FILE__, "r");
  48. fclose($fp);
  49. $dfp = opendir ( dirname(__FILE__) );
  50. closedir($dfp);
  51. // unset variable
  52. $unset_var = 10;
  53. unset ($unset_var);
  54. // other types in a array
  55. $not_int_types = array (
  56. /* float values */
  57. '-2147483649', // float value
  58. '2147483648', // float value
  59. '-0x80000001', // float value, beyond max negative int
  60. '0x800000001', // float value, beyond max positive int
  61. '020000000001', // float value, beyond max positive int
  62. '-020000000001', // float value, beyond max negative int
  63. 0.0,
  64. -0.1,
  65. 1.0,
  66. 1e5,
  67. -1e6,
  68. 1E8,
  69. -1E9,
  70. 10.0000000000000000005,
  71. 10.5e+5,
  72. /* resources */
  73. $fp,
  74. $dfp,
  75. /* arrays */
  76. array(),
  77. array(0),
  78. array(1),
  79. array(NULL),
  80. array(null),
  81. array("string"),
  82. array(true),
  83. array(TRUE),
  84. array(false),
  85. array(FALSE),
  86. array(1,2,3,4),
  87. array(1 => "One", "two" => 2),
  88. /* strings */
  89. "",
  90. '',
  91. "0",
  92. '0',
  93. "1",
  94. '1',
  95. "\x01",
  96. '\x01',
  97. "\01",
  98. '\01',
  99. 'string',
  100. "string",
  101. "true",
  102. "FALSE",
  103. 'false',
  104. 'TRUE',
  105. "NULL",
  106. 'null',
  107. /* booleans */
  108. true,
  109. false,
  110. TRUE,
  111. FALSE,
  112. /* undefined and unset vars */
  113. @$unset_var,
  114. @$undefined_var
  115. );
  116. /* loop through the $not_int_types to see working of
  117. intval() on non integer types, expected output: integer value in decimal notation for valid integers */
  118. foreach ($not_int_types as $type ) {
  119. var_dump( intval($type) );
  120. }
  121. echo "\n*** Testing error conditions ***\n";
  122. //Zero argument
  123. var_dump( intval() );
  124. //arguments more than expected
  125. var_dump( intval(TRUE, FALSE, TRUE) );
  126. echo "\n--- Done ---\n";
  127. ?>