| 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 \x85\xa0"; | 11 const String oneByteWhiteSpace = "\x09\x0a\x0b\x0c\x0d\x20" |
| 12 "\x85" /// 01: ok |
| 13 "\xa0"; |
| 12 const String whiteSpace = "$oneByteWhiteSpace\u1680\u180e" | 14 const String whiteSpace = "$oneByteWhiteSpace\u1680\u180e" |
| 13 "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a" | 15 "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a" |
| 14 "\u2028\u2029\u202f\u205f\u3000\ufeff"; | 16 "\u2028\u2029\u202f\u205f\u3000\ufeff"; |
| 15 | 17 |
| 16 var digits = "0123456789abcdefghijklmnopqrstuvwxyz"; | 18 var digits = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 17 var zeros = "0" * 64; | 19 var zeros = "0" * 64; |
| 20 |
| 21 for (int i = 0; i < whiteSpace.length; i++) { |
| 22 var ws = whiteSpace[i]; |
| 23 print(ws.codeUnitAt(0).toRadixString(16)); |
| 24 Expect.equals(0, int.parse("${ws}0${ws}", radix: 2)); |
| 25 } |
| 26 |
| 18 void testParse(int result, String radixString, int radix) { | 27 void testParse(int result, String radixString, int radix) { |
| 19 var m = "$radixString/$radix->$result"; | 28 var m = "$radixString/$radix->$result"; |
| 20 Expect.equals(result, | 29 Expect.equals(result, |
| 21 int.parse(radixString.toLowerCase(), radix: radix), m); | 30 int.parse(radixString.toLowerCase(), radix: radix), m); |
| 22 Expect.equals(result, | 31 Expect.equals(result, |
| 23 int.parse(radixString.toUpperCase(), radix: radix), m); | 32 int.parse(radixString.toUpperCase(), radix: radix), m); |
| 24 Expect.equals(result, int.parse(" $radixString", radix: radix), m); | 33 Expect.equals(result, int.parse(" $radixString", radix: radix), m); |
| 25 Expect.equals(result, int.parse("$radixString ", radix: radix), m); | 34 Expect.equals(result, int.parse("$radixString ", radix: radix), m); |
| 26 Expect.equals(result, int.parse(" $radixString ", radix: radix), m); | 35 Expect.equals(result, int.parse(" $radixString ", radix: radix), m); |
| 27 Expect.equals(result, int.parse("+$radixString", radix: radix), m); | 36 Expect.equals(result, int.parse("+$radixString", radix: radix), m); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 46 Expect.equals(-result, int.parse("-$zeros$radixString", radix: radix), m); | 55 Expect.equals(-result, int.parse("-$zeros$radixString", radix: radix), m); |
| 47 } | 56 } |
| 48 | 57 |
| 49 for (int i = 0; i <= 36 * 36; i++) { | 58 for (int i = 0; i <= 36 * 36; i++) { |
| 50 for (int r = 2; r <= 36; r++) { | 59 for (int r = 2; r <= 36; r++) { |
| 51 String radixString = i.toRadixString(r); | 60 String radixString = i.toRadixString(r); |
| 52 testParse(i, radixString, r); | 61 testParse(i, radixString, r); |
| 53 } | 62 } |
| 54 } | 63 } |
| 55 | 64 |
| 56 for (int i = 2; i <= 36; i++) { | 65 for (int i = 2; i <= 36; i++) { /// 02: ok |
| 57 // Test with bignums. | 66 // Test with bignums. /// 02: continued |
| 58 var digit = digits[i - 1]; | 67 var digit = digits[i - 1]; /// 02: continued |
| 59 testParse(pow(i, 64) - 1, digit * 64, i); | 68 testParse(pow(i, 64) - 1, digit * 64, i); /// 02: continued |
| 60 testParse(0, zeros, i); | 69 testParse(0, zeros, i); /// 02: continued |
| 61 } | 70 } /// 02: continued |
| 62 | 71 |
| 63 // Allow both upper- and lower-case letters. | 72 // Allow both upper- and lower-case letters. |
| 64 Expect.equals(0xABCD, int.parse("ABCD", radix: 16)); | 73 Expect.equals(0xABCD, int.parse("ABCD", radix: 16)); |
| 65 Expect.equals(0xABCD, int.parse("abcd", radix: 16)); | 74 Expect.equals(0xABCD, int.parse("abcd", radix: 16)); |
| 66 Expect.equals(15628859, int.parse("09azAZ", radix: 36)); | 75 Expect.equals(15628859, int.parse("09azAZ", radix: 36)); |
| 67 // Big number. | 76 // Big number. |
| 68 Expect.equals(0x12345678123456781234567812345678, | 77 Expect.equals(0x12345678123456781234567812345678, /// 02: continued |
| 69 int.parse("0x12345678123456781234567812345678")); | 78 int.parse("0x1234567812345678" /// 02: continued |
| 79 "1234567812345678")); /// 02: continued |
| 70 // Allow whitespace before and after the number. | 80 // Allow whitespace before and after the number. |
| 71 Expect.equals(1, int.parse(" 1", radix: 2)); | 81 Expect.equals(1, int.parse(" 1", radix: 2)); |
| 72 Expect.equals(1, int.parse("1 ", radix: 2)); | 82 Expect.equals(1, int.parse("1 ", radix: 2)); |
| 73 Expect.equals(1, int.parse(" 1 ", radix: 2)); | 83 Expect.equals(1, int.parse(" 1 ", radix: 2)); |
| 74 Expect.equals(1, int.parse("\n1", radix: 2)); | 84 Expect.equals(1, int.parse("\n1", radix: 2)); |
| 75 Expect.equals(1, int.parse("1\n", radix: 2)); | 85 Expect.equals(1, int.parse("1\n", radix: 2)); |
| 76 Expect.equals(1, int.parse("\n1\n", radix: 2)); | 86 Expect.equals(1, int.parse("\n1\n", radix: 2)); |
| 77 Expect.equals(1, int.parse("+1", radix: 2)); | 87 Expect.equals(1, int.parse("+1", radix: 2)); |
| 78 | 88 |
| 79 void testFails(String source, int radix) { | 89 void testFails(String source, int radix) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 testBadArguments("0", 0); | 133 testBadArguments("0", 0); |
| 124 testBadArguments("0", 1); | 134 testBadArguments("0", 1); |
| 125 testBadArguments("0", 37); | 135 testBadArguments("0", 37); |
| 126 | 136 |
| 127 // If handleError isn't an unary function, and it's called, it also throws | 137 // If handleError isn't an unary function, and it's called, it also throws |
| 128 // (either TypeError in checked mode, or some failure in unchecked mode). | 138 // (either TypeError in checked mode, or some failure in unchecked mode). |
| 129 Expect.throws(() => int.parse("9", radix: 8, onError: "not a function")); | 139 Expect.throws(() => int.parse("9", radix: 8, onError: "not a function")); |
| 130 Expect.throws(() => int.parse("9", radix: 8, onError: () => 42)); | 140 Expect.throws(() => int.parse("9", radix: 8, onError: () => 42)); |
| 131 Expect.throws(() => int.parse("9", radix: 8, onError: (v1, v2) => 42)); | 141 Expect.throws(() => int.parse("9", radix: 8, onError: (v1, v2) => 42)); |
| 132 } | 142 } |
| OLD | NEW |