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

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

Issue 84993004: Add more tests of num.parse. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove unnecessary {} Created 7 years 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import "package:expect/expect.dart"; 5 import "package:expect/expect.dart";
6 6
7 const whiteSpace = const [ 7 const whiteSpace = const [
8 "", 8 "",
9 "\x09", 9 "\x09",
10 "\x0a", 10 "\x0a",
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 padded = "$source$ws1$ws2"; 46 padded = "$source$ws1$ws2";
47 Expect.identical(result, num.parse(padded), "parse '$padded'"); 47 Expect.identical(result, num.parse(padded), "parse '$padded'");
48 } 48 }
49 } 49 }
50 } 50 }
51 51
52 void testInt(int value) { 52 void testInt(int value) {
53 testParse("$value", value); 53 testParse("$value", value);
54 testParse("+$value", value); 54 testParse("+$value", value);
55 testParse("-$value", -value); 55 testParse("-$value", -value);
56 testParse("0x${value.toRadixString(16)}", value); 56 var hex = "0x${value.toRadixString(16)}";
57 testParse("+0x${value.toRadixString(16)}", value); 57 var lchex = hex.toLowerCase();
58 testParse("-0x${value.toRadixString(16)}", -value); 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);
59 } 65 }
60 66
61 void testIntAround(int value) { 67 void testIntAround(int value) {
62 testInt(value - 1); 68 testInt(value - 1);
63 testInt(value); 69 testInt(value);
64 testInt(value + 1); 70 testInt(value + 1);
65 } 71 }
66 72
67 void testDouble(double value) { 73 void testDouble(double value) {
68 testParse("$value", value); 74 testParse("$value", value);
69 testParse("+$value", value); 75 testParse("+$value", value);
70 testParse("-$value", -value); 76 testParse("-$value", -value);
71 testParse("${value.toStringAsExponential()}", value); 77 if (value.isFinite) {
72 testParse("+${value.toStringAsExponential()}", value); 78 String exp = value.toStringAsExponential();
73 testParse("-${value.toStringAsExponential()}", -value); 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'");
74 } 98 }
75 99
76 void main() { 100 void main() {
77 testInt(0); 101 testInt(0);
78 testInt(1); 102 testInt(1);
79 testInt(9); 103 testInt(9);
80 testInt(10); 104 testInt(10);
81 testInt(99); 105 testInt(99);
82 testInt(100); 106 testInt(100);
83 testIntAround(256); 107 testIntAround(256);
(...skipping 18 matching lines...) Expand all
102 testDouble(1.0000000000000002); 126 testDouble(1.0000000000000002);
103 testDouble(4294967295.0); 127 testDouble(4294967295.0);
104 testDouble(4294967296.0); 128 testDouble(4294967296.0);
105 testDouble(4503599627370495.5); 129 testDouble(4503599627370495.5);
106 testDouble(4503599627370497.0); 130 testDouble(4503599627370497.0);
107 testDouble(9007199254740991.0); 131 testDouble(9007199254740991.0);
108 testDouble(9007199254740992.0); 132 testDouble(9007199254740992.0);
109 testDouble(1.7976931348623157e+308); 133 testDouble(1.7976931348623157e+308);
110 testDouble(double.INFINITY); 134 testDouble(double.INFINITY);
111 testDouble(double.NAN); /// 01: ok 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
156 // Negative tests - things not to allow.
157
158 // Spaces inside the numeral.
159 testFail("- 1");
160 testFail("+ 1");
161 testFail("2 2");
162 testFail("0x 42");
163 testFail("1 .");
164 testFail(". 1");
165 testFail("1e 2");
166 testFail("1 e2");
167 // Invalid characters.
168 testFail("0x1H");
169 testFail("12H");
170 testFail("1x2");
171 testFail("00x2");
172 // Empty hex number.
173 testFail("0x");
174 testFail("-0x");
175 testFail("+0x");
176 // Double exponent without value.
177 testFail("e1");
178 testFail("e+1");
179 testFail("e-1");
180 testFail("-e1");
181 testFail("-e+1");
182 testFail("-e-1");
183 // Incorrect ways to write NaN/Infinity.
184 testFail("infinity");
185 testFail("INFINITY");
186 testFail("inf");
187 testFail("nan");
188 testFail("NAN");
189 testFail("qnan");
190 testFail("snan");
112 } 191 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698