blob: 21f84bbf0472776a92bd4ed452c285fa91e3c245 [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
Brian Carlstrom5d40f182011-09-26 22:29:18 -070057 if (!(null instanceof IntMathBase)) {
58 x = x + 42;
59 }
60
buzbee2a475e72011-09-07 17:19:17 -070061 if (a instanceof IntMathBase) {
62 x = x * 2;
63 }
64
65 if (a instanceof IntMath) {
66 x = x + 13;
67 }
68
69 if (b instanceof IntMathBase) {
70 x = x -1;
71 }
72
73 if (b instanceof IntMath) {
74 x = x + 1333;
75 }
76 return x;
77 }
78
buzbee4a3164f2011-09-03 11:25:10 -070079 int tryThing() {
80 int val = super.tryThing();
81 return val + 10;
82 }
83
84 static int superTest(int x) {
85 IntMath instance = new IntMath();
86 IntMath base = instance;
87 int val1 = instance.tryThing();
88 int val2 = base.tryThing();
89 return val1 + val2 + x;
90 }
91
buzbee561227c2011-09-02 15:28:19 -070092 static int constClassTest(int x) {
93 Class c = String.class;
94 if (c != null) {
95 return x * 2;
96 } else {
97 return x;
Brian Carlstrombbf1e412011-09-18 14:14:51 -070098 }
buzbee561227c2011-09-02 15:28:19 -070099 }
100
buzbee1b4c8592011-08-31 10:43:51 -0700101 static int constStringTest(int x) {
buzbee1b4c8592011-08-31 10:43:51 -0700102 String str = "Hello World!";
buzbee2a475e72011-09-07 17:19:17 -0700103 return x + str.length();
buzbee1b4c8592011-08-31 10:43:51 -0700104 }
105
106 static void throwNullPointerException() {
107 throw new NullPointerException();
108 }
109
Ian Rogers93dd9662011-09-17 23:21:22 -0700110 static void throwImplicitNullPointerException() {
111 throw null;
112 }
113
buzbee1b4c8592011-08-31 10:43:51 -0700114 static int catchBlock(int x) {
115 try {
Ian Rogers93dd9662011-09-17 23:21:22 -0700116 if (x == 1000) {
117 x += 123;
118 throwNullPointerException();
119 } else {
120 x += 321;
121 throwImplicitNullPointerException();
122 }
buzbee1b4c8592011-08-31 10:43:51 -0700123 } catch (NullPointerException npe) {
124 x += 456;
125 }
126 return x;
127 }
128
buzbeee9a72f62011-09-04 17:59:07 -0700129 static int catchBlockNoThrow(int x) {
130 try {
131 x += 123;
132 } catch (NullPointerException npe) {
133 x += 456;
134 }
135 return x;
136 }
137
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700138 static int staticFieldTest(int x) {
139 mBoolean1 = true;
140 mBoolean2 = false;
141 mByte1 = 127;
142 mByte2 = -128;
143 mChar1 = 32767;
144 mChar2 = 65535;
145 mShort1 = 32767;
146 mShort2 = -32768;
147 mInt1 = 65537;
148 mInt2 = -65537;
149 mFloat1 = 3.1415f;
150 mFloat2 = -1.0f / 0.0f; // -inf
151 mLong1 = 1234605616436508552L; // 0x1122334455667788
152 mLong2 = -1234605616436508552L;
153 mDouble1 = 3.1415926535;
154 mDouble2 = 1.0 / 0.0; // +inf
155 mVolatileLong1 = mLong1 - 1;
156 mVolatileLong2 = mLong2 + 1;
157
158 if (!mBoolean1) { return 10; }
159 if (mBoolean2) { return 11; }
160 if (mByte1 != 127) { return 12; }
161 if (mByte2 != -128) { return 13; }
162 if (mChar1 != 32767) { return 14; }
163 if (mChar2 != 65535) { return 15; }
164 if (mShort1 != 32767) { return 16; }
165 if (mShort2 != -32768) { return 17; }
166 if (mInt1 != 65537) { return 18; }
167 if (mInt2 != -65537) { return 19; }
168 if (!(mFloat1 > 3.141f && mFloat1 < 3.142f)) { return 20; }
169 if (mFloat2 >= mFloat1) { return 21; }
170 if (mLong1 != 1234605616436508552L) { return 22; }
171 if (mLong2 != -1234605616436508552L) { return 23; }
172 if (!(mDouble1 > 3.141592653 && mDouble1 < 3.141592654)) { return 24; }
173 if (mDouble2 <= mDouble1) { return 25; }
174 if (mVolatileLong1 != 1234605616436508551L) { return 26; }
175 if (mVolatileLong2 != -1234605616436508551L) { return 27; }
176
177 return 1000 + x;
178 }
179
180 /*
181 * Try to cause some unary operations.
182 */
183 static int unopTest(int x) {
184 x = -x;
185 x ^= 0xffffffff;
186 return x;
187 }
188
189 static int shiftTest1() {
190 final int[] mBytes = {
191 0x11, 0x22, 0x33, 0x44, 0x88, 0x99, 0xaa, 0xbb
192 };
193 long l;
194 int i1, i2;
195
196 if (mBytes[0] != 0x11) return 20;
197 if (mBytes[1] != 0x22) return 21;
198 if (mBytes[2] != 0x33) return 22;
199 if (mBytes[3] != 0x44) return 23;
200 if (mBytes[4] != 0x88) return 24;
201 if (mBytes[5] != 0x99) return 25;
202 if (mBytes[6] != 0xaa) return 26;
203 if (mBytes[7] != 0xbb) return 27;
204
205 i1 = mBytes[0] | mBytes[1] << 8 | mBytes[2] << 16 | mBytes[3] << 24;
206 i2 = mBytes[4] | mBytes[5] << 8 | mBytes[6] << 16 | mBytes[7] << 24;
207 l = i1 | ((long)i2 << 32);
208
209 if (i1 != 0x44332211) { return 0x80000000 | i1; }
210 if (i2 != 0xbbaa9988) { return 2; }
211 if (l != 0xbbaa998844332211L) { return 3; }
212
213 l = (long)mBytes[0]
214 | (long)mBytes[1] << 8
215 | (long)mBytes[2] << 16
216 | (long)mBytes[3] << 24
217 | (long)mBytes[4] << 32
218 | (long)mBytes[5] << 40
219 | (long)mBytes[6] << 48
220 | (long)mBytes[7] << 56;
221
222 if (l != 0xbbaa998844332211L) { return 4; }
223 return 0;
224 }
225
226 static int shiftTest2() {
227
228 long a = 0x11;
229 long b = 0x22;
230 long c = 0x33;
231 long d = 0x44;
232 long e = 0x55;
233 long f = 0x66;
234 long g = 0x77;
235 long h = 0x88;
236
237 long result = ((a << 56) | (b << 48) | (c << 40) | (d << 32) |
238 (e << 24) | (f << 16) | (g << 8) | h);
239
240 if (result != 0x1122334455667788L) { return 1; }
241 return 0;
242 }
243
244 static int unsignedShiftTest() {
245 byte b = -4;
246 short s = -4;
247 char c = 0xfffc;
248 int i = -4;
249
250 b >>>= 4;
251 s >>>= 4;
252 c >>>= 4;
253 i >>>= 4;
254
255 if ((int) b != -1) { return 1; }
256 if ((int) s != -1) { return 2; }
257 if ((int) c != 0x0fff) { return 3; }
258 if (i != 268435455) { return 4; }
259 return 0;
260 }
261
262 static int convTest() {
263
264 float f;
265 double d;
266 int i;
267 long l;
268
269 /* int --> long */
270 i = 7654;
271 l = (long) i;
272 if (l != 7654L) { return 1; }
273
274 i = -7654;
275 l = (long) i;
276 if (l != -7654L) { return 2; }
277
278 /* long --> int (with truncation) */
279 l = 5678956789L;
280 i = (int) l;
281 if (i != 1383989493) { return 3; }
282
283 l = -5678956789L;
284 i = (int) l;
285 if (i != -1383989493) { return 4; }
286 return 0;
287 }
288
289 static int charSubTest() {
290
291 char char1 = 0x00e9;
292 char char2 = 0xffff;
293 int i;
294
295 /* chars are unsigned-expanded to ints before subtraction */
296 i = char1 - char2;
297 if (i != 0xffff00ea) { return 1; }
298 return 0;
299 }
300
301 /*
302 * We pass in the arguments and return the results so the compiler
303 * doesn't do the math for us. (x=70000, y=-3)
304 */
305 static int intOperTest(int x, int y) {
306 int[] results = new int[10];
307
308 /* this seems to generate "op-int" instructions */
309 results[0] = x + y;
310 results[1] = x - y;
311 results[2] = x * y;
312 results[3] = x * x;
313 results[4] = x / y;
314 results[5] = x % -y;
315 results[6] = x & y;
316 results[7] = x | y;
317 results[8] = x ^ y;
318
319 /* this seems to generate "op-int/2addr" instructions */
320 results[9] = x + ((((((((x + y) - y) * y) / y) % y) & y) | y) ^ y);
321
322 /* check this edge case while we're here (div-int/2addr) */
323 int minInt = -2147483648;
324 int negOne = -results[5];
325 int plusOne = 1;
326 int result = (((minInt + plusOne) - plusOne) / negOne) / negOne;
327
328 if (result != minInt) { return 1;};
329 if (results[0] != 69997) { return 2;};
330 if (results[1] != 70003) { return 3;};
331 if (results[2] != -210000) { return 4;};
332 if (results[3] != 605032704) { return 5;};
333 if (results[4] != -23333) { return 6;};
334 if (results[5] != 1) { return 7;};
335 if (results[6] != 70000) { return 8;};
336 if (results[7] != -3) { return 9;};
337 if (results[8] != -70003) { return 10;};
338 if (results[9] != 70000) { return 11;};
339
340 return 0;
341 }
342
343 /*
344 * More operations, this time with 16-bit constants. (x=77777)
345 */
346 static int lit16Test(int x) {
347
348 int[] results = new int[8];
349
350 /* try to generate op-int/lit16" instructions */
351 results[0] = x + 1000;
352 results[1] = 1000 - x;
353 results[2] = x * 1000;
354 results[3] = x / 1000;
355 results[4] = x % 1000;
356 results[5] = x & 1000;
357 results[6] = x | -1000;
358 results[7] = x ^ -1000;
359
360 if (results[0] != 78777) { return 1; }
361 if (results[1] != -76777) { return 2; }
362 if (results[2] != 77777000) { return 3; }
363 if (results[3] != 77) { return 4; }
364 if (results[4] != 777) { return 5; }
365 if (results[5] != 960) { return 6; }
366 if (results[6] != -39) { return 7; }
367 if (results[7] != -76855) { return 8; }
368 return 0;
369 }
370
371 /*
372 * More operations, this time with 8-bit constants. (x=-55555)
373 */
374 static int lit8Test(int x) {
375
376 int[] results = new int[8];
377
378 /* try to generate op-int/lit8" instructions */
379 results[0] = x + 10;
380 results[1] = 10 - x;
381 results[2] = x * 10;
382 results[3] = x / 10;
383 results[4] = x % 10;
384 results[5] = x & 10;
385 results[6] = x | -10;
386 results[7] = x ^ -10;
387 int minInt = -2147483648;
388 int result = minInt / -1;
389 if (result != minInt) {return 1; }
390 if (results[0] != -55545) {return 2; }
391 if (results[1] != 55565) {return 3; }
392 if (results[2] != -555550) {return 4; }
393 if (results[3] != -5555) {return 5; }
394 if (results[4] != -5) {return 6; }
395 if (results[5] != 8) {return 7; }
396 if (results[6] != -1) {return 8; }
397 if (results[7] != 55563) {return 9; }
398 return 0;
399 }
400
401
402 /*
403 * Shift some data. (value=0xff00aa01, dist=8)
404 */
405 static int intShiftTest(int value, int dist) {
406 int results[] = new int[4];
407 results[0] = value << dist;
408 results[1] = value >> dist;
409 results[2] = value >>> dist;
410 results[3] = (((value << dist) >> dist) >>> dist) << dist;
411 if (results[0] != 0x00aa0100) {return 1; }
412 if (results[1] != 0xffff00aa) {return 2; }
413 if (results[2] != 0x00ff00aa) {return 3; }
414 if (results[3] != 0xaa00) {return 4; }
415 return 0;
416 }
417
418 /*
419 * We pass in the arguments and return the results so the compiler
420 * doesn't do the math for us. (x=70000000000, y=-3)
421 */
422 static int longOperTest(long x, long y) {
423 long[] results = new long[10];
424
425 /* this seems to generate "op-long" instructions */
426 results[0] = x + y;
427 results[1] = x - y;
428 results[2] = x * y;
429 results[3] = x * x;
430 results[4] = x / y;
431 results[5] = x % -y;
432 results[6] = x & y;
433 results[7] = x | y;
434 results[8] = x ^ y;
435 /* this seems to generate "op-long/2addr" instructions */
436 results[9] = x + ((((((((x + y) - y) * y) / y) % y) & y) | y) ^ y);
437 /* check this edge case while we're here (div-long/2addr) */
438 long minLong = -9223372036854775808L;
439 long negOne = -results[5];
440 long plusOne = 1;
441 long result = (((minLong + plusOne) - plusOne) / negOne) / negOne;
442 if (result != minLong) { return 1; }
443 if (results[0] != 69999999997L) { return 2; }
444 if (results[1] != 70000000003L) { return 3; }
445 if (results[2] != -210000000000L) { return 4; }
446 if (results[3] != -6833923606740729856L) { return 5; } // overflow
447 if (results[4] != -23333333333L) { return 6; }
448 if (results[5] != 1) { return 7; }
449 if (results[6] != 70000000000L) { return 8; }
450 if (results[7] != -3) { return 9; }
451 if (results[8] != -70000000003L) { return 10; }
452 if (results[9] != 70000000000L) { return 11; }
453 if (results.length != 10) { return 12; }
454 return 0;
455 }
456
457 /*
458 * Shift some data. (value=0xd5aa96deff00aa01, dist=16)
459 */
460 static long longShiftTest(long value, int dist) {
461 long results[] = new long[4];
462 results[0] = value << dist;
463 results[1] = value >> dist;
464 results[2] = value >>> dist;
465 results[3] = (((value << dist) >> dist) >>> dist) << dist;
466 if (results[0] != 0x96deff00aa010000L) { return results[0]; }
467 if (results[1] != 0xffffd5aa96deff00L) { return results[1]; }
468 if (results[2] != 0x0000d5aa96deff00L) { return results[2]; }
469 if (results[3] != 0xffff96deff000000L) { return results[3]; }
470 if (results.length != 4) { return 5; }
471
472 return results[0]; // test return-long
473 }
474
475 static int switchTest(int a) {
476 int res = 1234;
477
478 switch (a) {
479 case -1: res = 1; return res;
480 case 0: res = 2; return res;
481 case 1: /*correct*/ break;
482 case 2: res = 3; return res;
483 case 3: res = 4; return res;
484 case 4: res = 5; return res;
485 default: res = 6; return res;
486 }
487 switch (a) {
488 case 3: res = 7; return res;
489 case 4: res = 8; return res;
490 default: /*correct*/ break;
491 }
492
493 a = 0x12345678;
494
495 switch (a) {
496 case 0x12345678: /*correct*/ break;
497 case 0x12345679: res = 9; return res;
498 default: res = 1; return res;
499 }
500 switch (a) {
501 case 57: res = 10; return res;
502 case -6: res = 11; return res;
503 case 0x12345678: /*correct*/ break;
504 case 22: res = 12; return res;
505 case 3: res = 13; return res;
506 default: res = 14; return res;
507 }
508 switch (a) {
509 case -6: res = 15; return res;
510 case 3: res = 16; return res;
511 default: /*correct*/ break;
512 }
513
514 a = -5;
515 switch (a) {
516 case 12: res = 17; return res;
517 case -5: /*correct*/ break;
518 case 0: res = 18; return res;
519 default: res = 19; return res;
520 }
521
522 switch (a) {
523 default: /*correct*/ break;
524 }
525 return res;
526 }
527 /*
528 * Test the integer comparisons in various ways.
529 */
530 static int testIntCompare(int minus, int plus, int plus2, int zero) {
531 int res = 1111;
532
533 if (minus > plus)
534 return 1;
535 if (minus >= plus)
536 return 2;
537 if (plus < minus)
538 return 3;
539 if (plus <= minus)
540 return 4;
541 if (plus == minus)
542 return 5;
543 if (plus != plus2)
544 return 6;
545
546 /* try a branch-taken */
547 if (plus != minus) {
548 res = res;
549 } else {
550 return 7;
551 }
552
553 if (minus > 0)
554 return 8;
555 if (minus >= 0)
556 return 9;
557 if (plus < 0)
558 return 10;
559 if (plus <= 0)
560 return 11;
561 if (plus == 0)
562 return 12;
563 if (zero != 0)
564 return 13;
565
566 if (zero == 0) {
567 res = res;
568 } else {
569 return 14;
570 }
571 return res;
572 }
573
574 /*
575 * Test cmp-long.
576 *
577 * minus=-5, alsoMinus=0xFFFFFFFF00000009, plus=4, alsoPlus=8
578 */
579 static int testLongCompare(long minus, long alsoMinus, long plus,
580 long alsoPlus) {
581 int res = 2222;
582
583 if (minus > plus)
584 return 2;
585 if (plus < minus)
586 return 3;
587 if (plus == minus)
588 return 4;
589
590 if (plus >= plus+1)
591 return 5;
592 if (minus >= minus+1)
593 return 6;
594
595 /* try a branch-taken */
596 if (plus != minus) {
597 res = res;
598 } else {
599 return 7;
600 }
601
602 /* compare when high words are equal but low words differ */
603 if (plus > alsoPlus)
604 return 8;
605 if (alsoPlus < plus)
606 return 9;
607 if (alsoPlus == plus)
608 return 10;
609
610 /* high words are equal, low words have apparently different signs */
611 if (minus < alsoMinus) // bug!
612 return 11;
613 if (alsoMinus > minus)
614 return 12;
615 if (alsoMinus == minus)
616 return 13;
617
618 return res;
619 }
620
621 /*
622 * Test cmpl-float and cmpg-float.
623 */
624 static int testFloatCompare(float minus, float plus, float plus2,
625 float nan) {
626
627 int res = 3333;
628 if (minus > plus)
629 res = 1;
630 if (plus < minus)
631 res = 2;
632 if (plus == minus)
633 res = 3;
634 if (plus != plus2)
635 res = 4;
636
637 if (plus <= nan)
638 res = 5;
639 if (plus >= nan)
640 res = 6;
641 if (minus <= nan)
642 res = 7;
643 if (minus >= nan)
644 res = 8;
645 if (nan >= plus)
646 res = 9;
647 if (nan <= plus)
648 res = 10;
649
650 if (nan == nan)
651 res = 1212;
652
653 return res;
654 }
655
656 static int testDoubleCompare(double minus, double plus, double plus2,
657 double nan) {
658
659 int res = 4444;
660
661 if (minus > plus)
662 return 1;
663 if (plus < minus)
664 return 2;
665 if (plus == minus)
666 return 3;
667 if (plus != plus2)
668 return 4;
669
670 if (plus <= nan)
671 return 5;
672 if (plus >= nan)
673 return 6;
674 if (minus <= nan)
675 return 7;
676 if (minus >= nan)
677 return 8;
678 if (nan >= plus)
679 return 9;
680 if (nan <= plus)
681 return 10;
682
683 if (nan == nan)
684 return 11;
685 return res;
686 }
687
688 static int fibonacci(int n) {
689 if (n == 0) {
690 return 0;
691 } else if (n == 1) {
692 return 1;
693 } else {
694 return fibonacci(n - 1) + fibonacci(n - 2);
695 }
696 }
697
buzbeee9a72f62011-09-04 17:59:07 -0700698 static int throwAndCatch() {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700699 try {
700 throwNullPointerException();
701 return 1;
702 } catch (NullPointerException npe) {
703 return 0;
704 }
buzbeee9a72f62011-09-04 17:59:07 -0700705 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700706
707 static int manyArgs(int a0, long a1, int a2, long a3, int a4, long a5,
708 int a6, int a7, double a8, float a9, double a10, short a11, int a12,
709 char a13, int a14, int a15, byte a16, boolean a17, int a18, int a19,
710 long a20, long a21, int a22, int a23, int a24, int a25, int a26)
711 {
712 if (a0 != 0) return 0;
713 if (a1 != 1L) return 1;
714 if (a2 != 2) return 2;
715 if (a3 != 3L) return 3;
716 if (a4 != 4) return 4;
717 if (a5 != 5L) return 5;
718 if (a6 != 6) return 6;
719 if (a7 != 7) return 7;
720 if (a8 != 8.0) return 8;
721 if (a9 != 9.0f) return 9;
722 if (a10 != 10.0) return 10;
723 if (a11 != (short)11) return 11;
724 if (a12 != 12) return 12;
725 if (a13 != (char)13) return 13;
726 if (a14 != 14) return 14;
727 if (a15 != 15) return 15;
728 if (a16 != (byte)-16) return 16;
729 if (a17 != true) return 17;
730 if (a18 != 18) return 18;
731 if (a19 != 19) return 19;
732 if (a20 != 20L) return 20;
733 if (a21 != 21L) return 21;
734 if (a22 != 22) return 22;
735 if (a23 != 23) return 23;
736 if (a24 != 24) return 24;
737 if (a25 != 25) return 25;
738 if (a26 != 26) return 26;
739 return -1;
740 }
741
742 int virtualCall(int a)
743 {
744 return a * 2;
745 }
746
747 void setFoo(int a)
748 {
749 foo_ = a;
750 }
751
752 int getFoo()
753 {
754 return foo_;
755 }
756
757 static int staticCall(int a)
758 {
759 IntMath foo = new IntMath();
760 return foo.virtualCall(a);
761 }
762
763 static int testIGetPut(int a)
764 {
765 IntMath foo = new IntMath(99);
766 IntMath foo123 = new IntMath();
767 int z = foo.getFoo();
768 z += a;
769 z += foo123.getFoo();
770 foo.setFoo(z);
771 return foo.getFoo();
772 }
773
Ian Rogersff1ed472011-09-20 13:46:24 -0700774 static int throwClassCast(Object o) {
775 return ((Integer)o).intValue();
776 }
777
778 static int testClassCast() {
779 int res = 0;
780 try {
781 res += throwClassCast(Integer.valueOf(123));
782 } catch(ClassCastException e) {
783 res += 456;
784 }
785 try {
786 res += throwClassCast(new Short((short)321));
787 } catch(ClassCastException e) {
788 res += 765;
789 }
790 return res;
791 }
792
Ian Rogerse51a5112011-09-23 14:16:35 -0700793 static void throwArrayStoreException(Object[] array, Object element) {
794 array[0] = element;
795 }
796
797 static int testArrayStoreException() {
798 int res=0;
799 Object[] array = new Number[2];
800 try {
801 throwArrayStoreException(array, null);
802 res += 1;
803 } catch(ArrayStoreException e) {
804 res += 2;
805 }
806 try {
807 throwArrayStoreException(array, Integer.valueOf(1));
808 res += 10;
809 } catch(ArrayStoreException e) {
810 res += 20;
811 }
812 try {
813 throwArrayStoreException(array, "hello MTV-44");
814 res += 100;
815 } catch(ArrayStoreException e) {
816 res += 200;
817 }
818 return res;
819 }
820
Ian Rogers932746a2011-09-22 18:57:50 -0700821 static long recursion_count_;
822 static void throwStackOverflow(long l) {
823 recursion_count_++;
824 throwStackOverflow(recursion_count_);
825 }
826
827 static long testStackOverflow() {
828 try {
829 throwStackOverflow(0);
830 if (recursion_count_ != 0) {
831 return recursion_count_;
832 } else {
833 return -1;
834 }
835 } catch(StackOverflowError soe) {
836 return 0;
837 }
838 }
839
Ian Rogersb886da82011-09-23 16:27:54 -0700840 static int testArrayAllocation() {
841 int res = 0;
842 try {
843 int[] x = new int[-1];
844 res += 1;
845 } catch (NegativeArraySizeException e) {
846 res += 2;
847 }
848 try {
849 int[] x = new int [1];
850 res += 10;
851 } catch (Throwable e) {
852 res += 20;
853 }
854 return res;
855 }
856
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700857 public static void main(String[] args) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700858 boolean failure = false;
buzbeef0cde542011-09-13 14:55:02 -0700859 int res;
860 long lres;
861
862 lres = divideLongByBillion(123000000000L);
863 if (lres == 123) {
864 System.out.println("divideLongByBillion PASSED");
865 } else {
866 System.out.println("divideLongByBillion FAILED: " + lres);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700867 failure = true;
buzbeef0cde542011-09-13 14:55:02 -0700868 }
869 res = unopTest(38);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700870 if (res == 37) {
buzbee1da522d2011-09-04 11:22:20 -0700871 System.out.println("unopTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700872 } else {
buzbee1da522d2011-09-04 11:22:20 -0700873 System.out.println("unopTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700874 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700875 }
876 res = shiftTest1();
877 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700878 System.out.println("shiftTest1 PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700879 } else {
buzbee1da522d2011-09-04 11:22:20 -0700880 System.out.println("shiftTest1 FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700881 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700882 }
883 res = shiftTest2();
884 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700885 System.out.println("shiftTest2 PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700886 } else {
buzbee1da522d2011-09-04 11:22:20 -0700887 System.out.println("shiftTest2 FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700888 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700889 }
890 res = unsignedShiftTest();
891 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700892 System.out.println("unsignedShiftTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700893 } else {
buzbee1da522d2011-09-04 11:22:20 -0700894 System.out.println("unsignedShiftTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700895 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700896 }
897 res = convTest();
898 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700899 System.out.println("convTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700900 } else {
buzbee1da522d2011-09-04 11:22:20 -0700901 System.out.println("convTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700902 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700903 }
904 res = charSubTest();
905 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700906 System.out.println("charSubTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700907 } else {
buzbee1da522d2011-09-04 11:22:20 -0700908 System.out.println("charSubTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700909 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700910 }
911 res = intOperTest(70000, -3);
912 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700913 System.out.println("intOperTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700914 } else {
buzbee1da522d2011-09-04 11:22:20 -0700915 System.out.println("intOperTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700916 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700917 }
Brian Carlstrom25c33252011-09-18 15:58:35 -0700918 res = lit16Test(77777);
919 if (res == 0) {
920 System.out.println("lit16Test PASSED");
921 } else {
922 System.out.println("lit16Test FAILED: " + res);
923 failure = true;
924 }
925 res = lit8Test(-55555);
926 if (res == 0) {
927 System.out.println("lit8Test PASSED");
928 } else {
929 System.out.println("lit8Test FAILED: " + res);
930 failure = true;
931 }
932 res = intShiftTest(0xff00aa01, 8);
933 if (res == 0) {
934 System.out.println("intShiftTest PASSED");
935 } else {
936 System.out.println("intShiftTest FAILED: " + res);
937 failure = true;
938 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700939 res = longOperTest(70000000000L, -3L);
940 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700941 System.out.println("longOperTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700942 } else {
buzbee1da522d2011-09-04 11:22:20 -0700943 System.out.println("longOperTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700944 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700945 }
buzbeef0cde542011-09-13 14:55:02 -0700946 lres = longShiftTest(0xd5aa96deff00aa01L, 16);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700947 if (lres == 0x96deff00aa010000L) {
buzbee1da522d2011-09-04 11:22:20 -0700948 System.out.println("longShiftTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700949 } else {
buzbee1da522d2011-09-04 11:22:20 -0700950 System.out.println("longShiftTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700951 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700952 }
953
954 res = switchTest(1);
955 if (res == 1234) {
buzbee1da522d2011-09-04 11:22:20 -0700956 System.out.println("switchTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700957 } else {
buzbee1da522d2011-09-04 11:22:20 -0700958 System.out.println("switchTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700959 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700960 }
961
962 res = testIntCompare(-5, 4, 4, 0);
963 if (res == 1111) {
buzbee1da522d2011-09-04 11:22:20 -0700964 System.out.println("testIntCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700965 } else {
buzbee1da522d2011-09-04 11:22:20 -0700966 System.out.println("testIntCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700967 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700968 }
969
970 res = testLongCompare(-5L, -4294967287L, 4L, 8L);
971 if (res == 2222) {
buzbee1da522d2011-09-04 11:22:20 -0700972 System.out.println("testLongCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700973 } else {
buzbee1da522d2011-09-04 11:22:20 -0700974 System.out.println("testLongCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700975 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700976 }
977
978 res = testFloatCompare(-5.0f, 4.0f, 4.0f, (1.0f/0.0f) / (1.0f/0.0f));
979 if (res == 3333) {
buzbee1da522d2011-09-04 11:22:20 -0700980 System.out.println("testFloatCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700981 } else {
buzbee1da522d2011-09-04 11:22:20 -0700982 System.out.println("testFloatCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700983 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700984 }
985
986 res = testDoubleCompare(-5.0, 4.0, 4.0, (1.0/0.0) / (1.0/0.0));
987 if (res == 4444) {
buzbee1da522d2011-09-04 11:22:20 -0700988 System.out.println("testDoubleCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700989 } else {
buzbee1da522d2011-09-04 11:22:20 -0700990 System.out.println("testDoubleCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700991 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700992 }
993
994 res = fibonacci(10);
995 if (res == 55) {
buzbee1da522d2011-09-04 11:22:20 -0700996 System.out.println("fibonacci PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700997 } else {
buzbee1da522d2011-09-04 11:22:20 -0700998 System.out.println("fibonacci FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700999 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001000 }
1001
buzbeee9a72f62011-09-04 17:59:07 -07001002 res = throwAndCatch();
1003 if (res == 0) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001004 System.out.println("throwAndCatch PASSED");
buzbeee9a72f62011-09-04 17:59:07 -07001005 } else {
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001006 System.out.println("throwAndCatch FAILED: " + res);
1007 failure = true;
buzbeee9a72f62011-09-04 17:59:07 -07001008 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001009
Ian Rogersff1ed472011-09-20 13:46:24 -07001010 res = testClassCast();
1011 if (res == 888) {
1012 System.out.println("testClassCast PASSED");
1013 } else {
1014 System.out.println("testClassCast FAILED: " + res);
1015 failure = true;
1016 }
1017
Ian Rogerse51a5112011-09-23 14:16:35 -07001018 res = testArrayStoreException();
1019 if (res == 211) {
1020 System.out.println("testArrayStore PASSED");
1021 } else {
1022 System.out.println("testArrayStore FAILED: " + res);
1023 failure = true;
1024 }
1025
Ian Rogers932746a2011-09-22 18:57:50 -07001026 lres= testStackOverflow();
1027 if (lres == 0) {
1028 System.out.println("testStackOverflow PASSED");
1029 } else {
1030 System.out.println("testStackOverflow FAILED: " + lres);
1031 failure = true;
1032 }
1033
Ian Rogersb886da82011-09-23 16:27:54 -07001034 res = testArrayAllocation();
1035 if (res == 12) {
1036 System.out.println("testArrayAllocation PASSED");
1037 } else {
1038 System.out.println("testArrayAllocation FAILED: " + res);
1039 failure = true;
1040 }
1041
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001042 res = manyArgs(0, 1L, 2, 3L, 4, 5L, 6, 7, 8.0, 9.0f, 10.0,
1043 (short)11, 12, (char)13, 14, 15, (byte)-16, true, 18,
1044 19, 20L, 21L, 22, 23, 24, 25, 26);
1045 if (res == -1) {
buzbee1da522d2011-09-04 11:22:20 -07001046 System.out.println("manyArgs PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001047 } else {
buzbee1da522d2011-09-04 11:22:20 -07001048 System.out.println("manyArgs FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001049 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001050 }
1051
1052 res = staticCall(3);
1053 if (res == 6) {
buzbee1da522d2011-09-04 11:22:20 -07001054 System.out.println("virtualCall PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001055 } else {
buzbee1da522d2011-09-04 11:22:20 -07001056 System.out.println("virtualCall FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001057 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001058 }
1059
1060 res = testIGetPut(111);
1061 if (res == 333) {
buzbee1da522d2011-09-04 11:22:20 -07001062 System.out.println("testGetPut PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001063 } else {
buzbee1da522d2011-09-04 11:22:20 -07001064 System.out.println("testGetPut FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001065 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001066 }
1067
1068 res = staticFieldTest(404);
1069 if (res == 1404) {
buzbee1da522d2011-09-04 11:22:20 -07001070 System.out.println("staticFieldTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001071 } else {
buzbee1da522d2011-09-04 11:22:20 -07001072 System.out.println("staticFieldTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001073 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001074 }
buzbee1b4c8592011-08-31 10:43:51 -07001075
1076 res = catchBlock(1000);
1077 if (res == 1579) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001078 System.out.println("catchBlock(1000) PASSED");
buzbee1b4c8592011-08-31 10:43:51 -07001079 } else {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001080 System.out.println("catchBlock(1000) FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001081 failure = true;
buzbee1b4c8592011-08-31 10:43:51 -07001082 }
Brian Carlstrom25c33252011-09-18 15:58:35 -07001083 res = catchBlock(7000);
1084 if (res == 7777) {
1085 System.out.println("catchBlock(7000) PASSED");
1086 } else {
1087 System.out.println("catchBlock(7000) FAILED: " + res);
1088 failure = true;
1089 }
buzbeee9a72f62011-09-04 17:59:07 -07001090 res = catchBlockNoThrow(1000);
1091 if (res == 1123) {
1092 System.out.println("catchBlockNoThrow PASSED");
1093 } else {
1094 System.out.println("catchBlockNoThrow FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001095 failure = true;
buzbeee9a72f62011-09-04 17:59:07 -07001096 }
1097
buzbee4a3164f2011-09-03 11:25:10 -07001098 res = superTest(4141);
1099 if (res == 4175) {
buzbee1da522d2011-09-04 11:22:20 -07001100 System.out.println("superTest PASSED");
buzbee4a3164f2011-09-03 11:25:10 -07001101 } else {
buzbee1da522d2011-09-04 11:22:20 -07001102 System.out.println("superTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001103 failure = true;
buzbee4a3164f2011-09-03 11:25:10 -07001104 }
buzbee2a475e72011-09-07 17:19:17 -07001105
Brian Carlstrom25c33252011-09-18 15:58:35 -07001106 res = constClassTest(1111);
1107 if (res == 2222) {
1108 System.out.println("constClassTest PASSED");
1109 } else {
1110 System.out.println("constClassTest FAILED: " + res);
1111 failure = true;
1112 }
1113
buzbee2a475e72011-09-07 17:19:17 -07001114 res = constStringTest(10);
1115 if (res == 22) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001116 System.out.println("constStringTest PASSED");
buzbee2a475e72011-09-07 17:19:17 -07001117 } else {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001118 System.out.println("constStringTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001119 failure = true;
buzbee2a475e72011-09-07 17:19:17 -07001120 }
1121
1122 res = instanceTest(10);
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001123 if (res == 1436) {
buzbee2a475e72011-09-07 17:19:17 -07001124 System.out.println("instanceTest PASSED");
1125 } else {
1126 System.out.println("instanceTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001127 failure = true;
buzbee2a475e72011-09-07 17:19:17 -07001128 }
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001129
1130 System.exit(failure ? 1 : 0);
buzbee4a3164f2011-09-03 11:25:10 -07001131 }
1132}
1133
1134class IntMathBase {
1135 IntMathBase() {
1136 }
1137
1138 int tryThing() {
1139 return 7;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001140 }
1141}