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 | 6 |
7 main() { | 7 main() { |
8 // Test that we accept radix 2 to 36 and that we use lower-case | 8 // Test that we accept radix 2 to 36 and that we use lower-case |
9 // letters. | 9 // letters. |
10 var expected = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | 10 var expected = [ |
11 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', | 11 '0', |
12 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', | 12 '1', |
13 'u', 'v', 'w', 'x', 'y', 'z']; | 13 '2', |
| 14 '3', |
| 15 '4', |
| 16 '5', |
| 17 '6', |
| 18 '7', |
| 19 '8', |
| 20 '9', |
| 21 'a', |
| 22 'b', |
| 23 'c', |
| 24 'd', |
| 25 'e', |
| 26 'f', |
| 27 'g', |
| 28 'h', |
| 29 'i', |
| 30 'j', |
| 31 'k', |
| 32 'l', |
| 33 'm', |
| 34 'n', |
| 35 'o', |
| 36 'p', |
| 37 'q', |
| 38 'r', |
| 39 's', |
| 40 't', |
| 41 'u', |
| 42 'v', |
| 43 'w', |
| 44 'x', |
| 45 'y', |
| 46 'z' |
| 47 ]; |
14 for (var radix = 2; radix <= 36; radix++) { | 48 for (var radix = 2; radix <= 36; radix++) { |
15 for (var i = 0; i < radix; i++) { | 49 for (var i = 0; i < radix; i++) { |
16 Expect.equals(expected[i], i.toRadixString(radix)); | 50 Expect.equals(expected[i], i.toRadixString(radix)); |
17 } | 51 } |
18 } | 52 } |
19 | 53 |
20 var illegalRadices = [ -1, 0, 1, 37 ]; | 54 var illegalRadices = [-1, 0, 1, 37]; |
21 for (var radix in illegalRadices) { | 55 for (var radix in illegalRadices) { |
22 try { | 56 try { |
23 42.toRadixString(radix); | 57 42.toRadixString(radix); |
24 Expect.fail("Exception expected"); | 58 Expect.fail("Exception expected"); |
25 } on ArgumentError catch (e) { | 59 } on ArgumentError catch (e) { |
26 // Nothing to do. | 60 // Nothing to do. |
27 } | 61 } |
28 } | 62 } |
29 | 63 |
30 // Try large numbers (regression test for issue 15316). | 64 // Try large numbers (regression test for issue 15316). |
31 var bignums = [ | 65 var bignums = [ |
32 0x80000000, | 66 0x80000000, |
33 0x100000000, | 67 0x100000000, |
34 0x10000000000000, | 68 0x10000000000000, |
35 0x10000000000001, // 53 significant bits. | 69 0x10000000000001, // 53 significant bits. |
36 0x20000000000000, | 70 0x20000000000000, |
37 0x20000000000002, | 71 0x20000000000002, |
38 0x1000000000000000, | 72 0x1000000000000000, |
39 0x1000000000000100, | 73 0x1000000000000100, |
40 0x2000000000000000, | 74 0x2000000000000000, |
41 0x2000000000000200, | 75 0x2000000000000200, |
42 0x8000000000000000, | 76 0x8000000000000000, |
43 0x8000000000000800, | 77 0x8000000000000800, |
44 0x10000000000000000, | 78 0x10000000000000000, |
45 0x10000000000001000, | 79 0x10000000000001000, |
46 0x100000000000010000, | 80 0x100000000000010000, |
47 0x1000000000000100000, | 81 0x1000000000000100000, |
48 0x10000000000001000000, | 82 0x10000000000001000000, |
49 0x100000000000010000000, | 83 0x100000000000010000000, |
50 0x1000000000000100000000, | 84 0x1000000000000100000000, |
51 0x10000000000001000000000, | 85 0x10000000000001000000000, |
52 ]; | 86 ]; |
53 for (var bignum in bignums) { | 87 for (var bignum in bignums) { |
54 for (int radix = 2; radix <= 36; radix++) { | 88 for (int radix = 2; radix <= 36; radix++) { |
55 String digits = bignum.toRadixString(radix); | 89 String digits = bignum.toRadixString(radix); |
56 int result = int.parse(digits, radix: radix); | 90 int result = int.parse(digits, radix: radix); |
57 Expect.equals(bignum, result, | 91 Expect.equals( |
58 "${bignum.toRadixString(16)} -> $digits/$radix"); | 92 bignum, result, "${bignum.toRadixString(16)} -> $digits/$radix"); |
59 } | 93 } |
60 } | 94 } |
61 } | 95 } |
OLD | NEW |