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