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