blob: 65894a3c6fe9c15a0b182fa01e6703887de71b31 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom9f30b382011-08-28 22:41:38 -070016
buzbee4a3164f2011-09-03 11:25:10 -070017class IntMath extends IntMathBase {
Brian Carlstrom9f30b382011-08-28 22:41:38 -070018
19 public static boolean mBoolean1, mBoolean2;
20 public static byte mByte1, mByte2;
21 public static char mChar1, mChar2;
22 public static short mShort1, mShort2;
23 public static int mInt1, mInt2;
24 public static float mFloat1, mFloat2;
25 public static long mLong1, mLong2;
26 public static double mDouble1, mDouble2;
27 public static volatile long mVolatileLong1, mVolatileLong2;
28
29
30 private int foo_;
31
32 public IntMath(int stuff) {
buzbee4a3164f2011-09-03 11:25:10 -070033 super();
Brian Carlstrom9f30b382011-08-28 22:41:38 -070034 foo_ = stuff;
35 }
36
37 public IntMath() {
buzbee4a3164f2011-09-03 11:25:10 -070038 super();
Brian Carlstrom9f30b382011-08-28 22:41:38 -070039 foo_ = 123;
40 }
41
buzbeef0cde542011-09-13 14:55:02 -070042 /* Regression test: triggered an SSA renaming bug. */
43 static long divideLongByBillion(long a) {
44 long quot;
45 long rem;
46
47 if (a >= 0) {
48 long bLong = 1000000000L;
49 quot = (a / bLong);
50 rem = (a % bLong);
51 } else {
52 /*
53 * Make the dividend positive shifting it right by 1 bit then get
54 * the quotient an remainder and correct them properly
55 */
56 long aPos = a >>> 1;
57 long bPos = 1000000000L >>> 1;
58 quot = aPos / bPos;
59 rem = aPos % bPos;
60 // double the remainder and add 1 if 'a' is odd
61 rem = (rem << 1) + (a & 1);
62 }
63 return ((rem << 32) | (quot & 0xFFFFFFFFL));
64 }
65
66
buzbee2a475e72011-09-07 17:19:17 -070067 static int instanceTest(int x) {
68 IntMathBase a = new IntMathBase();
69 IntMath b = new IntMath();
70
Brian Carlstrom5d40f182011-09-26 22:29:18 -070071 if (!(null instanceof IntMathBase)) {
72 x = x + 42;
73 }
74
buzbee2a475e72011-09-07 17:19:17 -070075 if (a instanceof IntMathBase) {
76 x = x * 2;
77 }
78
79 if (a instanceof IntMath) {
80 x = x + 13;
81 }
82
83 if (b instanceof IntMathBase) {
84 x = x -1;
85 }
86
87 if (b instanceof IntMath) {
88 x = x + 1333;
89 }
90 return x;
91 }
92
buzbee4a3164f2011-09-03 11:25:10 -070093 int tryThing() {
94 int val = super.tryThing();
95 return val + 10;
96 }
97
98 static int superTest(int x) {
99 IntMath instance = new IntMath();
100 IntMath base = instance;
101 int val1 = instance.tryThing();
102 int val2 = base.tryThing();
103 return val1 + val2 + x;
104 }
105
buzbee561227c2011-09-02 15:28:19 -0700106 static int constClassTest(int x) {
107 Class c = String.class;
108 if (c != null) {
109 return x * 2;
110 } else {
111 return x;
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700112 }
buzbee561227c2011-09-02 15:28:19 -0700113 }
114
buzbee1b4c8592011-08-31 10:43:51 -0700115 static int constStringTest(int x) {
buzbee1b4c8592011-08-31 10:43:51 -0700116 String str = "Hello World!";
buzbee2a475e72011-09-07 17:19:17 -0700117 return x + str.length();
buzbee1b4c8592011-08-31 10:43:51 -0700118 }
119
120 static void throwNullPointerException() {
121 throw new NullPointerException();
122 }
123
Ian Rogers93dd9662011-09-17 23:21:22 -0700124 static void throwImplicitNullPointerException() {
125 throw null;
126 }
127
buzbee1b4c8592011-08-31 10:43:51 -0700128 static int catchBlock(int x) {
129 try {
Ian Rogers93dd9662011-09-17 23:21:22 -0700130 if (x == 1000) {
131 x += 123;
132 throwNullPointerException();
133 } else {
134 x += 321;
135 throwImplicitNullPointerException();
136 }
buzbee1b4c8592011-08-31 10:43:51 -0700137 } catch (NullPointerException npe) {
138 x += 456;
139 }
140 return x;
141 }
142
buzbeee9a72f62011-09-04 17:59:07 -0700143 static int catchBlockNoThrow(int x) {
144 try {
145 x += 123;
146 } catch (NullPointerException npe) {
147 x += 456;
148 }
149 return x;
150 }
151
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700152 static int staticFieldTest(int x) {
153 mBoolean1 = true;
154 mBoolean2 = false;
155 mByte1 = 127;
156 mByte2 = -128;
157 mChar1 = 32767;
158 mChar2 = 65535;
159 mShort1 = 32767;
160 mShort2 = -32768;
161 mInt1 = 65537;
162 mInt2 = -65537;
163 mFloat1 = 3.1415f;
164 mFloat2 = -1.0f / 0.0f; // -inf
165 mLong1 = 1234605616436508552L; // 0x1122334455667788
166 mLong2 = -1234605616436508552L;
167 mDouble1 = 3.1415926535;
168 mDouble2 = 1.0 / 0.0; // +inf
169 mVolatileLong1 = mLong1 - 1;
170 mVolatileLong2 = mLong2 + 1;
171
172 if (!mBoolean1) { return 10; }
173 if (mBoolean2) { return 11; }
174 if (mByte1 != 127) { return 12; }
175 if (mByte2 != -128) { return 13; }
176 if (mChar1 != 32767) { return 14; }
177 if (mChar2 != 65535) { return 15; }
178 if (mShort1 != 32767) { return 16; }
179 if (mShort2 != -32768) { return 17; }
180 if (mInt1 != 65537) { return 18; }
181 if (mInt2 != -65537) { return 19; }
182 if (!(mFloat1 > 3.141f && mFloat1 < 3.142f)) { return 20; }
183 if (mFloat2 >= mFloat1) { return 21; }
184 if (mLong1 != 1234605616436508552L) { return 22; }
185 if (mLong2 != -1234605616436508552L) { return 23; }
186 if (!(mDouble1 > 3.141592653 && mDouble1 < 3.141592654)) { return 24; }
187 if (mDouble2 <= mDouble1) { return 25; }
188 if (mVolatileLong1 != 1234605616436508551L) { return 26; }
189 if (mVolatileLong2 != -1234605616436508551L) { return 27; }
190
191 return 1000 + x;
192 }
193
194 /*
195 * Try to cause some unary operations.
196 */
197 static int unopTest(int x) {
198 x = -x;
199 x ^= 0xffffffff;
200 return x;
201 }
202
203 static int shiftTest1() {
204 final int[] mBytes = {
205 0x11, 0x22, 0x33, 0x44, 0x88, 0x99, 0xaa, 0xbb
206 };
207 long l;
208 int i1, i2;
209
210 if (mBytes[0] != 0x11) return 20;
211 if (mBytes[1] != 0x22) return 21;
212 if (mBytes[2] != 0x33) return 22;
213 if (mBytes[3] != 0x44) return 23;
214 if (mBytes[4] != 0x88) return 24;
215 if (mBytes[5] != 0x99) return 25;
216 if (mBytes[6] != 0xaa) return 26;
217 if (mBytes[7] != 0xbb) return 27;
218
219 i1 = mBytes[0] | mBytes[1] << 8 | mBytes[2] << 16 | mBytes[3] << 24;
220 i2 = mBytes[4] | mBytes[5] << 8 | mBytes[6] << 16 | mBytes[7] << 24;
221 l = i1 | ((long)i2 << 32);
222
223 if (i1 != 0x44332211) { return 0x80000000 | i1; }
224 if (i2 != 0xbbaa9988) { return 2; }
225 if (l != 0xbbaa998844332211L) { return 3; }
226
227 l = (long)mBytes[0]
228 | (long)mBytes[1] << 8
229 | (long)mBytes[2] << 16
230 | (long)mBytes[3] << 24
231 | (long)mBytes[4] << 32
232 | (long)mBytes[5] << 40
233 | (long)mBytes[6] << 48
234 | (long)mBytes[7] << 56;
235
236 if (l != 0xbbaa998844332211L) { return 4; }
237 return 0;
238 }
239
240 static int shiftTest2() {
241
242 long a = 0x11;
243 long b = 0x22;
244 long c = 0x33;
245 long d = 0x44;
246 long e = 0x55;
247 long f = 0x66;
248 long g = 0x77;
249 long h = 0x88;
250
251 long result = ((a << 56) | (b << 48) | (c << 40) | (d << 32) |
252 (e << 24) | (f << 16) | (g << 8) | h);
253
254 if (result != 0x1122334455667788L) { return 1; }
255 return 0;
256 }
257
258 static int unsignedShiftTest() {
259 byte b = -4;
260 short s = -4;
261 char c = 0xfffc;
262 int i = -4;
263
264 b >>>= 4;
265 s >>>= 4;
266 c >>>= 4;
267 i >>>= 4;
268
269 if ((int) b != -1) { return 1; }
270 if ((int) s != -1) { return 2; }
271 if ((int) c != 0x0fff) { return 3; }
272 if (i != 268435455) { return 4; }
273 return 0;
274 }
275
276 static int convTest() {
277
278 float f;
279 double d;
280 int i;
281 long l;
282
283 /* int --> long */
284 i = 7654;
285 l = (long) i;
286 if (l != 7654L) { return 1; }
287
288 i = -7654;
289 l = (long) i;
290 if (l != -7654L) { return 2; }
291
292 /* long --> int (with truncation) */
293 l = 5678956789L;
294 i = (int) l;
295 if (i != 1383989493) { return 3; }
296
297 l = -5678956789L;
298 i = (int) l;
299 if (i != -1383989493) { return 4; }
300 return 0;
301 }
302
303 static int charSubTest() {
304
305 char char1 = 0x00e9;
306 char char2 = 0xffff;
307 int i;
308
309 /* chars are unsigned-expanded to ints before subtraction */
310 i = char1 - char2;
311 if (i != 0xffff00ea) { return 1; }
312 return 0;
313 }
314
315 /*
316 * We pass in the arguments and return the results so the compiler
317 * doesn't do the math for us. (x=70000, y=-3)
318 */
319 static int intOperTest(int x, int y) {
320 int[] results = new int[10];
321
322 /* this seems to generate "op-int" instructions */
323 results[0] = x + y;
324 results[1] = x - y;
325 results[2] = x * y;
326 results[3] = x * x;
327 results[4] = x / y;
328 results[5] = x % -y;
329 results[6] = x & y;
330 results[7] = x | y;
331 results[8] = x ^ y;
332
333 /* this seems to generate "op-int/2addr" instructions */
334 results[9] = x + ((((((((x + y) - y) * y) / y) % y) & y) | y) ^ y);
335
336 /* check this edge case while we're here (div-int/2addr) */
337 int minInt = -2147483648;
338 int negOne = -results[5];
339 int plusOne = 1;
340 int result = (((minInt + plusOne) - plusOne) / negOne) / negOne;
TDYa127f8641ce2012-04-02 06:40:40 -0700341 int shouldBeZero = minInt % negOne;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700342
343 if (result != minInt) { return 1;};
344 if (results[0] != 69997) { return 2;};
345 if (results[1] != 70003) { return 3;};
346 if (results[2] != -210000) { return 4;};
347 if (results[3] != 605032704) { return 5;};
348 if (results[4] != -23333) { return 6;};
349 if (results[5] != 1) { return 7;};
350 if (results[6] != 70000) { return 8;};
351 if (results[7] != -3) { return 9;};
352 if (results[8] != -70003) { return 10;};
353 if (results[9] != 70000) { return 11;};
TDYa127f8641ce2012-04-02 06:40:40 -0700354 if (shouldBeZero != 0) { return 12;};
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700355
356 return 0;
357 }
358
359 /*
360 * More operations, this time with 16-bit constants. (x=77777)
361 */
362 static int lit16Test(int x) {
363
364 int[] results = new int[8];
365
366 /* try to generate op-int/lit16" instructions */
367 results[0] = x + 1000;
368 results[1] = 1000 - x;
369 results[2] = x * 1000;
370 results[3] = x / 1000;
371 results[4] = x % 1000;
372 results[5] = x & 1000;
373 results[6] = x | -1000;
374 results[7] = x ^ -1000;
375
376 if (results[0] != 78777) { return 1; }
377 if (results[1] != -76777) { return 2; }
378 if (results[2] != 77777000) { return 3; }
379 if (results[3] != 77) { return 4; }
380 if (results[4] != 777) { return 5; }
381 if (results[5] != 960) { return 6; }
382 if (results[6] != -39) { return 7; }
383 if (results[7] != -76855) { return 8; }
384 return 0;
385 }
386
387 /*
388 * More operations, this time with 8-bit constants. (x=-55555)
389 */
390 static int lit8Test(int x) {
391
392 int[] results = new int[8];
393
394 /* try to generate op-int/lit8" instructions */
395 results[0] = x + 10;
396 results[1] = 10 - x;
397 results[2] = x * 10;
398 results[3] = x / 10;
399 results[4] = x % 10;
400 results[5] = x & 10;
401 results[6] = x | -10;
402 results[7] = x ^ -10;
403 int minInt = -2147483648;
404 int result = minInt / -1;
405 if (result != minInt) {return 1; }
406 if (results[0] != -55545) {return 2; }
407 if (results[1] != 55565) {return 3; }
408 if (results[2] != -555550) {return 4; }
409 if (results[3] != -5555) {return 5; }
410 if (results[4] != -5) {return 6; }
411 if (results[5] != 8) {return 7; }
412 if (results[6] != -1) {return 8; }
413 if (results[7] != 55563) {return 9; }
414 return 0;
415 }
416
417
418 /*
419 * Shift some data. (value=0xff00aa01, dist=8)
420 */
421 static int intShiftTest(int value, int dist) {
422 int results[] = new int[4];
423 results[0] = value << dist;
424 results[1] = value >> dist;
425 results[2] = value >>> dist;
426 results[3] = (((value << dist) >> dist) >>> dist) << dist;
427 if (results[0] != 0x00aa0100) {return 1; }
428 if (results[1] != 0xffff00aa) {return 2; }
429 if (results[2] != 0x00ff00aa) {return 3; }
430 if (results[3] != 0xaa00) {return 4; }
431 return 0;
432 }
433
434 /*
435 * We pass in the arguments and return the results so the compiler
436 * doesn't do the math for us. (x=70000000000, y=-3)
437 */
438 static int longOperTest(long x, long y) {
439 long[] results = new long[10];
440
441 /* this seems to generate "op-long" instructions */
442 results[0] = x + y;
443 results[1] = x - y;
444 results[2] = x * y;
445 results[3] = x * x;
446 results[4] = x / y;
447 results[5] = x % -y;
448 results[6] = x & y;
449 results[7] = x | y;
450 results[8] = x ^ y;
451 /* this seems to generate "op-long/2addr" instructions */
452 results[9] = x + ((((((((x + y) - y) * y) / y) % y) & y) | y) ^ y);
453 /* check this edge case while we're here (div-long/2addr) */
454 long minLong = -9223372036854775808L;
455 long negOne = -results[5];
456 long plusOne = 1;
457 long result = (((minLong + plusOne) - plusOne) / negOne) / negOne;
458 if (result != minLong) { return 1; }
459 if (results[0] != 69999999997L) { return 2; }
460 if (results[1] != 70000000003L) { return 3; }
461 if (results[2] != -210000000000L) { return 4; }
462 if (results[3] != -6833923606740729856L) { return 5; } // overflow
463 if (results[4] != -23333333333L) { return 6; }
464 if (results[5] != 1) { return 7; }
465 if (results[6] != 70000000000L) { return 8; }
466 if (results[7] != -3) { return 9; }
467 if (results[8] != -70000000003L) { return 10; }
468 if (results[9] != 70000000000L) { return 11; }
469 if (results.length != 10) { return 12; }
470 return 0;
471 }
472
473 /*
474 * Shift some data. (value=0xd5aa96deff00aa01, dist=16)
475 */
476 static long longShiftTest(long value, int dist) {
477 long results[] = new long[4];
478 results[0] = value << dist;
479 results[1] = value >> dist;
480 results[2] = value >>> dist;
481 results[3] = (((value << dist) >> dist) >>> dist) << dist;
482 if (results[0] != 0x96deff00aa010000L) { return results[0]; }
483 if (results[1] != 0xffffd5aa96deff00L) { return results[1]; }
484 if (results[2] != 0x0000d5aa96deff00L) { return results[2]; }
485 if (results[3] != 0xffff96deff000000L) { return results[3]; }
486 if (results.length != 4) { return 5; }
487
488 return results[0]; // test return-long
489 }
490
491 static int switchTest(int a) {
492 int res = 1234;
493
494 switch (a) {
495 case -1: res = 1; return res;
496 case 0: res = 2; return res;
497 case 1: /*correct*/ break;
498 case 2: res = 3; return res;
499 case 3: res = 4; return res;
500 case 4: res = 5; return res;
501 default: res = 6; return res;
502 }
503 switch (a) {
504 case 3: res = 7; return res;
505 case 4: res = 8; return res;
506 default: /*correct*/ break;
507 }
508
509 a = 0x12345678;
510
511 switch (a) {
512 case 0x12345678: /*correct*/ break;
513 case 0x12345679: res = 9; return res;
514 default: res = 1; return res;
515 }
516 switch (a) {
517 case 57: res = 10; return res;
518 case -6: res = 11; return res;
519 case 0x12345678: /*correct*/ break;
520 case 22: res = 12; return res;
521 case 3: res = 13; return res;
522 default: res = 14; return res;
523 }
524 switch (a) {
525 case -6: res = 15; return res;
526 case 3: res = 16; return res;
527 default: /*correct*/ break;
528 }
529
530 a = -5;
531 switch (a) {
532 case 12: res = 17; return res;
533 case -5: /*correct*/ break;
534 case 0: res = 18; return res;
535 default: res = 19; return res;
536 }
537
538 switch (a) {
539 default: /*correct*/ break;
540 }
541 return res;
542 }
543 /*
544 * Test the integer comparisons in various ways.
545 */
546 static int testIntCompare(int minus, int plus, int plus2, int zero) {
547 int res = 1111;
548
549 if (minus > plus)
550 return 1;
551 if (minus >= plus)
552 return 2;
553 if (plus < minus)
554 return 3;
555 if (plus <= minus)
556 return 4;
557 if (plus == minus)
558 return 5;
559 if (plus != plus2)
560 return 6;
561
562 /* try a branch-taken */
563 if (plus != minus) {
564 res = res;
565 } else {
566 return 7;
567 }
568
569 if (minus > 0)
570 return 8;
571 if (minus >= 0)
572 return 9;
573 if (plus < 0)
574 return 10;
575 if (plus <= 0)
576 return 11;
577 if (plus == 0)
578 return 12;
579 if (zero != 0)
580 return 13;
581
582 if (zero == 0) {
583 res = res;
584 } else {
585 return 14;
586 }
587 return res;
588 }
589
590 /*
591 * Test cmp-long.
592 *
593 * minus=-5, alsoMinus=0xFFFFFFFF00000009, plus=4, alsoPlus=8
594 */
595 static int testLongCompare(long minus, long alsoMinus, long plus,
596 long alsoPlus) {
597 int res = 2222;
598
599 if (minus > plus)
600 return 2;
601 if (plus < minus)
602 return 3;
603 if (plus == minus)
604 return 4;
605
606 if (plus >= plus+1)
607 return 5;
608 if (minus >= minus+1)
609 return 6;
610
611 /* try a branch-taken */
612 if (plus != minus) {
613 res = res;
614 } else {
615 return 7;
616 }
617
618 /* compare when high words are equal but low words differ */
619 if (plus > alsoPlus)
620 return 8;
621 if (alsoPlus < plus)
622 return 9;
623 if (alsoPlus == plus)
624 return 10;
625
626 /* high words are equal, low words have apparently different signs */
627 if (minus < alsoMinus) // bug!
628 return 11;
629 if (alsoMinus > minus)
630 return 12;
631 if (alsoMinus == minus)
632 return 13;
633
634 return res;
635 }
636
637 /*
638 * Test cmpl-float and cmpg-float.
639 */
640 static int testFloatCompare(float minus, float plus, float plus2,
641 float nan) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700642 if (minus > plus)
jeffhao644d5312012-05-03 19:04:49 -0700643 return 1;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700644 if (plus < minus)
jeffhao644d5312012-05-03 19:04:49 -0700645 return 2;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700646 if (plus == minus)
jeffhao644d5312012-05-03 19:04:49 -0700647 return 3;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700648 if (plus != plus2)
jeffhao644d5312012-05-03 19:04:49 -0700649 return 4;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700650
651 if (plus <= nan)
jeffhao644d5312012-05-03 19:04:49 -0700652 return 5;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700653 if (plus >= nan)
jeffhao644d5312012-05-03 19:04:49 -0700654 return 6;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700655 if (minus <= nan)
jeffhao644d5312012-05-03 19:04:49 -0700656 return 7;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700657 if (minus >= nan)
jeffhao644d5312012-05-03 19:04:49 -0700658 return 8;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700659 if (nan >= plus)
jeffhao644d5312012-05-03 19:04:49 -0700660 return 9;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700661 if (nan <= plus)
jeffhao644d5312012-05-03 19:04:49 -0700662 return 10;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700663
664 if (nan == nan)
jeffhao644d5312012-05-03 19:04:49 -0700665 return 11;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700666
jeffhao644d5312012-05-03 19:04:49 -0700667 return 3333;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700668 }
669
670 static int testDoubleCompare(double minus, double plus, double plus2,
671 double nan) {
672
673 int res = 4444;
674
675 if (minus > plus)
676 return 1;
677 if (plus < minus)
678 return 2;
679 if (plus == minus)
680 return 3;
681 if (plus != plus2)
682 return 4;
683
684 if (plus <= nan)
685 return 5;
686 if (plus >= nan)
687 return 6;
688 if (minus <= nan)
689 return 7;
690 if (minus >= nan)
691 return 8;
692 if (nan >= plus)
693 return 9;
694 if (nan <= plus)
695 return 10;
696
697 if (nan == nan)
698 return 11;
699 return res;
700 }
701
702 static int fibonacci(int n) {
703 if (n == 0) {
704 return 0;
705 } else if (n == 1) {
706 return 1;
707 } else {
708 return fibonacci(n - 1) + fibonacci(n - 2);
709 }
710 }
711
buzbeee9a72f62011-09-04 17:59:07 -0700712 static int throwAndCatch() {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700713 try {
714 throwNullPointerException();
715 return 1;
716 } catch (NullPointerException npe) {
717 return 0;
718 }
buzbeee9a72f62011-09-04 17:59:07 -0700719 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700720
721 static int manyArgs(int a0, long a1, int a2, long a3, int a4, long a5,
722 int a6, int a7, double a8, float a9, double a10, short a11, int a12,
723 char a13, int a14, int a15, byte a16, boolean a17, int a18, int a19,
724 long a20, long a21, int a22, int a23, int a24, int a25, int a26)
725 {
726 if (a0 != 0) return 0;
727 if (a1 != 1L) return 1;
728 if (a2 != 2) return 2;
729 if (a3 != 3L) return 3;
730 if (a4 != 4) return 4;
731 if (a5 != 5L) return 5;
732 if (a6 != 6) return 6;
733 if (a7 != 7) return 7;
734 if (a8 != 8.0) return 8;
735 if (a9 != 9.0f) return 9;
736 if (a10 != 10.0) return 10;
737 if (a11 != (short)11) return 11;
738 if (a12 != 12) return 12;
739 if (a13 != (char)13) return 13;
740 if (a14 != 14) return 14;
741 if (a15 != 15) return 15;
742 if (a16 != (byte)-16) return 16;
743 if (a17 != true) return 17;
744 if (a18 != 18) return 18;
745 if (a19 != 19) return 19;
746 if (a20 != 20L) return 20;
747 if (a21 != 21L) return 21;
748 if (a22 != 22) return 22;
749 if (a23 != 23) return 23;
750 if (a24 != 24) return 24;
751 if (a25 != 25) return 25;
752 if (a26 != 26) return 26;
753 return -1;
754 }
755
756 int virtualCall(int a)
757 {
758 return a * 2;
759 }
760
761 void setFoo(int a)
762 {
763 foo_ = a;
764 }
765
766 int getFoo()
767 {
768 return foo_;
769 }
770
771 static int staticCall(int a)
772 {
773 IntMath foo = new IntMath();
774 return foo.virtualCall(a);
775 }
776
777 static int testIGetPut(int a)
778 {
779 IntMath foo = new IntMath(99);
780 IntMath foo123 = new IntMath();
781 int z = foo.getFoo();
782 z += a;
783 z += foo123.getFoo();
784 foo.setFoo(z);
785 return foo.getFoo();
786 }
787
Ian Rogersff1ed472011-09-20 13:46:24 -0700788 static int throwClassCast(Object o) {
789 return ((Integer)o).intValue();
790 }
791
792 static int testClassCast() {
793 int res = 0;
794 try {
795 res += throwClassCast(Integer.valueOf(123));
796 } catch(ClassCastException e) {
797 res += 456;
798 }
799 try {
800 res += throwClassCast(new Short((short)321));
801 } catch(ClassCastException e) {
802 res += 765;
803 }
804 return res;
805 }
806
Ian Rogerse51a5112011-09-23 14:16:35 -0700807 static void throwArrayStoreException(Object[] array, Object element) {
808 array[0] = element;
809 }
810
811 static int testArrayStoreException() {
812 int res=0;
813 Object[] array = new Number[2];
814 try {
815 throwArrayStoreException(array, null);
816 res += 1;
817 } catch(ArrayStoreException e) {
818 res += 2;
819 }
820 try {
821 throwArrayStoreException(array, Integer.valueOf(1));
822 res += 10;
823 } catch(ArrayStoreException e) {
824 res += 20;
825 }
826 try {
827 throwArrayStoreException(array, "hello MTV-44");
828 res += 100;
829 } catch(ArrayStoreException e) {
830 res += 200;
831 }
832 return res;
833 }
834
Ian Rogers932746a2011-09-22 18:57:50 -0700835 static long recursion_count_;
836 static void throwStackOverflow(long l) {
837 recursion_count_++;
838 throwStackOverflow(recursion_count_);
839 }
840
841 static long testStackOverflow() {
842 try {
843 throwStackOverflow(0);
844 if (recursion_count_ != 0) {
845 return recursion_count_;
846 } else {
847 return -1;
848 }
849 } catch(StackOverflowError soe) {
850 return 0;
851 }
852 }
853
Ian Rogersb886da82011-09-23 16:27:54 -0700854 static int testArrayAllocation() {
855 int res = 0;
856 try {
857 int[] x = new int[-1];
858 res += 1;
859 } catch (NegativeArraySizeException e) {
860 res += 2;
861 }
862 try {
863 int[] x = new int [1];
864 res += 10;
865 } catch (Throwable e) {
866 res += 20;
867 }
868 return res;
869 }
870
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700871 public static void main(String[] args) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700872 boolean failure = false;
buzbeef0cde542011-09-13 14:55:02 -0700873 int res;
874 long lres;
875
876 lres = divideLongByBillion(123000000000L);
877 if (lres == 123) {
878 System.out.println("divideLongByBillion PASSED");
879 } else {
880 System.out.println("divideLongByBillion FAILED: " + lres);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700881 failure = true;
buzbeef0cde542011-09-13 14:55:02 -0700882 }
883 res = unopTest(38);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700884 if (res == 37) {
buzbee1da522d2011-09-04 11:22:20 -0700885 System.out.println("unopTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700886 } else {
buzbee1da522d2011-09-04 11:22:20 -0700887 System.out.println("unopTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700888 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700889 }
890 res = shiftTest1();
891 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700892 System.out.println("shiftTest1 PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700893 } else {
buzbee1da522d2011-09-04 11:22:20 -0700894 System.out.println("shiftTest1 FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700895 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700896 }
897 res = shiftTest2();
898 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700899 System.out.println("shiftTest2 PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700900 } else {
buzbee1da522d2011-09-04 11:22:20 -0700901 System.out.println("shiftTest2 FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700902 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700903 }
904 res = unsignedShiftTest();
905 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700906 System.out.println("unsignedShiftTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700907 } else {
buzbee1da522d2011-09-04 11:22:20 -0700908 System.out.println("unsignedShiftTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700909 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700910 }
911 res = convTest();
912 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700913 System.out.println("convTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700914 } else {
buzbee1da522d2011-09-04 11:22:20 -0700915 System.out.println("convTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700916 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700917 }
918 res = charSubTest();
919 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700920 System.out.println("charSubTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700921 } else {
buzbee1da522d2011-09-04 11:22:20 -0700922 System.out.println("charSubTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700923 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700924 }
925 res = intOperTest(70000, -3);
926 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700927 System.out.println("intOperTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700928 } else {
buzbee1da522d2011-09-04 11:22:20 -0700929 System.out.println("intOperTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700930 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700931 }
Brian Carlstrom25c33252011-09-18 15:58:35 -0700932 res = lit16Test(77777);
933 if (res == 0) {
934 System.out.println("lit16Test PASSED");
935 } else {
936 System.out.println("lit16Test FAILED: " + res);
937 failure = true;
938 }
939 res = lit8Test(-55555);
940 if (res == 0) {
941 System.out.println("lit8Test PASSED");
942 } else {
943 System.out.println("lit8Test FAILED: " + res);
944 failure = true;
945 }
946 res = intShiftTest(0xff00aa01, 8);
947 if (res == 0) {
948 System.out.println("intShiftTest PASSED");
949 } else {
950 System.out.println("intShiftTest FAILED: " + res);
951 failure = true;
952 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700953 res = longOperTest(70000000000L, -3L);
954 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700955 System.out.println("longOperTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700956 } else {
buzbee1da522d2011-09-04 11:22:20 -0700957 System.out.println("longOperTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700958 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700959 }
buzbeef0cde542011-09-13 14:55:02 -0700960 lres = longShiftTest(0xd5aa96deff00aa01L, 16);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700961 if (lres == 0x96deff00aa010000L) {
buzbee1da522d2011-09-04 11:22:20 -0700962 System.out.println("longShiftTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700963 } else {
buzbee1da522d2011-09-04 11:22:20 -0700964 System.out.println("longShiftTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700965 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700966 }
967
968 res = switchTest(1);
969 if (res == 1234) {
buzbee1da522d2011-09-04 11:22:20 -0700970 System.out.println("switchTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700971 } else {
buzbee1da522d2011-09-04 11:22:20 -0700972 System.out.println("switchTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700973 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700974 }
975
976 res = testIntCompare(-5, 4, 4, 0);
977 if (res == 1111) {
buzbee1da522d2011-09-04 11:22:20 -0700978 System.out.println("testIntCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700979 } else {
buzbee1da522d2011-09-04 11:22:20 -0700980 System.out.println("testIntCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700981 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700982 }
983
984 res = testLongCompare(-5L, -4294967287L, 4L, 8L);
985 if (res == 2222) {
buzbee1da522d2011-09-04 11:22:20 -0700986 System.out.println("testLongCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700987 } else {
buzbee1da522d2011-09-04 11:22:20 -0700988 System.out.println("testLongCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700989 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700990 }
991
992 res = testFloatCompare(-5.0f, 4.0f, 4.0f, (1.0f/0.0f) / (1.0f/0.0f));
993 if (res == 3333) {
buzbee1da522d2011-09-04 11:22:20 -0700994 System.out.println("testFloatCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700995 } else {
buzbee1da522d2011-09-04 11:22:20 -0700996 System.out.println("testFloatCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700997 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700998 }
999
1000 res = testDoubleCompare(-5.0, 4.0, 4.0, (1.0/0.0) / (1.0/0.0));
1001 if (res == 4444) {
buzbee1da522d2011-09-04 11:22:20 -07001002 System.out.println("testDoubleCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001003 } else {
buzbee1da522d2011-09-04 11:22:20 -07001004 System.out.println("testDoubleCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001005 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001006 }
1007
1008 res = fibonacci(10);
1009 if (res == 55) {
buzbee1da522d2011-09-04 11:22:20 -07001010 System.out.println("fibonacci PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001011 } else {
buzbee1da522d2011-09-04 11:22:20 -07001012 System.out.println("fibonacci FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001013 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001014 }
1015
buzbeee9a72f62011-09-04 17:59:07 -07001016 res = throwAndCatch();
1017 if (res == 0) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001018 System.out.println("throwAndCatch PASSED");
buzbeee9a72f62011-09-04 17:59:07 -07001019 } else {
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001020 System.out.println("throwAndCatch FAILED: " + res);
1021 failure = true;
buzbeee9a72f62011-09-04 17:59:07 -07001022 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001023
Ian Rogersff1ed472011-09-20 13:46:24 -07001024 res = testClassCast();
1025 if (res == 888) {
1026 System.out.println("testClassCast PASSED");
1027 } else {
1028 System.out.println("testClassCast FAILED: " + res);
1029 failure = true;
1030 }
1031
Ian Rogerse51a5112011-09-23 14:16:35 -07001032 res = testArrayStoreException();
1033 if (res == 211) {
1034 System.out.println("testArrayStore PASSED");
1035 } else {
1036 System.out.println("testArrayStore FAILED: " + res);
1037 failure = true;
1038 }
1039
Ian Rogers932746a2011-09-22 18:57:50 -07001040 lres= testStackOverflow();
1041 if (lres == 0) {
1042 System.out.println("testStackOverflow PASSED");
1043 } else {
1044 System.out.println("testStackOverflow FAILED: " + lres);
1045 failure = true;
1046 }
1047
Ian Rogersb886da82011-09-23 16:27:54 -07001048 res = testArrayAllocation();
1049 if (res == 12) {
1050 System.out.println("testArrayAllocation PASSED");
1051 } else {
1052 System.out.println("testArrayAllocation FAILED: " + res);
1053 failure = true;
1054 }
1055
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001056 res = manyArgs(0, 1L, 2, 3L, 4, 5L, 6, 7, 8.0, 9.0f, 10.0,
1057 (short)11, 12, (char)13, 14, 15, (byte)-16, true, 18,
1058 19, 20L, 21L, 22, 23, 24, 25, 26);
1059 if (res == -1) {
buzbee1da522d2011-09-04 11:22:20 -07001060 System.out.println("manyArgs PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001061 } else {
buzbee1da522d2011-09-04 11:22:20 -07001062 System.out.println("manyArgs FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001063 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001064 }
1065
1066 res = staticCall(3);
1067 if (res == 6) {
buzbee1da522d2011-09-04 11:22:20 -07001068 System.out.println("virtualCall PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001069 } else {
buzbee1da522d2011-09-04 11:22:20 -07001070 System.out.println("virtualCall FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001071 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001072 }
1073
1074 res = testIGetPut(111);
1075 if (res == 333) {
buzbee1da522d2011-09-04 11:22:20 -07001076 System.out.println("testGetPut PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001077 } else {
buzbee1da522d2011-09-04 11:22:20 -07001078 System.out.println("testGetPut FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001079 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001080 }
1081
1082 res = staticFieldTest(404);
1083 if (res == 1404) {
buzbee1da522d2011-09-04 11:22:20 -07001084 System.out.println("staticFieldTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001085 } else {
buzbee1da522d2011-09-04 11:22:20 -07001086 System.out.println("staticFieldTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001087 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001088 }
buzbee1b4c8592011-08-31 10:43:51 -07001089
1090 res = catchBlock(1000);
1091 if (res == 1579) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001092 System.out.println("catchBlock(1000) PASSED");
buzbee1b4c8592011-08-31 10:43:51 -07001093 } else {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001094 System.out.println("catchBlock(1000) FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001095 failure = true;
buzbee1b4c8592011-08-31 10:43:51 -07001096 }
Brian Carlstrom25c33252011-09-18 15:58:35 -07001097 res = catchBlock(7000);
1098 if (res == 7777) {
1099 System.out.println("catchBlock(7000) PASSED");
1100 } else {
1101 System.out.println("catchBlock(7000) FAILED: " + res);
1102 failure = true;
1103 }
buzbeee9a72f62011-09-04 17:59:07 -07001104 res = catchBlockNoThrow(1000);
1105 if (res == 1123) {
1106 System.out.println("catchBlockNoThrow PASSED");
1107 } else {
1108 System.out.println("catchBlockNoThrow FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001109 failure = true;
buzbeee9a72f62011-09-04 17:59:07 -07001110 }
1111
buzbee4a3164f2011-09-03 11:25:10 -07001112 res = superTest(4141);
1113 if (res == 4175) {
buzbee1da522d2011-09-04 11:22:20 -07001114 System.out.println("superTest PASSED");
buzbee4a3164f2011-09-03 11:25:10 -07001115 } else {
buzbee1da522d2011-09-04 11:22:20 -07001116 System.out.println("superTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001117 failure = true;
buzbee4a3164f2011-09-03 11:25:10 -07001118 }
buzbee2a475e72011-09-07 17:19:17 -07001119
Brian Carlstrom25c33252011-09-18 15:58:35 -07001120 res = constClassTest(1111);
1121 if (res == 2222) {
1122 System.out.println("constClassTest PASSED");
1123 } else {
1124 System.out.println("constClassTest FAILED: " + res);
1125 failure = true;
1126 }
1127
buzbee2a475e72011-09-07 17:19:17 -07001128 res = constStringTest(10);
1129 if (res == 22) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001130 System.out.println("constStringTest PASSED");
buzbee2a475e72011-09-07 17:19:17 -07001131 } else {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001132 System.out.println("constStringTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001133 failure = true;
buzbee2a475e72011-09-07 17:19:17 -07001134 }
1135
1136 res = instanceTest(10);
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001137 if (res == 1436) {
buzbee2a475e72011-09-07 17:19:17 -07001138 System.out.println("instanceTest PASSED");
1139 } else {
1140 System.out.println("instanceTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001141 failure = true;
buzbee2a475e72011-09-07 17:19:17 -07001142 }
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001143
1144 System.exit(failure ? 1 : 0);
buzbee4a3164f2011-09-03 11:25:10 -07001145 }
1146}
1147
1148class IntMathBase {
1149 IntMathBase() {
1150 }
1151
1152 int tryThing() {
1153 return 7;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001154 }
1155}