Return to floatval.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / general_functions |
1.1 misho 1: --TEST-- 2: Testing floatval() and its alias doubleval() Functions 3: --FILE-- 4: <?php 5: /* Prototype: float floatval( mixed $var ); 6: * Description: Returns the float value of var. 7: */ 8: 9: echo "*** Testing floatval() with valid float values ***\n"; 10: // different valid float values 11: $valid_floats = array( 12: 0.0, 13: 1.0, 14: -1.0, 15: 1.234, 16: -1.234, 17: 1.2e3, 18: -1.2e3, 19: 10.0000000000000000005, 20: 10.5e+5, 21: 1e5, 22: -1e5, 23: 1e-5, 24: -1e-1, 25: 1e+5, 26: -1e+5, 27: 1E5, 28: -1E5, 29: 1E+5, 30: -1E+5, 31: .5e+7, 32: -.5e+7 33: ); 34: 35: /* loop to check that floatval() recognizes different 36: float values, expected output:float value for valid floating point number */ 37: 38: foreach ($valid_floats as $value ) { 39: var_dump( floatval($value) ); 40: } 41: 42: 43: echo "\n*** Testing doubleval() with valid float values ***\n"; 44: /* loop to check that doubleval() also recognizes different 45: float values, expected output:float value for valid floating point number */ 46: 47: foreach ($valid_floats as $value ) { 48: var_dump( doubleval($value) ); 49: } 50: 51: 52: echo "\n*** Testing floatval() on non floating types ***\n"; 53: 54: // get a resource type variable 55: $fp = fopen (__FILE__, "r"); 56: fclose($fp); 57: $dfp = opendir ( dirname(__FILE__) ); 58: closedir($dfp); 59: 60: // other types in an array 61: $not_float_types = array ( 62: -2147483648, // max negative integer value 63: 2147483648, // max positive integer value 64: $fp, // resource 65: $dfp, 66: "0.0", // string 67: "1.0", 68: "-1.3e3", 69: "bob-1.3e3", 70: "10 Some dollars", 71: "10.2 Some Dollars", 72: "10.0 dollar" + 1, 73: "10.0 dollar" + 1.0, 74: "", 75: true, 76: NULL, 77: null, 78: ); 79: /* loop through the $not_float_types to see working of 80: floatval() on non float types, expected output: float value valid floating point numbers */ 81: foreach ($not_float_types as $type ) { 82: var_dump( floatval($type) ); 83: } 84: 85: 86: echo "\n*** Testing doubleval() on non floating types ***\n"; 87: 88: /* loop through the $not_float_types to see working of 89: doubleval() on non float types, expected output: float value valid floating point numbers */ 90: foreach ($not_float_types as $type ) { 91: var_dump( doubleval($type) ); 92: } 93: 94: 95: 96: 97: echo "\n*** Testing error conditions ***\n"; 98: //Zero argument 99: var_dump( floatval() ); 100: var_dump( doubleval() ); 101: 102: //arguments more than expected 103: var_dump( floatval(TRUE, FALSE) ); 104: var_dump( doubleval(TRUE, FALSE) ); 105: 106: echo "\nDone\n"; 107: 108: 109: ?> 110: --EXPECTF-- 111: *** Testing floatval() with valid float values *** 112: float(0) 113: float(1) 114: float(-1) 115: float(1.234) 116: float(-1.234) 117: float(1200) 118: float(-1200) 119: float(10) 120: float(1050000) 121: float(100000) 122: float(-100000) 123: float(1.0E-5) 124: float(-0.1) 125: float(100000) 126: float(-100000) 127: float(100000) 128: float(-100000) 129: float(100000) 130: float(-100000) 131: float(5000000) 132: float(-5000000) 133: 134: *** Testing doubleval() with valid float values *** 135: float(0) 136: float(1) 137: float(-1) 138: float(1.234) 139: float(-1.234) 140: float(1200) 141: float(-1200) 142: float(10) 143: float(1050000) 144: float(100000) 145: float(-100000) 146: float(1.0E-5) 147: float(-0.1) 148: float(100000) 149: float(-100000) 150: float(100000) 151: float(-100000) 152: float(100000) 153: float(-100000) 154: float(5000000) 155: float(-5000000) 156: 157: *** Testing floatval() on non floating types *** 158: float(-2147483648) 159: float(2147483648) 160: float(5) 161: float(6) 162: float(0) 163: float(1) 164: float(-1300) 165: float(0) 166: float(10) 167: float(10.2) 168: float(11) 169: float(11) 170: float(0) 171: float(1) 172: float(0) 173: float(0) 174: 175: *** Testing doubleval() on non floating types *** 176: float(-2147483648) 177: float(2147483648) 178: float(5) 179: float(6) 180: float(0) 181: float(1) 182: float(-1300) 183: float(0) 184: float(10) 185: float(10.2) 186: float(11) 187: float(11) 188: float(0) 189: float(1) 190: float(0) 191: float(0) 192: 193: *** Testing error conditions *** 194: 195: Warning: floatval() expects exactly 1 parameter, 0 given in %s on line %d 196: NULL 197: 198: Warning: doubleval() expects exactly 1 parameter, 0 given in %s on line %d 199: NULL 200: 201: Warning: floatval() expects exactly 1 parameter, 2 given in %s on line %d 202: NULL 203: 204: Warning: doubleval() expects exactly 1 parameter, 2 given in %s on line %d 205: NULL 206: 207: Done