OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "dart:math" show pow; | 6 import "dart:math" show pow; |
7 | 7 |
8 void main() { | 8 void main() { |
9 bool checkedMode = false; | 9 bool checkedMode = false; |
10 assert(checkedMode = true); | 10 assert(checkedMode = true); |
11 const String oneByteWhiteSpace = "\x09\x0a\x0b\x0c\x0d\x20" | 11 const String oneByteWhiteSpace = "\x09\x0a\x0b\x0c\x0d\x20" |
12 "\x85" /// 01: ok | 12 "\x85" /// 01: ok |
13 "\xa0"; | 13 "\xa0"; |
14 const String whiteSpace = "$oneByteWhiteSpace\u1680\u180e" | 14 const String whiteSpace = "$oneByteWhiteSpace\u1680\u180e" |
15 "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a" | 15 "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a" |
16 "\u2028\u2029\u202f\u205f\u3000\ufeff"; | 16 "\u2028\u2029\u202f\u205f\u3000\ufeff"; |
17 | 17 |
18 var digits = "0123456789abcdefghijklmnopqrstuvwxyz"; | 18 var digits = "0123456789abcdefghijklmnopqrstuvwxyz"; |
19 var zeros = "0" * 64; | 19 var zeros = "0" * 64; |
20 | 20 |
21 for (int i = 0; i < whiteSpace.length; i++) { | 21 for (int i = 0; i < whiteSpace.length; i++) { |
22 var ws = whiteSpace[i]; | 22 var ws = whiteSpace[i]; |
23 print(ws.codeUnitAt(0).toRadixString(16)); | |
24 Expect.equals(0, int.parse("${ws}0${ws}", radix: 2)); | 23 Expect.equals(0, int.parse("${ws}0${ws}", radix: 2)); |
25 } | 24 } |
26 | 25 |
27 void testParse(int result, String radixString, int radix) { | 26 void testParse(int result, String radixString, int radix) { |
28 var m = "$radixString/$radix->$result"; | 27 var m = "$radixString/$radix->$result"; |
29 Expect.equals(result, | 28 Expect.equals(result, |
30 int.parse(radixString.toLowerCase(), radix: radix), m); | 29 int.parse(radixString.toLowerCase(), radix: radix), m); |
31 Expect.equals(result, | 30 Expect.equals(result, |
32 int.parse(radixString.toUpperCase(), radix: radix), m); | 31 int.parse(radixString.toUpperCase(), radix: radix), m); |
33 Expect.equals(result, int.parse(" $radixString", radix: radix), m); | 32 Expect.equals(result, int.parse(" $radixString", radix: radix), m); |
(...skipping 14 matching lines...) Expand all Loading... |
48 Expect.equals(result, int.parse( | 47 Expect.equals(result, int.parse( |
49 "$whiteSpace$radixString$whiteSpace", radix: radix), m); | 48 "$whiteSpace$radixString$whiteSpace", radix: radix), m); |
50 Expect.equals(-result, int.parse( | 49 Expect.equals(-result, int.parse( |
51 "$whiteSpace-$radixString$whiteSpace", radix: radix), m); | 50 "$whiteSpace-$radixString$whiteSpace", radix: radix), m); |
52 | 51 |
53 Expect.equals(result, int.parse("$zeros$radixString", radix: radix), m); | 52 Expect.equals(result, int.parse("$zeros$radixString", radix: radix), m); |
54 Expect.equals(result, int.parse("+$zeros$radixString", radix: radix), m); | 53 Expect.equals(result, int.parse("+$zeros$radixString", radix: radix), m); |
55 Expect.equals(-result, int.parse("-$zeros$radixString", radix: radix), m); | 54 Expect.equals(-result, int.parse("-$zeros$radixString", radix: radix), m); |
56 } | 55 } |
57 | 56 |
58 for (int i = 0; i <= 36 * 36; i++) { | 57 for (int r = 2; r <= 36; r++) { |
59 for (int r = 2; r <= 36; r++) { | 58 for (int i = 0; i <= r * r; i++) { |
60 String radixString = i.toRadixString(r); | 59 String radixString = i.toRadixString(r); |
61 testParse(i, radixString, r); | 60 testParse(i, radixString, r); |
62 } | 61 } |
63 } | 62 } |
64 | 63 |
65 for (int i = 2; i <= 36; i++) { /// 02: ok | 64 for (int i = 2; i <= 36; i++) { /// 02: ok |
66 // Test with bignums. /// 02: continued | 65 // Test with bignums. /// 02: continued |
67 var digit = digits[i - 1]; /// 02: continued | 66 var digit = digits[i - 1]; /// 02: continued |
68 testParse(pow(i, 64) - 1, digit * 64, i); /// 02: continued | 67 testParse(pow(i, 64) - 1, digit * 64, i); /// 02: continued |
69 testParse(0, zeros, i); /// 02: continued | 68 testParse(0, zeros, i); /// 02: continued |
(...skipping 12 matching lines...) Expand all Loading... |
82 Expect.equals(1, int.parse("1 ", radix: 2)); | 81 Expect.equals(1, int.parse("1 ", radix: 2)); |
83 Expect.equals(1, int.parse(" 1 ", radix: 2)); | 82 Expect.equals(1, int.parse(" 1 ", radix: 2)); |
84 Expect.equals(1, int.parse("\n1", radix: 2)); | 83 Expect.equals(1, int.parse("\n1", radix: 2)); |
85 Expect.equals(1, int.parse("1\n", radix: 2)); | 84 Expect.equals(1, int.parse("1\n", radix: 2)); |
86 Expect.equals(1, int.parse("\n1\n", radix: 2)); | 85 Expect.equals(1, int.parse("\n1\n", radix: 2)); |
87 Expect.equals(1, int.parse("+1", radix: 2)); | 86 Expect.equals(1, int.parse("+1", radix: 2)); |
88 | 87 |
89 void testFails(String source, int radix) { | 88 void testFails(String source, int radix) { |
90 Expect.throws(() { throw int.parse(source, radix: radix, | 89 Expect.throws(() { throw int.parse(source, radix: radix, |
91 onError: (s) { throw "FAIL"; }); }, | 90 onError: (s) { throw "FAIL"; }); }, |
92 (e) => e == "FAIL", | 91 isFail, |
93 "$source/$radix"); | 92 "$source/$radix"); |
94 Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999)); | 93 Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999)); |
95 } | 94 } |
96 for (int i = 2; i < 36; i++) { | 95 for (int i = 2; i < 36; i++) { |
97 testFails(i.toRadixString(36).toLowerCase(), i); | 96 var char = i.toRadixString(36); |
98 testFails(i.toRadixString(36).toUpperCase(), i); | 97 testFails(char.toLowerCase(), i); |
| 98 testFails(char.toUpperCase(), i); |
99 } | 99 } |
100 testFails("", 2); | 100 testFails("", 2); |
101 testFails("+ 1", 2); // No space between sign and digits. | 101 testFails("+ 1", 2); // No space between sign and digits. |
102 testFails("- 1", 2); // No space between sign and digits. | 102 testFails("- 1", 2); // No space between sign and digits. |
103 testFails("0x", null); | 103 testFails("0x", null); |
104 for (int i = 2; i <= 33; i++) { | 104 for (int i = 2; i <= 33; i++) { |
105 // No 0x specially allowed. | 105 // No 0x specially allowed. |
106 // At radix 34 and above, "x" is a valid digit. | 106 // At radix 34 and above, "x" is a valid digit. |
107 testFails("0x10", i); | 107 testFails("0x10", i); |
108 } | 108 } |
(...skipping 25 matching lines...) Expand all Loading... |
134 testBadArguments("0", 0); | 134 testBadArguments("0", 0); |
135 testBadArguments("0", 1); | 135 testBadArguments("0", 1); |
136 testBadArguments("0", 37); | 136 testBadArguments("0", 37); |
137 | 137 |
138 // If handleError isn't an unary function, and it's called, it also throws | 138 // If handleError isn't an unary function, and it's called, it also throws |
139 // (either TypeError in checked mode, or some failure in unchecked mode). | 139 // (either TypeError in checked mode, or some failure in unchecked mode). |
140 Expect.throws(() => int.parse("9", radix: 8, onError: "not a function")); | 140 Expect.throws(() => int.parse("9", radix: 8, onError: "not a function")); |
141 Expect.throws(() => int.parse("9", radix: 8, onError: () => 42)); | 141 Expect.throws(() => int.parse("9", radix: 8, onError: () => 42)); |
142 Expect.throws(() => int.parse("9", radix: 8, onError: (v1, v2) => 42)); | 142 Expect.throws(() => int.parse("9", radix: 8, onError: (v1, v2) => 42)); |
143 } | 143 } |
| 144 |
| 145 bool isFail(e) => e == "FAIL"; |
OLD | NEW |