blob: c1cb9767e816ee933ad5355aedf357dc1f2730e2 [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) {
642
643 int res = 3333;
644 if (minus > plus)
645 res = 1;
646 if (plus < minus)
647 res = 2;
648 if (plus == minus)
649 res = 3;
650 if (plus != plus2)
651 res = 4;
652
653 if (plus <= nan)
654 res = 5;
655 if (plus >= nan)
656 res = 6;
657 if (minus <= nan)
658 res = 7;
659 if (minus >= nan)
660 res = 8;
661 if (nan >= plus)
662 res = 9;
663 if (nan <= plus)
664 res = 10;
665
666 if (nan == nan)
667 res = 1212;
668
669 return res;
670 }
671
672 static int testDoubleCompare(double minus, double plus, double plus2,
673 double nan) {
674
675 int res = 4444;
676
677 if (minus > plus)
678 return 1;
679 if (plus < minus)
680 return 2;
681 if (plus == minus)
682 return 3;
683 if (plus != plus2)
684 return 4;
685
686 if (plus <= nan)
687 return 5;
688 if (plus >= nan)
689 return 6;
690 if (minus <= nan)
691 return 7;
692 if (minus >= nan)
693 return 8;
694 if (nan >= plus)
695 return 9;
696 if (nan <= plus)
697 return 10;
698
699 if (nan == nan)
700 return 11;
701 return res;
702 }
703
704 static int fibonacci(int n) {
705 if (n == 0) {
706 return 0;
707 } else if (n == 1) {
708 return 1;
709 } else {
710 return fibonacci(n - 1) + fibonacci(n - 2);
711 }
712 }
713
buzbeee9a72f62011-09-04 17:59:07 -0700714 static int throwAndCatch() {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700715 try {
716 throwNullPointerException();
717 return 1;
718 } catch (NullPointerException npe) {
719 return 0;
720 }
buzbeee9a72f62011-09-04 17:59:07 -0700721 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700722
723 static int manyArgs(int a0, long a1, int a2, long a3, int a4, long a5,
724 int a6, int a7, double a8, float a9, double a10, short a11, int a12,
725 char a13, int a14, int a15, byte a16, boolean a17, int a18, int a19,
726 long a20, long a21, int a22, int a23, int a24, int a25, int a26)
727 {
728 if (a0 != 0) return 0;
729 if (a1 != 1L) return 1;
730 if (a2 != 2) return 2;
731 if (a3 != 3L) return 3;
732 if (a4 != 4) return 4;
733 if (a5 != 5L) return 5;
734 if (a6 != 6) return 6;
735 if (a7 != 7) return 7;
736 if (a8 != 8.0) return 8;
737 if (a9 != 9.0f) return 9;
738 if (a10 != 10.0) return 10;
739 if (a11 != (short)11) return 11;
740 if (a12 != 12) return 12;
741 if (a13 != (char)13) return 13;
742 if (a14 != 14) return 14;
743 if (a15 != 15) return 15;
744 if (a16 != (byte)-16) return 16;
745 if (a17 != true) return 17;
746 if (a18 != 18) return 18;
747 if (a19 != 19) return 19;
748 if (a20 != 20L) return 20;
749 if (a21 != 21L) return 21;
750 if (a22 != 22) return 22;
751 if (a23 != 23) return 23;
752 if (a24 != 24) return 24;
753 if (a25 != 25) return 25;
754 if (a26 != 26) return 26;
755 return -1;
756 }
757
758 int virtualCall(int a)
759 {
760 return a * 2;
761 }
762
763 void setFoo(int a)
764 {
765 foo_ = a;
766 }
767
768 int getFoo()
769 {
770 return foo_;
771 }
772
773 static int staticCall(int a)
774 {
775 IntMath foo = new IntMath();
776 return foo.virtualCall(a);
777 }
778
779 static int testIGetPut(int a)
780 {
781 IntMath foo = new IntMath(99);
782 IntMath foo123 = new IntMath();
783 int z = foo.getFoo();
784 z += a;
785 z += foo123.getFoo();
786 foo.setFoo(z);
787 return foo.getFoo();
788 }
789
Ian Rogersff1ed472011-09-20 13:46:24 -0700790 static int throwClassCast(Object o) {
791 return ((Integer)o).intValue();
792 }
793
794 static int testClassCast() {
795 int res = 0;
796 try {
797 res += throwClassCast(Integer.valueOf(123));
798 } catch(ClassCastException e) {
799 res += 456;
800 }
801 try {
802 res += throwClassCast(new Short((short)321));
803 } catch(ClassCastException e) {
804 res += 765;
805 }
806 return res;
807 }
808
Ian Rogerse51a5112011-09-23 14:16:35 -0700809 static void throwArrayStoreException(Object[] array, Object element) {
810 array[0] = element;
811 }
812
813 static int testArrayStoreException() {
814 int res=0;
815 Object[] array = new Number[2];
816 try {
817 throwArrayStoreException(array, null);
818 res += 1;
819 } catch(ArrayStoreException e) {
820 res += 2;
821 }
822 try {
823 throwArrayStoreException(array, Integer.valueOf(1));
824 res += 10;
825 } catch(ArrayStoreException e) {
826 res += 20;
827 }
828 try {
829 throwArrayStoreException(array, "hello MTV-44");
830 res += 100;
831 } catch(ArrayStoreException e) {
832 res += 200;
833 }
834 return res;
835 }
836
Ian Rogers932746a2011-09-22 18:57:50 -0700837 static long recursion_count_;
838 static void throwStackOverflow(long l) {
839 recursion_count_++;
840 throwStackOverflow(recursion_count_);
841 }
842
843 static long testStackOverflow() {
844 try {
845 throwStackOverflow(0);
846 if (recursion_count_ != 0) {
847 return recursion_count_;
848 } else {
849 return -1;
850 }
851 } catch(StackOverflowError soe) {
852 return 0;
853 }
854 }
855
Ian Rogersb886da82011-09-23 16:27:54 -0700856 static int testArrayAllocation() {
857 int res = 0;
858 try {
859 int[] x = new int[-1];
860 res += 1;
861 } catch (NegativeArraySizeException e) {
862 res += 2;
863 }
864 try {
865 int[] x = new int [1];
866 res += 10;
867 } catch (Throwable e) {
868 res += 20;
869 }
870 return res;
871 }
872
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700873 public static void main(String[] args) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700874 boolean failure = false;
buzbeef0cde542011-09-13 14:55:02 -0700875 int res;
876 long lres;
877
878 lres = divideLongByBillion(123000000000L);
879 if (lres == 123) {
880 System.out.println("divideLongByBillion PASSED");
881 } else {
882 System.out.println("divideLongByBillion FAILED: " + lres);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700883 failure = true;
buzbeef0cde542011-09-13 14:55:02 -0700884 }
885 res = unopTest(38);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700886 if (res == 37) {
buzbee1da522d2011-09-04 11:22:20 -0700887 System.out.println("unopTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700888 } else {
buzbee1da522d2011-09-04 11:22:20 -0700889 System.out.println("unopTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700890 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700891 }
892 res = shiftTest1();
893 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700894 System.out.println("shiftTest1 PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700895 } else {
buzbee1da522d2011-09-04 11:22:20 -0700896 System.out.println("shiftTest1 FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700897 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700898 }
899 res = shiftTest2();
900 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700901 System.out.println("shiftTest2 PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700902 } else {
buzbee1da522d2011-09-04 11:22:20 -0700903 System.out.println("shiftTest2 FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700904 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700905 }
906 res = unsignedShiftTest();
907 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700908 System.out.println("unsignedShiftTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700909 } else {
buzbee1da522d2011-09-04 11:22:20 -0700910 System.out.println("unsignedShiftTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700911 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700912 }
913 res = convTest();
914 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700915 System.out.println("convTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700916 } else {
buzbee1da522d2011-09-04 11:22:20 -0700917 System.out.println("convTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700918 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700919 }
920 res = charSubTest();
921 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700922 System.out.println("charSubTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700923 } else {
buzbee1da522d2011-09-04 11:22:20 -0700924 System.out.println("charSubTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700925 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700926 }
927 res = intOperTest(70000, -3);
928 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700929 System.out.println("intOperTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700930 } else {
buzbee1da522d2011-09-04 11:22:20 -0700931 System.out.println("intOperTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700932 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700933 }
Brian Carlstrom25c33252011-09-18 15:58:35 -0700934 res = lit16Test(77777);
935 if (res == 0) {
936 System.out.println("lit16Test PASSED");
937 } else {
938 System.out.println("lit16Test FAILED: " + res);
939 failure = true;
940 }
941 res = lit8Test(-55555);
942 if (res == 0) {
943 System.out.println("lit8Test PASSED");
944 } else {
945 System.out.println("lit8Test FAILED: " + res);
946 failure = true;
947 }
948 res = intShiftTest(0xff00aa01, 8);
949 if (res == 0) {
950 System.out.println("intShiftTest PASSED");
951 } else {
952 System.out.println("intShiftTest FAILED: " + res);
953 failure = true;
954 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700955 res = longOperTest(70000000000L, -3L);
956 if (res == 0) {
buzbee1da522d2011-09-04 11:22:20 -0700957 System.out.println("longOperTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700958 } else {
buzbee1da522d2011-09-04 11:22:20 -0700959 System.out.println("longOperTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700960 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700961 }
buzbeef0cde542011-09-13 14:55:02 -0700962 lres = longShiftTest(0xd5aa96deff00aa01L, 16);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700963 if (lres == 0x96deff00aa010000L) {
buzbee1da522d2011-09-04 11:22:20 -0700964 System.out.println("longShiftTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700965 } else {
buzbee1da522d2011-09-04 11:22:20 -0700966 System.out.println("longShiftTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700967 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700968 }
969
970 res = switchTest(1);
971 if (res == 1234) {
buzbee1da522d2011-09-04 11:22:20 -0700972 System.out.println("switchTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700973 } else {
buzbee1da522d2011-09-04 11:22:20 -0700974 System.out.println("switchTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700975 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700976 }
977
978 res = testIntCompare(-5, 4, 4, 0);
979 if (res == 1111) {
buzbee1da522d2011-09-04 11:22:20 -0700980 System.out.println("testIntCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700981 } else {
buzbee1da522d2011-09-04 11:22:20 -0700982 System.out.println("testIntCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700983 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700984 }
985
986 res = testLongCompare(-5L, -4294967287L, 4L, 8L);
987 if (res == 2222) {
buzbee1da522d2011-09-04 11:22:20 -0700988 System.out.println("testLongCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700989 } else {
buzbee1da522d2011-09-04 11:22:20 -0700990 System.out.println("testLongCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700991 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700992 }
993
994 res = testFloatCompare(-5.0f, 4.0f, 4.0f, (1.0f/0.0f) / (1.0f/0.0f));
995 if (res == 3333) {
buzbee1da522d2011-09-04 11:22:20 -0700996 System.out.println("testFloatCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700997 } else {
buzbee1da522d2011-09-04 11:22:20 -0700998 System.out.println("testFloatCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700999 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001000 }
1001
1002 res = testDoubleCompare(-5.0, 4.0, 4.0, (1.0/0.0) / (1.0/0.0));
1003 if (res == 4444) {
buzbee1da522d2011-09-04 11:22:20 -07001004 System.out.println("testDoubleCompare PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001005 } else {
buzbee1da522d2011-09-04 11:22:20 -07001006 System.out.println("testDoubleCompare FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001007 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001008 }
1009
1010 res = fibonacci(10);
1011 if (res == 55) {
buzbee1da522d2011-09-04 11:22:20 -07001012 System.out.println("fibonacci PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001013 } else {
buzbee1da522d2011-09-04 11:22:20 -07001014 System.out.println("fibonacci FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001015 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001016 }
1017
buzbeee9a72f62011-09-04 17:59:07 -07001018 res = throwAndCatch();
1019 if (res == 0) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001020 System.out.println("throwAndCatch PASSED");
buzbeee9a72f62011-09-04 17:59:07 -07001021 } else {
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001022 System.out.println("throwAndCatch FAILED: " + res);
1023 failure = true;
buzbeee9a72f62011-09-04 17:59:07 -07001024 }
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001025
Ian Rogersff1ed472011-09-20 13:46:24 -07001026 res = testClassCast();
1027 if (res == 888) {
1028 System.out.println("testClassCast PASSED");
1029 } else {
1030 System.out.println("testClassCast FAILED: " + res);
1031 failure = true;
1032 }
1033
Ian Rogerse51a5112011-09-23 14:16:35 -07001034 res = testArrayStoreException();
1035 if (res == 211) {
1036 System.out.println("testArrayStore PASSED");
1037 } else {
1038 System.out.println("testArrayStore FAILED: " + res);
1039 failure = true;
1040 }
1041
Ian Rogers932746a2011-09-22 18:57:50 -07001042 lres= testStackOverflow();
1043 if (lres == 0) {
1044 System.out.println("testStackOverflow PASSED");
1045 } else {
1046 System.out.println("testStackOverflow FAILED: " + lres);
1047 failure = true;
1048 }
1049
Ian Rogersb886da82011-09-23 16:27:54 -07001050 res = testArrayAllocation();
1051 if (res == 12) {
1052 System.out.println("testArrayAllocation PASSED");
1053 } else {
1054 System.out.println("testArrayAllocation FAILED: " + res);
1055 failure = true;
1056 }
1057
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001058 res = manyArgs(0, 1L, 2, 3L, 4, 5L, 6, 7, 8.0, 9.0f, 10.0,
1059 (short)11, 12, (char)13, 14, 15, (byte)-16, true, 18,
1060 19, 20L, 21L, 22, 23, 24, 25, 26);
1061 if (res == -1) {
buzbee1da522d2011-09-04 11:22:20 -07001062 System.out.println("manyArgs PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001063 } else {
buzbee1da522d2011-09-04 11:22:20 -07001064 System.out.println("manyArgs FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001065 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001066 }
1067
1068 res = staticCall(3);
1069 if (res == 6) {
buzbee1da522d2011-09-04 11:22:20 -07001070 System.out.println("virtualCall PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001071 } else {
buzbee1da522d2011-09-04 11:22:20 -07001072 System.out.println("virtualCall FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001073 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001074 }
1075
1076 res = testIGetPut(111);
1077 if (res == 333) {
buzbee1da522d2011-09-04 11:22:20 -07001078 System.out.println("testGetPut PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001079 } else {
buzbee1da522d2011-09-04 11:22:20 -07001080 System.out.println("testGetPut FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001081 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001082 }
1083
1084 res = staticFieldTest(404);
1085 if (res == 1404) {
buzbee1da522d2011-09-04 11:22:20 -07001086 System.out.println("staticFieldTest PASSED");
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001087 } else {
buzbee1da522d2011-09-04 11:22:20 -07001088 System.out.println("staticFieldTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001089 failure = true;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001090 }
buzbee1b4c8592011-08-31 10:43:51 -07001091
1092 res = catchBlock(1000);
1093 if (res == 1579) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001094 System.out.println("catchBlock(1000) PASSED");
buzbee1b4c8592011-08-31 10:43:51 -07001095 } else {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001096 System.out.println("catchBlock(1000) FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001097 failure = true;
buzbee1b4c8592011-08-31 10:43:51 -07001098 }
Brian Carlstrom25c33252011-09-18 15:58:35 -07001099 res = catchBlock(7000);
1100 if (res == 7777) {
1101 System.out.println("catchBlock(7000) PASSED");
1102 } else {
1103 System.out.println("catchBlock(7000) FAILED: " + res);
1104 failure = true;
1105 }
buzbeee9a72f62011-09-04 17:59:07 -07001106 res = catchBlockNoThrow(1000);
1107 if (res == 1123) {
1108 System.out.println("catchBlockNoThrow PASSED");
1109 } else {
1110 System.out.println("catchBlockNoThrow FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001111 failure = true;
buzbeee9a72f62011-09-04 17:59:07 -07001112 }
1113
buzbee4a3164f2011-09-03 11:25:10 -07001114 res = superTest(4141);
1115 if (res == 4175) {
buzbee1da522d2011-09-04 11:22:20 -07001116 System.out.println("superTest PASSED");
buzbee4a3164f2011-09-03 11:25:10 -07001117 } else {
buzbee1da522d2011-09-04 11:22:20 -07001118 System.out.println("superTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001119 failure = true;
buzbee4a3164f2011-09-03 11:25:10 -07001120 }
buzbee2a475e72011-09-07 17:19:17 -07001121
Brian Carlstrom25c33252011-09-18 15:58:35 -07001122 res = constClassTest(1111);
1123 if (res == 2222) {
1124 System.out.println("constClassTest PASSED");
1125 } else {
1126 System.out.println("constClassTest FAILED: " + res);
1127 failure = true;
1128 }
1129
buzbee2a475e72011-09-07 17:19:17 -07001130 res = constStringTest(10);
1131 if (res == 22) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001132 System.out.println("constStringTest PASSED");
buzbee2a475e72011-09-07 17:19:17 -07001133 } else {
Brian Carlstrom25c33252011-09-18 15:58:35 -07001134 System.out.println("constStringTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001135 failure = true;
buzbee2a475e72011-09-07 17:19:17 -07001136 }
1137
1138 res = instanceTest(10);
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001139 if (res == 1436) {
buzbee2a475e72011-09-07 17:19:17 -07001140 System.out.println("instanceTest PASSED");
1141 } else {
1142 System.out.println("instanceTest FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001143 failure = true;
buzbee2a475e72011-09-07 17:19:17 -07001144 }
Brian Carlstrombbf1e412011-09-18 14:14:51 -07001145
1146 System.exit(failure ? 1 : 0);
buzbee4a3164f2011-09-03 11:25:10 -07001147 }
1148}
1149
1150class IntMathBase {
1151 IntMathBase() {
1152 }
1153
1154 int tryThing() {
1155 return 7;
Brian Carlstrom9f30b382011-08-28 22:41:38 -07001156 }
1157}