-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathnum_parse_test.dart
234 lines (217 loc) · 6.19 KB
/
num_parse_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Formatting can break multitests, so don't format them.
// dart format off
import "package:expect/expect.dart";
const whiteSpace = const [
"",
"\x09",
"\x0a",
"\x0b",
"\x0c",
"\x0d",
"\x85",
"\xa0",
"\u1680",
"\u2000",
"\u2001",
"\u2002",
"\u2003",
"\u2004",
"\u2005",
"\u2006",
"\u2007",
"\u2008",
"\u2009",
"\u200a",
"\u2028",
"\u2029",
"\u202f",
"\u205f",
"\u3000",
"\uFEFF"
];
void expectNumEquals(num expect, num actual, String message) {
if (expect is double && expect.isNaN) {
Expect.isTrue(actual is double && actual.isNaN, "isNaN: $message");
} else {
Expect.identical(expect, actual, message);
}
}
// Test source surrounded by any combination of whitespace.
void testParseAllWhitespace(String source, num result) {
for (String ws1 in whiteSpace) {
for (String ws2 in whiteSpace) {
String padded = "$ws1$source$ws2";
// Use Expect.identical because it also handles NaN and 0.0/-0.0.
// Except on dart2js: http://dartbug.com/11551
expectNumEquals(result, num.parse(padded), "parse '$padded'");
padded = "$ws1$ws2$source";
expectNumEquals(result, num.parse(padded), "parse '$padded'");
padded = "$source$ws1$ws2";
expectNumEquals(result, num.parse(padded), "parse '$padded'");
}
}
}
// Test source and -source surrounded by any combination of whitespace.
void testParseWhitespace(String source, num result) {
assert(result >= 0);
testParseAllWhitespace(source, result);
testParseAllWhitespace("-$source", -result);
}
// Test parsing source, optionally preceded and/or followed by whitespace.
void testParse(String source, num result) {
expectNumEquals(result, num.parse(source), "parse '$source'");
expectNumEquals(result, num.parse(" $source"), "parse ' $source'");
expectNumEquals(result, num.parse("$source "), "parse '$source '");
expectNumEquals(result, num.parse(" $source "), "parse ' $source '");
}
// Test parsing an integer in decimal or hex format, with or without signs.
void testInt(int value) {
testParse("$value", value);
testParse("+$value", value);
testParse("-$value", -value);
var hex = "0x${value.toRadixString(16)}";
var lchex = hex.toLowerCase();
testParse(lchex, value);
testParse("+$lchex", value);
testParse("-$lchex", -value);
var uchex = hex.toUpperCase();
testParse(uchex, value);
testParse("+$uchex", value);
testParse("-$uchex", -value);
}
// Test parsing an integer, and the integers just around it.
void testIntAround(int value) {
testInt(value - 1);
testInt(value);
testInt(value + 1);
}
void testDouble(double value) {
testParse("$value", value);
testParse("+$value", value);
testParse("-$value", -value);
if (value.isFinite) {
String exp = value.toStringAsExponential();
String lcexp = exp.toLowerCase();
testParse(lcexp, value);
testParse("+$lcexp", value);
testParse("-$lcexp", -value);
String ucexp = exp.toUpperCase();
testParse(ucexp, value);
testParse("+$ucexp", value);
testParse("-$ucexp", -value);
}
}
void testFail(String source) {
var object = new Object();
Expect.throwsFormatException(() {
num.parse(source);
});
}
void main() {
testInt(0);
testInt(1);
testInt(9);
testInt(10);
testInt(99);
testInt(100);
testIntAround(256);
testIntAround(0x80000000); // 2^31
testIntAround(0x100000000); // 2^32
testIntAround(0x10000000000000); // 2^52
testIntAround(0x20000000000000); // 2^53
testIntAround(0x40000000000000); // 2^54
// 0x7ffffffffffffffe on int-is-64-bit implementations, rounded up on
// int-is-double implementations.
testIntAround(0x7ffffffffffff000 + 0xffe); // 2^63
testDouble(0.0);
testDouble(5e-324);
testDouble(2.225073858507201e-308);
testDouble(2.2250738585072014e-308);
testDouble(0.49999999999999994);
testDouble(0.5);
testDouble(0.50000000000000006);
testDouble(0.9999999999999999);
testDouble(1.0);
testDouble(1.0000000000000002);
testDouble(4294967295.0);
testDouble(4294967296.0);
testDouble(4503599627370495.5);
testDouble(4503599627370497.0);
testDouble(9007199254740991.0);
testDouble(9007199254740992.0);
testDouble(1.7976931348623157e+308);
testDouble(double.infinity);
testDouble(double.nan); // //# 01: ok
// Strings that cannot occur from toString of a number.
testParse("000000000000", 0);
testParse("000000000001", 1);
testParse("000000000000.0000000000000", 0.0);
testParse("000000000001.0000000000000", 1.0);
testParse("0x0000000000", 0);
testParse("0e0", 0.0);
testParse("0e+0", 0.0);
testParse("0e-0", 0.0);
testParse("-0e0", -0.0);
testParse("-0e+0", -0.0);
testParse("-0e-0", -0.0);
testParse("1e0", 1.0);
testParse("1e+0", 1.0);
testParse("1e-0", 1.0);
testParse("-1e0", -1.0);
testParse("-1e+0", -1.0);
testParse("-1e-0", -1.0);
testParse("1.", 1.0);
testParse(".1", 0.1);
testParse("1.e1", 10.0);
testParse(".1e1", 1.0);
testParseWhitespace("0x1", 1);
testParseWhitespace("1", 1);
testParseWhitespace("1.0", 1.0);
testParseWhitespace("1e1", 10.0);
testParseWhitespace(".1e1", 1.0);
testParseWhitespace("1.e1", 10.0);
testParseWhitespace("1e+1", 10.0);
testParseWhitespace("1e-1", 0.1);
// Negative tests - things not to allow.
// Spaces inside the numeral.
testFail("- 1");
testFail("+ 1");
testFail("2 2");
testFail("0x 42");
testFail("1 .");
testFail(". 1");
testFail("1e 2");
testFail("1 e2");
// Invalid characters.
testFail("0x1H");
testFail("12H");
testFail("1x2");
testFail("00x2");
testFail("0x2.2");
// Empty hex number.
testFail("0x");
testFail("-0x");
testFail("+0x");
// Double exponent without value.
testFail(".e1");
testFail("e1");
testFail("e+1");
testFail("e-1");
testFail("-e1");
testFail("-e+1");
testFail("-e-1");
// Incorrect ways to write NaN/Infinity.
testFail("infinity");
testFail("INFINITY");
testFail("1.#INF");
testFail("inf");
testFail("nan");
testFail("NAN");
testFail("1.#IND");
testFail("indef");
testFail("qnan");
testFail("snan");
}