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