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