Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(770)

Side by Side Diff: tests/corelib/num_parse_test.dart

Issue 2987203002: Migrated test block 17 to Dart 2.0. (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "\u2000",
18 "\u2001",
19 "\u2002",
20 "\u2003",
21 "\u2004",
22 "\u2005",
23 "\u2006",
24 "\u2007",
25 "\u2008",
26 "\u2009",
27 "\u200a",
28 "\u2028",
29 "\u2029",
30 "\u202f",
31 "\u205f",
32 "\u3000",
33 "\uFEFF"
34 ];
35
36 void expectNumEquals(num expect, num actual, String message) {
37 if (expect is double && expect.isNaN) {
38 Expect.isTrue(actual is double && actual.isNaN, "isNaN: $message");
39 } else {
40 Expect.identical(expect, actual, message);
41 }
42 }
43
44 // Test source surrounded by any combination of whitespace.
45 void testParseAllWhitespace(String source, num result) {
46 for (String ws1 in whiteSpace) {
47 for (String ws2 in whiteSpace) {
48 String padded = "$ws1$source$ws2";
49 // Use Expect.identical because it also handles NaN and 0.0/-0.0.
50 // Except on dart2js: http://dartbug.com/11551
51 expectNumEquals(result, num.parse(padded), "parse '$padded'");
52 padded = "$ws1$ws2$source";
53 expectNumEquals(result, num.parse(padded), "parse '$padded'");
54 padded = "$source$ws1$ws2";
55 expectNumEquals(result, num.parse(padded), "parse '$padded'");
56 }
57 }
58 }
59
60 // Test source and -source surrounded by any combination of whitespace.
61 void testParseWhitespace(String source, num result) {
62 assert(result >= 0);
63 testParseAllWhitespace(source, result);
64 testParseAllWhitespace("-$source", -result);
65 }
66
67 // Test parsing source, optionally preceeded and/or followed by whitespace.
68 void testParse(String source, num result) {
69 expectNumEquals(result, num.parse(source), "parse '$source'");
70 expectNumEquals(result, num.parse(" $source"), "parse ' $source'");
71 expectNumEquals(result, num.parse("$source "), "parse '$source '");
72 expectNumEquals(result, num.parse(" $source "), "parse ' $source '");
73 }
74
75 // Test parsing an integer in decimal or hex format, with or without signs.
76 void testInt(int value) {
77 testParse("$value", value);
78 testParse("+$value", value);
79 testParse("-$value", -value);
80 var hex = "0x${value.toRadixString(16)}";
81 var lchex = hex.toLowerCase();
82 testParse(lchex, value);
83 testParse("+$lchex", value);
84 testParse("-$lchex", -value);
85 var uchex = hex.toUpperCase();
86 testParse(uchex, value);
87 testParse("+$uchex", value);
88 testParse("-$uchex", -value);
89 }
90
91 // Test parsing an integer, and the integers just around it.
92 void testIntAround(int value) {
93 testInt(value - 1);
94 testInt(value);
95 testInt(value + 1);
96 }
97
98 void testDouble(double value) {
99 testParse("$value", value);
100 testParse("+$value", value);
101 testParse("-$value", -value);
102 if (value.isFinite) {
103 String exp = value.toStringAsExponential();
104 String lcexp = exp.toLowerCase();
105 testParse(lcexp, value);
106 testParse("+$lcexp", value);
107 testParse("-$lcexp", -value);
108 String ucexp = exp.toUpperCase();
109 testParse(ucexp, value);
110 testParse("+$ucexp", value);
111 testParse("-$ucexp", -value);
112 }
113 }
114
115 void testFail(String source) {
116 var object = new Object();
117 Expect.throws(() {
118 num.parse(source, (s) {
119 Expect.equals(source, s);
120 throw object;
121 });
122 }, (e) => identical(object, e), "Fail: '$source'");
123 }
124
125 void main() {
126 testInt(0);
127 testInt(1);
128 testInt(9);
129 testInt(10);
130 testInt(99);
131 testInt(100);
132 testIntAround(256);
133 testIntAround(0x80000000); // 2^31
134 testIntAround(0x100000000); // 2^32
135 testIntAround(0x10000000000000); // 2^52
136 testIntAround(0x20000000000000); // 2^53
137 testIntAround(0x40000000000000); // 2^54
138 testIntAround(0x8000000000000000); // 2^63
139 testIntAround(0x10000000000000000); // 2^64
140 testIntAround(0x100000000000000000000); // 2^80
141
142 testDouble(0.0);
143 testDouble(5e-324);
144 testDouble(2.225073858507201e-308);
145 testDouble(2.2250738585072014e-308);
146 testDouble(0.49999999999999994);
147 testDouble(0.5);
148 testDouble(0.50000000000000006);
149 testDouble(0.9999999999999999);
150 testDouble(1.0);
151 testDouble(1.0000000000000002);
152 testDouble(4294967295.0);
153 testDouble(4294967296.0);
154 testDouble(4503599627370495.5);
155 testDouble(4503599627370497.0);
156 testDouble(9007199254740991.0);
157 testDouble(9007199254740992.0);
158 testDouble(1.7976931348623157e+308);
159 testDouble(double.INFINITY);
160 testDouble(double.NAN); // //# 01: ok
161
162 // Strings that cannot occur from toString of a number.
163 testParse("000000000000", 0);
164 testParse("000000000001", 1);
165 testParse("000000000000.0000000000000", 0.0);
166 testParse("000000000001.0000000000000", 1.0);
167 testParse("0x0000000000", 0);
168 testParse("0e0", 0.0);
169 testParse("0e+0", 0.0);
170 testParse("0e-0", 0.0);
171 testParse("-0e0", -0.0);
172 testParse("-0e+0", -0.0);
173 testParse("-0e-0", -0.0);
174 testParse("1e0", 1.0);
175 testParse("1e+0", 1.0);
176 testParse("1e-0", 1.0);
177 testParse("-1e0", -1.0);
178 testParse("-1e+0", -1.0);
179 testParse("-1e-0", -1.0);
180 testParse("1.", 1.0);
181 testParse(".1", 0.1);
182 testParse("1.e1", 10.0);
183 testParse(".1e1", 1.0);
184
185 testParseWhitespace("0x1", 1);
186 testParseWhitespace("1", 1);
187 testParseWhitespace("1.0", 1.0);
188 testParseWhitespace("1e1", 10.0);
189 testParseWhitespace(".1e1", 1.0);
190 testParseWhitespace("1.e1", 10.0);
191 testParseWhitespace("1e+1", 10.0);
192 testParseWhitespace("1e-1", 0.1);
193
194 // Negative tests - things not to allow.
195
196 // Spaces inside the numeral.
197 testFail("- 1");
198 testFail("+ 1");
199 testFail("2 2");
200 testFail("0x 42");
201 testFail("1 .");
202 testFail(". 1");
203 testFail("1e 2");
204 testFail("1 e2");
205 // Invalid characters.
206 testFail("0x1H");
207 testFail("12H");
208 testFail("1x2");
209 testFail("00x2");
210 testFail("0x2.2");
211 // Empty hex number.
212 testFail("0x");
213 testFail("-0x");
214 testFail("+0x");
215 // Double exponent without value.
216 testFail(".e1");
217 testFail("e1");
218 testFail("e+1");
219 testFail("e-1");
220 testFail("-e1");
221 testFail("-e+1");
222 testFail("-e-1");
223 // Incorrect ways to write NaN/Infinity.
224 testFail("infinity");
225 testFail("INFINITY");
226 testFail("1.#INF");
227 testFail("inf");
228 testFail("nan");
229 testFail("NAN");
230 testFail("1.#IND");
231 testFail("indef");
232 testFail("qnan");
233 testFail("snan");
234 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698