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 28function toNumber(val) { 29 return Number(val); 30} 31 32function repeat(s, num) { 33 var result = ''; 34 while (num > 0) { 35 if ((num & 1) != 0) result += s; 36 s += s; 37 num >>= 1; 38 } 39 40 return result; 41} 42 43assertEquals('0000000000', repeat('0', 10)); 44 45// assertEquals(, toNumber()); 46 47assertEquals(123, toNumber(" 123")); 48assertEquals(123, toNumber("\n123")); 49assertEquals(123, toNumber("\r123")); 50assertEquals(123, toNumber("\t123")); 51assertEquals(123, toNumber("\f123")); 52 53assertEquals(123, toNumber("123 ")); 54assertEquals(123, toNumber("123\n")); 55assertEquals(123, toNumber("123\r")); 56assertEquals(123, toNumber("123\t")); 57assertEquals(123, toNumber("123\f")); 58 59assertEquals(123, toNumber(" 123 ")); 60assertEquals(123, toNumber("\n123\n")); 61assertEquals(123, toNumber("\r123\r")); 62assertEquals(123, toNumber("\t123\t")); 63assertEquals(123, toNumber("\f123\f")); 64 65assertEquals(16, toNumber(" 0x10 ")); 66assertEquals(NaN, toNumber("0x")); 67assertEquals(NaN, toNumber("0x ")); 68 69assertTrue(isNaN(toNumber(" NaN "))); 70assertEquals(Infinity, toNumber(" Infinity ") ," Infinity"); 71assertEquals(-Infinity, toNumber(" -Infinity ")); 72assertEquals(Infinity, toNumber(" +Infinity "), " +Infinity"); 73assertEquals(Infinity, toNumber("Infinity ") ,"Infinity"); 74assertEquals(-Infinity, toNumber("-Infinity ")); 75assertEquals(Infinity, toNumber("+Infinity "), "+Infinity"); 76 77assertEquals(0, toNumber("0")); 78assertEquals(0, toNumber("+0")); 79assertEquals(-0, toNumber("-0")); 80assertEquals(-Infinity, 1 / toNumber("-0")); 81 82assertEquals(1, toNumber("1")); 83assertEquals(1, toNumber("+1")); 84assertEquals(-1, toNumber("-1")); 85 86assertEquals(2, toNumber("2")); 87assertEquals(2, toNumber("+2")); 88assertEquals(-2, toNumber("-2")); 89 90assertEquals(3.1415926, toNumber("3.1415926")); 91assertEquals(3.1415926, toNumber("+3.1415926")); 92assertEquals(-3.1415926, toNumber("-3.1415926")); 93 94assertEquals(5, toNumber("5.")); 95assertEquals(5, toNumber("+5.")); 96assertEquals(-5, toNumber("-5.")); 97 98assertEquals(500, toNumber("5e2")); 99assertEquals(500, toNumber("+5e2")); 100assertEquals(-500, toNumber("-5e2")); 101assertEquals(500, toNumber("5e+2")); 102assertEquals(500, toNumber("+5e+2")); 103assertEquals(-500, toNumber("-5e+2")); 104assertEquals(0.05, toNumber("5e-2")); 105assertEquals(0.05, toNumber("+5e-2")); 106assertEquals(-0.05, toNumber("-5e-2")); 107 108assertEquals(0.00001, toNumber(".00001")); 109assertEquals(0.00001, toNumber("+.00001")); 110assertEquals(-0.00001, toNumber("-.00001")); 111assertEquals(1, toNumber(".00001e5")); 112assertEquals(1, toNumber("+.00001e5")); 113assertEquals(-1, toNumber("-.00001e5")); 114assertEquals(1, toNumber(".00001e+5")); 115assertEquals(1, toNumber("+.00001e+5")); 116assertEquals(-1, toNumber("-.00001e+5")); 117assertEquals(0.00001, toNumber(".001e-2")); 118assertEquals(0.00001, toNumber("+.001e-2")); 119assertEquals(-0.00001, toNumber("-.001e-2")); 120 121assertEquals(12340000, toNumber("1234e4")); 122assertEquals(12340000, toNumber("+1234e4")); 123assertEquals(-12340000, toNumber("-1234e4")); 124assertEquals(12340000, toNumber("1234e+4")); 125assertEquals(12340000, toNumber("+1234e+4")); 126assertEquals(-12340000, toNumber("-1234e+4")); 127assertEquals(0.1234, toNumber("1234e-4")); 128assertEquals(0.1234, toNumber("+1234e-4")); 129assertEquals(-0.1234, toNumber("-1234e-4")); 130 131assertEquals(0, toNumber("0x0")); 132assertEquals(1, toNumber("0x1")); 133assertEquals(2, toNumber("0x2")); 134assertEquals(9, toNumber("0x9")); 135assertEquals(10, toNumber("0xa")); 136assertEquals(11, toNumber("0xb")); 137assertEquals(15, toNumber("0xf")); 138assertEquals(10, toNumber("0xA")); 139assertEquals(11, toNumber("0xB")); 140assertEquals(15, toNumber("0xF")); 141 142assertEquals(0, toNumber("0X0")); 143assertEquals(9, toNumber("0X9")); 144assertEquals(10, toNumber("0Xa")); 145assertEquals(10, toNumber("0XA")); 146assertEquals(15, toNumber("0Xf")); 147assertEquals(15, toNumber("0XF")); 148 149assertEquals(0, toNumber("0x000")); 150assertEquals(-Infinity, 1 / toNumber("-0x000")); 151assertEquals(0, toNumber("0x000" + repeat('0', 1000))); 152assertEquals(9, toNumber("0x009")); 153assertEquals(10, toNumber("0x00a")); 154assertEquals(10, toNumber("0x00A")); 155assertEquals(15, toNumber("0x00f")); 156assertEquals(15, toNumber("0x00F")); 157assertEquals(15, toNumber("0x00F ")); 158assertEquals(Infinity, toNumber("0x" + repeat('0', 1000) + '1' 159 + repeat('0', 1000))); 160assertEquals(-Infinity, toNumber("-0x1" + repeat('0', 1000))); 161 162assertEquals(0x1000000 * 0x10000000, toNumber("0x10000000000000")); 163assertEquals(0x1000000 * 0x10000000 + 1, toNumber("0x10000000000001")); 164assertEquals(0x10 * 0x1000000 * 0x10000000, toNumber("0x100000000000000")); 165assertEquals(0x10 * 0x1000000 * 0x10000000, toNumber("0x100000000000001")); 166assertEquals(0x10 * 0x1000000 * 0x10000000, toNumber("0x100000000000007")); 167assertEquals(0x10 * 0x1000000 * 0x10000000, toNumber("0x100000000000008")); 168assertEquals(0x10 * (0x1000000 * 0x10000000 + 1), 169 toNumber("0x100000000000009")); 170assertEquals(0x10 * (0x1000000 * 0x10000000 + 1), 171 toNumber("0x10000000000000F")); 172assertEquals(0x10 * (0x1000000 * 0x10000000 + 1), 173 toNumber("0x100000000000010")); 174assertEquals(0x100000000000 * 0x1000000 * 0x10000000, 175 toNumber("0x1000000000000000000000000")); 176assertEquals(0x100000000000 * 0x1000000 * 0x10000000, 177 toNumber("0x1000000000000080000000000")); 178assertEquals(0x100000000000 * (0x1000000 * 0x10000000 + 1), 179 toNumber("0x1000000000000080000000001")); 180assertEquals(0x100000000000 * 0x1000000 * 0x10000000, 181 toNumber(" 0x1000000000000000000000000 ")); 182 183assertEquals(0, toNumber("00")); 184assertEquals(1, toNumber("01")); 185assertEquals(2, toNumber("02")); 186assertEquals(10, toNumber("010")); 187assertEquals(100, toNumber("0100")); 188assertEquals(100, toNumber("000100")); 189 190assertEquals(Infinity, toNumber("1e999"), "1e999"); 191assertEquals(-Infinity, toNumber("-1e999")); 192assertEquals(0, toNumber("1e-999")); 193assertEquals(-0, toNumber("-1e-999")); 194assertEquals(Infinity, 1 / toNumber("1e-999"), "1e-999"); 195assertEquals(-Infinity, 1 / toNumber("-1e-999")); 196 197assertTrue(isNaN(toNumber("junk")), "junk"); 198assertTrue(isNaN(toNumber("100 junk")), "100 junk"); 199assertTrue(isNaN(toNumber("0x100 junk")), "0x100 junk"); 200assertTrue(isNaN(toNumber("100.0 junk")), "100.0 junk"); 201assertTrue(isNaN(toNumber(".1e4 junk")), ".1e4 junk"); 202assertTrue(isNaN(toNumber("Infinity junk")), "Infinity junk"); 203assertTrue(isNaN(toNumber("1e")), "1e"); 204assertTrue(isNaN(toNumber("1e ")), "1e_"); 205assertTrue(isNaN(toNumber("1" + repeat('0', 1000) + 'junk')), "1e1000 junk"); 206 207for (var i = 1; i < 12; i++) { 208 assertEquals(toNumber('1' + repeat('0', i)), Math.pow(10.0, i)); 209} 210