Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: tests/language/src/ArithmeticTest.dart

Issue 10248007: test rename overhaul: step 8 - language tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011, 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 // Dart test program to test arithmetic operations.
5
6 class ArithmeticTest {
7
8 static bool exceptionCaughtParseInt(String s) {
9 try {
10 Math.parseInt(s);
11 return false;
12 } catch (BadNumberFormatException e) {
13 return true;
14 }
15 }
16
17 static bool exceptionCaughtParseDouble(String s) {
18 try {
19 Math.parseDouble(s);
20 return false;
21 } catch (BadNumberFormatException e) {
22 return true;
23 }
24 }
25
26 static bool toIntThrowsBadNumberFormatException(String str) {
27 // No exception allowed for parse double.
28 double d = Math.parseDouble(str);
29 try {
30 var a = d.toInt();
31 return false;
32 } catch (BadNumberFormatException e) {
33 return true;
34 }
35 }
36
37 static runOne() {
38 var a = 22;
39 var b = 4;
40 // Smi & smi.
41 Expect.equals(26, a + b);
42 Expect.equals(18, a - b);
43 Expect.equals(88, a * b);
44 Expect.equals(5, a ~/ b);
45 Expect.equals(5.5, a / b);
46 Expect.equals(2.0, 10 / 5);
47 Expect.equals(2, a % b);
48 Expect.equals(2, a.remainder(b));
49 // Smi corner cases.
50 for (int i = 0; i < 80; i++) {
51 a = -(1 << i);
52 b = -1;
53 Expect.equals(1 << i, a ~/ b);
54 }
55 a = 22;
56 b = 4.0;
57 // Smi & double.
58 Expect.equals(26.0, a + b);
59 Expect.equals(18.0, a - b);
60 Expect.equals(88.0, a * b);
61 Expect.equals(5.0, a ~/ b);
62 Expect.equals(5.5, a / b);
63 Expect.equals(2.0, a % b);
64 Expect.equals(2.0, a.remainder(b));
65 a = 22.0;
66 b = 4;
67 // Double & smi.
68 Expect.equals(26.0, a + b);
69 Expect.equals(18.0, a - b);
70 Expect.equals(88.0, a * b);
71 Expect.equals(5.0, a ~/ b);
72 Expect.equals(5.5, a / b);
73 Expect.equals(2.0, a % b);
74 Expect.equals(2.0, a.remainder(b));
75 a = 22.0;
76 b = 4.0;
77 // Double & double.
78 Expect.equals(26.0, a + b);
79 Expect.equals(18.0, a - b);
80 Expect.equals(88.0, a * b);
81 Expect.equals(5.0, a ~/ b);
82 Expect.equals(5.5, a / b);
83 Expect.equals(2.0, a % b);
84 Expect.equals(2.0, a.remainder(b));
85
86 // Special int operations.
87 Expect.equals(2, (2).floor());
88 Expect.equals(2, (2).ceil());
89 Expect.equals(2, (2).round());
90 Expect.equals(2, (2).truncate());
91
92 Expect.equals(-2, (-2).floor());
93 Expect.equals(-2, (-2).ceil());
94 Expect.equals(-2, (-2).round());
95 Expect.equals(-2, (-2).truncate());
96
97 // Note that this number fits into 53 bits of a double.
98 int big = 123456789012345;
99
100 Expect.equals(big, big.floor());
101 Expect.equals(big, big.ceil());
102 Expect.equals(big, big.round());
103 Expect.equals(big, big.truncate());
104 big = -big;
105 Expect.equals(big, big.floor());
106 Expect.equals(big, big.ceil());
107 Expect.equals(big, big.round());
108 Expect.equals(big, big.truncate());
109
110 // Test if double is contagious. The assignment will check the type.
111 { double d = 1 + 1.0; }
112 { double d = 1.0 + 1; }
113 { double d = 1 * 1.0; }
114 { double d = 0 * 1.0; }
115 { double d = 1.0 * 0; }
116 { double d = 1 / 1.0; }
117 { double d = 1.0 / 0; }
118 { double d = 1 - 1.0; }
119 { double d = 1.0 - 1; }
120 { double d = big * 1.0; }
121 { double d = 1.0 * big; }
122
123 // Reset big to positive value.
124 big = 123456789012345;
125 // -- isNegative --.
126 // Smi.
127 Expect.equals(false, (0).isNegative());
128 Expect.equals(false, (1).isNegative());
129 Expect.equals(true, (-1).isNegative());
130 // Big.
131 Expect.equals(false, big.isNegative());
132 Expect.equals(true, (-big).isNegative());
133 // Double.
134 // TODO(srdjan): enable the following test once isNegative works.
135 // Expect.equals(true, (-0.0).isNegative());
136 Expect.equals(false, (0.0).isNegative());
137 Expect.equals(false, (2.0).isNegative());
138 Expect.equals(true, (-2.0).isNegative());
139
140 double negateDouble(double x) {
141 return -x;
142 }
143
144 Expect.isTrue(negateDouble(0.0).isNegative());
145 Expect.isFalse(negateDouble(-0.0).isNegative());
146 Expect.isTrue(negateDouble(3.5e3).isNegative());
147 Expect.isFalse(negateDouble(-3.5e3).isNegative());
148
149
150 // Constants.
151 final nan = 0.0/0.0;
152 final infinity = 1.0/0.0;
153
154 // -- isInfinite --.
155 // Smi.
156 Expect.equals(false, (0).isInfinite());
157 Expect.equals(false, (1).isInfinite());
158 Expect.equals(false, (-1).isInfinite());
159 // Big.
160 Expect.equals(false, big.isInfinite());
161 Expect.equals(false, (-big).isInfinite());
162 // Double.
163 Expect.equals(false, (0.0).isInfinite());
164 Expect.equals(true, infinity.isInfinite());
165 Expect.equals(true, (-infinity).isInfinite());
166 Expect.equals(false, (12.0).isInfinite());
167 Expect.equals(false, (-12.0).isInfinite());
168 Expect.equals(false, nan.isInfinite());
169
170 // -- isNaN --.
171 // Smi.
172 Expect.equals(false, (0).isNaN());
173 Expect.equals(false, (1).isNaN());
174 Expect.equals(false, (-1).isNaN());
175 // Big.
176 Expect.equals(false, big.isNaN());
177 Expect.equals(false, (-big).isNaN());
178 // Double.
179 Expect.equals(true, nan.isNaN());
180 Expect.equals(false, (12.0).isNaN());
181 Expect.equals(false, infinity.isNaN());
182
183 // -- abs --.
184 // Smi.
185 Expect.equals(0, (0).abs());
186 Expect.equals(2, (2).abs());
187 Expect.equals(2, (-2).abs());
188 // Big.
189 Expect.equals(big, big.abs());
190 Expect.equals(big, (-big).abs());
191 // Double.
192 Expect.equals(false, (0.0).abs().isNegative());
193 Expect.equals(false, (-0.0).abs().isNegative());
194 Expect.equals(2.0, (2.0).abs());
195 Expect.equals(2.0, (-2.0).abs());
196
197 // -- ceil --.
198 // Smi.
199 Expect.equals(0, (0).ceil());
200 Expect.equals(1, (1).ceil());
201 Expect.equals(-1, (-1).ceil());
202 // Big.
203 Expect.equals(big, big.ceil());
204 Expect.equals(-big, (-big).ceil());
205 // Double.
206 Expect.equals(0.0, (0.0).ceil());
207 Expect.equals(false, (0.0).ceil().isNegative());
208 Expect.equals(1.0, (0.1).ceil());
209 Expect.equals(-0.0, (-0.0).ceil());
210 Expect.equals(-0.0, (-0.3).ceil());
211 // TODO(srdjan): enable the following tests once isNegative works.
212 // Expect.equals(true, (-0.0).ceil().isNegative());
213 // Expect.equals(true, (-0.3).ceil().isNegative());
214 Expect.equals(3.0, (2.1).ceil());
215 Expect.equals(-2.0, (-2.1).ceil());
216
217 // -- floor --.
218 // Smi.
219 Expect.equals(0, (0).floor());
220 Expect.equals(1, (1).floor());
221 Expect.equals(-1, (-1).floor());
222 // Big.
223 Expect.equals(big, big.floor());
224 Expect.equals(-big, (-big).floor());
225 // Double.
226 Expect.equals(0.0, (0.0).floor());
227 Expect.equals(0.0, (0.1).floor());
228 Expect.equals(false, (0.0).floor().isNegative());
229 Expect.equals(false, (0.1).floor().isNegative());
230 Expect.equals(-0.0, (-0.0).floor());
231 // TODO(srdjan): enable the following tests once isNegative works.
232 // Expect.equals(true, (-0.0).floor().isNegative());
233 Expect.equals(-1.0, (-0.1).floor());
234 Expect.equals(2.0, (2.1).floor());
235 Expect.equals(-3.0, (-2.1).floor());
236
237 // -- truncate --.
238 // Smi.
239 Expect.equals(0, (0).truncate());
240 Expect.equals(1, (1).truncate());
241 Expect.equals(-1, (-1).truncate());
242 // Big.
243 Expect.equals(big, big.truncate());
244 Expect.equals(-big, (-big).truncate());
245 // Double.
246 Expect.equals(0.0, (0.0).truncate());
247 Expect.equals(0.0, (0.1).truncate());
248 Expect.equals(false, (0.0).truncate().isNegative());
249 Expect.equals(false, (0.1).truncate().isNegative());
250 Expect.equals(-0.0, (-0.0).truncate());
251 Expect.equals(-0.0, (-0.3).truncate());
252 // TODO(srdjan): enable the following tests once isNegative works.
253 // Expect.equals(true, (-0.0).truncate().isNegative());
254 // Expect.equals(true, (-0.3).truncate().isNegative());
255 Expect.equals(2.0, (2.1).truncate());
256 Expect.equals(-2.0, (-2.1).truncate());
257
258 double b1 = (1234567890123.0).truncate();
259 double b2 = (1234567890124.0).truncate();
260 Expect.equals(b2, b1 + 1.0);
261
262 // -- round --.
263 // Smi.
264 Expect.equals(0, (0).round());
265 Expect.equals(1, (1).round());
266 Expect.equals(-1, (-1).round());
267 // Big.
268 Expect.equals(big, big.round());
269 Expect.equals(-big, (-big).round());
270 // Double.
271 Expect.equals(3.0, (2.6).round());
272 Expect.equals(-3.0, (-2.6).round());
273 Expect.equals(0.0, (0.0).round());
274 Expect.equals(0.0, (0.1).round());
275 Expect.equals(false, (0.0).round().isNegative());
276 Expect.equals(false, (0.1).round().isNegative());
277 Expect.equals(-0.0, (-0.0).round());
278 Expect.equals(-0.0, (-0.3).round());
279 Expect.equals(2.0, (2.1).round());
280 Expect.equals(-2.0, (-2.1).round());
281 Expect.equals(1.0, (0.5).round());
282 // TODO(floitsch): enable or adapt test, once we reached conclusion on
283 // b/4539188.
284 // Expect.equals(-0.0, (-0.5).round());
285 // TODO(srdjan): enable the following tests once isNegative works.
286 // Expect.equals(true, (-0.0).round().isNegative());
287 // Expect.equals(true, (-0.3).round().isNegative());
288 // Expect.equals(true, (-0.5).round().isNegative());
289 Expect.equals(2.0, (1.5).round());
290 // TODO(floitsch): enable or adapt test, once we reached conclusion on
291 // b/4539188.
292 // Expect.equals(-1.0, (-1.5).round());
293 Expect.equals(1.0, (0.99).round());
294
295 // -- toInt --.
296 // Smi.
297 Expect.equals(0, (0).toInt());
298 Expect.equals(1, (1).toInt());
299 Expect.equals(-1, (-1).toInt());
300 // Type checks.
301 { int i = (0).toInt(); }
302 { int i = (1).toInt(); }
303 { int i = (-1).toInt(); }
304 // Big.
305 Expect.equals(big, big.toInt());
306 Expect.equals(-big, (-big).toInt());
307 { int i = big.toInt(); }
308 { int i = (-big).toInt(); }
309 // Double.
310 Expect.equals(1234567890123, (1234567890123.0).toInt());
311 Expect.equals(-1234567890123, (-1234567890123.0).toInt());
312 { int i = (1234567890123.0).toInt(); }
313 { int i = (-1234567890123.0).toInt(); }
314 // 32bit Smi border cases.
315 Expect.equals(-1073741824, (-1073741824.0).toInt());
316 Expect.equals(-1073741825, (-1073741825.0).toInt());
317 Expect.equals(1073741823, (1073741823.0).toInt());
318 Expect.equals(1073741824, (1073741824.0).toInt());
319 { int i = (-1073741824.0).toInt(); }
320 { int i = (-1073741825.0).toInt(); }
321 { int i = (1073741823.0).toInt(); }
322 { int i = (1073741824.0).toInt(); }
323
324 // -- toDouble --.
325 // Smi.
326 Expect.equals(0.0, (0).toDouble());
327 Expect.equals(1.0, (1).toDouble());
328 Expect.equals(-1.0, (-1).toDouble());
329 // Type checks.
330 { double d = (0).toDouble(); }
331 { double d = (1).toDouble(); }
332 { double d = (-1).toDouble(); }
333 // Big.
334 Expect.equals(big, big.toInt());
335 Expect.equals(-big, (-big).toInt());
336 { int i = big.toInt(); }
337 { int i = (-big).toInt(); }
338
339 // Math functions.
340 Expect.equals(2.0, Math.sqrt(4.0));
341 Expect.approxEquals(1.0, Math.sin(3.14159265 / 2.0));
342 Expect.approxEquals(-1.0, Math.cos(3.14159265));
343
344 Expect.equals(12, Math.parseInt("12"));
345 Expect.equals(-12, Math.parseInt("-12"));
346 Expect.equals(12345678901234567890,
347 Math.parseInt("12345678901234567890"));
348 Expect.equals(-12345678901234567890,
349 Math.parseInt("-12345678901234567890"));
350 // Type checks.
351 { int i = Math.parseInt("12"); }
352 { int i = Math.parseInt("-12"); }
353 { int i = Math.parseInt("12345678901234567890"); }
354 { int i = Math.parseInt("-12345678901234567890"); }
355
356 Expect.equals(1.2, Math.parseDouble("1.2"));
357 Expect.equals(-1.2, Math.parseDouble("-1.2"));
358 // Type checks.
359 { double d = Math.parseDouble("1.2"); }
360 { double d = Math.parseDouble("-1.2"); }
361 { double d = Math.parseDouble("0"); }
362
363 // Random
364 { double d = Math.random(); }
365
366 Expect.equals(false, exceptionCaughtParseInt("22"));
367 Expect.equals(true, exceptionCaughtParseInt("alpha"));
368 Expect.equals(true, exceptionCaughtParseInt("-alpha"));
369 Expect.equals(false, exceptionCaughtParseDouble("22.2"));
370 Expect.equals(true, exceptionCaughtParseDouble("alpha"));
371 Expect.equals(true, exceptionCaughtParseDouble("-alpha"));
372
373 Expect.equals(false, Math.parseDouble("1.2").isNaN());
374 Expect.equals(false, Math.parseDouble("1.2").isInfinite());
375
376 Expect.equals(true, Math.parseDouble("NaN").isNaN());
377 Expect.equals(true, Math.parseDouble("Infinity").isInfinite());
378 Expect.equals(true, Math.parseDouble("-Infinity").isInfinite());
379
380 Expect.equals(false, Math.parseDouble("NaN").isNegative());
381 Expect.equals(false, Math.parseDouble("Infinity").isNegative());
382 Expect.equals(true, Math.parseDouble("-Infinity").isNegative());
383
384 Expect.equals("NaN", Math.parseDouble("NaN").toString());
385 Expect.equals("Infinity", Math.parseDouble("Infinity").toString());
386 Expect.equals("-Infinity", Math.parseDouble("-Infinity").toString());
387
388 Expect.equals(false, toIntThrowsBadNumberFormatException("1.2"));
389 Expect.equals(true, toIntThrowsBadNumberFormatException("Infinity"));
390 Expect.equals(true, toIntThrowsBadNumberFormatException("-Infinity"));
391 Expect.equals(true, toIntThrowsBadNumberFormatException("NaN"));
392
393 // Min/max
394 Expect.equals(1, Math.min(1, 12));
395 Expect.equals(12, Math.max(1, 12));
396 Expect.equals(1.0, Math.min(1.0, 12.0));
397 Expect.equals(12.0, Math.max(1.0, 12.0));
398 Expect.equals(false, 1.0 < Math.min(1.0, 12.0));
399 Expect.equals(true, 1.0 < Math.max(1.0, 12.0));
400
401 // Hashcode
402 Expect.equals(false, (3.4).hashCode() == (1.2).hashCode());
403 Expect.equals(true, (1.2).hashCode() == (1.2).hashCode());
404 Expect.equals(false, (3).hashCode() == (1).hashCode());
405 Expect.equals(true, (10).hashCode() == (10).hashCode());
406 }
407
408 static testMain() {
409 for (int i = 0; i < 1500; i++) {
410 runOne();
411 }
412 }
413 }
414
415 main() {
416 ArithmeticTest.testMain();
417 }
OLDNEW
« no previous file with comments | « tests/language/src/Arithmetic2Test.dart ('k') | tests/language/src/AssertKeywordNegativeTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698