blob: b1b7312fb28e4b1ac6cbd64affefa16ad23a346e [file] [log] [blame]
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
buzbee4a3164f2011-09-03 11:25:10 -07003class IntMath extends IntMathBase {
Brian Carlstrom9f30b382011-08-28 22:41:38 -07004
5 public static boolean mBoolean1, mBoolean2;
6 public static byte mByte1, mByte2;
7 public static char mChar1, mChar2;
8 public static short mShort1, mShort2;
9 public static int mInt1, mInt2;
10 public static float mFloat1, mFloat2;
11 public static long mLong1, mLong2;
12 public static double mDouble1, mDouble2;
13 public static volatile long mVolatileLong1, mVolatileLong2;
14
15
16 private int foo_;
17
18 public IntMath(int stuff) {
buzbee4a3164f2011-09-03 11:25:10 -070019 super();
Brian Carlstrom9f30b382011-08-28 22:41:38 -070020 foo_ = stuff;
21 }
22
23 public IntMath() {
buzbee4a3164f2011-09-03 11:25:10 -070024 super();
Brian Carlstrom9f30b382011-08-28 22:41:38 -070025 foo_ = 123;
26 }
27
buzbeef0cde542011-09-13 14:55:02 -070028 /* Regression test: triggered an SSA renaming bug. */
29 static long divideLongByBillion(long a) {
30 long quot;
31 long rem;
32
33 if (a >= 0) {
34 long bLong = 1000000000L;
35 quot = (a / bLong);
36 rem = (a % bLong);
37 } else {
38 /*
39 * Make the dividend positive shifting it right by 1 bit then get
40 * the quotient an remainder and correct them properly
41 */
42 long aPos = a >>> 1;
43 long bPos = 1000000000L >>> 1;
44 quot = aPos / bPos;
45 rem = aPos % bPos;
46 // double the remainder and add 1 if 'a' is odd
47 rem = (rem << 1) + (a & 1);
48 }
49 return ((rem << 32) | (quot & 0xFFFFFFFFL));
50 }
51
52
buzbee2a475e72011-09-07 17:19:17 -070053 static int instanceTest(int x) {
54 IntMathBase a = new IntMathBase();
55 IntMath b = new IntMath();
56
57 if (a instanceof IntMathBase) {
58 x = x * 2;
59 }
60
61 if (a instanceof IntMath) {
62 x = x + 13;
63 }
64
65 if (b instanceof IntMathBase) {
66 x = x -1;
67 }
68
69 if (b instanceof IntMath) {
70 x = x + 1333;
71 }
72 return x;
73 }
74
buzbee4a3164f2011-09-03 11:25:10 -070075 int tryThing() {
76 int val = super.tryThing();
77 return val + 10;
78 }
79
80 static int superTest(int x) {
81 IntMath instance = new IntMath();
82 IntMath base = instance;
83 int val1 = instance.tryThing();
84 int val2 = base.tryThing();
85 return val1 + val2 + x;
86 }
87
buzbee561227c2011-09-02 15:28:19 -070088 static int constClassTest(int x) {
89 Class c = String.class;
90 if (c != null) {
91 return x * 2;
92 } else {
93 return x;
Brian Carlstrombbf1e412011-09-18 14:14:51 -070094 }
buzbee561227c2011-09-02 15:28:19 -070095 }
96
buzbee1b4c8592011-08-31 10:43:51 -070097 static int constStringTest(int x) {
buzbee1b4c8592011-08-31 10:43:51 -070098 String str = "Hello World!";
buzbee2a475e72011-09-07 17:19:17 -070099 return x + str.length();
buzbee1b4c8592011-08-31 10:43:51 -0700100 }
101
102 static void throwNullPointerException() {
103 throw new NullPointerException();
104 }
105
Ian Rogers93dd9662011-09-17 23:21:22 -0700106 static void throwImplicitNullPointerException() {
107 throw null;
108 }
109
buzbee1b4c8592011-08-31 10:43:51 -0700110 static int catchBlock(int x) {
111 try {
Ian Rogers93dd9662011-09-17 23:21:22 -0700112 if (x == 1000) {
113 x += 123;
114 throwNullPointerException();
115 } else {
116 x += 321;
117 throwImplicitNullPointerException();
118 }
buzbee1b4c8592011-08-31 10:43:51 -0700119 } catch (NullPointerException npe) {
120 x += 456;
121 }
122 return x;
123 }
124
buzbeee9a72f62011-09-04 17:59:07 -0700125 static int catchBlockNoThrow(int x) {
126 try {
127 x += 123;
128 } catch (NullPointerException npe) {
129 x += 456;
130 }
131 return x;
132 }
133
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700134 static int staticFieldTest(int x) {
135 mBoolean1 = true;
136 mBoolean2 = false;
137 mByte1 = 127;
138 mByte2 = -128;
139 mChar1 = 32767;
140 mChar2 = 65535;
141 mShort1 = 32767;
142 mShort2 = -32768;
143 mInt1 = 65537;
144 mInt2 = -65537;
145 mFloat1 = 3.1415f;
146 mFloat2 = -1.0f / 0.0f; // -inf
147 mLong1 = 1234605616436508552L; // 0x1122334455667788
148 mLong2 = -1234605616436508552L;
149 mDouble1 = 3.1415926535;
150 mDouble2 = 1.0 / 0.0; // +inf
151 mVolatileLong1 = mLong1 - 1;
152 mVolatileLong2 = mLong2 + 1;
153
154 if (!mBoolean1) { return 10; }
155 if (mBoolean2) { return 11; }
156 if (mByte1 != 127) { return 12; }
157 if (mByte2 != -128) { return 13; }
158 if (mChar1 != 32767) { return 14; }
159 if (mChar2 != 65535) { return 15; }
160 if (mShort1 != 32767) { return 16; }
161 if (mShort2 != -32768) { return 17; }
162 if (mInt1 != 65537) { return 18; }
163 if (mInt2 != -65537) { return 19; }
164 if (!(mFloat1 > 3.141f && mFloat1 < 3.142f)) { return 20; }
165 if (mFloat2 >= mFloat1) { return 21; }
166 if (mLong1 != 1234605616436508552L) { return 22; }
167 if (mLong2 != -1234605616436508552L) { return 23; }
168 if (!(mDouble1 > 3.141592653 && mDouble1 < 3.141592654)) { return 24; }
169 if (mDouble2 <= mDouble1) { return 25; }
170 if (mVolatileLong1 != 1234605616436508551L) { return 26; }
171 if (mVolatileLong2 != -1234605616436508551L) { return 27; }
172
173 return 1000 + x;
174 }
175
176 /*
177 * Try to cause some unary operations.
178 */
179 static int unopTest(int x) {
180 x = -x;
181 x ^= 0xffffffff;
182 return x;
183 }
184
185 static int shiftTest1() {
186 final int[] mBytes = {
187 0x11, 0x22, 0x33, 0x44, 0x88, 0x99, 0xaa, 0xbb
188 };
189 long l;
190 int i1, i2;
191
192 if (mBytes[0] != 0x11) return 20;
193 if (mBytes[1] != 0x22) return 21;
194 if (mBytes[2] != 0x33) return 22;
195 if (mBytes[3] != 0x44) return 23;
196 if (mBytes[4] != 0x88) return 24;
197 if (mBytes[5] != 0x99) return 25;
198 if (mBytes[6] != 0xaa) return 26;
199 if (mBytes[7] != 0xbb) return 27;
200
201 i1 = mBytes[0] | mBytes[1] << 8 | mBytes[2] << 16 | mBytes[3] << 24;
202 i2 = mBytes[4] | mBytes[5] << 8 | mBytes[6] << 16 | mBytes[7] << 24;
203 l = i1 | ((long)i2 << 32);
204
205 if (i1 != 0x44332211) { return 0x80000000 | i1; }
206 if (i2 != 0xbbaa9988) { return 2; }
207 if (l != 0xbbaa998844332211L) { return 3; }
208
209 l = (long)mBytes[0]
210 | (long)mBytes[1] << 8
211 | (long)mBytes[2] << 16
212 | (long)mBytes[3] << 24
213 | (long)mBytes[4] << 32
214 | (long)mBytes[5] << 40
215 | (long)mBytes[6] << 48
216 | (long)mBytes[7] << 56;
217
218 if (l != 0xbbaa998844332211L) { return 4; }
219 return 0;
220 }
221
222 static int shiftTest2() {
223
224 long a = 0x11;
225 long b = 0x22;
226 long c = 0x33;
227 long d = 0x44;
228 long e = 0x55;
229 long f = 0x66;
230 long g = 0x77;
231 long h = 0x88;
232
233 long result = ((a << 56) | (b << 48) | (c << 40) | (d << 32) |
234 (e << 24) | (f << 16) | (g << 8) | h);
235
236 if (result != 0x1122334455667788L) { return 1; }
237 return 0;
238 }
239
240 static int unsignedShiftTest() {
241 byte b = -4;
242 short s = -4;
243 char c = 0xfffc;
244 int i = -4;
245
246 b >>>= 4;
247 s >>>= 4;
248 c >>>= 4;
249 i >>>= 4;
250
251 if ((int) b != -1) { return 1; }
252 if ((int) s != -1) { return 2; }
253 if ((int) c != 0x0fff) { return 3; }
254 if (i != 268435455) { return 4; }
255 return 0;
256 }
257
258 static int convTest() {
259
260 float f;
261 double d;
262 int i;
263 long l;
264
265 /* int --> long */
266 i = 7654;
267 l = (long) i;
268 if (l != 7654L) { return 1; }
269
270 i = -7654;
271 l = (long) i;
272 if (l != -7654L) { return 2; }
273
274 /* long --> int (with truncation) */
275 l = 5678956789L;
276 i = (int) l;
277 if (i != 1383989493) { return 3; }
278
279 l = -5678956789L;
280 i = (int) l;
281 if (i != -1383989493) { return 4; }
282 return 0;
283 }
284
285 static int charSubTest() {
286
287 char char1 = 0x00e9;
288 char char2 = 0xffff;
289 int i;
290
291 /* chars are unsigned-expanded to ints before subtraction */
292 i = char1 - char2;
293 if (i != 0xffff00ea) { return 1; }
294 return 0;
295 }
296
297 /*
298 * We pass in the arguments and return the results so the compiler
299 * doesn't do the math for us. (x=70000, y=-3)
300 */
301 static int intOperTest(int x, int y) {
302 int[] results = new int[10];
303
304 /* this seems to generate "op-int" instructions */
305 results[0] = x + y;
306 results[1] = x - y;
307 results[2] = x * y;
308 results[3] = x * x;
309 results[4] = x / y;
310 results[5] = x % -y;
311 results[6] = x & y;
312 results[7] = x | y;
313 results[8] = x ^ y;
314
315 /* this seems to generate "op-int/2addr" instructions */
316 results[9] = x + ((((((((x + y) - y) * y) / y) % y) & y) | y) ^ y);
317
318 /* check this edge case while we're here (div-int/2addr) */
319 int minInt = -2147483648;
320 int negOne = -results[5];
321 int plusOne = 1;
322 int result = (((minInt + plusOne) - plusOne) / negOne) / negOne;
323
324 if (result != minInt) { return 1;};
325 if (results[0] != 69997) { return 2;};
326 if (results[1] != 70003) { return 3;};
327 if (results[2] != -210000) { return 4;};
328 if (results[3] != 605032704) { return 5;};
329 if (results[4] != -23333) { return 6;};
330 if (results[5] != 1) { return 7;};
331 if (results[6] != 70000) { return 8;};
332 if (results[7] != -3) { return 9;};
333 if (results[8] != -70003) { return 10;};
334 if (results[9] != 70000) { return 11;};
335
336 return 0;
337 }
338
339 /*
340 * More operations, this time with 16-bit constants. (x=77777)
341 */
342 static int lit16Test(int x) {
343
344 int[] results = new int[8];
345
346 /* try to generate op-int/lit16" instructions */
347 results[0] = x + 1000;
348 results[1] = 1000 - x;
349 results[2] = x * 1000;
350 results[3] = x / 1000;
351 results[4] = x % 1000;
352 results[5] = x & 1000;
353 results[6] = x | -1000;
354 results[7] = x ^ -1000;
355
356 if (results[0] != 78777) { return 1; }
357 if (results[1] != -76777) { return 2; }
358 if (results[2] != 77777000) { return 3; }
359 if (results[3] != 77) { return 4; }
360 if (results[4] != 777) { return 5; }
361 if (results[5] != 960) { return 6; }
362 if (results[6] != -39) { return 7; }
363 if (results[7] != -76855) { return 8; }
364 return 0;
365 }
366
367 /*
368 * More operations, this time with 8-bit constants. (x=-55555)
369 */
370 static int lit8Test(int x) {
371
372 int[] results = new int[8];
373
374 /* try to generate op-int/lit8" instructions */
375 results[0] = x + 10;
376 results[1] = 10 - x;
377 results[2] = x * 10;
378 results[3] = x / 10;
379 results[4] = x % 10;
380 results[5] = x & 10;
381 results[6] = x | -10;
382 results[7] = x ^ -10;
383 int minInt = -2147483648;
384 int result = minInt / -1;
385 if (result != minInt) {return 1; }
386 if (results[0] != -55545) {return 2; }
387 if (results[1] != 55565) {return 3; }
388 if (results[2] != -555550) {return 4; }
389 if (results[3] != -5555) {return 5; }
390 if (results[4] != -5) {return 6; }
391 if (results[5] != 8) {return 7; }
392 if (results[6] != -1) {return 8; }
393 if (results[7] != 55563) {return 9; }
394 return 0;
395 }
396
397
398 /*
399 * Shift some data. (value=0xff00aa01, dist=8)
400 */
401 static int intShiftTest(int value, int dist) {
402 int results[] = new int[4];
403 results[0] = value << dist;
404 results[1] = value >> dist;
405 results[2] = value >>> dist;
406 results[3] = (((value << dist) >> dist) >>> dist) << dist;
407 if (results[0] != 0x00aa0100) {return 1; }
408 if (results[1] != 0xffff00aa) {return 2; }
409 if (results[2] != 0x00ff00aa) {return 3; }
410 if (results[3] != 0xaa00) {return 4; }
411 return 0;
412 }
413
414 /*
415 * We pass in the arguments and return the results so the compiler
416 * doesn't do the math for us. (x=70000000000, y=-3)
417 */
418 static int longOperTest(long x, long y) {
419 long[] results = new long[10];
420
421 /* this seems to generate "op-long" instructions */
422 results[0] = x + y;
423 results[1] = x - y;
424 results[2] = x * y;
425 results[3] = x * x;
426 results[4] = x / y;
427 results[5] = x % -y;
428 results[6] = x & y;
429 results[7] = x | y;
430 results[8] = x ^ y;
431 /* this seems to generate "op-long/2addr" instructions */
432 results[9] = x + ((((((((x + y) - y) * y) / y) % y) & y) | y) ^ y);
433 /* check this edge case while we're here (div-long/2addr) */
434 long minLong = -9223372036854775808L;
435 long negOne = -results[5];
436 long plusOne = 1;
437 long result = (((minLong + plusOne) - plusOne) / negOne) / negOne;
438 if (result != minLong) { return 1; }
439 if (results[0] != 69999999997L) { return 2; }
440 if (results[1] != 70000000003L) { return 3; }
441 if (results[2] != -210000000000L) { return 4; }
442 if (results[3] != -6833923606740729856L) { return 5; } // overflow
443 if (results[4] != -23333333333L) { return 6; }
444 if (results[5] != 1) { return 7; }
445 if (results[6] != 70000000000L) { return 8; }
446 if (results[7] != -3) { return 9; }
447 if (results[8] != -70000000003L) { return 10; }
448 if (results[9] != 70000000000L) { return 11; }
449 if (results.length != 10) { return 12; }
450 return 0;
451 }
452
453 /*
454 * Shift some data. (value=0xd5aa96deff00aa01, dist=16)
455 */
456 static long longShiftTest(long value, int dist) {
457 long results[] = new long[4];
458 results[0] = value << dist;
459 results[1] = value >> dist;
460 results[2] = value >>> dist;
461 results[3] = (((value << dist) >> dist) >>> dist) << dist;
462 if (results[0] != 0x96deff00aa010000L) { return results[0]; }
463 if (results[1] != 0xffffd5aa96deff00L) { return results[1]; }
464 if (results[2] != 0x0000d5aa96deff00L) { return results[2]; }
465 if (results[3] != 0xffff96deff000000L) { return results[3]; }
466 if (results.length != 4) { return 5; }
467
468 return results[0]; // test return-long
469 }
470
471 static int switchTest(int a) {
472 int res = 1234;
473
474 switch (a) {
475 case -1: res = 1; return res;
476 case 0: res = 2; return res;
477 case 1: /*correct*/ break;
478 case 2: res = 3; return res;
479 case 3: res = 4; return res;
480 case 4: res = 5; return res;
481 default: res = 6; return res;
482 }
483 switch (a) {
484 case 3: res = 7; return res;
485 case 4: res = 8; return res;
486 default: /*correct*/ break;
487 }
488
489 a = 0x12345678;
490
491 switch (a) {
492 case 0x12345678: /*correct*/ break;
493 case 0x12345679: res = 9; return res;
494 default: res = 1; return res;
495 }
496 switch (a) {
497 case 57: res = 10; return res;
498 case -6: res = 11; return res;
499 case 0x12345678: /*correct*/ break;
500 case 22: res = 12; return res;
501 case 3: res = 13; return res;
502 default: res = 14; return res;
503 }
504 switch (a) {
505 case -6: res = 15; return res;
506 case 3: res = 16; return res;
507 default: /*correct*/ break;
508 }
509
510 a = -5;
511 switch (a) {
512 case 12: res = 17; return res;
513 case -5: /*correct*/ break;
514 case 0: res = 18; return res;
515 default: res = 19; return res;
516 }
517
518 switch (a) {
519 default: /*correct*/ break;
520 }
521 return res;
522 }
523 /*
524 * Test the integer comparisons in various ways.
525 */
526 static int testIntCompare(int minus, int plus, int plus2, int zero) {
527 int res = 1111;
528
529 if (minus > plus)
530 return 1;
531 if (minus >= plus)
532 return 2;
533 if (plus < minus)
534 return 3;
535 if (plus <= minus)
536 return 4;
537 if (plus == minus)
538 return 5;
539 if (plus != plus2)
540 return 6;
541
542 /* try a branch-taken */
543 if (plus != minus) {
544 res = res;
545 } else {
546 return 7;
547 }
548
549 if (minus > 0)
550 return 8;
551 if (minus >= 0)
552 return 9;
553 if (plus < 0)
554 return 10;
555 if (plus <= 0)
556 return 11;
557 if (plus == 0)
558 return 12;
559 if (zero != 0)
560 return 13;
561
562 if (zero == 0) {
563 res = res;
564 } else {
565 return 14;
566 }
567 return res;
568 }
569
570 /*
571 * Test cmp-long.
572 *
573 * minus=-5, alsoMinus=0xFFFFFFFF00000009, plus=4, alsoPlus=8
574 */
575 static int testLongCompare(long minus, long alsoMinus, long plus,
576 long alsoPlus) {
577 int res = 2222;
578
579 if (minus > plus)
580 return 2;
581 if (plus < minus)
582 return 3;
583 if (plus == minus)
584 return 4;
585
586 if (plus >= plus+1)
587 return 5;
588 if (minus >= minus+1)
589 return 6;
590
591 /* try a branch-taken */
592 if (plus != minus) {
593 res = res;
594 } else {
595 return 7;
596 }
597
598 /* compare when high words are equal but low words differ */
599 if (plus > alsoPlus)
600 return 8;
601 if (alsoPlus < plus)
602 return 9;
603 if (alsoPlus == plus)
604 return 10;
605
606 /* high words are equal, low words have apparently different signs */
607 if (minus < alsoMinus) // bug!
608 return 11;
609 if (alsoMinus > minus)
610 return 12;
611 if (alsoMinus == minus)
612 return 13;
613
614 return res;
615 }
616
617 /*
618 * Test cmpl-float and cmpg-float.
619 */
620 static int testFloatCompare(float minus, float plus, float plus2,
621 float nan) {
622
623 int res = 3333;
624 if (minus > plus)
625 res = 1;
626 if (plus < minus)
627 res = 2;
628 if (plus == minus)
629 res = 3;
630 if (plus != plus2)
631 res = 4;
632
633 if (plus <= nan)
634 res = 5;
635 if (plus >= nan)
636 res = 6;
637 if (minus <= nan)
638 res = 7;
639 if (minus >= nan)
640 res = 8;
641 if (nan >= plus)
642 res = 9;
643 if (nan <= plus)
644 res = 10;
645
646 if (nan == nan)
647 res = 1212;
648
649 return res;
650 }
651
652 static int testDoubleCompare(double minus, double plus, double plus2,
653 double nan) {
654
655 int res = 4444;
656
657 if (minus > plus)
658 return 1;
659 if (plus < minus)
660 return 2;
661 if (plus == minus)
662 return 3;
663 if (plus != plus2)
664 return 4;
665
666 if (plus <= nan)
667 return 5;
668 if (plus >= nan)
669 return 6;
670 if (minus <= nan)
671 return 7;
672 if (minus >= nan)
673 return 8;
674 if (nan >= plus)
675 return 9;
676 if (nan <= plus)
677 return 10;
678
679 if (nan == nan)
680 return 11;
681 return res;
682 }
683
684 static int fibonacci(int n) {
685 if (n == 0) {
686 return 0;
687 } else if (n == 1) {
688 return 1;
689 } else {
690 return fibonacci(n - 1) + fibonacci(n - 2);
691 }
692 }
693
buzbeee9a72f62011-09-04 17:59:07 -0700694 static int throwAndCatch() {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700695 try {
696 throwNullPointerException();
697 return 1;
698 } catch (NullPointerException npe) {
699 return 0;
700 }
buzbeee9a72f62011-09-04 17:59:07 -0700701 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700702
703 static int manyArgs(int a0, long a1, int a2, long a3, int a4, long a5,
704 int a6, int a7, double a8, float a9, double a10, short a11, int a12,
705 char a13, int a14, int a15, byte a16, boolean a17, int a18, int a19,
706 long a20, long a21, int a22, int a23, int a24, int a25, int a26)
707 {
708 if (a0 != 0) return 0;
709 if (a1 != 1L) return 1;
710 if (a2 != 2) return 2;
711 if (a3 != 3L) return 3;
712 if (a4 != 4) return 4;
713 if (a5 != 5L) return 5;
714 if (a6 != 6) return 6;
715 if (a7 != 7) return 7;
716 if (a8 != 8.0) return 8;
717 if (a9 != 9.0f) return 9;
718 if (a10 != 10.0) return 10;
719 if (a11 != (short)11) return 11;
720 if (a12 != 12) return 12;
721 if (a13 != (char)13) return 13;
722 if (a14 != 14) return 14;
723 if (a15 != 15) return 15;
724 if (a16 != (byte)-16) return 16;
725 if (a17 != true) return 17;
726 if (a18 != 18) return 18;
727 if (a19 != 19) return 19;
728 if (a20 != 20L) return 20;
729 if (a21 != 21L) return 21;
730 if (a22 != 22) return 22;
731 if (a23 != 23) return 23;
732 if (a24 != 24) return 24;
733 if (a25 != 25) return 25;
734 if (a26 != 26) return 26;
735 return -1;
736 }
737
738 int virtualCall(int a)
739 {
740 return a * 2;
741 }
742
743 void setFoo(int a)
744 {
745 foo_ = a;
746 }
747
748 int getFoo()
749 {
750 return foo_;
751 }
752
753 static int staticCall(int a)
754 {
755 IntMath foo = new IntMath();
756 return foo.virtualCall(a);
757 }
758
759 static int testIGetPut(int a)
760 {
761 IntMath foo = new IntMath(99);
762 IntMath foo123 = new IntMath();
763 int z = foo.getFoo();
764 z += a;
765 z += foo123.getFoo();
766 foo.setFoo(z);
767 return foo.getFoo();
768 }
769
Ian Rogersff1ed472011-09-20 13:46:24 -0700770 static int throwClassCast(Object o) {
771 return ((Integer)o).intValue();
772 }
773
774 static int testClassCast() {
775 int res = 0;
776 try {
777 res += throwClassCast(Integer.valueOf(123));
778 } catch(ClassCastException e) {
779 res += 456;
780 }
781 try {
782 res += throwClassCast(new Short((short)321));
783 } catch(ClassCastException e) {
784 res += 765;
785 }
786 return res;
787 }
788
Ian Rogerse51a5112011-09-23 14:16:35 -0700789 static void throwArrayStoreException(Object[] array, Object element) {
790 array[0] = element;
791 }
792
793 static int testArrayStoreException() {
794 int res=0;
795 Object[] array = new Number[2];
796 try {
797 throwArrayStoreException(array, null);
798 res += 1;
799 } catch(ArrayStoreException e) {
800 res += 2;
801 }
802 try {
803 throwArrayStoreException(array, Integer.valueOf(1));
804 res += 10;
805 } catch(ArrayStoreException e) {
806 res += 20;
807 }
808 try {
809 throwArrayStoreException(array, "hello MTV-44");
810 res += 100;
811 } catch(ArrayStoreException e) {
812 res += 200;
813 }
814 return res;
815 }
816
Ian Rogers932746a2011-09-22 18:57:50 -0700817 static long recursion_count_;
818 static void throwStackOverflow(long l) {
819 recursion_count_++;
820 throwStackOverflow(recursion_count_);
821 }
822
823 static long testStackOverflow() {
824 try {
825 throwStackOverflow(0);
826 if (recursion_count_ != 0) {
827 return recursion_count_;
828 } else {
829 return -1;
830 }
831 } catch(StackOverflowError soe) {
832 return 0;
833 }
834 }
835
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700836 public static void main(String[] args) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700837 boolean failure = false;
buzbeef0cde542011-09-13 14:55:02 -0700838 int res;
839 long lres;
840
841 lres = divideLongByBillion(123000000000L);
842 if (lres == 123) {
843 System.out.println("divideLongByBillion PASSED");
844 } else {
845 System.out.println("divideLongByBillion FAILED: " + lres);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700846 failure = true;
buzbeef0cde542011-09-13 14:55:02 -0700847 }
848 res = unopTest(38);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700849 if (res == 37) {
buzbee1da522d2011-09-04 11:22:20 -0700850 System.out.println("unopTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700851 } else {
buzbee1da522d2011-09-04 11:22:20 -0700852 System.out.println("unopTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700853 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700854 }
855 res = shiftTest1();
856 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700857 System.out.println("shiftTest1 PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700858 } else {
buzbee1da522d2011-09-04 11:22:20 -0700859 System.out.println("shiftTest1 FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700860 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700861 }
862 res = shiftTest2();
863 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700864 System.out.println("shiftTest2 PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700865 } else {
buzbee1da522d2011-09-04 11:22:20 -0700866 System.out.println("shiftTest2 FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700867 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700868 }
869 res = unsignedShiftTest();
870 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700871 System.out.println("unsignedShiftTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700872 } else {
buzbee1da522d2011-09-04 11:22:20 -0700873 System.out.println("unsignedShiftTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700874 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700875 }
876 res = convTest();
877 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700878 System.out.println("convTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700879 } else {
buzbee1da522d2011-09-04 11:22:20 -0700880 System.out.println("convTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700881 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700882 }
883 res = charSubTest();
884 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700885 System.out.println("charSubTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700886 } else {
buzbee1da522d2011-09-04 11:22:20 -0700887 System.out.println("charSubTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700888 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700889 }
890 res = intOperTest(70000, -3);
891 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700892 System.out.println("intOperTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700893 } else {
buzbee1da522d2011-09-04 11:22:20 -0700894 System.out.println("intOperTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700895 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700896 }
Brian Carlstrom25c33252011-09-18 15:58:35 -0700897 res = lit16Test(77777);
898 if (res == 0) {
899 System.out.println("lit16Test PASSED");
900 } else {
901 System.out.println("lit16Test FAILED: " + res);
902 failure = true;
903 }
904 res = lit8Test(-55555);
905 if (res == 0) {
906 System.out.println("lit8Test PASSED");
907 } else {
908 System.out.println("lit8Test FAILED: " + res);
909 failure = true;
910 }
911 res = intShiftTest(0xff00aa01, 8);
912 if (res == 0) {
913 System.out.println("intShiftTest PASSED");
914 } else {
915 System.out.println("intShiftTest FAILED: " + res);
916 failure = true;
917 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700918 res = longOperTest(70000000000L, -3L);
919 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700920 System.out.println("longOperTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700921 } else {
buzbee1da522d2011-09-04 11:22:20 -0700922 System.out.println("longOperTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700923 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700924 }
buzbeef0cde542011-09-13 14:55:02 -0700925 lres = longShiftTest(0xd5aa96deff00aa01L, 16);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700926 if (lres == 0x96deff00aa010000L) {
buzbee1da522d2011-09-04 11:22:20 -0700927 System.out.println("longShiftTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700928 } else {
buzbee1da522d2011-09-04 11:22:20 -0700929 System.out.println("longShiftTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700930 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700931 }
932
933 res = switchTest(1);
934 if (res == 1234) {
buzbee1da522d2011-09-04 11:22:20 -0700935 System.out.println("switchTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700936 } else {
buzbee1da522d2011-09-04 11:22:20 -0700937 System.out.println("switchTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700938 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700939 }
940
941 res = testIntCompare(-5, 4, 4, 0);
942 if (res == 1111) {
buzbee1da522d2011-09-04 11:22:20 -0700943 System.out.println("testIntCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700944 } else {
buzbee1da522d2011-09-04 11:22:20 -0700945 System.out.println("testIntCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700946 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700947 }
948
949 res = testLongCompare(-5L, -4294967287L, 4L, 8L);
950 if (res == 2222) {
buzbee1da522d2011-09-04 11:22:20 -0700951 System.out.println("testLongCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700952 } else {
buzbee1da522d2011-09-04 11:22:20 -0700953 System.out.println("testLongCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700954 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700955 }
956
957 res = testFloatCompare(-5.0f, 4.0f, 4.0f, (1.0f/0.0f) / (1.0f/0.0f));
958 if (res == 3333) {
buzbee1da522d2011-09-04 11:22:20 -0700959 System.out.println("testFloatCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700960 } else {
buzbee1da522d2011-09-04 11:22:20 -0700961 System.out.println("testFloatCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700962 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700963 }
964
965 res = testDoubleCompare(-5.0, 4.0, 4.0, (1.0/0.0) / (1.0/0.0));
966 if (res == 4444) {
buzbee1da522d2011-09-04 11:22:20 -0700967 System.out.println("testDoubleCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700968 } else {
buzbee1da522d2011-09-04 11:22:20 -0700969 System.out.println("testDoubleCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700970 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700971 }
972
973 res = fibonacci(10);
974 if (res == 55) {
buzbee1da522d2011-09-04 11:22:20 -0700975 System.out.println("fibonacci PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700976 } else {
buzbee1da522d2011-09-04 11:22:20 -0700977 System.out.println("fibonacci FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700978 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700979 }
980
buzbeee9a72f62011-09-04 17:59:07 -0700981 res = throwAndCatch();
982 if (res == 0) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700983 System.out.println("throwAndCatch PASSED");
buzbeee9a72f62011-09-04 17:59:07 -0700984 } else {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700985 System.out.println("throwAndCatch FAILED: " + res);
986 failure = true;
buzbeee9a72f62011-09-04 17:59:07 -0700987 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700988
Ian Rogersff1ed472011-09-20 13:46:24 -0700989 res = testClassCast();
990 if (res == 888) {
991 System.out.println("testClassCast PASSED");
992 } else {
993 System.out.println("testClassCast FAILED: " + res);
994 failure = true;
995 }
996
Ian Rogerse51a5112011-09-23 14:16:35 -0700997 res = testArrayStoreException();
998 if (res == 211) {
999 System.out.println("testArrayStore PASSED");
1000 } else {
1001 System.out.println("testArrayStore FAILED: " + res);
1002 failure = true;
1003 }
1004
Ian Rogers932746a2011-09-22 18:57:50 -07001005 lres= testStackOverflow();
1006 if (lres == 0) {
1007 System.out.println("testStackOverflow PASSED");
1008 } else {
1009 System.out.println("testStackOverflow FAILED: " + lres);
1010 failure = true;
1011 }
1012
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001013 res = manyArgs(0, 1L, 2, 3L, 4, 5L, 6, 7, 8.0, 9.0f, 10.0,
1014 (short)11, 12, (char)13, 14, 15, (byte)-16, true, 18,
1015 19, 20L, 21L, 22, 23, 24, 25, 26);
1016 if (res == -1) {
buzbee1da522d2011-09-04 11:22:20 -07001017 System.out.println("manyArgs PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001018 } else {
buzbee1da522d2011-09-04 11:22:20 -07001019 System.out.println("manyArgs FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001020 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001021 }
1022
1023 res = staticCall(3);
1024 if (res == 6) {
buzbee1da522d2011-09-04 11:22:20 -07001025 System.out.println("virtualCall PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001026 } else {
buzbee1da522d2011-09-04 11:22:20 -07001027 System.out.println("virtualCall FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001028 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001029 }
1030
1031 res = testIGetPut(111);
1032 if (res == 333) {
buzbee1da522d2011-09-04 11:22:20 -07001033 System.out.println("testGetPut PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001034 } else {
buzbee1da522d2011-09-04 11:22:20 -07001035 System.out.println("testGetPut FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001036 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001037 }
1038
1039 res = staticFieldTest(404);
1040 if (res == 1404) {
buzbee1da522d2011-09-04 11:22:20 -07001041 System.out.println("staticFieldTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001042 } else {
buzbee1da522d2011-09-04 11:22:20 -07001043 System.out.println("staticFieldTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001044 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001045 }
buzbee1b4c8592011-08-31 10:43:51 -07001046
1047 res = catchBlock(1000);
1048 if (res == 1579) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001049 System.out.println("catchBlock(1000) PASSED");
buzbee1b4c8592011-08-31 10:43:51 -07001050 } else {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001051 System.out.println("catchBlock(1000) FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001052 failure = true;
buzbee1b4c8592011-08-31 10:43:51 -07001053 }
Brian Carlstrom25c33252011-09-18 15:58:35 -07001054 res = catchBlock(7000);
1055 if (res == 7777) {
1056 System.out.println("catchBlock(7000) PASSED");
1057 } else {
1058 System.out.println("catchBlock(7000) FAILED: " + res);
1059 failure = true;
1060 }
buzbeee9a72f62011-09-04 17:59:07 -07001061 res = catchBlockNoThrow(1000);
1062 if (res == 1123) {
1063 System.out.println("catchBlockNoThrow PASSED");
1064 } else {
1065 System.out.println("catchBlockNoThrow FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001066 failure = true;
buzbeee9a72f62011-09-04 17:59:07 -07001067 }
1068
buzbee4a3164f2011-09-03 11:25:10 -07001069 res = superTest(4141);
1070 if (res == 4175) {
buzbee1da522d2011-09-04 11:22:20 -07001071 System.out.println("superTest PASSED");
buzbee4a3164f2011-09-03 11:25:10 -07001072 } else {
buzbee1da522d2011-09-04 11:22:20 -07001073 System.out.println("superTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001074 failure = true;
buzbee4a3164f2011-09-03 11:25:10 -07001075 }
buzbee2a475e72011-09-07 17:19:17 -07001076
Brian Carlstrom25c33252011-09-18 15:58:35 -07001077 res = constClassTest(1111);
1078 if (res == 2222) {
1079 System.out.println("constClassTest PASSED");
1080 } else {
1081 System.out.println("constClassTest FAILED: " + res);
1082 failure = true;
1083 }
1084
buzbee2a475e72011-09-07 17:19:17 -07001085 res = constStringTest(10);
1086 if (res == 22) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001087 System.out.println("constStringTest PASSED");
buzbee2a475e72011-09-07 17:19:17 -07001088 } else {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001089 System.out.println("constStringTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001090 failure = true;
buzbee2a475e72011-09-07 17:19:17 -07001091 }
1092
1093 res = instanceTest(10);
1094 if (res == 1352) {
1095 System.out.println("instanceTest PASSED");
1096 } else {
1097 System.out.println("instanceTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001098 failure = true;
buzbee2a475e72011-09-07 17:19:17 -07001099 }
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001100
1101 System.exit(failure ? 1 : 0);
buzbee4a3164f2011-09-03 11:25:10 -07001102 }
1103}
1104
1105class IntMathBase {
1106 IntMathBase() {
1107 }
1108
1109 int tryThing() {
1110 return 7;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001111 }
1112}