| 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" | 14 const String whiteSpace = "$oneByteWhiteSpace\u1680" |
| 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 Expect.equals(0, int.parse("${ws}0${ws}", radix: 2)); | 23 Expect.equals(0, int.parse("${ws}0${ws}", radix: 2)); |
| 24 } | 24 } |
| 25 | 25 |
| 26 void testParse(int result, String radixString, int radix) { | 26 void testParse(int result, String radixString, int radix) { |
| 27 var m = "$radixString/$radix->$result"; | 27 var m = "$radixString/$radix->$result"; |
| 28 Expect.equals(result, | 28 Expect.equals( |
| 29 int.parse(radixString.toLowerCase(), radix: radix), m); | 29 result, int.parse(radixString.toLowerCase(), radix: radix), m); |
| 30 Expect.equals(result, | 30 Expect.equals( |
| 31 int.parse(radixString.toUpperCase(), radix: radix), m); | 31 result, int.parse(radixString.toUpperCase(), radix: radix), m); |
| 32 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); | 33 Expect.equals(result, int.parse("$radixString ", radix: radix), m); |
| 34 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); | 35 Expect.equals(result, int.parse("+$radixString", radix: radix), m); |
| 36 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); | 37 Expect.equals(result, int.parse("+$radixString ", radix: radix), m); |
| 38 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); | 39 Expect.equals(-result, int.parse("-$radixString", radix: radix), m); |
| 40 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); | 41 Expect.equals(-result, int.parse("-$radixString ", radix: radix), m); |
| 42 Expect.equals(-result, int.parse(" -$radixString ", radix: radix), m); | 42 Expect.equals(-result, int.parse(" -$radixString ", radix: radix), m); |
| 43 Expect.equals(result, int.parse( | 43 Expect.equals( |
| 44 "$oneByteWhiteSpace$radixString$oneByteWhiteSpace", radix: radix), m); | 44 result, |
| 45 Expect.equals(-result, int.parse( | 45 int.parse("$oneByteWhiteSpace$radixString$oneByteWhiteSpace", |
| 46 "$oneByteWhiteSpace-$radixString$oneByteWhiteSpace", radix: radix), m); | 46 radix: radix), |
| 47 Expect.equals(result, int.parse( | 47 m); |
| 48 "$whiteSpace$radixString$whiteSpace", radix: radix), m); | 48 Expect.equals( |
| 49 Expect.equals(-result, int.parse( | 49 -result, |
| 50 "$whiteSpace-$radixString$whiteSpace", radix: radix), m); | 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); |
| 51 | 57 |
| 52 Expect.equals(result, int.parse("$zeros$radixString", radix: radix), m); | 58 Expect.equals(result, int.parse("$zeros$radixString", radix: radix), m); |
| 53 Expect.equals(result, int.parse("+$zeros$radixString", radix: radix), m); | 59 Expect.equals(result, int.parse("+$zeros$radixString", radix: radix), m); |
| 54 Expect.equals(-result, int.parse("-$zeros$radixString", radix: radix), m); | 60 Expect.equals(-result, int.parse("-$zeros$radixString", radix: radix), m); |
| 55 } | 61 } |
| 56 | 62 |
| 57 for (int r = 2; r <= 36; r++) { | 63 for (int r = 2; r <= 36; r++) { |
| 58 for (int i = 0; i <= r * r; i++) { | 64 for (int i = 0; i <= r * r; i++) { |
| 59 String radixString = i.toRadixString(r); | 65 String radixString = i.toRadixString(r); |
| 60 testParse(i, radixString, r); | 66 testParse(i, radixString, r); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 79 // Allow whitespace before and after the number. | 85 // Allow whitespace before and after the number. |
| 80 Expect.equals(1, int.parse(" 1", radix: 2)); | 86 Expect.equals(1, int.parse(" 1", radix: 2)); |
| 81 Expect.equals(1, int.parse("1 ", radix: 2)); | 87 Expect.equals(1, int.parse("1 ", radix: 2)); |
| 82 Expect.equals(1, int.parse(" 1 ", radix: 2)); | 88 Expect.equals(1, int.parse(" 1 ", radix: 2)); |
| 83 Expect.equals(1, int.parse("\n1", radix: 2)); | 89 Expect.equals(1, int.parse("\n1", radix: 2)); |
| 84 Expect.equals(1, int.parse("1\n", radix: 2)); | 90 Expect.equals(1, int.parse("1\n", radix: 2)); |
| 85 Expect.equals(1, int.parse("\n1\n", radix: 2)); | 91 Expect.equals(1, int.parse("\n1\n", radix: 2)); |
| 86 Expect.equals(1, int.parse("+1", radix: 2)); | 92 Expect.equals(1, int.parse("+1", radix: 2)); |
| 87 | 93 |
| 88 void testFails(String source, int radix) { | 94 void testFails(String source, int radix) { |
| 89 Expect.throws(() { throw int.parse(source, radix: radix, | 95 Expect.throws(() { |
| 90 onError: (s) { throw "FAIL"; }); }, | 96 throw int.parse(source, radix: radix, onError: (s) { |
| 91 isFail, | 97 throw "FAIL"; |
| 92 "$source/$radix"); | 98 }); |
| 99 }, isFail, "$source/$radix"); |
| 93 Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999)); | 100 Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999)); |
| 94 } | 101 } |
| 102 |
| 95 for (int i = 2; i < 36; i++) { | 103 for (int i = 2; i < 36; i++) { |
| 96 var char = i.toRadixString(36); | 104 var char = i.toRadixString(36); |
| 97 testFails(char.toLowerCase(), i); | 105 testFails(char.toLowerCase(), i); |
| 98 testFails(char.toUpperCase(), i); | 106 testFails(char.toUpperCase(), i); |
| 99 } | 107 } |
| 100 testFails("", 2); | 108 testFails("", 2); |
| 101 testFails("+ 1", 2); // No space between sign and digits. | 109 testFails("+ 1", 2); // No space between sign and digits. |
| 102 testFails("- 1", 2); // No space between sign and digits. | 110 testFails("- 1", 2); // No space between sign and digits. |
| 103 testFails("0x", null); | 111 testFails("0x", null); |
| 104 for (int i = 2; i <= 33; i++) { | 112 for (int i = 2; i <= 33; i++) { |
| 105 // No 0x specially allowed. | 113 // No 0x specially allowed. |
| 106 // At radix 34 and above, "x" is a valid digit. | 114 // At radix 34 and above, "x" is a valid digit. |
| 107 testFails("0x10", i); | 115 testFails("0x10", i); |
| 108 } | 116 } |
| 109 | 117 |
| 110 testBadTypes(var source, var radix) { | 118 testBadTypes(var source, var radix) { |
| 111 if (!checkedMode) { | 119 if (!checkedMode) { |
| 112 // No promises on what error is thrown if the type doesn't match. | 120 // No promises on what error is thrown if the type doesn't match. |
| 113 // Likely either ArgumentError or NoSuchMethodError. | 121 // Likely either ArgumentError or NoSuchMethodError. |
| 114 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0)); | 122 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0)); |
| 115 return; | 123 return; |
| 116 } | 124 } |
| 117 // In checked mode, it's always a TypeError. | 125 // In checked mode, it's always a TypeError. |
| 118 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0), | 126 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0), |
| 119 (e) => e is TypeError || e is CastError); | 127 (e) => e is TypeError || e is CastError); |
| 120 } | 128 } |
| 121 | 129 |
| 122 testBadTypes(9, 10); | 130 testBadTypes(9, 10); |
| 123 testBadTypes(true, 10); | 131 testBadTypes(true, 10); |
| 124 testBadTypes("0", true); | 132 testBadTypes("0", true); |
| 125 testBadTypes("0", "10"); | 133 testBadTypes("0", "10"); |
| 126 | 134 |
| 127 testBadArguments(String source, int radix) { | 135 testBadArguments(String source, int radix) { |
| 128 // If the types match, it should be an ArgumentError of some sort. | 136 // If the types match, it should be an ArgumentError of some sort. |
| 129 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0), | 137 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0), |
| 130 (e) => e is ArgumentError); | 138 (e) => e is ArgumentError); |
| 131 } | 139 } |
| 132 | 140 |
| 133 testBadArguments("0", -1); | 141 testBadArguments("0", -1); |
| 134 testBadArguments("0", 0); | 142 testBadArguments("0", 0); |
| 135 testBadArguments("0", 1); | 143 testBadArguments("0", 1); |
| 136 testBadArguments("0", 37); | 144 testBadArguments("0", 37); |
| 137 | 145 |
| 138 // See also int_parse_radix_bad_handler_test.dart | 146 // See also int_parse_radix_bad_handler_test.dart |
| 139 } | 147 } |
| 140 | 148 |
| 141 bool isFail(e) => e == "FAIL"; | 149 bool isFail(e) => e == "FAIL"; |
| OLD | NEW |