intval.phpt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. --TEST--
  2. Test intval() function
  3. --SKIPIF--
  4. <?php
  5. if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only");
  6. ?>
  7. --FILE--
  8. <?php
  9. /* Prototype: int intval( mixed $var [.int $base] );
  10. * Description: Returns the integer value of var, using the specified base for the conversion(the default is base 10).
  11. */
  12. echo "*** Testing intval() with valid integer values ***\n";
  13. // different valid integer vlaues
  14. $valid_ints = array(
  15. '0',
  16. '1',
  17. '-1',
  18. '-2147483648', // max negative integer value
  19. '-2147483647',
  20. 2147483647, // max positive integer value
  21. 2147483640,
  22. 0x123B, // integer as hexadecimal
  23. '0x12ab',
  24. '0Xfff',
  25. '0XFA',
  26. -0x80000000, // max negative integer as hexadecimal
  27. '0x7fffffff', // max postive integer as hexadecimal
  28. 0x7FFFFFFF, // max postive integer as hexadecimal
  29. '0123', // integer as octal
  30. 01912, // should be quivalent to octal 1
  31. -020000000000, // max negative integer as octal
  32. 017777777777, // max positive integer as octal
  33. );
  34. /* loop to check that intval() recognizes different
  35. integer values, expected output:integer value in decimal notation for valid integer */
  36. echo "\n***Output with default base value ie 10 ***\n";
  37. foreach ($valid_ints as $value ) {
  38. var_dump( intval($value) );
  39. }
  40. echo "\n***Output with base value of 10( explicitly passed as argument) ***\n";
  41. foreach ($valid_ints as $value ) {
  42. var_dump( intval($value, 10) );
  43. }
  44. echo "\n***Output with base value of 16 ***\n";
  45. foreach ($valid_ints as $value ) {
  46. var_dump( intval($value, 16) );
  47. }
  48. echo "\n***Output with base value of 8 ***\n";
  49. foreach ($valid_ints as $value ) {
  50. var_dump( intval($value, 8) );
  51. }
  52. echo "\n*** Testing intval() on non integer types ***\n";
  53. // get a resource type variable
  54. $fp = fopen (__FILE__, "r");
  55. fclose($fp);
  56. $dfp = opendir ( dirname(__FILE__) );
  57. closedir($dfp);
  58. // unset variable
  59. $unset_var = 10;
  60. unset ($unset_var);
  61. // other types in a array
  62. $not_int_types = array (
  63. /* float values */
  64. '-2147483649', // float value
  65. '2147483648', // float value
  66. '-0x80000001', // float value, beyond max negative int
  67. '0x800000001', // float value, beyond max positive int
  68. '020000000001', // float value, beyond max positive int
  69. '-020000000001', // float value, beyond max negative int
  70. 0.0,
  71. -0.1,
  72. 1.0,
  73. 1e5,
  74. -1e6,
  75. 1E8,
  76. -1E9,
  77. 10.0000000000000000005,
  78. 10.5e+5,
  79. /* resources */
  80. $fp,
  81. $dfp,
  82. /* arrays */
  83. array(),
  84. array(0),
  85. array(1),
  86. array(NULL),
  87. array(null),
  88. array("string"),
  89. array(true),
  90. array(TRUE),
  91. array(false),
  92. array(FALSE),
  93. array(1,2,3,4),
  94. array(1 => "One", "two" => 2),
  95. /* strings */
  96. "",
  97. '',
  98. "0",
  99. '0',
  100. "1",
  101. '1',
  102. "\x01",
  103. '\x01',
  104. "\01",
  105. '\01',
  106. 'string',
  107. "string",
  108. "true",
  109. "FALSE",
  110. 'false',
  111. 'TRUE',
  112. "NULL",
  113. 'null',
  114. /* booleans */
  115. true,
  116. false,
  117. TRUE,
  118. FALSE,
  119. /* undefined and unset vars */
  120. @$unset_var,
  121. @$undefined_var
  122. );
  123. /* loop through the $not_int_types to see working of
  124. intval() on non integer types, expected output: integer value in decimal notation for valid integers */
  125. foreach ($not_int_types as $type ) {
  126. var_dump( intval($type) );
  127. }
  128. echo "\n*** Testing error conditions ***\n";
  129. //Zero argument
  130. var_dump( intval() );
  131. //arguments more than expected
  132. var_dump( intval(TRUE, FALSE, TRUE) );
  133. echo "\n--- Done ---\n";
  134. ?>
  135. --EXPECTF--
  136. *** Testing intval() with valid integer values ***
  137. ***Output with default base value ie 10 ***
  138. int(0)
  139. int(1)
  140. int(-1)
  141. int(-2147483648)
  142. int(-2147483647)
  143. int(2147483647)
  144. int(2147483640)
  145. int(4667)
  146. int(0)
  147. int(0)
  148. int(0)
  149. int(-2147483648)
  150. int(0)
  151. int(2147483647)
  152. int(123)
  153. int(1)
  154. int(-2147483648)
  155. int(2147483647)
  156. ***Output with base value of 10( explicitly passed as argument) ***
  157. int(0)
  158. int(1)
  159. int(-1)
  160. int(-2147483648)
  161. int(-2147483647)
  162. int(2147483647)
  163. int(2147483640)
  164. int(4667)
  165. int(0)
  166. int(0)
  167. int(0)
  168. int(-2147483648)
  169. int(0)
  170. int(2147483647)
  171. int(123)
  172. int(1)
  173. int(-2147483648)
  174. int(2147483647)
  175. ***Output with base value of 16 ***
  176. int(0)
  177. int(1)
  178. int(-1)
  179. int(-2147483648)
  180. int(-2147483648)
  181. int(2147483647)
  182. int(2147483640)
  183. int(4667)
  184. int(4779)
  185. int(4095)
  186. int(250)
  187. int(-2147483648)
  188. int(2147483647)
  189. int(2147483647)
  190. int(291)
  191. int(1)
  192. int(-2147483648)
  193. int(2147483647)
  194. ***Output with base value of 8 ***
  195. int(0)
  196. int(1)
  197. int(-1)
  198. int(-9020)
  199. int(-9020)
  200. int(2147483647)
  201. int(2147483640)
  202. int(4667)
  203. int(0)
  204. int(0)
  205. int(0)
  206. int(-2147483648)
  207. int(0)
  208. int(2147483647)
  209. int(83)
  210. int(1)
  211. int(-2147483648)
  212. int(2147483647)
  213. *** Testing intval() on non integer types ***
  214. int(-2147483648)
  215. int(2147483647)
  216. int(0)
  217. int(0)
  218. int(2147483647)
  219. int(-2147483648)
  220. int(0)
  221. int(0)
  222. int(1)
  223. int(100000)
  224. int(-1000000)
  225. int(100000000)
  226. int(-1000000000)
  227. int(10)
  228. int(1050000)
  229. int(%d)
  230. int(%d)
  231. int(0)
  232. int(1)
  233. int(1)
  234. int(1)
  235. int(1)
  236. int(1)
  237. int(1)
  238. int(1)
  239. int(1)
  240. int(1)
  241. int(1)
  242. int(1)
  243. int(0)
  244. int(0)
  245. int(0)
  246. int(0)
  247. int(1)
  248. int(1)
  249. int(0)
  250. int(0)
  251. int(0)
  252. int(0)
  253. int(0)
  254. int(0)
  255. int(0)
  256. int(0)
  257. int(0)
  258. int(0)
  259. int(0)
  260. int(0)
  261. int(1)
  262. int(0)
  263. int(1)
  264. int(0)
  265. int(0)
  266. int(0)
  267. *** Testing error conditions ***
  268. Warning: Wrong parameter count for intval() in %s on line %d
  269. NULL
  270. Warning: Wrong parameter count for intval() in %s on line %d
  271. NULL
  272. --- Done ---