1 // Copyright 2008 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 // ---------------------------------------------------------------------- 29 // toString 30 assertEquals("NaN", (NaN).toString()); 31 assertEquals("Infinity", (1/0).toString()); 32 assertEquals("-Infinity", (-1/0).toString()); 33 assertEquals("0", (0).toString()); 34 assertEquals("9", (9).toString()); 35 assertEquals("90", (90).toString()); 36 assertEquals("90.12", (90.12).toString()); 37 assertEquals("0.1", (0.1).toString()); 38 assertEquals("0.01", (0.01).toString()); 39 assertEquals("0.0123", (0.0123).toString()); 40 assertEquals("111111111111111110000", (111111111111111111111).toString()); 41 assertEquals("1.1111111111111111e+21", (1111111111111111111111).toString()); 42 assertEquals("1.1111111111111111e+22", (11111111111111111111111).toString()); 43 assertEquals("0.00001", (0.00001).toString()); 44 assertEquals("0.000001", (0.000001).toString()); 45 assertEquals("1e-7", (0.0000001).toString()); 46 assertEquals("1.2e-7", (0.00000012).toString()); 47 assertEquals("1.23e-7", (0.000000123).toString()); 48 assertEquals("1e-8", (0.00000001).toString()); 49 assertEquals("1.2e-8", (0.000000012).toString()); 50 assertEquals("1.23e-8", (0.0000000123).toString()); 51 52 assertEquals("0", (-0).toString()); 53 assertEquals("-9", (-9).toString()); 54 assertEquals("-90", (-90).toString()); 55 assertEquals("-90.12", (-90.12).toString()); 56 assertEquals("-0.1", (-0.1).toString()); 57 assertEquals("-0.01", (-0.01).toString()); 58 assertEquals("-0.0123", (-0.0123).toString()); 59 assertEquals("-111111111111111110000", (-111111111111111111111).toString()); 60 assertEquals("-1.1111111111111111e+21", (-1111111111111111111111).toString()); 61 assertEquals("-1.1111111111111111e+22", (-11111111111111111111111).toString()); 62 assertEquals("-0.00001", (-0.00001).toString()); 63 assertEquals("-0.000001", (-0.000001).toString()); 64 assertEquals("-1e-7", (-0.0000001).toString()); 65 assertEquals("-1.2e-7", (-0.00000012).toString()); 66 assertEquals("-1.23e-7", (-0.000000123).toString()); 67 assertEquals("-1e-8", (-0.00000001).toString()); 68 assertEquals("-1.2e-8", (-0.000000012).toString()); 69 assertEquals("-1.23e-8", (-0.0000000123).toString()); 70 71 assertEquals("NaN", (NaN).toString(16)); 72 assertEquals("Infinity", (1/0).toString(16)); 73 assertEquals("-Infinity", (-1/0).toString(16)); 74 assertEquals("0", (0).toString(16)); 75 assertEquals("9", (9).toString(16)); 76 assertEquals("5a", (90).toString(16)); 77 assertEquals("5a.1eb851eb852", (90.12).toString(16)); 78 assertEquals("0.1999999999999a", (0.1).toString(16)); 79 assertEquals("0.028f5c28f5c28f6", (0.01).toString(16)); 80 assertEquals("0.032617c1bda511a", (0.0123).toString(16)); 81 assertEquals("605f9f6dd18bc8000", (111111111111111111111).toString(16)); 82 assertEquals("3c3bc3a4a2f75c0000", (1111111111111111111111).toString(16)); 83 assertEquals("25a55a46e5da9a00000", (11111111111111111111111).toString(16)); 84 assertEquals("0.0000a7c5ac471b4788", (0.00001).toString(16)); 85 assertEquals("0.000010c6f7a0b5ed8d", (0.000001).toString(16)); 86 assertEquals("0.000001ad7f29abcaf48", (0.0000001).toString(16)); 87 assertEquals("0.000002036565348d256", (0.00000012).toString(16)); 88 assertEquals("0.0000021047ee22aa466", (0.000000123).toString(16)); 89 assertEquals("0.0000002af31dc4611874", (0.00000001).toString(16)); 90 assertEquals("0.000000338a23b87483be", (0.000000012).toString(16)); 91 assertEquals("0.00000034d3fe36aaa0a2", (0.0000000123).toString(16)); 92 93 assertEquals("0", (-0).toString(16)); 94 assertEquals("-9", (-9).toString(16)); 95 assertEquals("-5a", (-90).toString(16)); 96 assertEquals("-5a.1eb851eb852", (-90.12).toString(16)); 97 assertEquals("-0.1999999999999a", (-0.1).toString(16)); 98 assertEquals("-0.028f5c28f5c28f6", (-0.01).toString(16)); 99 assertEquals("-0.032617c1bda511a", (-0.0123).toString(16)); 100 assertEquals("-605f9f6dd18bc8000", (-111111111111111111111).toString(16)); 101 assertEquals("-3c3bc3a4a2f75c0000", (-1111111111111111111111).toString(16)); 102 assertEquals("-25a55a46e5da9a00000", (-11111111111111111111111).toString(16)); 103 assertEquals("-0.0000a7c5ac471b4788", (-0.00001).toString(16)); 104 assertEquals("-0.000010c6f7a0b5ed8d", (-0.000001).toString(16)); 105 assertEquals("-0.000001ad7f29abcaf48", (-0.0000001).toString(16)); 106 assertEquals("-0.000002036565348d256", (-0.00000012).toString(16)); 107 assertEquals("-0.0000021047ee22aa466", (-0.000000123).toString(16)); 108 assertEquals("-0.0000002af31dc4611874", (-0.00000001).toString(16)); 109 assertEquals("-0.000000338a23b87483be", (-0.000000012).toString(16)); 110 assertEquals("-0.00000034d3fe36aaa0a2", (-0.0000000123).toString(16)); 111 112 assertEquals("4294967296", Math.pow(2,32).toString()); 113 assertEquals("ffffffff", (Math.pow(2,32)-1).toString(16)); 114 assertEquals("11111111111111111111111111111111", (Math.pow(2,32)-1).toString(2)); 115 assertEquals("5yc1z", (10000007).toString(36)); 116 assertEquals("0", (0).toString(36)); 117 assertEquals("0", (0).toString(16)); 118 assertEquals("0", (0).toString(10)); 119 assertEquals("0", (0).toString(8)); 120 assertEquals("0", (0).toString(2)); 121 assertEquals("100000000000000000000000000000000", Math.pow(2,32).toString(2)); 122 assertEquals("100000000000000000000000000000001", (Math.pow(2,32) + 1).toString(2)); 123 assertEquals("100000000000080", (0x100000000000081).toString(16)); 124 assertEquals("1000000000000100", (-(-'0x1000000000000081')).toString(16)); 125 assertEquals("1000000000000000", (-(-'0x1000000000000080')).toString(16)); 126 assertEquals("1000000000000000", (-(-'0x100000000000007F')).toString(16)); 127 assertEquals("100000000000000000000000000000000000000000000000010000000", (0x100000000000081).toString(2)); 128 assertEquals("-11111111111111111111111111111111", (-(Math.pow(2,32)-1)).toString(2)); 129 assertEquals("-5yc1z", (-10000007).toString(36)); 130 assertEquals("-100000000000000000000000000000000", (-Math.pow(2,32)).toString(2)); 131 assertEquals("-100000000000000000000000000000001", (-(Math.pow(2,32) + 1)).toString(2)); 132 assertEquals("-100000000000080", (-0x100000000000081).toString(16)); 133 assertEquals("-100000000000000000000000000000000000000000000000010000000", (-0x100000000000081).toString(2)); 134 assertEquals("1000", (1000).toString()); 135 assertEquals("0.00001", (0.00001).toString()); 136 assertEquals("1000000000000000100", (1000000000000000128).toString()); 137 assertEquals("1e+21", (1000000000000000012800).toString()); 138 assertEquals("-1e+21", (-1000000000000000012800).toString()); 139 assertEquals("1e-7", (0.0000001).toString()); 140 assertEquals("-1e-7", (-0.0000001).toString()); 141 assertEquals("1.0000000000000001e+21", (1000000000000000128000).toString()); 142 assertEquals("0.000001", (0.000001).toString()); 143 assertEquals("1e-7", (0.0000001).toString()); 144 assertEquals("8.8", (8.5).toString(16)); 145 assertEquals("-8.8", (-8.5).toString(16)); 146 147 // ---------------------------------------------------------------------- 148 // toFixed 149 assertEquals("NaN", (NaN).toFixed(2)); 150 assertEquals("Infinity", (1/0).toFixed(2)); 151 assertEquals("-Infinity", (-1/0).toFixed(2)); 152 153 assertEquals("1.1111111111111111e+21", (1111111111111111111111).toFixed(8)); 154 assertEquals("0.1", (0.1).toFixed(1)); 155 assertEquals("0.10", (0.1).toFixed(2)); 156 assertEquals("0.100", (0.1).toFixed(3)); 157 assertEquals("0.01", (0.01).toFixed(2)); 158 assertEquals("0.010", (0.01).toFixed(3)); 159 assertEquals("0.0100", (0.01).toFixed(4)); 160 assertEquals("0.00", (0.001).toFixed(2)); 161 assertEquals("0.001", (0.001).toFixed(3)); 162 assertEquals("0.0010", (0.001).toFixed(4)); 163 assertEquals("1.0000", (1).toFixed(4)); 164 assertEquals("1.0", (1).toFixed(1)); 165 assertEquals("1", (1).toFixed(0)); 166 assertEquals("12", (12).toFixed(0)); 167 assertEquals("1", (1.1).toFixed(0)); 168 assertEquals("12", (12.1).toFixed(0)); 169 assertEquals("1", (1.12).toFixed(0)); 170 assertEquals("12", (12.12).toFixed(0)); 171 assertEquals("0.0000006", (0.0000006).toFixed(7)); 172 assertEquals("0.00000006", (0.00000006).toFixed(8)); 173 assertEquals("0.000000060", (0.00000006).toFixed(9)); 174 assertEquals("0.0000000600", (0.00000006).toFixed(10)); 175 assertEquals("0", (0).toFixed(0)); 176 assertEquals("0.0", (0).toFixed(1)); 177 assertEquals("0.00", (0).toFixed(2)); 178 179 assertEquals("-1.1111111111111111e+21", (-1111111111111111111111).toFixed(8)); 180 assertEquals("-0.1", (-0.1).toFixed(1)); 181 assertEquals("-0.10", (-0.1).toFixed(2)); 182 assertEquals("-0.100", (-0.1).toFixed(3)); 183 assertEquals("-0.01", (-0.01).toFixed(2)); 184 assertEquals("-0.010", (-0.01).toFixed(3)); 185 assertEquals("-0.0100", (-0.01).toFixed(4)); 186 assertEquals("-0.00", (-0.001).toFixed(2)); 187 assertEquals("-0.001", (-0.001).toFixed(3)); 188 assertEquals("-0.0010", (-0.001).toFixed(4)); 189 assertEquals("-1.0000", (-1).toFixed(4)); 190 assertEquals("-1.0", (-1).toFixed(1)); 191 assertEquals("-1", (-1).toFixed(0)); 192 assertEquals("-1", (-1.1).toFixed(0)); 193 assertEquals("-12", (-12.1).toFixed(0)); 194 assertEquals("-1", (-1.12).toFixed(0)); 195 assertEquals("-12", (-12.12).toFixed(0)); 196 assertEquals("-0.0000006", (-0.0000006).toFixed(7)); 197 assertEquals("-0.00000006", (-0.00000006).toFixed(8)); 198 assertEquals("-0.000000060", (-0.00000006).toFixed(9)); 199 assertEquals("-0.0000000600", (-0.00000006).toFixed(10)); 200 assertEquals("0", (-0).toFixed(0)); 201 assertEquals("0.0", (-0).toFixed(1)); 202 assertEquals("0.00", (-0).toFixed(2)); 203 204 assertEquals("1000", (1000).toFixed()); 205 assertEquals("0", (0.00001).toFixed()); 206 assertEquals("0.00001", (0.00001).toFixed(5)); 207 assertEquals("0.00000000000000000010", (0.0000000000000000001).toFixed(20)); 208 assertEquals("0.00001000000000000", (0.00001).toFixed(17)); 209 assertEquals("1.00000000000000000", (1).toFixed(17)); 210 assertEquals("1000000000000000128", (1000000000000000128).toFixed()); 211 assertEquals("100000000000000128.0", (100000000000000128).toFixed(1)); 212 assertEquals("10000000000000128.00", (10000000000000128).toFixed(2)); 213 assertEquals("10000000000000128.00000000000000000000", (10000000000000128).toFixed(20)); 214 assertEquals("0", (0).toFixed()); 215 assertEquals("-42.000", ((-42).toFixed(3))); 216 assertEquals("-1000000000000000128", (-1000000000000000128).toFixed()); 217 assertEquals("-0.00000000000000000010", (-0.0000000000000000001).toFixed(20)); 218 assertEquals("0.12312312312312299889", (0.123123123123123).toFixed(20)); 219 // Test that we round up even when the last digit generated is even. 220 // dtoa does not do this in its original form. 221 assertEquals("1", 0.5.toFixed(0), "0.5.toFixed(0)"); 222 assertEquals("-1", (-0.5).toFixed(0), "(-0.5).toFixed(0)"); 223 assertEquals("1.3", 1.25.toFixed(1), "1.25.toFixed(1)"); 224 // This is bizare, but Spidermonkey and KJS behave the same. 225 assertEquals("234.2040", (234.20405).toFixed(4), "234.2040.toFixed(4)"); 226 assertEquals("234.2041", (234.2040506).toFixed(4)); 227 228 // ---------------------------------------------------------------------- 229 // toExponential 230 assertEquals("1e+0", (1).toExponential()); 231 assertEquals("1.1e+1", (11).toExponential()); 232 assertEquals("1.12e+2", (112).toExponential()); 233 assertEquals("1e+0", (1).toExponential(0)); 234 assertEquals("1e+1", (11).toExponential(0)); 235 assertEquals("1e+2", (112).toExponential(0)); 236 assertEquals("1.0e+0", (1).toExponential(1)); 237 assertEquals("1.1e+1", (11).toExponential(1)); 238 assertEquals("1.1e+2", (112).toExponential(1)); 239 assertEquals("1.00e+0", (1).toExponential(2)); 240 assertEquals("1.10e+1", (11).toExponential(2)); 241 assertEquals("1.12e+2", (112).toExponential(2)); 242 assertEquals("1.000e+0", (1).toExponential(3)); 243 assertEquals("1.100e+1", (11).toExponential(3)); 244 assertEquals("1.120e+2", (112).toExponential(3)); 245 assertEquals("1e-1", (0.1).toExponential()); 246 assertEquals("1.1e-1", (0.11).toExponential()); 247 assertEquals("1.12e-1", (0.112).toExponential()); 248 assertEquals("1e-1", (0.1).toExponential(0)); 249 assertEquals("1e-1", (0.11).toExponential(0)); 250 assertEquals("1e-1", (0.112).toExponential(0)); 251 assertEquals("1.0e-1", (0.1).toExponential(1)); 252 assertEquals("1.1e-1", (0.11).toExponential(1)); 253 assertEquals("1.1e-1", (0.112).toExponential(1)); 254 assertEquals("1.00e-1", (0.1).toExponential(2)); 255 assertEquals("1.10e-1", (0.11).toExponential(2)); 256 assertEquals("1.12e-1", (0.112).toExponential(2)); 257 assertEquals("1.000e-1", (0.1).toExponential(3)); 258 assertEquals("1.100e-1", (0.11).toExponential(3)); 259 assertEquals("1.120e-1", (0.112).toExponential(3)); 260 261 assertEquals("-1e+0", (-1).toExponential()); 262 assertEquals("-1.1e+1", (-11).toExponential()); 263 assertEquals("-1.12e+2", (-112).toExponential()); 264 assertEquals("-1e+0", (-1).toExponential(0)); 265 assertEquals("-1e+1", (-11).toExponential(0)); 266 assertEquals("-1e+2", (-112).toExponential(0)); 267 assertEquals("-1.0e+0", (-1).toExponential(1)); 268 assertEquals("-1.1e+1", (-11).toExponential(1)); 269 assertEquals("-1.1e+2", (-112).toExponential(1)); 270 assertEquals("-1.00e+0", (-1).toExponential(2)); 271 assertEquals("-1.10e+1", (-11).toExponential(2)); 272 assertEquals("-1.12e+2", (-112).toExponential(2)); 273 assertEquals("-1.000e+0", (-1).toExponential(3)); 274 assertEquals("-1.100e+1", (-11).toExponential(3)); 275 assertEquals("-1.120e+2", (-112).toExponential(3)); 276 assertEquals("-1e-1", (-0.1).toExponential()); 277 assertEquals("-1.1e-1", (-0.11).toExponential()); 278 assertEquals("-1.12e-1", (-0.112).toExponential()); 279 assertEquals("-1e-1", (-0.1).toExponential(0)); 280 assertEquals("-1e-1", (-0.11).toExponential(0)); 281 assertEquals("-1e-1", (-0.112).toExponential(0)); 282 assertEquals("-1.0e-1", (-0.1).toExponential(1)); 283 assertEquals("-1.1e-1", (-0.11).toExponential(1)); 284 assertEquals("-1.1e-1", (-0.112).toExponential(1)); 285 assertEquals("-1.00e-1", (-0.1).toExponential(2)); 286 assertEquals("-1.10e-1", (-0.11).toExponential(2)); 287 assertEquals("-1.12e-1", (-0.112).toExponential(2)); 288 assertEquals("-1.000e-1", (-0.1).toExponential(3)); 289 assertEquals("-1.100e-1", (-0.11).toExponential(3)); 290 assertEquals("-1.120e-1", (-0.112).toExponential(3)); 291 292 assertEquals("NaN", (NaN).toExponential(2)); 293 assertEquals("Infinity", (Infinity).toExponential(2)); 294 assertEquals("-Infinity", (-Infinity).toExponential(2)); 295 assertEquals("1e+0", (1).toExponential(0)); 296 assertEquals("0e+0", (0).toExponential()); 297 assertEquals("0.00e+0", (0).toExponential(2)); 298 assertEquals("1e+1", (11.2356).toExponential(0)); 299 assertEquals("1.1236e+1", (11.2356).toExponential(4)); 300 assertEquals("1.1236e-4", (0.000112356).toExponential(4)); 301 assertEquals("-1.1236e-4", (-0.000112356).toExponential(4)); 302 assertEquals("1.12356e-4", (0.000112356).toExponential()); 303 assertEquals("-1.12356e-4", (-0.000112356).toExponential()); 304 305 // ---------------------------------------------------------------------- 306 // toPrecision 307 assertEquals("NaN", (NaN).toPrecision(1)); 308 assertEquals("Infinity", (Infinity).toPrecision(2)); 309 assertEquals("-Infinity", (-Infinity).toPrecision(2)); 310 assertEquals("0.000555000000000000", (0.000555).toPrecision(15)); 311 assertEquals("5.55000000000000e-7", (0.000000555).toPrecision(15)); 312 assertEquals("-5.55000000000000e-7", (-0.000000555).toPrecision(15)); 313 assertEquals("1e+8", (123456789).toPrecision(1)); 314 assertEquals("123456789", (123456789).toPrecision(9)); 315 assertEquals("1.2345679e+8", (123456789).toPrecision(8)); 316 assertEquals("1.234568e+8", (123456789).toPrecision(7)); 317 assertEquals("-1.234568e+8", (-123456789).toPrecision(7)); 318 assertEquals("-1.2e-9", Number(-.0000000012345).toPrecision(2)); 319 assertEquals("-1.2e-8", Number(-.000000012345).toPrecision(2)); 320 assertEquals("-1.2e-7", Number(-.00000012345).toPrecision(2)); 321 assertEquals("-0.0000012", Number(-.0000012345).toPrecision(2)); 322 assertEquals("-0.000012", Number(-.000012345).toPrecision(2)); 323 assertEquals("-0.00012", Number(-.00012345).toPrecision(2)); 324 assertEquals("-0.0012", Number(-.0012345).toPrecision(2)); 325 assertEquals("-0.012", Number(-.012345).toPrecision(2)); 326 assertEquals("-0.12", Number(-.12345).toPrecision(2)); 327 assertEquals("-1.2", Number(-1.2345).toPrecision(2)); 328 assertEquals("-12", Number(-12.345).toPrecision(2)); 329 assertEquals("-1.2e+2", Number(-123.45).toPrecision(2)); 330 assertEquals("-1.2e+3", Number(-1234.5).toPrecision(2)); 331 assertEquals("-1.2e+4", Number(-12345).toPrecision(2)); 332 assertEquals("-1.235e+4", Number(-12345.67).toPrecision(4)); 333 assertEquals("-1.234e+4", Number(-12344.67).toPrecision(4)); 334 // Test that we round up even when the last digit generated is even. 335 // dtoa does not do this in its original form. 336 assertEquals("1.3", 1.25.toPrecision(2), "1.25.toPrecision(2)"); 337 assertEquals("1.4", 1.35.toPrecision(2), "1.35.toPrecision(2)"); 338 339 340 341