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 // We temporarily test both the new math library and the old Math | 5 // We temporarily test both the new math library and the old Math |
6 // class. This can easily be simplified once we get rid of the Math | 6 // class. This can easily be simplified once we get rid of the Math |
7 // class entirely. | 7 // class entirely. |
8 library math_parse_double_test; | 8 library math_parse_double_test; |
9 | 9 |
10 import 'dart:math'; | |
11 | |
12 void parseDoubleThrowsFormatException(str) { | 10 void parseDoubleThrowsFormatException(str) { |
13 Expect.throws(() => parseDouble(str), (e) => e is FormatException); | 11 Expect.throws(() => double.parse(str), (e) => e is FormatException); |
14 } | 12 } |
15 | 13 |
16 void main() { | 14 void main() { |
17 Expect.equals(499.0, parseDouble("499")); | 15 Expect.equals(499.0, double.parse("499")); |
18 Expect.equals(499.0, parseDouble("499.0")); | 16 Expect.equals(499.0, double.parse("499.0")); |
19 Expect.equals(499.0, parseDouble("499.0")); | 17 Expect.equals(499.0, double.parse("499.0")); |
20 Expect.equals(499.0, parseDouble("+499")); | 18 Expect.equals(499.0, double.parse("+499")); |
21 Expect.equals(-499.0, parseDouble("-499")); | 19 Expect.equals(-499.0, double.parse("-499")); |
22 Expect.equals(499.0, parseDouble(" 499 ")); | 20 Expect.equals(499.0, double.parse(" 499 ")); |
23 Expect.equals(499.0, parseDouble(" +499 ")); | 21 Expect.equals(499.0, double.parse(" +499 ")); |
24 Expect.equals(-499.0, parseDouble(" -499 ")); | 22 Expect.equals(-499.0, double.parse(" -499 ")); |
25 Expect.equals(0.0, parseDouble("0")); | 23 Expect.equals(0.0, double.parse("0")); |
26 Expect.equals(0.0, parseDouble("+0")); | 24 Expect.equals(0.0, double.parse("+0")); |
27 Expect.equals(-0.0, parseDouble("-0")); | 25 Expect.equals(-0.0, double.parse("-0")); |
28 Expect.equals(true, parseDouble("-0").isNegative); | 26 Expect.equals(true, double.parse("-0").isNegative); |
29 Expect.equals(0.0, parseDouble(" 0 ")); | 27 Expect.equals(0.0, double.parse(" 0 ")); |
30 Expect.equals(0.0, parseDouble(" +0 ")); | 28 Expect.equals(0.0, double.parse(" +0 ")); |
31 Expect.equals(-0.0, parseDouble(" -0 ")); | 29 Expect.equals(-0.0, double.parse(" -0 ")); |
32 Expect.equals(true, parseDouble(" -0 ").isNegative); | 30 Expect.equals(true, double.parse(" -0 ").isNegative); |
33 Expect.equals(1.0 * 0x1234567890, parseDouble("0x1234567890")); | 31 Expect.equals(1.0 * 0x1234567890, double.parse("0x1234567890")); |
34 Expect.equals(1.0 * -0x1234567890, parseDouble("-0x1234567890")); | 32 Expect.equals(1.0 * -0x1234567890, double.parse("-0x1234567890")); |
35 Expect.equals(1.0 * 0x1234567890, parseDouble(" 0x1234567890 ")); | 33 Expect.equals(1.0 * 0x1234567890, double.parse(" 0x1234567890 ")); |
36 Expect.equals(1.0 * -0x1234567890, parseDouble(" -0x1234567890 ")); | 34 Expect.equals(1.0 * -0x1234567890, double.parse(" -0x1234567890 ")); |
37 Expect.equals(256.0, parseDouble("0x100")); | 35 Expect.equals(256.0, double.parse("0x100")); |
38 Expect.equals(-256.0, parseDouble("-0x100")); | 36 Expect.equals(-256.0, double.parse("-0x100")); |
39 Expect.equals(256.0, parseDouble(" 0x100 ")); | 37 Expect.equals(256.0, double.parse(" 0x100 ")); |
40 Expect.equals(-256.0, parseDouble(" -0x100 ")); | 38 Expect.equals(-256.0, double.parse(" -0x100 ")); |
41 Expect.equals(1.0 * 0xabcdef, parseDouble("0xabcdef")); | 39 Expect.equals(1.0 * 0xabcdef, double.parse("0xabcdef")); |
42 Expect.equals(1.0 * 0xABCDEF, parseDouble("0xABCDEF")); | 40 Expect.equals(1.0 * 0xABCDEF, double.parse("0xABCDEF")); |
43 Expect.equals(1.0 * 0xabcdef, parseDouble("0xabCDEf")); | 41 Expect.equals(1.0 * 0xabcdef, double.parse("0xabCDEf")); |
44 Expect.equals(1.0 * -0xabcdef, parseDouble("-0xabcdef")); | 42 Expect.equals(1.0 * -0xabcdef, double.parse("-0xabcdef")); |
45 Expect.equals(1.0 * -0xABCDEF, parseDouble("-0xABCDEF")); | 43 Expect.equals(1.0 * -0xABCDEF, double.parse("-0xABCDEF")); |
46 Expect.equals(1.0 * 0xabcdef, parseDouble(" 0xabcdef ")); | 44 Expect.equals(1.0 * 0xabcdef, double.parse(" 0xabcdef ")); |
47 Expect.equals(1.0 * 0xABCDEF, parseDouble(" 0xABCDEF ")); | 45 Expect.equals(1.0 * 0xABCDEF, double.parse(" 0xABCDEF ")); |
48 Expect.equals(1.0 * -0xabcdef, parseDouble(" -0xabcdef ")); | 46 Expect.equals(1.0 * -0xabcdef, double.parse(" -0xabcdef ")); |
49 Expect.equals(1.0 * -0xABCDEF, parseDouble(" -0xABCDEF ")); | 47 Expect.equals(1.0 * -0xABCDEF, double.parse(" -0xABCDEF ")); |
50 Expect.equals(1.0 * 0xabcdef, parseDouble("0x00000abcdef")); | 48 Expect.equals(1.0 * 0xabcdef, double.parse("0x00000abcdef")); |
51 Expect.equals(1.0 * 0xABCDEF, parseDouble("0x00000ABCDEF")); | 49 Expect.equals(1.0 * 0xABCDEF, double.parse("0x00000ABCDEF")); |
52 Expect.equals(1.0 * -0xabcdef, parseDouble("-0x00000abcdef")); | 50 Expect.equals(1.0 * -0xabcdef, double.parse("-0x00000abcdef")); |
53 Expect.equals(1.0 * -0xABCDEF, parseDouble("-0x00000ABCDEF")); | 51 Expect.equals(1.0 * -0xABCDEF, double.parse("-0x00000ABCDEF")); |
54 Expect.equals(1.0 * 0xabcdef, parseDouble(" 0x00000abcdef ")); | 52 Expect.equals(1.0 * 0xabcdef, double.parse(" 0x00000abcdef ")); |
55 Expect.equals(1.0 * 0xABCDEF, parseDouble(" 0x00000ABCDEF ")); | 53 Expect.equals(1.0 * 0xABCDEF, double.parse(" 0x00000ABCDEF ")); |
56 Expect.equals(1.0 * -0xabcdef, parseDouble(" -0x00000abcdef ")); | 54 Expect.equals(1.0 * -0xabcdef, double.parse(" -0x00000abcdef ")); |
57 Expect.equals(1.0 * -0xABCDEF, parseDouble(" -0x00000ABCDEF ")); | 55 Expect.equals(1.0 * -0xABCDEF, double.parse(" -0x00000ABCDEF ")); |
58 Expect.equals(10.0, parseDouble("010")); | 56 Expect.equals(10.0, double.parse("010")); |
59 Expect.equals(-10.0, parseDouble("-010")); | 57 Expect.equals(-10.0, double.parse("-010")); |
60 Expect.equals(10.0, parseDouble(" 010 ")); | 58 Expect.equals(10.0, double.parse(" 010 ")); |
61 Expect.equals(-10.0, parseDouble(" -010 ")); | 59 Expect.equals(-10.0, double.parse(" -010 ")); |
62 Expect.equals(0.1, parseDouble("0.1")); | 60 Expect.equals(0.1, double.parse("0.1")); |
63 Expect.equals(0.1, parseDouble(" 0.1 ")); | 61 Expect.equals(0.1, double.parse(" 0.1 ")); |
64 Expect.equals(0.1, parseDouble(" +0.1 ")); | 62 Expect.equals(0.1, double.parse(" +0.1 ")); |
65 Expect.equals(-0.1, parseDouble(" -0.1 ")); | 63 Expect.equals(-0.1, double.parse(" -0.1 ")); |
66 Expect.equals(0.1, parseDouble(".1")); | 64 Expect.equals(0.1, double.parse(".1")); |
67 Expect.equals(0.1, parseDouble(" .1 ")); | 65 Expect.equals(0.1, double.parse(" .1 ")); |
68 Expect.equals(0.1, parseDouble(" +.1 ")); | 66 Expect.equals(0.1, double.parse(" +.1 ")); |
69 Expect.equals(-0.1, parseDouble(" -.1 ")); | 67 Expect.equals(-0.1, double.parse(" -.1 ")); |
70 Expect.equals(1.5, parseDouble("1.5")); | 68 Expect.equals(1.5, double.parse("1.5")); |
71 Expect.equals(1234567.89, parseDouble("1234567.89")); | 69 Expect.equals(1234567.89, double.parse("1234567.89")); |
72 Expect.equals(1234567.89, parseDouble(" 1234567.89 ")); | 70 Expect.equals(1234567.89, double.parse(" 1234567.89 ")); |
73 Expect.equals(1234567.89, parseDouble(" +1234567.89 ")); | 71 Expect.equals(1234567.89, double.parse(" +1234567.89 ")); |
74 Expect.equals(-1234567.89, parseDouble(" -1234567.89 ")); | 72 Expect.equals(-1234567.89, double.parse(" -1234567.89 ")); |
75 Expect.equals(1234567e89, parseDouble("1234567e89")); | 73 Expect.equals(1234567e89, double.parse("1234567e89")); |
76 Expect.equals(1234567e89, parseDouble(" 1234567e89 ")); | 74 Expect.equals(1234567e89, double.parse(" 1234567e89 ")); |
77 Expect.equals(1234567e89, parseDouble(" +1234567e89 ")); | 75 Expect.equals(1234567e89, double.parse(" +1234567e89 ")); |
78 Expect.equals(-1234567e89, parseDouble(" -1234567e89 ")); | 76 Expect.equals(-1234567e89, double.parse(" -1234567e89 ")); |
79 Expect.equals(1234567.89e2, parseDouble("1234567.89e2")); | 77 Expect.equals(1234567.89e2, double.parse("1234567.89e2")); |
80 Expect.equals(1234567.89e2, parseDouble(" 1234567.89e2 ")); | 78 Expect.equals(1234567.89e2, double.parse(" 1234567.89e2 ")); |
81 Expect.equals(1234567.89e2, parseDouble(" +1234567.89e2 ")); | 79 Expect.equals(1234567.89e2, double.parse(" +1234567.89e2 ")); |
82 Expect.equals(-1234567.89e2, parseDouble(" -1234567.89e2 ")); | 80 Expect.equals(-1234567.89e2, double.parse(" -1234567.89e2 ")); |
83 Expect.equals(1234567.89e2, parseDouble("1234567.89E2")); | 81 Expect.equals(1234567.89e2, double.parse("1234567.89E2")); |
84 Expect.equals(1234567.89e2, parseDouble(" 1234567.89E2 ")); | 82 Expect.equals(1234567.89e2, double.parse(" 1234567.89E2 ")); |
85 Expect.equals(1234567.89e2, parseDouble(" +1234567.89E2 ")); | 83 Expect.equals(1234567.89e2, double.parse(" +1234567.89E2 ")); |
86 Expect.equals(-1234567.89e2, parseDouble(" -1234567.89E2 ")); | 84 Expect.equals(-1234567.89e2, double.parse(" -1234567.89E2 ")); |
87 Expect.equals(1234567.89e-2, parseDouble("1234567.89e-2")); | 85 Expect.equals(1234567.89e-2, double.parse("1234567.89e-2")); |
88 Expect.equals(1234567.89e-2, parseDouble(" 1234567.89e-2 ")); | 86 Expect.equals(1234567.89e-2, double.parse(" 1234567.89e-2 ")); |
89 Expect.equals(1234567.89e-2, parseDouble(" +1234567.89e-2 ")); | 87 Expect.equals(1234567.89e-2, double.parse(" +1234567.89e-2 ")); |
90 Expect.equals(-1234567.89e-2, parseDouble(" -1234567.89e-2 ")); | 88 Expect.equals(-1234567.89e-2, double.parse(" -1234567.89e-2 ")); |
91 // TODO(floitsch): add tests for NaN and Infinity. | 89 // TODO(floitsch): add tests for NaN and Infinity. |
92 parseDoubleThrowsFormatException("1b"); | 90 parseDoubleThrowsFormatException("1b"); |
93 parseDoubleThrowsFormatException(" 1b "); | 91 parseDoubleThrowsFormatException(" 1b "); |
94 parseDoubleThrowsFormatException(" 1 b "); | 92 parseDoubleThrowsFormatException(" 1 b "); |
95 parseDoubleThrowsFormatException(" e3 "); | 93 parseDoubleThrowsFormatException(" e3 "); |
96 parseDoubleThrowsFormatException(" .e3 "); | 94 parseDoubleThrowsFormatException(" .e3 "); |
97 parseDoubleThrowsFormatException("00x12"); | 95 parseDoubleThrowsFormatException("00x12"); |
98 parseDoubleThrowsFormatException(" 00x12 "); | 96 parseDoubleThrowsFormatException(" 00x12 "); |
99 parseDoubleThrowsFormatException("-1b"); | 97 parseDoubleThrowsFormatException("-1b"); |
100 parseDoubleThrowsFormatException(" -1b "); | 98 parseDoubleThrowsFormatException(" -1b "); |
(...skipping 12 matching lines...) Expand all Loading... |
113 parseDoubleThrowsFormatException(" -5. "); | 111 parseDoubleThrowsFormatException(" -5. "); |
114 parseDoubleThrowsFormatException("1234567.e2"); | 112 parseDoubleThrowsFormatException("1234567.e2"); |
115 parseDoubleThrowsFormatException(" 1234567.e2 "); | 113 parseDoubleThrowsFormatException(" 1234567.e2 "); |
116 parseDoubleThrowsFormatException(" +1234567.e2 "); | 114 parseDoubleThrowsFormatException(" +1234567.e2 "); |
117 parseDoubleThrowsFormatException(" -1234567.e2 "); | 115 parseDoubleThrowsFormatException(" -1234567.e2 "); |
118 parseDoubleThrowsFormatException("+0x1234567890"); | 116 parseDoubleThrowsFormatException("+0x1234567890"); |
119 parseDoubleThrowsFormatException(" +0x1234567890 "); | 117 parseDoubleThrowsFormatException(" +0x1234567890 "); |
120 parseDoubleThrowsFormatException(" +0x100 "); | 118 parseDoubleThrowsFormatException(" +0x100 "); |
121 parseDoubleThrowsFormatException("+0x100"); | 119 parseDoubleThrowsFormatException("+0x100"); |
122 } | 120 } |
OLD | NEW |