number-parseInt-double.js (4505B)
1 // Test inlining parseInt with a Double input. 2 3 const doubleValues = [ 4 // Values around INT32_MIN. 5 -2147483648.5, 6 -2147483647.5, 7 -2147483646.5, 8 9 // Negative values. 10 -65536.1, -65535.2, -256.3, -255.4, -100.5, -50.6, -10.7, 11 12 // Values around zero. 13 -2.1, -1.1, -0, +0, 0.1, 1.1, 2.1, 14 15 // Positive values. 16 10.7, 50.6, 100.5, 255.4, 256.3, 65535.2, 65536.1, 17 18 // Values around INT32_MAX. 19 2147483645.5, 20 2147483646.5, 21 2147483647.5, 22 ]; 23 24 // Test double input without an explicit radix. 25 function testRadixAbsent() { 26 for (let i = 0; i < 200; ++i) { 27 let x = doubleValues[i % doubleValues.length]; 28 let y = x|0; 29 30 let r = Number.parseInt(x); 31 assertEq(r, y); 32 } 33 } 34 for (let i = 0; i < 2; ++i) testRadixAbsent(); 35 36 // Test double input with radix=10. 37 function testRadixTen() { 38 for (let i = 0; i < 200; ++i) { 39 let x = doubleValues[i % doubleValues.length]; 40 41 let r = Number.parseInt(x, 10); 42 assertEq(r, x|0); 43 } 44 } 45 for (let i = 0; i < 2; ++i) testRadixTen(); 46 47 // Test double input in the exclusive range (0, 1.0e-6). 48 function testBadTooSmallPositive() { 49 const goodValues = [ 50 +0, +0.5, +1.5, +2.5, +3.5, +4.5, +5.5, 51 -0, -1.5, -2.5, -3.5, -4.5, -5.5, 52 ]; 53 const badValues = [ 54 9.999999999999997e-7, // parseInt(9.999999999999997e-7) is 9. 55 1e-7, // parseInt(1e-7) is 1. 56 ]; 57 58 const values = [ 59 ...goodValues, 60 ...badValues, 61 ]; 62 63 for (let i = 0; i < 200; ++i) { 64 let xs = [goodValues, values][(i >= 150)|0]; 65 let x = xs[i % xs.length]; 66 let y; 67 if (0 < x && x < 1e-6) { 68 y = (String(x).match(/(.*)e.*/)[1])|0; 69 } else { 70 y = x|0; 71 } 72 73 let r = Number.parseInt(x); 74 assertEq(r, y); 75 } 76 } 77 for (let i = 0; i < 2; ++i) testBadTooSmallPositive(); 78 79 // Test double input in the exclusive range (-1.0e-6, -0). 80 function testBadTooSmallNegative() { 81 const goodValues = [ 82 +0, +0.5, +1.5, +2.5, +3.5, +4.5, +5.5, 83 -0, -1.5, -2.5, -3.5, -4.5, -5.5, 84 ]; 85 const badValues = [ 86 -9.999999999999997e-7, // parseInt(-9.999999999999997e-7) is -9. 87 -1e-7, // parseInt(-1e-7) is -1. 88 ]; 89 90 const values = [ 91 ...goodValues, 92 ...badValues, 93 ]; 94 95 for (let i = 0; i < 200; ++i) { 96 let xs = [goodValues, values][(i >= 150)|0]; 97 let x = xs[i % xs.length]; 98 let y; 99 if (-1e-6 < x && x < -0) { 100 y = (String(x).match(/(.*)e.*/)[1])|0; 101 } else { 102 y = x|0; 103 } 104 105 let r = Number.parseInt(x); 106 assertEq(r, y); 107 } 108 } 109 for (let i = 0; i < 2; ++i) testBadTooSmallNegative(); 110 111 // Test double input in the exclusive range (-1, -1.0e-6). 112 function testBadNegativeZero() { 113 const goodValues = [ 114 +0, +0.5, +1.5, +2.5, +3.5, +4.5, +5.5, 115 -0, -1.5, -2.5, -3.5, -4.5, -5.5, 116 ]; 117 const badValues = [ 118 -0.1, // parseInt(-0.1) is -0. 119 -0.5, // parseInt(-0.5) is -0. 120 -0.9, // parseInt(-0.9) is -0. 121 ]; 122 123 const values = [ 124 ...goodValues, 125 ...badValues, 126 ]; 127 128 for (let i = 0; i < 200; ++i) { 129 let xs = [goodValues, values][(i >= 150)|0]; 130 let x = xs[i % xs.length]; 131 let y; 132 if (-1 < x && x < 0) { 133 y = -0; 134 } else { 135 y = x|0; 136 } 137 138 let r = Number.parseInt(x); 139 assertEq(r, y); 140 } 141 } 142 for (let i = 0; i < 2; ++i) testBadNegativeZero(); 143 144 // Test double input with infinity values. 145 function testBadInfinity() { 146 const goodValues = [ 147 +0, +0.5, +1.5, +2.5, +3.5, +4.5, +5.5, 148 -0, -1.5, -2.5, -3.5, -4.5, -5.5, 149 ]; 150 const badValues = [ 151 Infinity, // parseInt(Infinity) is NaN 152 -Infinity, // parseInt(-Infinity) is NaN 153 ]; 154 155 const values = [ 156 ...goodValues, 157 ...badValues, 158 ]; 159 160 for (let i = 0; i < 200; ++i) { 161 let xs = [goodValues, values][(i >= 150)|0]; 162 let x = xs[i % xs.length]; 163 let y; 164 if (!Number.isFinite(x)) { 165 y = NaN; 166 } else { 167 y = x|0; 168 } 169 170 let r = Number.parseInt(x); 171 assertEq(r, y); 172 } 173 } 174 for (let i = 0; i < 2; ++i) testBadInfinity(); 175 176 // Test double input with NaN values. 177 function testBadNaN() { 178 const goodValues = [ 179 +0, +0.5, +1.5, +2.5, +3.5, +4.5, +5.5, 180 -0, -1.5, -2.5, -3.5, -4.5, -5.5, 181 ]; 182 const badValues = [ 183 NaN, // parseInt(NaN) is NaN 184 ]; 185 186 const values = [ 187 ...goodValues, 188 ...badValues, 189 ]; 190 191 for (let i = 0; i < 200; ++i) { 192 let xs = [goodValues, values][(i >= 150)|0]; 193 let x = xs[i % xs.length]; 194 let y; 195 if (!Number.isFinite(x)) { 196 y = NaN; 197 } else { 198 y = x|0; 199 } 200 201 let r = Number.parseInt(x); 202 assertEq(r, y); 203 } 204 } 205 for (let i = 0; i < 2; ++i) testBadNaN();