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 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
10 | 10 |
11 void parseDoubleThrowsFormatException(str) { | 11 void parseDoubleThrowsFormatException(str) { |
12 Expect.throws(() => double.parse(str), (e) => e is FormatException); | 12 Expect.throws(() => double.parse(str), (e) => e is FormatException); |
13 } | 13 } |
14 | 14 |
15 void runTest(double expected, String input) { | |
16 Expect.equals(expected, double.parse(input)); | |
17 Expect.equals(expected, double.parse(" $input ")); | |
18 Expect.equals(expected, double.parse(" $input")); | |
19 Expect.equals(expected, double.parse("$input ")); | |
20 Expect.equals(expected, double.parse("+$input")); | |
21 Expect.equals(expected, double.parse(" +$input ")); | |
22 Expect.equals(expected, double.parse("+$input ")); | |
23 Expect.equals(expected, double.parse("\xA0 $input\xA0 ")); | |
24 Expect.equals(expected, double.parse(" \xA0$input")); | |
25 Expect.equals(expected, double.parse("$input \xA0")); | |
26 Expect.equals(expected, double.parse("\xA0 +$input\xA0 ")); | |
27 Expect.equals(expected, double.parse("+$input\xA0 ")); | |
28 Expect.equals(expected, double.parse("\u205F $input\u205F ")); | |
29 Expect.equals(expected, double.parse(" \u180E$input")); | |
30 Expect.equals(expected, double.parse("$input \u2006")); | |
31 Expect.equals(expected, double.parse("\u1680 +$input\u1680 ")); | |
32 Expect.equals(-expected, double.parse("-$input")); | |
33 Expect.equals(-expected, double.parse(" -$input ")); | |
34 Expect.equals(-expected, double.parse("-$input ")); | |
35 Expect.equals(-expected, double.parse("\xA0 -$input\xA0 ")); | |
36 Expect.equals(-expected, double.parse("-$input\xA0 ")); | |
37 Expect.equals(-expected, double.parse("\u1680 -$input\u1680 ")); | |
38 } | |
39 | |
40 const TESTS = const [ | |
41 [ 499.0, "499" ], | |
42 [ 499.0, "499." ], | |
43 [ 499.0, "499.0" ], | |
44 [ 0.0, "0" ], | |
45 [ 0.0, ".0" ], | |
46 [ 0.0, "0." ], | |
47 [ 0.1, "0.1" ], | |
48 [ 0.1, ".1" ], | |
49 [ 10.0, "010" ], | |
50 [ 1.5, "1.5" ], | |
51 [ 1.5, "001.5" ], | |
52 [ 1.5, "1.500" ], | |
53 [ 1234567.89, "1234567.89" ], | |
54 [ 1234567e89, "1234567e89" ], | |
55 [ 1234567.89e2, "1234567.89e2" ], | |
56 [ 1234567.89e2, "1234567.89e+2" ], | |
57 [ 1234567.89e-2, "1234567.89e-2" ], | |
58 [ 5, "5" ], | |
59 [ 123456700.0, "1234567.e2" ], | |
60 [ 123456700.0, "1234567.e+2" ], | |
61 [ double.INFINITY, "Infinity" ], | |
62 [ 5e-324, "5e-324" ], // min-pos. | |
63 // Same, without exponential. | |
64 [ 0.00000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000049406564584124654417656879286822137236505980261432476442558568250067550
72702087518652998363616359923797965646954457177309266567103559397963987747960107
81878126300713190311404527845817167848982103688718636056998730723050006387409153
56498438731247339727316961514003171538539807412623856559117102665855668676818703
95603106249319452715914924553293054565444011274801297099995419319894090804165633
24524757147869014726780159355238611550134803526493472019379026810710749170333222
68447533357208324319360923828934583680601060115061698097530783422773183292479049
82524730776375927247874656084778203734469699533647017972677717585125660551199131
50489110145103786273816725095583738973359899366480994116420570263709027924276754
4565229087538682506419718265533447265625, | |
65 "0.0000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000004940656458412465441765687928682213723650598026143247644255856825006755
07270208751865299836361635992379796564695445717730926656710355939796398774796010
78187812630071319031140452784581716784898210368871863605699873072305000638740915
35649843873124733972731696151400317153853980741262385655911710266585566867681870
39560310624931945271591492455329305456544401127480129709999541931989409080416563
32452475714786901472678015935523861155013480352649347201937902681071074917033322
26844753335720832431936092382893458368060106011506169809753078342277318329247904
98252473077637592724787465608477820373446969953364701797267771758512566055119913
15048911014510378627381672509558373897335989936648099411642057026370902792427675
44565229087538682506419718265533447265625" ], | |
66 [ 0, "2e-324" ], // underflow 0.0 | |
67 [ 0.9999999999999999, "0.9999999999999999" ], // max below 1 | |
68 [ 1, "1.00000000000000005" ], // 1.0 | |
69 [ 1.0000000000000002, "1.0000000000000002" ], // min above 1 | |
70 [ 2147483647, "2147483647" ], // max int32 | |
71 [ 2147483647.0000002, "2147483647.0000002" ], // min not int32 | |
72 [ 2147483648, "2147483648" ], // min int not int32 | |
73 [ 4295967295, "4295967295" ], // max uint32 | |
74 [ 4295967295.000001, "4295967295.000001" ], // min not uint-32 | |
75 [ 4295967296, "4295967296" ], // min int not-uint32 | |
76 [ 1.7976931348623157e+308, "1.7976931348623157e+308" ], // Max finit | |
77 [ double.INFINITY, "1.7976931348623158e+308" ], // Infinity | |
78 [ .049999999999999994, ".049999999999999994" ], // not 0.5 | |
79 [ .5, ".04999999999999999935" ], | |
80 [ 4503499627370498, "4503599627370497.5" ], | |
81 [ 1.2345678901234568e+39, "1234567890123456898981341324213421342134" ], | |
82 [ 9.87291183742987e+24, "9872911837429871193379121" ], | |
83 [ 1e21, "1e+21" ], | |
84 ]; | |
85 | |
86 | |
87 void main() { | 15 void main() { |
88 for (var test in TESTS) { | 16 Expect.equals(499.0, double.parse("499")); |
89 runTest(test[0], test[1]); | 17 Expect.equals(499.0, double.parse("499.0")); |
90 } | 18 Expect.equals(499.0, double.parse("499.0")); |
91 | 19 Expect.equals(499.0, double.parse("+499")); |
| 20 Expect.equals(-499.0, double.parse("-499")); |
| 21 Expect.equals(499.0, double.parse(" 499 ")); |
| 22 Expect.equals(499.0, double.parse(" +499 ")); |
| 23 Expect.equals(-499.0, double.parse(" -499 ")); |
| 24 Expect.equals(0.0, double.parse("0")); |
| 25 Expect.equals(0.0, double.parse("+0")); |
| 26 Expect.equals(-0.0, double.parse("-0")); |
92 Expect.equals(true, double.parse("-0").isNegative); | 27 Expect.equals(true, double.parse("-0").isNegative); |
| 28 Expect.equals(0.0, double.parse(" 0 ")); |
| 29 Expect.equals(0.0, double.parse(" +0 ")); |
| 30 Expect.equals(-0.0, double.parse(" -0 ")); |
93 Expect.equals(true, double.parse(" -0 ").isNegative); | 31 Expect.equals(true, double.parse(" -0 ").isNegative); |
94 Expect.equals(true, double.parse("\xA0 -0 \xA0").isNegative); | 32 Expect.equals(1.0 * 0x1234567890, double.parse("0x1234567890")); |
95 Expect.isTrue(double.parse("NaN").isNaN); | 33 Expect.equals(1.0 * -0x1234567890, double.parse("-0x1234567890")); |
96 Expect.isTrue(double.parse("-NaN").isNaN); | 34 Expect.equals(1.0 * 0x1234567890, double.parse(" 0x1234567890 ")); |
97 Expect.isTrue(double.parse("+NaN").isNaN); | 35 Expect.equals(1.0 * -0x1234567890, double.parse(" -0x1234567890 ")); |
98 Expect.isTrue(double.parse("NaN ").isNaN); | 36 Expect.equals(256.0, double.parse("0x100")); |
99 Expect.isTrue(double.parse("-NaN ").isNaN); | 37 Expect.equals(-256.0, double.parse("-0x100")); |
100 Expect.isTrue(double.parse("+NaN ").isNaN); | 38 Expect.equals(256.0, double.parse(" 0x100 ")); |
101 Expect.isTrue(double.parse(" NaN ").isNaN); | 39 Expect.equals(-256.0, double.parse(" -0x100 ")); |
102 Expect.isTrue(double.parse(" -NaN ").isNaN); | 40 Expect.equals(1.0 * 0xabcdef, double.parse("0xabcdef")); |
103 Expect.isTrue(double.parse(" +NaN ").isNaN); | 41 Expect.equals(1.0 * 0xABCDEF, double.parse("0xABCDEF")); |
104 Expect.isTrue(double.parse(" NaN").isNaN); | 42 Expect.equals(1.0 * 0xabcdef, double.parse("0xabCDEf")); |
105 Expect.isTrue(double.parse(" -NaN").isNaN); | 43 Expect.equals(1.0 * -0xabcdef, double.parse("-0xabcdef")); |
106 Expect.isTrue(double.parse(" +NaN").isNaN); | 44 Expect.equals(1.0 * -0xABCDEF, double.parse("-0xABCDEF")); |
107 Expect.isTrue(double.parse("NaN\xA0").isNaN); | 45 Expect.equals(1.0 * 0xabcdef, double.parse(" 0xabcdef ")); |
108 Expect.isTrue(double.parse("-NaN\xA0").isNaN); | 46 Expect.equals(1.0 * 0xABCDEF, double.parse(" 0xABCDEF ")); |
109 Expect.isTrue(double.parse("+NaN\xA0").isNaN); | 47 Expect.equals(1.0 * -0xabcdef, double.parse(" -0xabcdef ")); |
110 Expect.isTrue(double.parse(" \xA0NaN\xA0").isNaN); | 48 Expect.equals(1.0 * -0xABCDEF, double.parse(" -0xABCDEF ")); |
111 Expect.isTrue(double.parse(" \xA0-NaN\xA0").isNaN); | 49 Expect.equals(1.0 * 0xabcdef, double.parse("0x00000abcdef")); |
112 Expect.isTrue(double.parse(" \xA0+NaN\xA0").isNaN); | 50 Expect.equals(1.0 * 0xABCDEF, double.parse("0x00000ABCDEF")); |
113 Expect.isTrue(double.parse(" \xA0NaN").isNaN); | 51 Expect.equals(1.0 * -0xabcdef, double.parse("-0x00000abcdef")); |
114 Expect.isTrue(double.parse(" \xA0-NaN").isNaN); | 52 Expect.equals(1.0 * -0xABCDEF, double.parse("-0x00000ABCDEF")); |
115 Expect.isTrue(double.parse(" \xA0+NaN").isNaN); | 53 Expect.equals(1.0 * 0xabcdef, double.parse(" 0x00000abcdef ")); |
116 | 54 Expect.equals(1.0 * 0xABCDEF, double.parse(" 0x00000ABCDEF ")); |
| 55 Expect.equals(1.0 * -0xabcdef, double.parse(" -0x00000abcdef ")); |
| 56 Expect.equals(1.0 * -0xABCDEF, double.parse(" -0x00000ABCDEF ")); |
| 57 Expect.equals(10.0, double.parse("010")); |
| 58 Expect.equals(-10.0, double.parse("-010")); |
| 59 Expect.equals(10.0, double.parse(" 010 ")); |
| 60 Expect.equals(-10.0, double.parse(" -010 ")); |
| 61 Expect.equals(0.1, double.parse("0.1")); |
| 62 Expect.equals(0.1, double.parse(" 0.1 ")); |
| 63 Expect.equals(0.1, double.parse(" +0.1 ")); |
| 64 Expect.equals(-0.1, double.parse(" -0.1 ")); |
| 65 Expect.equals(0.1, double.parse(".1")); |
| 66 Expect.equals(0.1, double.parse(" .1 ")); |
| 67 Expect.equals(0.1, double.parse(" +.1 ")); |
| 68 Expect.equals(-0.1, double.parse(" -.1 ")); |
| 69 Expect.equals(1.5, double.parse("1.5")); |
| 70 Expect.equals(1234567.89, double.parse("1234567.89")); |
| 71 Expect.equals(1234567.89, double.parse(" 1234567.89 ")); |
| 72 Expect.equals(1234567.89, double.parse(" +1234567.89 ")); |
| 73 Expect.equals(-1234567.89, double.parse(" -1234567.89 ")); |
| 74 Expect.equals(1234567e89, double.parse("1234567e89")); |
| 75 Expect.equals(1234567e89, double.parse(" 1234567e89 ")); |
| 76 Expect.equals(1234567e89, double.parse(" +1234567e89 ")); |
| 77 Expect.equals(-1234567e89, double.parse(" -1234567e89 ")); |
| 78 Expect.equals(1234567.89e2, double.parse("1234567.89e2")); |
| 79 Expect.equals(1234567.89e2, double.parse(" 1234567.89e2 ")); |
| 80 Expect.equals(1234567.89e2, double.parse(" +1234567.89e2 ")); |
| 81 Expect.equals(-1234567.89e2, double.parse(" -1234567.89e2 ")); |
| 82 Expect.equals(1234567.89e2, double.parse("1234567.89E2")); |
| 83 Expect.equals(1234567.89e2, double.parse(" 1234567.89E2 ")); |
| 84 Expect.equals(1234567.89e2, double.parse(" +1234567.89E2 ")); |
| 85 Expect.equals(-1234567.89e2, double.parse(" -1234567.89E2 ")); |
| 86 Expect.equals(1234567.89e-2, double.parse("1234567.89e-2")); |
| 87 Expect.equals(1234567.89e-2, double.parse(" 1234567.89e-2 ")); |
| 88 Expect.equals(1234567.89e-2, double.parse(" +1234567.89e-2 ")); |
| 89 Expect.equals(-1234567.89e-2, double.parse(" -1234567.89e-2 ")); |
| 90 // TODO(floitsch): add tests for NaN and Infinity. |
117 parseDoubleThrowsFormatException("1b"); | 91 parseDoubleThrowsFormatException("1b"); |
118 parseDoubleThrowsFormatException(" 1b "); | 92 parseDoubleThrowsFormatException(" 1b "); |
119 parseDoubleThrowsFormatException(" 1 b "); | 93 parseDoubleThrowsFormatException(" 1 b "); |
120 parseDoubleThrowsFormatException(" e3 "); | 94 parseDoubleThrowsFormatException(" e3 "); |
121 parseDoubleThrowsFormatException(" .e3 "); | 95 parseDoubleThrowsFormatException(" .e3 "); |
122 parseDoubleThrowsFormatException("00x12"); | 96 parseDoubleThrowsFormatException("00x12"); |
123 parseDoubleThrowsFormatException(" 00x12 "); | 97 parseDoubleThrowsFormatException(" 00x12 "); |
124 parseDoubleThrowsFormatException("-1b"); | 98 parseDoubleThrowsFormatException("-1b"); |
125 parseDoubleThrowsFormatException(" -1b "); | 99 parseDoubleThrowsFormatException(" -1b "); |
126 parseDoubleThrowsFormatException(" -1 b "); | 100 parseDoubleThrowsFormatException(" -1 b "); |
127 parseDoubleThrowsFormatException("-00x12"); | 101 parseDoubleThrowsFormatException("-00x12"); |
128 parseDoubleThrowsFormatException(" -00x12 "); | 102 parseDoubleThrowsFormatException(" -00x12 "); |
129 parseDoubleThrowsFormatException(" -00x12 "); | 103 parseDoubleThrowsFormatException(" -00x12 "); |
130 parseDoubleThrowsFormatException("0x0x12"); | 104 parseDoubleThrowsFormatException("0x0x12"); |
131 parseDoubleThrowsFormatException("+ 1.5"); | 105 parseDoubleThrowsFormatException("+ 1.5"); |
132 parseDoubleThrowsFormatException("- 1.5"); | 106 parseDoubleThrowsFormatException("- 1.5"); |
133 parseDoubleThrowsFormatException(""); | 107 parseDoubleThrowsFormatException(""); |
134 parseDoubleThrowsFormatException(" "); | 108 parseDoubleThrowsFormatException(" "); |
| 109 parseDoubleThrowsFormatException("5."); |
| 110 parseDoubleThrowsFormatException(" 5. "); |
| 111 parseDoubleThrowsFormatException(" +5. "); |
| 112 parseDoubleThrowsFormatException(" -5. "); |
| 113 parseDoubleThrowsFormatException("1234567.e2"); |
| 114 parseDoubleThrowsFormatException(" 1234567.e2 "); |
| 115 parseDoubleThrowsFormatException(" +1234567.e2 "); |
| 116 parseDoubleThrowsFormatException(" -1234567.e2 "); |
135 parseDoubleThrowsFormatException("+0x1234567890"); | 117 parseDoubleThrowsFormatException("+0x1234567890"); |
136 parseDoubleThrowsFormatException(" +0x1234567890 "); | 118 parseDoubleThrowsFormatException(" +0x1234567890 "); |
137 parseDoubleThrowsFormatException(" +0x100 "); | 119 parseDoubleThrowsFormatException(" +0x100 "); |
138 parseDoubleThrowsFormatException("+0x100"); | 120 parseDoubleThrowsFormatException("+0x100"); |
139 parseDoubleThrowsFormatException("0x1234567890"); | |
140 parseDoubleThrowsFormatException("-0x1234567890"); | |
141 parseDoubleThrowsFormatException(" 0x1234567890 "); | |
142 parseDoubleThrowsFormatException(" -0x1234567890 "); | |
143 parseDoubleThrowsFormatException("0x100"); | |
144 parseDoubleThrowsFormatException("-0x100"); | |
145 parseDoubleThrowsFormatException(" 0x100 "); | |
146 parseDoubleThrowsFormatException(" -0x100 "); | |
147 parseDoubleThrowsFormatException("0xabcdef"); | |
148 parseDoubleThrowsFormatException("0xABCDEF"); | |
149 parseDoubleThrowsFormatException("0xabCDEf"); | |
150 parseDoubleThrowsFormatException("-0xabcdef"); | |
151 parseDoubleThrowsFormatException("-0xABCDEF"); | |
152 parseDoubleThrowsFormatException(" 0xabcdef "); | |
153 parseDoubleThrowsFormatException(" 0xABCDEF "); | |
154 parseDoubleThrowsFormatException(" -0xabcdef "); | |
155 parseDoubleThrowsFormatException(" -0xABCDEF "); | |
156 parseDoubleThrowsFormatException("0x00000abcdef"); | |
157 parseDoubleThrowsFormatException("0x00000ABCDEF"); | |
158 parseDoubleThrowsFormatException("-0x00000abcdef"); | |
159 parseDoubleThrowsFormatException("-0x00000ABCDEF"); | |
160 parseDoubleThrowsFormatException(" 0x00000abcdef "); | |
161 parseDoubleThrowsFormatException(" 0x00000ABCDEF "); | |
162 parseDoubleThrowsFormatException(" -0x00000abcdef "); | |
163 parseDoubleThrowsFormatException(" -0x00000ABCDEF "); | |
164 parseDoubleThrowsFormatException(" -INFINITY "); | |
165 parseDoubleThrowsFormatException(" NAN "); | |
166 parseDoubleThrowsFormatException(" inf "); | |
167 parseDoubleThrowsFormatException(" nan "); | |
168 } | 121 } |
OLD | NEW |