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 30assertEquals("NaN", (NaN).toString()); 31assertEquals("Infinity", (1/0).toString()); 32assertEquals("-Infinity", (-1/0).toString()); 33assertEquals("0", (0).toString()); 34assertEquals("9", (9).toString()); 35assertEquals("90", (90).toString()); 36assertEquals("90.12", (90.12).toString()); 37assertEquals("0.1", (0.1).toString()); 38assertEquals("0.01", (0.01).toString()); 39assertEquals("0.0123", (0.0123).toString()); 40assertEquals("111111111111111110000", (111111111111111111111).toString()); 41assertEquals("1.1111111111111111e+21", (1111111111111111111111).toString()); 42assertEquals("1.1111111111111111e+22", (11111111111111111111111).toString()); 43assertEquals("0.00001", (0.00001).toString()); 44assertEquals("0.000001", (0.000001).toString()); 45assertEquals("1e-7", (0.0000001).toString()); 46assertEquals("1.2e-7", (0.00000012).toString()); 47assertEquals("1.23e-7", (0.000000123).toString()); 48assertEquals("1e-8", (0.00000001).toString()); 49assertEquals("1.2e-8", (0.000000012).toString()); 50assertEquals("1.23e-8", (0.0000000123).toString()); 51 52assertEquals("0", (-0).toString()); 53assertEquals("-9", (-9).toString()); 54assertEquals("-90", (-90).toString()); 55assertEquals("-90.12", (-90.12).toString()); 56assertEquals("-0.1", (-0.1).toString()); 57assertEquals("-0.01", (-0.01).toString()); 58assertEquals("-0.0123", (-0.0123).toString()); 59assertEquals("-111111111111111110000", (-111111111111111111111).toString()); 60assertEquals("-1.1111111111111111e+21", (-1111111111111111111111).toString()); 61assertEquals("-1.1111111111111111e+22", (-11111111111111111111111).toString()); 62assertEquals("-0.00001", (-0.00001).toString()); 63assertEquals("-0.000001", (-0.000001).toString()); 64assertEquals("-1e-7", (-0.0000001).toString()); 65assertEquals("-1.2e-7", (-0.00000012).toString()); 66assertEquals("-1.23e-7", (-0.000000123).toString()); 67assertEquals("-1e-8", (-0.00000001).toString()); 68assertEquals("-1.2e-8", (-0.000000012).toString()); 69assertEquals("-1.23e-8", (-0.0000000123).toString()); 70 71assertEquals("NaN", (NaN).toString(16)); 72assertEquals("Infinity", (1/0).toString(16)); 73assertEquals("-Infinity", (-1/0).toString(16)); 74assertEquals("0", (0).toString(16)); 75assertEquals("9", (9).toString(16)); 76assertEquals("5a", (90).toString(16)); 77assertEquals("5a.1eb851eb852", (90.12).toString(16)); 78assertEquals("0.1999999999999a", (0.1).toString(16)); 79assertEquals("0.028f5c28f5c28f6", (0.01).toString(16)); 80assertEquals("0.032617c1bda511a", (0.0123).toString(16)); 81assertEquals("605f9f6dd18bc8000", (111111111111111111111).toString(16)); 82assertEquals("3c3bc3a4a2f75c0000", (1111111111111111111111).toString(16)); 83assertEquals("25a55a46e5da9a00000", (11111111111111111111111).toString(16)); 84assertEquals("0.0000a7c5ac471b4788", (0.00001).toString(16)); 85assertEquals("0.000010c6f7a0b5ed8d", (0.000001).toString(16)); 86assertEquals("0.000001ad7f29abcaf48", (0.0000001).toString(16)); 87assertEquals("0.000002036565348d256", (0.00000012).toString(16)); 88assertEquals("0.0000021047ee22aa466", (0.000000123).toString(16)); 89assertEquals("0.0000002af31dc4611874", (0.00000001).toString(16)); 90assertEquals("0.000000338a23b87483be", (0.000000012).toString(16)); 91assertEquals("0.00000034d3fe36aaa0a2", (0.0000000123).toString(16)); 92 93assertEquals("0", (-0).toString(16)); 94assertEquals("-9", (-9).toString(16)); 95assertEquals("-5a", (-90).toString(16)); 96assertEquals("-5a.1eb851eb852", (-90.12).toString(16)); 97assertEquals("-0.1999999999999a", (-0.1).toString(16)); 98assertEquals("-0.028f5c28f5c28f6", (-0.01).toString(16)); 99assertEquals("-0.032617c1bda511a", (-0.0123).toString(16)); 100assertEquals("-605f9f6dd18bc8000", (-111111111111111111111).toString(16)); 101assertEquals("-3c3bc3a4a2f75c0000", (-1111111111111111111111).toString(16)); 102assertEquals("-25a55a46e5da9a00000", (-11111111111111111111111).toString(16)); 103assertEquals("-0.0000a7c5ac471b4788", (-0.00001).toString(16)); 104assertEquals("-0.000010c6f7a0b5ed8d", (-0.000001).toString(16)); 105assertEquals("-0.000001ad7f29abcaf48", (-0.0000001).toString(16)); 106assertEquals("-0.000002036565348d256", (-0.00000012).toString(16)); 107assertEquals("-0.0000021047ee22aa466", (-0.000000123).toString(16)); 108assertEquals("-0.0000002af31dc4611874", (-0.00000001).toString(16)); 109assertEquals("-0.000000338a23b87483be", (-0.000000012).toString(16)); 110assertEquals("-0.00000034d3fe36aaa0a2", (-0.0000000123).toString(16)); 111 112assertEquals("4294967296", Math.pow(2,32).toString()); 113assertEquals("ffffffff", (Math.pow(2,32)-1).toString(16)); 114assertEquals("11111111111111111111111111111111", (Math.pow(2,32)-1).toString(2)); 115assertEquals("5yc1z", (10000007).toString(36)); 116assertEquals("0", (0).toString(36)); 117assertEquals("0", (0).toString(16)); 118assertEquals("0", (0).toString(10)); 119assertEquals("0", (0).toString(8)); 120assertEquals("0", (0).toString(2)); 121assertEquals("100000000000000000000000000000000", Math.pow(2,32).toString(2)); 122assertEquals("100000000000000000000000000000001", (Math.pow(2,32) + 1).toString(2)); 123assertEquals("100000000000080", (0x100000000000081).toString(16)); 124assertEquals("1000000000000100", (-(-'0x1000000000000081')).toString(16)); 125assertEquals("1000000000000000", (-(-'0x1000000000000080')).toString(16)); 126assertEquals("1000000000000000", (-(-'0x100000000000007F')).toString(16)); 127assertEquals("100000000000000000000000000000000000000000000000010000000", (0x100000000000081).toString(2)); 128assertEquals("-11111111111111111111111111111111", (-(Math.pow(2,32)-1)).toString(2)); 129assertEquals("-5yc1z", (-10000007).toString(36)); 130assertEquals("-100000000000000000000000000000000", (-Math.pow(2,32)).toString(2)); 131assertEquals("-100000000000000000000000000000001", (-(Math.pow(2,32) + 1)).toString(2)); 132assertEquals("-100000000000080", (-0x100000000000081).toString(16)); 133assertEquals("-100000000000000000000000000000000000000000000000010000000", (-0x100000000000081).toString(2)); 134assertEquals("1000", (1000).toString()); 135assertEquals("0.00001", (0.00001).toString()); 136assertEquals("1000000000000000100", (1000000000000000128).toString()); 137assertEquals("1e+21", (1000000000000000012800).toString()); 138assertEquals("-1e+21", (-1000000000000000012800).toString()); 139assertEquals("1e-7", (0.0000001).toString()); 140assertEquals("-1e-7", (-0.0000001).toString()); 141assertEquals("1.0000000000000001e+21", (1000000000000000128000).toString()); 142assertEquals("0.000001", (0.000001).toString()); 143assertEquals("1e-7", (0.0000001).toString()); 144assertEquals("8.8", (8.5).toString(16)); 145assertEquals("-8.8", (-8.5).toString(16)); 146 147// ---------------------------------------------------------------------- 148// toFixed 149assertEquals("NaN", (NaN).toFixed(2)); 150assertEquals("Infinity", (1/0).toFixed(2)); 151assertEquals("-Infinity", (-1/0).toFixed(2)); 152 153assertEquals("1.1111111111111111e+21", (1111111111111111111111).toFixed(8)); 154assertEquals("0.1", (0.1).toFixed(1)); 155assertEquals("0.10", (0.1).toFixed(2)); 156assertEquals("0.100", (0.1).toFixed(3)); 157assertEquals("0.01", (0.01).toFixed(2)); 158assertEquals("0.010", (0.01).toFixed(3)); 159assertEquals("0.0100", (0.01).toFixed(4)); 160assertEquals("0.00", (0.001).toFixed(2)); 161assertEquals("0.001", (0.001).toFixed(3)); 162assertEquals("0.0010", (0.001).toFixed(4)); 163assertEquals("1.0000", (1).toFixed(4)); 164assertEquals("1.0", (1).toFixed(1)); 165assertEquals("1", (1).toFixed(0)); 166assertEquals("12", (12).toFixed(0)); 167assertEquals("1", (1.1).toFixed(0)); 168assertEquals("12", (12.1).toFixed(0)); 169assertEquals("1", (1.12).toFixed(0)); 170assertEquals("12", (12.12).toFixed(0)); 171assertEquals("0.0000006", (0.0000006).toFixed(7)); 172assertEquals("0.00000006", (0.00000006).toFixed(8)); 173assertEquals("0.000000060", (0.00000006).toFixed(9)); 174assertEquals("0.0000000600", (0.00000006).toFixed(10)); 175assertEquals("0", (0).toFixed(0)); 176assertEquals("0.0", (0).toFixed(1)); 177assertEquals("0.00", (0).toFixed(2)); 178 179assertEquals("-1.1111111111111111e+21", (-1111111111111111111111).toFixed(8)); 180assertEquals("-0.1", (-0.1).toFixed(1)); 181assertEquals("-0.10", (-0.1).toFixed(2)); 182assertEquals("-0.100", (-0.1).toFixed(3)); 183assertEquals("-0.01", (-0.01).toFixed(2)); 184assertEquals("-0.010", (-0.01).toFixed(3)); 185assertEquals("-0.0100", (-0.01).toFixed(4)); 186assertEquals("-0.00", (-0.001).toFixed(2)); 187assertEquals("-0.001", (-0.001).toFixed(3)); 188assertEquals("-0.0010", (-0.001).toFixed(4)); 189assertEquals("-1.0000", (-1).toFixed(4)); 190assertEquals("-1.0", (-1).toFixed(1)); 191assertEquals("-1", (-1).toFixed(0)); 192assertEquals("-1", (-1.1).toFixed(0)); 193assertEquals("-12", (-12.1).toFixed(0)); 194assertEquals("-1", (-1.12).toFixed(0)); 195assertEquals("-12", (-12.12).toFixed(0)); 196assertEquals("-0.0000006", (-0.0000006).toFixed(7)); 197assertEquals("-0.00000006", (-0.00000006).toFixed(8)); 198assertEquals("-0.000000060", (-0.00000006).toFixed(9)); 199assertEquals("-0.0000000600", (-0.00000006).toFixed(10)); 200assertEquals("0", (-0).toFixed(0)); 201assertEquals("0.0", (-0).toFixed(1)); 202assertEquals("0.00", (-0).toFixed(2)); 203 204assertEquals("1000", (1000).toFixed()); 205assertEquals("0", (0.00001).toFixed()); 206assertEquals("0.00001", (0.00001).toFixed(5)); 207assertEquals("0.00000000000000000010", (0.0000000000000000001).toFixed(20)); 208assertEquals("0.00001000000000000", (0.00001).toFixed(17)); 209assertEquals("1.00000000000000000", (1).toFixed(17)); 210assertEquals("1000000000000000128", (1000000000000000128).toFixed()); 211assertEquals("100000000000000128.0", (100000000000000128).toFixed(1)); 212assertEquals("10000000000000128.00", (10000000000000128).toFixed(2)); 213assertEquals("10000000000000128.00000000000000000000", (10000000000000128).toFixed(20)); 214assertEquals("0", (0).toFixed()); 215assertEquals("-42.000", ((-42).toFixed(3))); 216assertEquals("-1000000000000000128", (-1000000000000000128).toFixed()); 217assertEquals("-0.00000000000000000010", (-0.0000000000000000001).toFixed(20)); 218assertEquals("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. 221assertEquals("1", 0.5.toFixed(0), "0.5.toFixed(0)"); 222assertEquals("-1", (-0.5).toFixed(0), "(-0.5).toFixed(0)"); 223assertEquals("1.3", 1.25.toFixed(1), "1.25.toFixed(1)"); 224// This is bizare, but Spidermonkey and KJS behave the same. 225assertEquals("234.2040", (234.20405).toFixed(4), "234.2040.toFixed(4)"); 226assertEquals("234.2041", (234.2040506).toFixed(4)); 227 228// ---------------------------------------------------------------------- 229// toExponential 230assertEquals("1e+0", (1).toExponential()); 231assertEquals("1.1e+1", (11).toExponential()); 232assertEquals("1.12e+2", (112).toExponential()); 233assertEquals("1e+0", (1).toExponential(0)); 234assertEquals("1e+1", (11).toExponential(0)); 235assertEquals("1e+2", (112).toExponential(0)); 236assertEquals("1.0e+0", (1).toExponential(1)); 237assertEquals("1.1e+1", (11).toExponential(1)); 238assertEquals("1.1e+2", (112).toExponential(1)); 239assertEquals("1.00e+0", (1).toExponential(2)); 240assertEquals("1.10e+1", (11).toExponential(2)); 241assertEquals("1.12e+2", (112).toExponential(2)); 242assertEquals("1.000e+0", (1).toExponential(3)); 243assertEquals("1.100e+1", (11).toExponential(3)); 244assertEquals("1.120e+2", (112).toExponential(3)); 245assertEquals("1e-1", (0.1).toExponential()); 246assertEquals("1.1e-1", (0.11).toExponential()); 247assertEquals("1.12e-1", (0.112).toExponential()); 248assertEquals("1e-1", (0.1).toExponential(0)); 249assertEquals("1e-1", (0.11).toExponential(0)); 250assertEquals("1e-1", (0.112).toExponential(0)); 251assertEquals("1.0e-1", (0.1).toExponential(1)); 252assertEquals("1.1e-1", (0.11).toExponential(1)); 253assertEquals("1.1e-1", (0.112).toExponential(1)); 254assertEquals("1.00e-1", (0.1).toExponential(2)); 255assertEquals("1.10e-1", (0.11).toExponential(2)); 256assertEquals("1.12e-1", (0.112).toExponential(2)); 257assertEquals("1.000e-1", (0.1).toExponential(3)); 258assertEquals("1.100e-1", (0.11).toExponential(3)); 259assertEquals("1.120e-1", (0.112).toExponential(3)); 260 261assertEquals("-1e+0", (-1).toExponential()); 262assertEquals("-1.1e+1", (-11).toExponential()); 263assertEquals("-1.12e+2", (-112).toExponential()); 264assertEquals("-1e+0", (-1).toExponential(0)); 265assertEquals("-1e+1", (-11).toExponential(0)); 266assertEquals("-1e+2", (-112).toExponential(0)); 267assertEquals("-1.0e+0", (-1).toExponential(1)); 268assertEquals("-1.1e+1", (-11).toExponential(1)); 269assertEquals("-1.1e+2", (-112).toExponential(1)); 270assertEquals("-1.00e+0", (-1).toExponential(2)); 271assertEquals("-1.10e+1", (-11).toExponential(2)); 272assertEquals("-1.12e+2", (-112).toExponential(2)); 273assertEquals("-1.000e+0", (-1).toExponential(3)); 274assertEquals("-1.100e+1", (-11).toExponential(3)); 275assertEquals("-1.120e+2", (-112).toExponential(3)); 276assertEquals("-1e-1", (-0.1).toExponential()); 277assertEquals("-1.1e-1", (-0.11).toExponential()); 278assertEquals("-1.12e-1", (-0.112).toExponential()); 279assertEquals("-1e-1", (-0.1).toExponential(0)); 280assertEquals("-1e-1", (-0.11).toExponential(0)); 281assertEquals("-1e-1", (-0.112).toExponential(0)); 282assertEquals("-1.0e-1", (-0.1).toExponential(1)); 283assertEquals("-1.1e-1", (-0.11).toExponential(1)); 284assertEquals("-1.1e-1", (-0.112).toExponential(1)); 285assertEquals("-1.00e-1", (-0.1).toExponential(2)); 286assertEquals("-1.10e-1", (-0.11).toExponential(2)); 287assertEquals("-1.12e-1", (-0.112).toExponential(2)); 288assertEquals("-1.000e-1", (-0.1).toExponential(3)); 289assertEquals("-1.100e-1", (-0.11).toExponential(3)); 290assertEquals("-1.120e-1", (-0.112).toExponential(3)); 291 292assertEquals("NaN", (NaN).toExponential(2)); 293assertEquals("Infinity", (Infinity).toExponential(2)); 294assertEquals("-Infinity", (-Infinity).toExponential(2)); 295assertEquals("1e+0", (1).toExponential(0)); 296assertEquals("0e+0", (0).toExponential()); 297assertEquals("0.00e+0", (0).toExponential(2)); 298assertEquals("1e+1", (11.2356).toExponential(0)); 299assertEquals("1.1236e+1", (11.2356).toExponential(4)); 300assertEquals("1.1236e-4", (0.000112356).toExponential(4)); 301assertEquals("-1.1236e-4", (-0.000112356).toExponential(4)); 302assertEquals("1.12356e-4", (0.000112356).toExponential()); 303assertEquals("-1.12356e-4", (-0.000112356).toExponential()); 304 305// ---------------------------------------------------------------------- 306// toPrecision 307assertEquals("NaN", (NaN).toPrecision(1)); 308assertEquals("Infinity", (Infinity).toPrecision(2)); 309assertEquals("-Infinity", (-Infinity).toPrecision(2)); 310assertEquals("0.000555000000000000", (0.000555).toPrecision(15)); 311assertEquals("5.55000000000000e-7", (0.000000555).toPrecision(15)); 312assertEquals("-5.55000000000000e-7", (-0.000000555).toPrecision(15)); 313assertEquals("1e+8", (123456789).toPrecision(1)); 314assertEquals("123456789", (123456789).toPrecision(9)); 315assertEquals("1.2345679e+8", (123456789).toPrecision(8)); 316assertEquals("1.234568e+8", (123456789).toPrecision(7)); 317assertEquals("-1.234568e+8", (-123456789).toPrecision(7)); 318assertEquals("-1.2e-9", Number(-.0000000012345).toPrecision(2)); 319assertEquals("-1.2e-8", Number(-.000000012345).toPrecision(2)); 320assertEquals("-1.2e-7", Number(-.00000012345).toPrecision(2)); 321assertEquals("-0.0000012", Number(-.0000012345).toPrecision(2)); 322assertEquals("-0.000012", Number(-.000012345).toPrecision(2)); 323assertEquals("-0.00012", Number(-.00012345).toPrecision(2)); 324assertEquals("-0.0012", Number(-.0012345).toPrecision(2)); 325assertEquals("-0.012", Number(-.012345).toPrecision(2)); 326assertEquals("-0.12", Number(-.12345).toPrecision(2)); 327assertEquals("-1.2", Number(-1.2345).toPrecision(2)); 328assertEquals("-12", Number(-12.345).toPrecision(2)); 329assertEquals("-1.2e+2", Number(-123.45).toPrecision(2)); 330assertEquals("-1.2e+3", Number(-1234.5).toPrecision(2)); 331assertEquals("-1.2e+4", Number(-12345).toPrecision(2)); 332assertEquals("-1.235e+4", Number(-12345.67).toPrecision(4)); 333assertEquals("-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. 336assertEquals("1.3", 1.25.toPrecision(2), "1.25.toPrecision(2)"); 337assertEquals("1.4", 1.35.toPrecision(2), "1.35.toPrecision(2)"); 338