OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "package:expect/expect.dart"; | |
6 | |
7 main() { | |
8 // Test that we accept radix 2 to 36 and that we use lower-case | |
9 // letters. | |
10 var expected = [ | |
11 '0', | |
12 '1', | |
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 ]; | |
48 for (var radix = 2; radix <= 36; radix++) { | |
49 for (var i = 0; i < radix; i++) { | |
50 Expect.equals(expected[i], i.toRadixString(radix)); | |
51 } | |
52 } | |
53 | |
54 var illegalRadices = [-1, 0, 1, 37]; | |
55 for (var radix in illegalRadices) { | |
56 try { | |
57 42.toRadixString(radix); | |
58 Expect.fail("Exception expected"); | |
59 } on ArgumentError catch (e) { | |
60 // Nothing to do. | |
61 } | |
62 } | |
63 | |
64 // Try large numbers (regression test for issue 15316). | |
65 var bignums = [ | |
66 0x80000000, | |
67 0x100000000, | |
68 0x10000000000000, | |
69 0x10000000000001, // 53 significant bits. | |
70 0x20000000000000, | |
71 0x20000000000002, | |
72 0x1000000000000000, | |
73 0x1000000000000100, | |
74 0x2000000000000000, | |
75 0x2000000000000200, | |
76 0x8000000000000000, | |
77 0x8000000000000800, | |
78 0x10000000000000000, | |
79 0x10000000000001000, | |
80 0x100000000000010000, | |
81 0x1000000000000100000, | |
82 0x10000000000001000000, | |
83 0x100000000000010000000, | |
84 0x1000000000000100000000, | |
85 0x10000000000001000000000, | |
86 ]; | |
87 for (var bignum in bignums) { | |
88 for (int radix = 2; radix <= 36; radix++) { | |
89 String digits = bignum.toRadixString(radix); | |
90 int result = int.parse(digits, radix: radix); | |
91 Expect.equals( | |
92 bignum, result, "${bignum.toRadixString(16)} -> $digits/$radix"); | |
93 } | |
94 } | |
95 } | |
OLD | NEW |