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 // Dart test program to test arithmetic operations. | 4 // Dart test program to test arithmetic operations. |
5 | 5 |
6 #library('arithmetic_test'); | 6 #library('arithmetic_test'); |
7 #import('dart:math'); | 7 #import('dart:math'); |
8 | 8 |
9 class ArithmeticTest { | 9 class ArithmeticTest { |
10 | 10 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 { double d = 1.0 / 0; } | 120 { double d = 1.0 / 0; } |
121 { double d = 1 - 1.0; } | 121 { double d = 1 - 1.0; } |
122 { double d = 1.0 - 1; } | 122 { double d = 1.0 - 1; } |
123 { double d = big * 1.0; } | 123 { double d = big * 1.0; } |
124 { double d = 1.0 * big; } | 124 { double d = 1.0 * big; } |
125 | 125 |
126 // Reset big to positive value. | 126 // Reset big to positive value. |
127 big = 123456789012345; | 127 big = 123456789012345; |
128 // -- isNegative --. | 128 // -- isNegative --. |
129 // Smi. | 129 // Smi. |
130 Expect.equals(false, (0).isNegative()); | 130 Expect.equals(false, (0).isNegative); |
131 Expect.equals(false, (1).isNegative()); | 131 Expect.equals(false, (1).isNegative); |
132 Expect.equals(true, (-1).isNegative()); | 132 Expect.equals(true, (-1).isNegative); |
133 // Big. | 133 // Big. |
134 Expect.equals(false, big.isNegative()); | 134 Expect.equals(false, big.isNegative); |
135 Expect.equals(true, (-big).isNegative()); | 135 Expect.equals(true, (-big).isNegative); |
136 // Double. | 136 // Double. |
137 // TODO(srdjan): enable the following test once isNegative works. | 137 // TODO(srdjan): enable the following test once isNegative works. |
138 // Expect.equals(true, (-0.0).isNegative()); | 138 // Expect.equals(true, (-0.0).isNegative); |
139 Expect.equals(false, (0.0).isNegative()); | 139 Expect.equals(false, (0.0).isNegative); |
140 Expect.equals(false, (2.0).isNegative()); | 140 Expect.equals(false, (2.0).isNegative); |
141 Expect.equals(true, (-2.0).isNegative()); | 141 Expect.equals(true, (-2.0).isNegative); |
142 | 142 |
143 double negateDouble(double x) { | 143 double negateDouble(double x) { |
144 return -x; | 144 return -x; |
145 } | 145 } |
146 | 146 |
147 Expect.isTrue(negateDouble(0.0).isNegative()); | 147 Expect.isTrue(negateDouble(0.0).isNegative); |
148 Expect.isFalse(negateDouble(-0.0).isNegative()); | 148 Expect.isFalse(negateDouble(-0.0).isNegative); |
149 Expect.isTrue(negateDouble(3.5e3).isNegative()); | 149 Expect.isTrue(negateDouble(3.5e3).isNegative); |
150 Expect.isFalse(negateDouble(-3.5e3).isNegative()); | 150 Expect.isFalse(negateDouble(-3.5e3).isNegative); |
151 | 151 |
152 | 152 |
153 // Constants. | 153 // Constants. |
154 final nan = 0.0/0.0; | 154 final nan = 0.0/0.0; |
155 final infinity = 1.0/0.0; | 155 final infinity = 1.0/0.0; |
156 | 156 |
157 // -- isInfinite --. | 157 // -- isInfinite --. |
158 // Smi. | 158 // Smi. |
159 Expect.equals(false, (0).isInfinite()); | 159 Expect.equals(false, (0).isInfinite); |
160 Expect.equals(false, (1).isInfinite()); | 160 Expect.equals(false, (1).isInfinite); |
161 Expect.equals(false, (-1).isInfinite()); | 161 Expect.equals(false, (-1).isInfinite); |
162 // Big. | 162 // Big. |
163 Expect.equals(false, big.isInfinite()); | 163 Expect.equals(false, big.isInfinite); |
164 Expect.equals(false, (-big).isInfinite()); | 164 Expect.equals(false, (-big).isInfinite); |
165 // Double. | 165 // Double. |
166 Expect.equals(false, (0.0).isInfinite()); | 166 Expect.equals(false, (0.0).isInfinite); |
167 Expect.equals(true, infinity.isInfinite()); | 167 Expect.equals(true, infinity.isInfinite); |
168 Expect.equals(true, (-infinity).isInfinite()); | 168 Expect.equals(true, (-infinity).isInfinite); |
169 Expect.equals(false, (12.0).isInfinite()); | 169 Expect.equals(false, (12.0).isInfinite); |
170 Expect.equals(false, (-12.0).isInfinite()); | 170 Expect.equals(false, (-12.0).isInfinite); |
171 Expect.equals(false, nan.isInfinite()); | 171 Expect.equals(false, nan.isInfinite); |
172 | 172 |
173 // -- isNaN --. | 173 // -- isNaN --. |
174 // Smi. | 174 // Smi. |
175 Expect.equals(false, (0).isNaN()); | 175 Expect.equals(false, (0).isNaN); |
176 Expect.equals(false, (1).isNaN()); | 176 Expect.equals(false, (1).isNaN); |
177 Expect.equals(false, (-1).isNaN()); | 177 Expect.equals(false, (-1).isNaN); |
178 // Big. | 178 // Big. |
179 Expect.equals(false, big.isNaN()); | 179 Expect.equals(false, big.isNaN); |
180 Expect.equals(false, (-big).isNaN()); | 180 Expect.equals(false, (-big).isNaN); |
181 // Double. | 181 // Double. |
182 Expect.equals(true, nan.isNaN()); | 182 Expect.equals(true, nan.isNaN); |
183 Expect.equals(false, (12.0).isNaN()); | 183 Expect.equals(false, (12.0).isNaN); |
184 Expect.equals(false, infinity.isNaN()); | 184 Expect.equals(false, infinity.isNaN); |
185 | 185 |
186 // -- abs --. | 186 // -- abs --. |
187 // Smi. | 187 // Smi. |
188 Expect.equals(0, (0).abs()); | 188 Expect.equals(0, (0).abs()); |
189 Expect.equals(2, (2).abs()); | 189 Expect.equals(2, (2).abs()); |
190 Expect.equals(2, (-2).abs()); | 190 Expect.equals(2, (-2).abs()); |
191 // Big. | 191 // Big. |
192 Expect.equals(big, big.abs()); | 192 Expect.equals(big, big.abs()); |
193 Expect.equals(big, (-big).abs()); | 193 Expect.equals(big, (-big).abs()); |
194 // Double. | 194 // Double. |
195 Expect.equals(false, (0.0).abs().isNegative()); | 195 Expect.equals(false, (0.0).abs().isNegative); |
196 Expect.equals(false, (-0.0).abs().isNegative()); | 196 Expect.equals(false, (-0.0).abs().isNegative); |
197 Expect.equals(2.0, (2.0).abs()); | 197 Expect.equals(2.0, (2.0).abs()); |
198 Expect.equals(2.0, (-2.0).abs()); | 198 Expect.equals(2.0, (-2.0).abs()); |
199 | 199 |
200 // -- ceil --. | 200 // -- ceil --. |
201 // Smi. | 201 // Smi. |
202 Expect.equals(0, (0).ceil()); | 202 Expect.equals(0, (0).ceil()); |
203 Expect.equals(1, (1).ceil()); | 203 Expect.equals(1, (1).ceil()); |
204 Expect.equals(-1, (-1).ceil()); | 204 Expect.equals(-1, (-1).ceil()); |
205 // Big. | 205 // Big. |
206 Expect.equals(big, big.ceil()); | 206 Expect.equals(big, big.ceil()); |
207 Expect.equals(-big, (-big).ceil()); | 207 Expect.equals(-big, (-big).ceil()); |
208 // Double. | 208 // Double. |
209 Expect.equals(0.0, (0.0).ceil()); | 209 Expect.equals(0.0, (0.0).ceil()); |
210 Expect.equals(false, (0.0).ceil().isNegative()); | 210 Expect.equals(false, (0.0).ceil().isNegative); |
211 Expect.equals(1.0, (0.1).ceil()); | 211 Expect.equals(1.0, (0.1).ceil()); |
212 Expect.equals(-0.0, (-0.0).ceil()); | 212 Expect.equals(-0.0, (-0.0).ceil()); |
213 Expect.equals(-0.0, (-0.3).ceil()); | 213 Expect.equals(-0.0, (-0.3).ceil()); |
214 // TODO(srdjan): enable the following tests once isNegative works. | 214 // TODO(srdjan): enable the following tests once isNegative works. |
215 // Expect.equals(true, (-0.0).ceil().isNegative()); | 215 // Expect.equals(true, (-0.0).ceil().isNegative); |
216 // Expect.equals(true, (-0.3).ceil().isNegative()); | 216 // Expect.equals(true, (-0.3).ceil().isNegative); |
217 Expect.equals(3.0, (2.1).ceil()); | 217 Expect.equals(3.0, (2.1).ceil()); |
218 Expect.equals(-2.0, (-2.1).ceil()); | 218 Expect.equals(-2.0, (-2.1).ceil()); |
219 | 219 |
220 // -- floor --. | 220 // -- floor --. |
221 // Smi. | 221 // Smi. |
222 Expect.equals(0, (0).floor()); | 222 Expect.equals(0, (0).floor()); |
223 Expect.equals(1, (1).floor()); | 223 Expect.equals(1, (1).floor()); |
224 Expect.equals(-1, (-1).floor()); | 224 Expect.equals(-1, (-1).floor()); |
225 // Big. | 225 // Big. |
226 Expect.equals(big, big.floor()); | 226 Expect.equals(big, big.floor()); |
227 Expect.equals(-big, (-big).floor()); | 227 Expect.equals(-big, (-big).floor()); |
228 // Double. | 228 // Double. |
229 Expect.equals(0.0, (0.0).floor()); | 229 Expect.equals(0.0, (0.0).floor()); |
230 Expect.equals(0.0, (0.1).floor()); | 230 Expect.equals(0.0, (0.1).floor()); |
231 Expect.equals(false, (0.0).floor().isNegative()); | 231 Expect.equals(false, (0.0).floor().isNegative); |
232 Expect.equals(false, (0.1).floor().isNegative()); | 232 Expect.equals(false, (0.1).floor().isNegative); |
233 Expect.equals(-0.0, (-0.0).floor()); | 233 Expect.equals(-0.0, (-0.0).floor()); |
234 // TODO(srdjan): enable the following tests once isNegative works. | 234 // TODO(srdjan): enable the following tests once isNegative works. |
235 // Expect.equals(true, (-0.0).floor().isNegative()); | 235 // Expect.equals(true, (-0.0).floor().isNegative); |
236 Expect.equals(-1.0, (-0.1).floor()); | 236 Expect.equals(-1.0, (-0.1).floor()); |
237 Expect.equals(2.0, (2.1).floor()); | 237 Expect.equals(2.0, (2.1).floor()); |
238 Expect.equals(-3.0, (-2.1).floor()); | 238 Expect.equals(-3.0, (-2.1).floor()); |
239 | 239 |
240 // -- truncate --. | 240 // -- truncate --. |
241 // Smi. | 241 // Smi. |
242 Expect.equals(0, (0).truncate()); | 242 Expect.equals(0, (0).truncate()); |
243 Expect.equals(1, (1).truncate()); | 243 Expect.equals(1, (1).truncate()); |
244 Expect.equals(-1, (-1).truncate()); | 244 Expect.equals(-1, (-1).truncate()); |
245 // Big. | 245 // Big. |
246 Expect.equals(big, big.truncate()); | 246 Expect.equals(big, big.truncate()); |
247 Expect.equals(-big, (-big).truncate()); | 247 Expect.equals(-big, (-big).truncate()); |
248 // Double. | 248 // Double. |
249 Expect.equals(0.0, (0.0).truncate()); | 249 Expect.equals(0.0, (0.0).truncate()); |
250 Expect.equals(0.0, (0.1).truncate()); | 250 Expect.equals(0.0, (0.1).truncate()); |
251 Expect.equals(false, (0.0).truncate().isNegative()); | 251 Expect.equals(false, (0.0).truncate().isNegative); |
252 Expect.equals(false, (0.1).truncate().isNegative()); | 252 Expect.equals(false, (0.1).truncate().isNegative); |
253 Expect.equals(-0.0, (-0.0).truncate()); | 253 Expect.equals(-0.0, (-0.0).truncate()); |
254 Expect.equals(-0.0, (-0.3).truncate()); | 254 Expect.equals(-0.0, (-0.3).truncate()); |
255 // TODO(srdjan): enable the following tests once isNegative works. | 255 // TODO(srdjan): enable the following tests once isNegative works. |
256 // Expect.equals(true, (-0.0).truncate().isNegative()); | 256 // Expect.equals(true, (-0.0).truncate().isNegative); |
257 // Expect.equals(true, (-0.3).truncate().isNegative()); | 257 // Expect.equals(true, (-0.3).truncate().isNegative); |
258 Expect.equals(2.0, (2.1).truncate()); | 258 Expect.equals(2.0, (2.1).truncate()); |
259 Expect.equals(-2.0, (-2.1).truncate()); | 259 Expect.equals(-2.0, (-2.1).truncate()); |
260 | 260 |
261 double b1 = (1234567890123.0).truncate(); | 261 double b1 = (1234567890123.0).truncate(); |
262 double b2 = (1234567890124.0).truncate(); | 262 double b2 = (1234567890124.0).truncate(); |
263 Expect.equals(b2, b1 + 1.0); | 263 Expect.equals(b2, b1 + 1.0); |
264 | 264 |
265 // -- round --. | 265 // -- round --. |
266 // Smi. | 266 // Smi. |
267 Expect.equals(0, (0).round()); | 267 Expect.equals(0, (0).round()); |
268 Expect.equals(1, (1).round()); | 268 Expect.equals(1, (1).round()); |
269 Expect.equals(-1, (-1).round()); | 269 Expect.equals(-1, (-1).round()); |
270 // Big. | 270 // Big. |
271 Expect.equals(big, big.round()); | 271 Expect.equals(big, big.round()); |
272 Expect.equals(-big, (-big).round()); | 272 Expect.equals(-big, (-big).round()); |
273 // Double. | 273 // Double. |
274 Expect.equals(3.0, (2.6).round()); | 274 Expect.equals(3.0, (2.6).round()); |
275 Expect.equals(-3.0, (-2.6).round()); | 275 Expect.equals(-3.0, (-2.6).round()); |
276 Expect.equals(0.0, (0.0).round()); | 276 Expect.equals(0.0, (0.0).round()); |
277 Expect.equals(0.0, (0.1).round()); | 277 Expect.equals(0.0, (0.1).round()); |
278 Expect.equals(false, (0.0).round().isNegative()); | 278 Expect.equals(false, (0.0).round().isNegative); |
279 Expect.equals(false, (0.1).round().isNegative()); | 279 Expect.equals(false, (0.1).round().isNegative); |
280 Expect.equals(-0.0, (-0.0).round()); | 280 Expect.equals(-0.0, (-0.0).round()); |
281 Expect.equals(-0.0, (-0.3).round()); | 281 Expect.equals(-0.0, (-0.3).round()); |
282 Expect.equals(2.0, (2.1).round()); | 282 Expect.equals(2.0, (2.1).round()); |
283 Expect.equals(-2.0, (-2.1).round()); | 283 Expect.equals(-2.0, (-2.1).round()); |
284 Expect.equals(1.0, (0.5).round()); | 284 Expect.equals(1.0, (0.5).round()); |
285 // TODO(floitsch): enable or adapt test, once we reached conclusion on | 285 // TODO(floitsch): enable or adapt test, once we reached conclusion on |
286 // b/4539188. | 286 // b/4539188. |
287 // Expect.equals(-0.0, (-0.5).round()); | 287 // Expect.equals(-0.0, (-0.5).round()); |
288 // TODO(srdjan): enable the following tests once isNegative works. | 288 // TODO(srdjan): enable the following tests once isNegative works. |
289 // Expect.equals(true, (-0.0).round().isNegative()); | 289 // Expect.equals(true, (-0.0).round().isNegative); |
290 // Expect.equals(true, (-0.3).round().isNegative()); | 290 // Expect.equals(true, (-0.3).round().isNegative); |
291 // Expect.equals(true, (-0.5).round().isNegative()); | 291 // Expect.equals(true, (-0.5).round().isNegative); |
292 Expect.equals(2.0, (1.5).round()); | 292 Expect.equals(2.0, (1.5).round()); |
293 // TODO(floitsch): enable or adapt test, once we reached conclusion on | 293 // TODO(floitsch): enable or adapt test, once we reached conclusion on |
294 // b/4539188. | 294 // b/4539188. |
295 // Expect.equals(-1.0, (-1.5).round()); | 295 // Expect.equals(-1.0, (-1.5).round()); |
296 Expect.equals(1.0, (0.99).round()); | 296 Expect.equals(1.0, (0.99).round()); |
297 | 297 |
298 // -- toInt --. | 298 // -- toInt --. |
299 // Smi. | 299 // Smi. |
300 Expect.equals(0, (0).toInt()); | 300 Expect.equals(0, (0).toInt()); |
301 Expect.equals(1, (1).toInt()); | 301 Expect.equals(1, (1).toInt()); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 double d = rand.nextDouble(); | 370 double d = rand.nextDouble(); |
371 } | 371 } |
372 | 372 |
373 Expect.equals(false, exceptionCaughtParseInt("22")); | 373 Expect.equals(false, exceptionCaughtParseInt("22")); |
374 Expect.equals(true, exceptionCaughtParseInt("alpha")); | 374 Expect.equals(true, exceptionCaughtParseInt("alpha")); |
375 Expect.equals(true, exceptionCaughtParseInt("-alpha")); | 375 Expect.equals(true, exceptionCaughtParseInt("-alpha")); |
376 Expect.equals(false, exceptionCaughtParseDouble("22.2")); | 376 Expect.equals(false, exceptionCaughtParseDouble("22.2")); |
377 Expect.equals(true, exceptionCaughtParseDouble("alpha")); | 377 Expect.equals(true, exceptionCaughtParseDouble("alpha")); |
378 Expect.equals(true, exceptionCaughtParseDouble("-alpha")); | 378 Expect.equals(true, exceptionCaughtParseDouble("-alpha")); |
379 | 379 |
380 Expect.equals(false, parseDouble("1.2").isNaN()); | 380 Expect.equals(false, parseDouble("1.2").isNaN); |
381 Expect.equals(false, parseDouble("1.2").isInfinite()); | 381 Expect.equals(false, parseDouble("1.2").isInfinite); |
382 | 382 |
383 Expect.equals(true, parseDouble("NaN").isNaN()); | 383 Expect.equals(true, parseDouble("NaN").isNaN); |
384 Expect.equals(true, parseDouble("Infinity").isInfinite()); | 384 Expect.equals(true, parseDouble("Infinity").isInfinite); |
385 Expect.equals(true, parseDouble("-Infinity").isInfinite()); | 385 Expect.equals(true, parseDouble("-Infinity").isInfinite); |
386 | 386 |
387 Expect.equals(false, parseDouble("NaN").isNegative()); | 387 Expect.equals(false, parseDouble("NaN").isNegative); |
388 Expect.equals(false, parseDouble("Infinity").isNegative()); | 388 Expect.equals(false, parseDouble("Infinity").isNegative); |
389 Expect.equals(true, parseDouble("-Infinity").isNegative()); | 389 Expect.equals(true, parseDouble("-Infinity").isNegative); |
390 | 390 |
391 Expect.equals("NaN", parseDouble("NaN").toString()); | 391 Expect.equals("NaN", parseDouble("NaN").toString()); |
392 Expect.equals("Infinity", parseDouble("Infinity").toString()); | 392 Expect.equals("Infinity", parseDouble("Infinity").toString()); |
393 Expect.equals("-Infinity", parseDouble("-Infinity").toString()); | 393 Expect.equals("-Infinity", parseDouble("-Infinity").toString()); |
394 | 394 |
395 Expect.equals(false, toIntThrowsFormatException("1.2")); | 395 Expect.equals(false, toIntThrowsFormatException("1.2")); |
396 Expect.equals(true, toIntThrowsFormatException("Infinity")); | 396 Expect.equals(true, toIntThrowsFormatException("Infinity")); |
397 Expect.equals(true, toIntThrowsFormatException("-Infinity")); | 397 Expect.equals(true, toIntThrowsFormatException("-Infinity")); |
398 Expect.equals(true, toIntThrowsFormatException("NaN")); | 398 Expect.equals(true, toIntThrowsFormatException("NaN")); |
399 | 399 |
(...skipping 15 matching lines...) Expand all Loading... |
415 static testMain() { | 415 static testMain() { |
416 for (int i = 0; i < 1500; i++) { | 416 for (int i = 0; i < 1500; i++) { |
417 runOne(); | 417 runOne(); |
418 } | 418 } |
419 } | 419 } |
420 } | 420 } |
421 | 421 |
422 main() { | 422 main() { |
423 ArithmeticTest.testMain(); | 423 ArithmeticTest.testMain(); |
424 } | 424 } |
OLD | NEW |