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 12 matching lines...) Expand all Loading... |
23 return false; | 23 return false; |
24 } on FormatException catch (e) { | 24 } on FormatException catch (e) { |
25 return true; | 25 return true; |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 static bool toIntThrowsFormatException(String str) { | 29 static bool toIntThrowsFormatException(String str) { |
30 // No exception allowed for parse double. | 30 // No exception allowed for parse double. |
31 double d = double.parse(str); | 31 double d = double.parse(str); |
32 try { | 32 try { |
33 var a = d.toInt(); | 33 var a = d.truncate(); |
34 return false; | 34 return false; |
35 } on FormatException catch (e) { | 35 } on FormatException catch (e) { |
36 return true; | 36 return true; |
37 } | 37 } |
38 } | 38 } |
39 | 39 |
40 static runOne() { | 40 static runOne() { |
41 var a = 22; | 41 var a = 22; |
42 var b = 4; | 42 var b = 4; |
43 // Smi & smi. | 43 // Smi & smi. |
(...skipping 10 matching lines...) Expand all Loading... |
54 a = -(1 << i); | 54 a = -(1 << i); |
55 b = -1; | 55 b = -1; |
56 Expect.equals(1 << i, a ~/ b); | 56 Expect.equals(1 << i, a ~/ b); |
57 } | 57 } |
58 a = 22; | 58 a = 22; |
59 b = 4.0; | 59 b = 4.0; |
60 // Smi & double. | 60 // Smi & double. |
61 Expect.equals(26.0, a + b); | 61 Expect.equals(26.0, a + b); |
62 Expect.equals(18.0, a - b); | 62 Expect.equals(18.0, a - b); |
63 Expect.equals(88.0, a * b); | 63 Expect.equals(88.0, a * b); |
64 Expect.equals(5.0, a ~/ b); | 64 Expect.equals(5, a ~/ b); |
65 Expect.equals(5.5, a / b); | 65 Expect.equals(5.5, a / b); |
66 Expect.equals(2.0, a % b); | 66 Expect.equals(2.0, a % b); |
67 Expect.equals(2.0, a.remainder(b)); | 67 Expect.equals(2.0, a.remainder(b)); |
68 a = 22.0; | 68 a = 22.0; |
69 b = 4; | 69 b = 4; |
70 // Double & smi. | 70 // Double & smi. |
71 Expect.equals(26.0, a + b); | 71 Expect.equals(26.0, a + b); |
72 Expect.equals(18.0, a - b); | 72 Expect.equals(18.0, a - b); |
73 Expect.equals(88.0, a * b); | 73 Expect.equals(88.0, a * b); |
74 Expect.equals(5.0, a ~/ b); | 74 Expect.equals(5, a ~/ b); |
75 Expect.equals(5.5, a / b); | 75 Expect.equals(5.5, a / b); |
76 Expect.equals(2.0, a % b); | 76 Expect.equals(2.0, a % b); |
77 Expect.equals(2.0, a.remainder(b)); | 77 Expect.equals(2.0, a.remainder(b)); |
78 a = 22.0; | 78 a = 22.0; |
79 b = 4.0; | 79 b = 4.0; |
80 // Double & double. | 80 // Double & double. |
81 Expect.equals(26.0, a + b); | 81 Expect.equals(26.0, a + b); |
82 Expect.equals(18.0, a - b); | 82 Expect.equals(18.0, a - b); |
83 Expect.equals(88.0, a * b); | 83 Expect.equals(88.0, a * b); |
84 Expect.equals(5.0, a ~/ b); | 84 Expect.equals(5, a ~/ b); |
85 Expect.equals(5.5, a / b); | 85 Expect.equals(5.5, a / b); |
86 Expect.equals(2.0, a % b); | 86 Expect.equals(2.0, a % b); |
87 Expect.equals(2.0, a.remainder(b)); | 87 Expect.equals(2.0, a.remainder(b)); |
88 | 88 |
89 // Special int operations. | 89 // Special int operations. |
90 Expect.equals(2, (2).floor()); | 90 Expect.equals(2, (2).floor()); |
91 Expect.equals(2, (2).ceil()); | 91 Expect.equals(2, (2).ceil()); |
92 Expect.equals(2, (2).round()); | 92 Expect.equals(2, (2).round()); |
93 Expect.equals(2, (2).truncate()); | 93 Expect.equals(2, (2).truncate()); |
94 | 94 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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).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.1).ceil()); |
212 Expect.equals(-0.0, (-0.0).ceil()); | 212 Expect.equals(0, (-0.0).ceil()); |
213 Expect.equals(-0.0, (-0.3).ceil()); | 213 Expect.equals(0, (-0.3).ceil()); |
214 // TODO(srdjan): enable the following tests once isNegative works. | 214 Expect.isTrue((-0.0).ceil() is int); |
215 // Expect.equals(true, (-0.0).ceil().isNegative); | 215 Expect.isTrue((-0.3).ceil() is int); |
216 // Expect.equals(true, (-0.3).ceil().isNegative); | 216 Expect.equals(3, (2.1).ceil()); |
217 Expect.equals(3.0, (2.1).ceil()); | 217 Expect.equals(-2, (-2.1).ceil()); |
218 Expect.equals(-2.0, (-2.1).ceil()); | |
219 | 218 |
220 // -- floor --. | 219 // -- floor --. |
221 // Smi. | 220 // Smi. |
222 Expect.equals(0, (0).floor()); | 221 Expect.equals(0, (0).floor()); |
223 Expect.equals(1, (1).floor()); | 222 Expect.equals(1, (1).floor()); |
224 Expect.equals(-1, (-1).floor()); | 223 Expect.equals(-1, (-1).floor()); |
225 // Big. | 224 // Big. |
226 Expect.equals(big, big.floor()); | 225 Expect.equals(big, big.floor()); |
227 Expect.equals(-big, (-big).floor()); | 226 Expect.equals(-big, (-big).floor()); |
228 // Double. | 227 // Double. |
229 Expect.equals(0.0, (0.0).floor()); | 228 Expect.equals(0, (0.0).floor()); |
230 Expect.equals(0.0, (0.1).floor()); | 229 Expect.equals(0, (0.1).floor()); |
231 Expect.equals(false, (0.0).floor().isNegative); | 230 Expect.isTrue((0.0).floor() is int); |
232 Expect.equals(false, (0.1).floor().isNegative); | 231 Expect.isTrue((0.1).floor() is int); |
233 Expect.equals(-0.0, (-0.0).floor()); | 232 Expect.equals(0, (-0.0).floor()); |
234 // TODO(srdjan): enable the following tests once isNegative works. | 233 Expect.isTrue((-0.0).floor() is int); |
235 // Expect.equals(true, (-0.0).floor().isNegative); | 234 Expect.equals(-1, (-0.1).floor()); |
236 Expect.equals(-1.0, (-0.1).floor()); | 235 Expect.equals(2, (2.1).floor()); |
237 Expect.equals(2.0, (2.1).floor()); | 236 Expect.equals(-3, (-2.1).floor()); |
238 Expect.equals(-3.0, (-2.1).floor()); | |
239 | 237 |
240 // -- truncate --. | 238 // -- truncate --. |
241 // Smi. | 239 // Smi. |
242 Expect.equals(0, (0).truncate()); | 240 Expect.equals(0, (0).truncate()); |
243 Expect.equals(1, (1).truncate()); | 241 Expect.equals(1, (1).truncate()); |
244 Expect.equals(-1, (-1).truncate()); | 242 Expect.equals(-1, (-1).truncate()); |
245 // Big. | 243 // Big. |
246 Expect.equals(big, big.truncate()); | 244 Expect.equals(big, big.truncate()); |
247 Expect.equals(-big, (-big).truncate()); | 245 Expect.equals(-big, (-big).truncate()); |
248 // Double. | 246 // Double. |
249 Expect.equals(0.0, (0.0).truncate()); | 247 Expect.equals(0, (0.0).truncate()); |
250 Expect.equals(0.0, (0.1).truncate()); | 248 Expect.equals(0, (0.1).truncate()); |
251 Expect.equals(false, (0.0).truncate().isNegative); | 249 Expect.isTrue((0.0).truncate() is int); |
252 Expect.equals(false, (0.1).truncate().isNegative); | 250 Expect.isTrue((0.1).truncate() is int); |
253 Expect.equals(-0.0, (-0.0).truncate()); | 251 Expect.equals(0, (-0.0).truncate()); |
254 Expect.equals(-0.0, (-0.3).truncate()); | 252 Expect.equals(0, (-0.3).truncate()); |
255 // TODO(srdjan): enable the following tests once isNegative works. | 253 Expect.isTrue((-0.0).truncate() is int); |
256 // Expect.equals(true, (-0.0).truncate().isNegative); | 254 Expect.isTrue((-0.3).truncate() is int); |
257 // Expect.equals(true, (-0.3).truncate().isNegative); | 255 Expect.equals(2, (2.1).truncate()); |
258 Expect.equals(2.0, (2.1).truncate()); | 256 Expect.equals(-2, (-2.1).truncate()); |
259 Expect.equals(-2.0, (-2.1).truncate()); | |
260 | 257 |
261 double b1 = (1234567890123.0).truncate(); | 258 int b1 = (1234567890123.0).truncate(); |
262 double b2 = (1234567890124.0).truncate(); | 259 int b2 = (1234567890124.0).truncate(); |
263 Expect.equals(b2, b1 + 1.0); | 260 Expect.equals(b2, b1 + 1.0); |
264 | 261 |
265 // -- round --. | 262 // -- round --. |
266 // Smi. | 263 // Smi. |
267 Expect.equals(0, (0).round()); | 264 Expect.equals(0, (0).round()); |
268 Expect.equals(1, (1).round()); | 265 Expect.equals(1, (1).round()); |
269 Expect.equals(-1, (-1).round()); | 266 Expect.equals(-1, (-1).round()); |
270 // Big. | 267 // Big. |
271 Expect.equals(big, big.round()); | 268 Expect.equals(big, big.round()); |
272 Expect.equals(-big, (-big).round()); | 269 Expect.equals(-big, (-big).round()); |
273 // Double. | 270 // Double. |
274 Expect.equals(3.0, (2.6).round()); | 271 Expect.equals(3, (2.6).round()); |
275 Expect.equals(-3.0, (-2.6).round()); | 272 Expect.equals(-3, (-2.6).round()); |
276 Expect.equals(0.0, (0.0).round()); | 273 Expect.equals(0, (0.0).round()); |
277 Expect.equals(0.0, (0.1).round()); | 274 Expect.equals(0, (0.1).round()); |
278 Expect.equals(false, (0.0).round().isNegative); | 275 Expect.isFalse((0.0).round().isNegative); |
279 Expect.equals(false, (0.1).round().isNegative); | 276 Expect.isFalse((0.1).round().isNegative); |
280 Expect.equals(-0.0, (-0.0).round()); | 277 Expect.equals(0, (-0.0).round()); |
281 Expect.equals(-0.0, (-0.3).round()); | 278 Expect.equals(0, (-0.3).round()); |
282 Expect.equals(2.0, (2.1).round()); | 279 Expect.equals(2, (2.1).round()); |
283 Expect.equals(-2.0, (-2.1).round()); | 280 Expect.equals(-2, (-2.1).round()); |
284 Expect.equals(1.0, (0.5).round()); | 281 Expect.equals(1, (0.5).round()); |
285 // TODO(floitsch): enable or adapt test, once we reached conclusion on | 282 Expect.isTrue((-0.0).round() is int); |
286 // b/4539188. | 283 Expect.isTrue((-0.3).round() is int); |
287 // Expect.equals(-0.0, (-0.5).round()); | 284 Expect.isTrue((-0.5).round() is int); |
288 // TODO(srdjan): enable the following tests once isNegative works. | 285 Expect.equals(2, (1.5).round()); |
289 // Expect.equals(true, (-0.0).round().isNegative); | 286 Expect.equals(-2, (-1.5).round()); |
290 // Expect.equals(true, (-0.3).round().isNegative); | 287 Expect.equals(1, (0.99).round()); |
291 // Expect.equals(true, (-0.5).round().isNegative); | |
292 Expect.equals(2.0, (1.5).round()); | |
293 // TODO(floitsch): enable or adapt test, once we reached conclusion on | |
294 // b/4539188. | |
295 // Expect.equals(-1.0, (-1.5).round()); | |
296 Expect.equals(1.0, (0.99).round()); | |
297 | 288 |
298 // -- toInt --. | 289 // -- toInt --. |
299 // Smi. | 290 // Smi. |
300 Expect.equals(0, (0).toInt()); | 291 Expect.equals(0, (0).truncate()); |
301 Expect.equals(1, (1).toInt()); | 292 Expect.equals(1, (1).truncate()); |
302 Expect.equals(-1, (-1).toInt()); | 293 Expect.equals(-1, (-1).truncate()); |
303 // Type checks. | 294 // Type checks. |
304 { int i = (0).toInt(); } | 295 { int i = (0).truncate(); } |
305 { int i = (1).toInt(); } | 296 { int i = (1).truncate(); } |
306 { int i = (-1).toInt(); } | 297 { int i = (-1).truncate(); } |
307 // Big. | 298 // Big. |
308 Expect.equals(big, big.toInt()); | 299 Expect.equals(big, big.truncate()); |
309 Expect.equals(-big, (-big).toInt()); | 300 Expect.equals(-big, (-big).truncate()); |
310 { int i = big.toInt(); } | 301 { int i = big.truncate(); } |
311 { int i = (-big).toInt(); } | 302 { int i = (-big).truncate(); } |
312 // Double. | 303 // Double. |
313 Expect.equals(1234567890123, (1234567890123.0).toInt()); | 304 Expect.equals(1234567890123, (1234567890123.0).truncate()); |
314 Expect.equals(-1234567890123, (-1234567890123.0).toInt()); | 305 Expect.equals(-1234567890123, (-1234567890123.0).truncate()); |
315 { int i = (1234567890123.0).toInt(); } | 306 { int i = (1234567890123.0).truncate(); } |
316 { int i = (-1234567890123.0).toInt(); } | 307 { int i = (-1234567890123.0).truncate(); } |
317 // 32bit Smi border cases. | 308 // 32bit Smi border cases. |
318 Expect.equals(-1073741824, (-1073741824.0).toInt()); | 309 Expect.equals(-1073741824, (-1073741824.0).truncate()); |
319 Expect.equals(-1073741825, (-1073741825.0).toInt()); | 310 Expect.equals(-1073741825, (-1073741825.0).truncate()); |
320 Expect.equals(1073741823, (1073741823.0).toInt()); | 311 Expect.equals(1073741823, (1073741823.0).truncate()); |
321 Expect.equals(1073741824, (1073741824.0).toInt()); | 312 Expect.equals(1073741824, (1073741824.0).truncate()); |
322 | 313 |
323 { int i = (-1073741824.0).toInt(); } | 314 { int i = (-1073741824.0).truncate(); } |
324 { int i = (-1073741825.0).toInt(); } | 315 { int i = (-1073741825.0).truncate(); } |
325 { int i = (1073741823.0).toInt(); } | 316 { int i = (1073741823.0).truncate(); } |
326 { int i = (1073741824.0).toInt(); } | 317 { int i = (1073741824.0).truncate(); } |
327 | 318 |
328 // -- toDouble --. | 319 // -- toDouble --. |
329 // Smi. | 320 // Smi. |
330 Expect.equals(0.0, (0).toDouble()); | 321 Expect.equals(0.0, (0).toDouble()); |
331 Expect.equals(1.0, (1).toDouble()); | 322 Expect.equals(1.0, (1).toDouble()); |
332 Expect.equals(-1.0, (-1).toDouble()); | 323 Expect.equals(-1.0, (-1).toDouble()); |
333 // Type checks. | 324 // Type checks. |
334 { double d = (0).toDouble(); } | 325 { double d = (0).toDouble(); } |
335 { double d = (1).toDouble(); } | 326 { double d = (1).toDouble(); } |
336 { double d = (-1).toDouble(); } | 327 { double d = (-1).toDouble(); } |
337 // Big. | 328 // Big. |
338 Expect.equals(big, big.toInt()); | 329 Expect.equals(big, big.truncate()); |
339 Expect.equals(-big, (-big).toInt()); | 330 Expect.equals(-big, (-big).truncate()); |
340 { int i = big.toInt(); } | 331 { int i = big.truncate(); } |
341 { int i = (-big).toInt(); } | 332 { int i = (-big).truncate(); } |
342 | 333 |
343 // Math functions. | 334 // Math functions. |
344 Expect.equals(2.0, sqrt(4.0)); | 335 Expect.equals(2.0, sqrt(4.0)); |
345 Expect.approxEquals(1.0, sin(3.14159265 / 2.0)); | 336 Expect.approxEquals(1.0, sin(3.14159265 / 2.0)); |
346 Expect.approxEquals(-1.0, cos(3.14159265)); | 337 Expect.approxEquals(-1.0, cos(3.14159265)); |
347 | 338 |
348 Expect.equals(12, int.parse("12")); | 339 Expect.equals(12, int.parse("12")); |
349 Expect.equals(-12, int.parse("-12")); | 340 Expect.equals(-12, int.parse("-12")); |
350 Expect.equals(12345678901234567890, | 341 Expect.equals(12345678901234567890, |
351 int.parse("12345678901234567890")); | 342 int.parse("12345678901234567890")); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 for (int i = 0; i < 1500; i++) { | 415 for (int i = 0; i < 1500; i++) { |
425 runOne(); | 416 runOne(); |
426 testSmiDivDeopt(); | 417 testSmiDivDeopt(); |
427 } | 418 } |
428 } | 419 } |
429 } | 420 } |
430 | 421 |
431 main() { | 422 main() { |
432 ArithmeticTest.testMain(); | 423 ArithmeticTest.testMain(); |
433 } | 424 } |
OLD | NEW |