OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "package:expect/expect.dart"; |
| 6 |
| 7 const whiteSpace = const [ |
| 8 "", |
| 9 "\x09", |
| 10 "\x0a", |
| 11 "\x0b", |
| 12 "\x0c", |
| 13 "\x0d", |
| 14 "\x85", |
| 15 "\xa0", |
| 16 "\u1680", |
| 17 "\u180e", |
| 18 "\u2000", |
| 19 "\u2001", |
| 20 "\u2002", |
| 21 "\u2003", |
| 22 "\u2004", |
| 23 "\u2005", |
| 24 "\u2006", |
| 25 "\u2007", |
| 26 "\u2008", |
| 27 "\u2009", |
| 28 "\u200a", |
| 29 "\u2028", |
| 30 "\u2029", |
| 31 "\u202f", |
| 32 "\u205f", |
| 33 "\u3000", |
| 34 "\uFEFF" |
| 35 ]; |
| 36 |
| 37 void testParse(String source, num result) { |
| 38 for (String ws1 in whiteSpace) { |
| 39 for (String ws2 in whiteSpace) { |
| 40 String padded = "$ws1$source$ws2"; |
| 41 // Use Expect.identical because it also handles NaN and 0.0/-0.0. |
| 42 // Except on dart2js: http://dartbug.com/11551 |
| 43 Expect.identical(result, num.parse(padded), "parse '$padded'"); |
| 44 padded = "$ws1$ws2$source"; |
| 45 Expect.identical(result, num.parse(padded), "parse '$padded'"); |
| 46 padded = "$source$ws1$ws2"; |
| 47 Expect.identical(result, num.parse(padded), "parse '$padded'"); |
| 48 } |
| 49 } |
| 50 } |
| 51 |
| 52 void testInt(int value) { |
| 53 testParse("$value", value); |
| 54 testParse("+$value", value); |
| 55 testParse("-$value", -value); |
| 56 var hex = "0x${value.toRadixString(16)}"; |
| 57 var lchex = hex.toLowerCase(); |
| 58 testParse(lchex, value); |
| 59 testParse("+$lchex", value); |
| 60 testParse("-$lchex", -value); |
| 61 var uchex = hex.toUpperCase(); |
| 62 testParse(uchex, value); |
| 63 testParse("+$uchex", value); |
| 64 testParse("-$uchex", -value); |
| 65 } |
| 66 |
| 67 void testIntAround(int value) { |
| 68 testInt(value - 1); |
| 69 testInt(value); |
| 70 testInt(value + 1); |
| 71 } |
| 72 |
| 73 void testDouble(double value) { |
| 74 testParse("$value", value); |
| 75 testParse("+$value", value); |
| 76 testParse("-$value", -value); |
| 77 if (value.isFinite) { |
| 78 String exp = value.toStringAsExponential(); |
| 79 String lcexp = exp.toLowerCase(); |
| 80 testParse(lcexp, value); |
| 81 testParse("+$lcexp", value); |
| 82 testParse("-$lcexp", -value); |
| 83 String ucexp = exp.toUpperCase(); |
| 84 testParse(ucexp, value); |
| 85 testParse("+$ucexp", value); |
| 86 testParse("-$ucexp", -value); |
| 87 } |
| 88 } |
| 89 |
| 90 void testFail(String source) { |
| 91 var object = new Object(); |
| 92 Expect.throws(() { |
| 93 num.parse(source, (s) { |
| 94 Expect.equals(source, s); |
| 95 throw object; |
| 96 }); |
| 97 }, (e) => identical(object, e), "Fail: '$source'"); |
| 98 } |
| 99 |
| 100 void main() { |
| 101 testInt(0); |
| 102 testInt(1); |
| 103 testInt(9); |
| 104 testInt(10); |
| 105 testInt(99); |
| 106 testInt(100); |
| 107 testIntAround(256); |
| 108 testIntAround(0x80000000); // 2^31 |
| 109 testIntAround(0x100000000); // 2^32 |
| 110 testIntAround(0x10000000000000); // 2^52 |
| 111 testIntAround(0x20000000000000); // 2^53 |
| 112 testIntAround(0x40000000000000); // 2^54 |
| 113 testIntAround(0x8000000000000000); // 2^63 |
| 114 testIntAround(0x10000000000000000); // 2^64 |
| 115 testIntAround(0x100000000000000000000); // 2^80 |
| 116 |
| 117 testDouble(0.0); |
| 118 testDouble(5e-324); |
| 119 testDouble(2.225073858507201e-308); |
| 120 testDouble(2.2250738585072014e-308); |
| 121 testDouble(0.49999999999999994); |
| 122 testDouble(0.5); |
| 123 testDouble(0.50000000000000006); |
| 124 testDouble(0.9999999999999999); |
| 125 testDouble(1.0); |
| 126 testDouble(1.0000000000000002); |
| 127 testDouble(4294967295.0); |
| 128 testDouble(4294967296.0); |
| 129 testDouble(4503599627370495.5); |
| 130 testDouble(4503599627370497.0); |
| 131 testDouble(9007199254740991.0); |
| 132 testDouble(9007199254740992.0); |
| 133 testDouble(1.7976931348623157e+308); |
| 134 testDouble(double.INFINITY); |
| 135 testDouble(double.NAN); /// 01: ok |
| 136 |
| 137 // Strings that cannot occur from toString of a number. |
| 138 testParse("000000000000", 0); |
| 139 testParse("000000000001", 1); |
| 140 testParse("000000000000.0000000000000", 0.0); |
| 141 testParse("000000000001.0000000000000", 1.0); |
| 142 testParse("0x0000000000", 0); |
| 143 testParse("0e0", 0.0); |
| 144 testParse("0e+0", 0.0); |
| 145 testParse("0e-0", 0.0); |
| 146 testParse("-0e0", -0.0); |
| 147 testParse("-0e+0", -0.0); |
| 148 testParse("-0e-0", -0.0); |
| 149 testParse("1e0", 1.0); |
| 150 testParse("1e+0", 1.0); |
| 151 testParse("1e-0", 1.0); |
| 152 testParse("-1e0", -1.0); |
| 153 testParse("-1e+0", -1.0); |
| 154 testParse("-1e-0", -1.0); |
| 155 testParse("1.", 1.0); |
| 156 testParse(".1", 0.1); |
| 157 testParse("1.e1", 10.0); |
| 158 testParse(".1e1", 1.0); |
| 159 |
| 160 // Negative tests - things not to allow. |
| 161 |
| 162 // Spaces inside the numeral. |
| 163 testFail("- 1"); |
| 164 testFail("+ 1"); |
| 165 testFail("2 2"); |
| 166 testFail("0x 42"); |
| 167 testFail("1 ."); |
| 168 testFail(". 1"); |
| 169 testFail("1e 2"); |
| 170 testFail("1 e2"); |
| 171 // Invalid characters. |
| 172 testFail("0x1H"); |
| 173 testFail("12H"); |
| 174 testFail("1x2"); |
| 175 testFail("00x2"); |
| 176 // Empty hex number. |
| 177 testFail("0x"); |
| 178 testFail("-0x"); |
| 179 testFail("+0x"); |
| 180 // Double exponent without value. |
| 181 testFail("e1"); |
| 182 testFail("e+1"); |
| 183 testFail("e-1"); |
| 184 testFail("-e1"); |
| 185 testFail("-e+1"); |
| 186 testFail("-e-1"); |
| 187 // Incorrect ways to write NaN/Infinity. |
| 188 testFail("infinity"); |
| 189 testFail("INFINITY"); |
| 190 testFail("inf"); |
| 191 testFail("nan"); |
| 192 testFail("NAN"); |
| 193 testFail("qnan"); |
| 194 testFail("snan"); |
| 195 } |
OLD | NEW |