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

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

Issue 2997533002: Migrate test block 10 to Dart 2.0. (Closed)
Patch Set: Fix remaining cases 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) 2012, 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 import "dart:math" show pow;
7
8 void main() {
9 const String oneByteWhiteSpace = "\x09\x0a\x0b\x0c\x0d\x20"
10 "\x85" // //# 01: ok
11 "\xa0";
12 const String whiteSpace = "$oneByteWhiteSpace\u1680"
13 "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a"
14 "\u2028\u2029\u202f\u205f\u3000\ufeff";
15
16 var digits = "0123456789abcdefghijklmnopqrstuvwxyz";
17 var zeros = "0" * 64;
18
19 for (int i = 0; i < whiteSpace.length; i++) {
20 var ws = whiteSpace[i];
21 Expect.equals(0, int.parse("${ws}0${ws}", radix: 2));
22 }
23
24 void testParse(int result, String radixString, int radix) {
25 var m = "$radixString/$radix->$result";
26 Expect.equals(
27 result, int.parse(radixString.toLowerCase(), radix: radix), m);
28 Expect.equals(
29 result, int.parse(radixString.toUpperCase(), radix: radix), m);
30 Expect.equals(result, int.parse(" $radixString", radix: radix), m);
31 Expect.equals(result, int.parse("$radixString ", radix: radix), m);
32 Expect.equals(result, int.parse(" $radixString ", radix: radix), m);
33 Expect.equals(result, int.parse("+$radixString", radix: radix), m);
34 Expect.equals(result, int.parse(" +$radixString", radix: radix), m);
35 Expect.equals(result, int.parse("+$radixString ", radix: radix), m);
36 Expect.equals(result, int.parse(" +$radixString ", radix: radix), m);
37 Expect.equals(-result, int.parse("-$radixString", radix: radix), m);
38 Expect.equals(-result, int.parse(" -$radixString", radix: radix), m);
39 Expect.equals(-result, int.parse("-$radixString ", radix: radix), m);
40 Expect.equals(-result, int.parse(" -$radixString ", radix: radix), m);
41 Expect.equals(
42 result,
43 int.parse("$oneByteWhiteSpace$radixString$oneByteWhiteSpace",
44 radix: radix),
45 m);
46 Expect.equals(
47 -result,
48 int.parse("$oneByteWhiteSpace-$radixString$oneByteWhiteSpace",
49 radix: radix),
50 m);
51 Expect.equals(result,
52 int.parse("$whiteSpace$radixString$whiteSpace", radix: radix), m);
53 Expect.equals(-result,
54 int.parse("$whiteSpace-$radixString$whiteSpace", radix: radix), m);
55
56 Expect.equals(result, int.parse("$zeros$radixString", radix: radix), m);
57 Expect.equals(result, int.parse("+$zeros$radixString", radix: radix), m);
58 Expect.equals(-result, int.parse("-$zeros$radixString", radix: radix), m);
59 }
60
61 for (int r = 2; r <= 36; r++) {
62 for (int i = 0; i <= r * r; i++) {
63 String radixString = i.toRadixString(r);
64 testParse(i, radixString, r);
65 }
66 }
67
68 for (int i = 2; i <= 36; i++) { // //# 02: ok
69 // Test with bignums. // //# 02: continued
70 var digit = digits[i - 1]; // //# 02: continued
71 testParse(pow(i, 64) - 1, digit * 64, i); // //# 02: continued
72 testParse(0, zeros, i); // //# 02: continued
73 } // //# 02: continued
74
75 // Allow both upper- and lower-case letters.
76 Expect.equals(0xABCD, int.parse("ABCD", radix: 16));
77 Expect.equals(0xABCD, int.parse("abcd", radix: 16));
78 Expect.equals(15628859, int.parse("09azAZ", radix: 36));
79 // Big number.
80 Expect.equals(0x12345678123456781234567812345678, // //# 02: continued
81 int.parse("0x1234567812345678" // //# 02: continued
82 "1234567812345678")); // //# 02: continued
83 // Allow whitespace before and after the number.
84 Expect.equals(1, int.parse(" 1", radix: 2));
85 Expect.equals(1, int.parse("1 ", radix: 2));
86 Expect.equals(1, int.parse(" 1 ", radix: 2));
87 Expect.equals(1, int.parse("\n1", radix: 2));
88 Expect.equals(1, int.parse("1\n", radix: 2));
89 Expect.equals(1, int.parse("\n1\n", radix: 2));
90 Expect.equals(1, int.parse("+1", radix: 2));
91
92 void testFails(String source, int radix) {
93 Expect.throws(() {
94 throw int.parse(source, radix: radix, onError: (s) {
95 throw "FAIL";
96 });
97 }, isFail, "$source/$radix");
98 Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999));
99 }
100
101 for (int i = 2; i < 36; i++) {
102 var char = i.toRadixString(36);
103 testFails(char.toLowerCase(), i);
104 testFails(char.toUpperCase(), i);
105 }
106 testFails("", 2);
107 testFails("+ 1", 2); // No space between sign and digits.
108 testFails("- 1", 2); // No space between sign and digits.
109 testFails("0x", null);
110 for (int i = 2; i <= 33; i++) {
111 // No 0x specially allowed.
112 // At radix 34 and above, "x" is a valid digit.
113 testFails("0x10", i);
114 }
115
116 testBadTypes(var source, var radix) {
117 if (!typeAssertionsEnabled) {
118 // No promises on what error is thrown if the type doesn't match.
119 // Likely either ArgumentError or NoSuchMethodError.
120 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0));
121 return;
122 }
123 // With type assertions enabled we can be more precise.
124 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0),
125 (e) => e is TypeError || e is CastError);
126 }
127
128 testBadTypes(9, 10);
129 testBadTypes(true, 10);
130 testBadTypes("0", true);
131 testBadTypes("0", "10");
132
133 testBadArguments(String source, int radix) {
134 // If the types match, it should be an ArgumentError of some sort.
135 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0),
136 (e) => e is ArgumentError);
137 }
138
139 testBadArguments("0", -1);
140 testBadArguments("0", 0);
141 testBadArguments("0", 1);
142 testBadArguments("0", 37);
143
144 // See also int_parse_radix_bad_handler_test.dart
145 }
146
147 bool isFail(e) => e == "FAIL";
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698