Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 3 | class IntMath extends IntMathBase { |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 4 | |
| 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) { |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 19 | super(); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 20 | foo_ = stuff; |
| 21 | } |
| 22 | |
| 23 | public IntMath() { |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 24 | super(); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 25 | foo_ = 123; |
| 26 | } |
| 27 | |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 28 | int tryThing() { |
| 29 | int val = super.tryThing(); |
| 30 | return val + 10; |
| 31 | } |
| 32 | |
| 33 | static int superTest(int x) { |
| 34 | IntMath instance = new IntMath(); |
| 35 | IntMath base = instance; |
| 36 | int val1 = instance.tryThing(); |
| 37 | int val2 = base.tryThing(); |
| 38 | return val1 + val2 + x; |
| 39 | } |
| 40 | |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 41 | static int constClassTest(int x) { |
| 42 | Class c = String.class; |
| 43 | if (c != null) { |
| 44 | return x * 2; |
| 45 | } else { |
| 46 | return x; |
| 47 | } |
| 48 | } |
| 49 | |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 50 | static int constStringTest(int x) { |
| 51 | /* TODO: flesh this test out when we can call string library */ |
| 52 | String str = "Hello World!"; |
| 53 | return x * 2; |
| 54 | } |
| 55 | |
| 56 | static void throwNullPointerException() { |
| 57 | throw new NullPointerException(); |
| 58 | } |
| 59 | |
| 60 | static int catchBlock(int x) { |
| 61 | try { |
| 62 | x += 123; |
| 63 | throwNullPointerException(); |
| 64 | } catch (NullPointerException npe) { |
| 65 | x += 456; |
| 66 | } |
| 67 | return x; |
| 68 | } |
| 69 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 70 | static int staticFieldTest(int x) { |
| 71 | mBoolean1 = true; |
| 72 | mBoolean2 = false; |
| 73 | mByte1 = 127; |
| 74 | mByte2 = -128; |
| 75 | mChar1 = 32767; |
| 76 | mChar2 = 65535; |
| 77 | mShort1 = 32767; |
| 78 | mShort2 = -32768; |
| 79 | mInt1 = 65537; |
| 80 | mInt2 = -65537; |
| 81 | mFloat1 = 3.1415f; |
| 82 | mFloat2 = -1.0f / 0.0f; // -inf |
| 83 | mLong1 = 1234605616436508552L; // 0x1122334455667788 |
| 84 | mLong2 = -1234605616436508552L; |
| 85 | mDouble1 = 3.1415926535; |
| 86 | mDouble2 = 1.0 / 0.0; // +inf |
| 87 | mVolatileLong1 = mLong1 - 1; |
| 88 | mVolatileLong2 = mLong2 + 1; |
| 89 | |
| 90 | if (!mBoolean1) { return 10; } |
| 91 | if (mBoolean2) { return 11; } |
| 92 | if (mByte1 != 127) { return 12; } |
| 93 | if (mByte2 != -128) { return 13; } |
| 94 | if (mChar1 != 32767) { return 14; } |
| 95 | if (mChar2 != 65535) { return 15; } |
| 96 | if (mShort1 != 32767) { return 16; } |
| 97 | if (mShort2 != -32768) { return 17; } |
| 98 | if (mInt1 != 65537) { return 18; } |
| 99 | if (mInt2 != -65537) { return 19; } |
| 100 | if (!(mFloat1 > 3.141f && mFloat1 < 3.142f)) { return 20; } |
| 101 | if (mFloat2 >= mFloat1) { return 21; } |
| 102 | if (mLong1 != 1234605616436508552L) { return 22; } |
| 103 | if (mLong2 != -1234605616436508552L) { return 23; } |
| 104 | if (!(mDouble1 > 3.141592653 && mDouble1 < 3.141592654)) { return 24; } |
| 105 | if (mDouble2 <= mDouble1) { return 25; } |
| 106 | if (mVolatileLong1 != 1234605616436508551L) { return 26; } |
| 107 | if (mVolatileLong2 != -1234605616436508551L) { return 27; } |
| 108 | |
| 109 | return 1000 + x; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Try to cause some unary operations. |
| 114 | */ |
| 115 | static int unopTest(int x) { |
| 116 | x = -x; |
| 117 | x ^= 0xffffffff; |
| 118 | return x; |
| 119 | } |
| 120 | |
| 121 | static int shiftTest1() { |
| 122 | final int[] mBytes = { |
| 123 | 0x11, 0x22, 0x33, 0x44, 0x88, 0x99, 0xaa, 0xbb |
| 124 | }; |
| 125 | long l; |
| 126 | int i1, i2; |
| 127 | |
| 128 | if (mBytes[0] != 0x11) return 20; |
| 129 | if (mBytes[1] != 0x22) return 21; |
| 130 | if (mBytes[2] != 0x33) return 22; |
| 131 | if (mBytes[3] != 0x44) return 23; |
| 132 | if (mBytes[4] != 0x88) return 24; |
| 133 | if (mBytes[5] != 0x99) return 25; |
| 134 | if (mBytes[6] != 0xaa) return 26; |
| 135 | if (mBytes[7] != 0xbb) return 27; |
| 136 | |
| 137 | i1 = mBytes[0] | mBytes[1] << 8 | mBytes[2] << 16 | mBytes[3] << 24; |
| 138 | i2 = mBytes[4] | mBytes[5] << 8 | mBytes[6] << 16 | mBytes[7] << 24; |
| 139 | l = i1 | ((long)i2 << 32); |
| 140 | |
| 141 | if (i1 != 0x44332211) { return 0x80000000 | i1; } |
| 142 | if (i2 != 0xbbaa9988) { return 2; } |
| 143 | if (l != 0xbbaa998844332211L) { return 3; } |
| 144 | |
| 145 | l = (long)mBytes[0] |
| 146 | | (long)mBytes[1] << 8 |
| 147 | | (long)mBytes[2] << 16 |
| 148 | | (long)mBytes[3] << 24 |
| 149 | | (long)mBytes[4] << 32 |
| 150 | | (long)mBytes[5] << 40 |
| 151 | | (long)mBytes[6] << 48 |
| 152 | | (long)mBytes[7] << 56; |
| 153 | |
| 154 | if (l != 0xbbaa998844332211L) { return 4; } |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int shiftTest2() { |
| 159 | |
| 160 | long a = 0x11; |
| 161 | long b = 0x22; |
| 162 | long c = 0x33; |
| 163 | long d = 0x44; |
| 164 | long e = 0x55; |
| 165 | long f = 0x66; |
| 166 | long g = 0x77; |
| 167 | long h = 0x88; |
| 168 | |
| 169 | long result = ((a << 56) | (b << 48) | (c << 40) | (d << 32) | |
| 170 | (e << 24) | (f << 16) | (g << 8) | h); |
| 171 | |
| 172 | if (result != 0x1122334455667788L) { return 1; } |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int unsignedShiftTest() { |
| 177 | byte b = -4; |
| 178 | short s = -4; |
| 179 | char c = 0xfffc; |
| 180 | int i = -4; |
| 181 | |
| 182 | b >>>= 4; |
| 183 | s >>>= 4; |
| 184 | c >>>= 4; |
| 185 | i >>>= 4; |
| 186 | |
| 187 | if ((int) b != -1) { return 1; } |
| 188 | if ((int) s != -1) { return 2; } |
| 189 | if ((int) c != 0x0fff) { return 3; } |
| 190 | if (i != 268435455) { return 4; } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int convTest() { |
| 195 | |
| 196 | float f; |
| 197 | double d; |
| 198 | int i; |
| 199 | long l; |
| 200 | |
| 201 | /* int --> long */ |
| 202 | i = 7654; |
| 203 | l = (long) i; |
| 204 | if (l != 7654L) { return 1; } |
| 205 | |
| 206 | i = -7654; |
| 207 | l = (long) i; |
| 208 | if (l != -7654L) { return 2; } |
| 209 | |
| 210 | /* long --> int (with truncation) */ |
| 211 | l = 5678956789L; |
| 212 | i = (int) l; |
| 213 | if (i != 1383989493) { return 3; } |
| 214 | |
| 215 | l = -5678956789L; |
| 216 | i = (int) l; |
| 217 | if (i != -1383989493) { return 4; } |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int charSubTest() { |
| 222 | |
| 223 | char char1 = 0x00e9; |
| 224 | char char2 = 0xffff; |
| 225 | int i; |
| 226 | |
| 227 | /* chars are unsigned-expanded to ints before subtraction */ |
| 228 | i = char1 - char2; |
| 229 | if (i != 0xffff00ea) { return 1; } |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * We pass in the arguments and return the results so the compiler |
| 235 | * doesn't do the math for us. (x=70000, y=-3) |
| 236 | */ |
| 237 | static int intOperTest(int x, int y) { |
| 238 | int[] results = new int[10]; |
| 239 | |
| 240 | /* this seems to generate "op-int" instructions */ |
| 241 | results[0] = x + y; |
| 242 | results[1] = x - y; |
| 243 | results[2] = x * y; |
| 244 | results[3] = x * x; |
| 245 | results[4] = x / y; |
| 246 | results[5] = x % -y; |
| 247 | results[6] = x & y; |
| 248 | results[7] = x | y; |
| 249 | results[8] = x ^ y; |
| 250 | |
| 251 | /* this seems to generate "op-int/2addr" instructions */ |
| 252 | results[9] = x + ((((((((x + y) - y) * y) / y) % y) & y) | y) ^ y); |
| 253 | |
| 254 | /* check this edge case while we're here (div-int/2addr) */ |
| 255 | int minInt = -2147483648; |
| 256 | int negOne = -results[5]; |
| 257 | int plusOne = 1; |
| 258 | int result = (((minInt + plusOne) - plusOne) / negOne) / negOne; |
| 259 | |
| 260 | if (result != minInt) { return 1;}; |
| 261 | if (results[0] != 69997) { return 2;}; |
| 262 | if (results[1] != 70003) { return 3;}; |
| 263 | if (results[2] != -210000) { return 4;}; |
| 264 | if (results[3] != 605032704) { return 5;}; |
| 265 | if (results[4] != -23333) { return 6;}; |
| 266 | if (results[5] != 1) { return 7;}; |
| 267 | if (results[6] != 70000) { return 8;}; |
| 268 | if (results[7] != -3) { return 9;}; |
| 269 | if (results[8] != -70003) { return 10;}; |
| 270 | if (results[9] != 70000) { return 11;}; |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * More operations, this time with 16-bit constants. (x=77777) |
| 277 | */ |
| 278 | static int lit16Test(int x) { |
| 279 | |
| 280 | int[] results = new int[8]; |
| 281 | |
| 282 | /* try to generate op-int/lit16" instructions */ |
| 283 | results[0] = x + 1000; |
| 284 | results[1] = 1000 - x; |
| 285 | results[2] = x * 1000; |
| 286 | results[3] = x / 1000; |
| 287 | results[4] = x % 1000; |
| 288 | results[5] = x & 1000; |
| 289 | results[6] = x | -1000; |
| 290 | results[7] = x ^ -1000; |
| 291 | |
| 292 | if (results[0] != 78777) { return 1; } |
| 293 | if (results[1] != -76777) { return 2; } |
| 294 | if (results[2] != 77777000) { return 3; } |
| 295 | if (results[3] != 77) { return 4; } |
| 296 | if (results[4] != 777) { return 5; } |
| 297 | if (results[5] != 960) { return 6; } |
| 298 | if (results[6] != -39) { return 7; } |
| 299 | if (results[7] != -76855) { return 8; } |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * More operations, this time with 8-bit constants. (x=-55555) |
| 305 | */ |
| 306 | static int lit8Test(int x) { |
| 307 | |
| 308 | int[] results = new int[8]; |
| 309 | |
| 310 | /* try to generate op-int/lit8" instructions */ |
| 311 | results[0] = x + 10; |
| 312 | results[1] = 10 - x; |
| 313 | results[2] = x * 10; |
| 314 | results[3] = x / 10; |
| 315 | results[4] = x % 10; |
| 316 | results[5] = x & 10; |
| 317 | results[6] = x | -10; |
| 318 | results[7] = x ^ -10; |
| 319 | int minInt = -2147483648; |
| 320 | int result = minInt / -1; |
| 321 | if (result != minInt) {return 1; } |
| 322 | if (results[0] != -55545) {return 2; } |
| 323 | if (results[1] != 55565) {return 3; } |
| 324 | if (results[2] != -555550) {return 4; } |
| 325 | if (results[3] != -5555) {return 5; } |
| 326 | if (results[4] != -5) {return 6; } |
| 327 | if (results[5] != 8) {return 7; } |
| 328 | if (results[6] != -1) {return 8; } |
| 329 | if (results[7] != 55563) {return 9; } |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /* |
| 335 | * Shift some data. (value=0xff00aa01, dist=8) |
| 336 | */ |
| 337 | static int intShiftTest(int value, int dist) { |
| 338 | int results[] = new int[4]; |
| 339 | results[0] = value << dist; |
| 340 | results[1] = value >> dist; |
| 341 | results[2] = value >>> dist; |
| 342 | results[3] = (((value << dist) >> dist) >>> dist) << dist; |
| 343 | if (results[0] != 0x00aa0100) {return 1; } |
| 344 | if (results[1] != 0xffff00aa) {return 2; } |
| 345 | if (results[2] != 0x00ff00aa) {return 3; } |
| 346 | if (results[3] != 0xaa00) {return 4; } |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * We pass in the arguments and return the results so the compiler |
| 352 | * doesn't do the math for us. (x=70000000000, y=-3) |
| 353 | */ |
| 354 | static int longOperTest(long x, long y) { |
| 355 | long[] results = new long[10]; |
| 356 | |
| 357 | /* this seems to generate "op-long" instructions */ |
| 358 | results[0] = x + y; |
| 359 | results[1] = x - y; |
| 360 | results[2] = x * y; |
| 361 | results[3] = x * x; |
| 362 | results[4] = x / y; |
| 363 | results[5] = x % -y; |
| 364 | results[6] = x & y; |
| 365 | results[7] = x | y; |
| 366 | results[8] = x ^ y; |
| 367 | /* this seems to generate "op-long/2addr" instructions */ |
| 368 | results[9] = x + ((((((((x + y) - y) * y) / y) % y) & y) | y) ^ y); |
| 369 | /* check this edge case while we're here (div-long/2addr) */ |
| 370 | long minLong = -9223372036854775808L; |
| 371 | long negOne = -results[5]; |
| 372 | long plusOne = 1; |
| 373 | long result = (((minLong + plusOne) - plusOne) / negOne) / negOne; |
| 374 | if (result != minLong) { return 1; } |
| 375 | if (results[0] != 69999999997L) { return 2; } |
| 376 | if (results[1] != 70000000003L) { return 3; } |
| 377 | if (results[2] != -210000000000L) { return 4; } |
| 378 | if (results[3] != -6833923606740729856L) { return 5; } // overflow |
| 379 | if (results[4] != -23333333333L) { return 6; } |
| 380 | if (results[5] != 1) { return 7; } |
| 381 | if (results[6] != 70000000000L) { return 8; } |
| 382 | if (results[7] != -3) { return 9; } |
| 383 | if (results[8] != -70000000003L) { return 10; } |
| 384 | if (results[9] != 70000000000L) { return 11; } |
| 385 | if (results.length != 10) { return 12; } |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Shift some data. (value=0xd5aa96deff00aa01, dist=16) |
| 391 | */ |
| 392 | static long longShiftTest(long value, int dist) { |
| 393 | long results[] = new long[4]; |
| 394 | results[0] = value << dist; |
| 395 | results[1] = value >> dist; |
| 396 | results[2] = value >>> dist; |
| 397 | results[3] = (((value << dist) >> dist) >>> dist) << dist; |
| 398 | if (results[0] != 0x96deff00aa010000L) { return results[0]; } |
| 399 | if (results[1] != 0xffffd5aa96deff00L) { return results[1]; } |
| 400 | if (results[2] != 0x0000d5aa96deff00L) { return results[2]; } |
| 401 | if (results[3] != 0xffff96deff000000L) { return results[3]; } |
| 402 | if (results.length != 4) { return 5; } |
| 403 | |
| 404 | return results[0]; // test return-long |
| 405 | } |
| 406 | |
| 407 | static int switchTest(int a) { |
| 408 | int res = 1234; |
| 409 | |
| 410 | switch (a) { |
| 411 | case -1: res = 1; return res; |
| 412 | case 0: res = 2; return res; |
| 413 | case 1: /*correct*/ break; |
| 414 | case 2: res = 3; return res; |
| 415 | case 3: res = 4; return res; |
| 416 | case 4: res = 5; return res; |
| 417 | default: res = 6; return res; |
| 418 | } |
| 419 | switch (a) { |
| 420 | case 3: res = 7; return res; |
| 421 | case 4: res = 8; return res; |
| 422 | default: /*correct*/ break; |
| 423 | } |
| 424 | |
| 425 | a = 0x12345678; |
| 426 | |
| 427 | switch (a) { |
| 428 | case 0x12345678: /*correct*/ break; |
| 429 | case 0x12345679: res = 9; return res; |
| 430 | default: res = 1; return res; |
| 431 | } |
| 432 | switch (a) { |
| 433 | case 57: res = 10; return res; |
| 434 | case -6: res = 11; return res; |
| 435 | case 0x12345678: /*correct*/ break; |
| 436 | case 22: res = 12; return res; |
| 437 | case 3: res = 13; return res; |
| 438 | default: res = 14; return res; |
| 439 | } |
| 440 | switch (a) { |
| 441 | case -6: res = 15; return res; |
| 442 | case 3: res = 16; return res; |
| 443 | default: /*correct*/ break; |
| 444 | } |
| 445 | |
| 446 | a = -5; |
| 447 | switch (a) { |
| 448 | case 12: res = 17; return res; |
| 449 | case -5: /*correct*/ break; |
| 450 | case 0: res = 18; return res; |
| 451 | default: res = 19; return res; |
| 452 | } |
| 453 | |
| 454 | switch (a) { |
| 455 | default: /*correct*/ break; |
| 456 | } |
| 457 | return res; |
| 458 | } |
| 459 | /* |
| 460 | * Test the integer comparisons in various ways. |
| 461 | */ |
| 462 | static int testIntCompare(int minus, int plus, int plus2, int zero) { |
| 463 | int res = 1111; |
| 464 | |
| 465 | if (minus > plus) |
| 466 | return 1; |
| 467 | if (minus >= plus) |
| 468 | return 2; |
| 469 | if (plus < minus) |
| 470 | return 3; |
| 471 | if (plus <= minus) |
| 472 | return 4; |
| 473 | if (plus == minus) |
| 474 | return 5; |
| 475 | if (plus != plus2) |
| 476 | return 6; |
| 477 | |
| 478 | /* try a branch-taken */ |
| 479 | if (plus != minus) { |
| 480 | res = res; |
| 481 | } else { |
| 482 | return 7; |
| 483 | } |
| 484 | |
| 485 | if (minus > 0) |
| 486 | return 8; |
| 487 | if (minus >= 0) |
| 488 | return 9; |
| 489 | if (plus < 0) |
| 490 | return 10; |
| 491 | if (plus <= 0) |
| 492 | return 11; |
| 493 | if (plus == 0) |
| 494 | return 12; |
| 495 | if (zero != 0) |
| 496 | return 13; |
| 497 | |
| 498 | if (zero == 0) { |
| 499 | res = res; |
| 500 | } else { |
| 501 | return 14; |
| 502 | } |
| 503 | return res; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Test cmp-long. |
| 508 | * |
| 509 | * minus=-5, alsoMinus=0xFFFFFFFF00000009, plus=4, alsoPlus=8 |
| 510 | */ |
| 511 | static int testLongCompare(long minus, long alsoMinus, long plus, |
| 512 | long alsoPlus) { |
| 513 | int res = 2222; |
| 514 | |
| 515 | if (minus > plus) |
| 516 | return 2; |
| 517 | if (plus < minus) |
| 518 | return 3; |
| 519 | if (plus == minus) |
| 520 | return 4; |
| 521 | |
| 522 | if (plus >= plus+1) |
| 523 | return 5; |
| 524 | if (minus >= minus+1) |
| 525 | return 6; |
| 526 | |
| 527 | /* try a branch-taken */ |
| 528 | if (plus != minus) { |
| 529 | res = res; |
| 530 | } else { |
| 531 | return 7; |
| 532 | } |
| 533 | |
| 534 | /* compare when high words are equal but low words differ */ |
| 535 | if (plus > alsoPlus) |
| 536 | return 8; |
| 537 | if (alsoPlus < plus) |
| 538 | return 9; |
| 539 | if (alsoPlus == plus) |
| 540 | return 10; |
| 541 | |
| 542 | /* high words are equal, low words have apparently different signs */ |
| 543 | if (minus < alsoMinus) // bug! |
| 544 | return 11; |
| 545 | if (alsoMinus > minus) |
| 546 | return 12; |
| 547 | if (alsoMinus == minus) |
| 548 | return 13; |
| 549 | |
| 550 | return res; |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Test cmpl-float and cmpg-float. |
| 555 | */ |
| 556 | static int testFloatCompare(float minus, float plus, float plus2, |
| 557 | float nan) { |
| 558 | |
| 559 | int res = 3333; |
| 560 | if (minus > plus) |
| 561 | res = 1; |
| 562 | if (plus < minus) |
| 563 | res = 2; |
| 564 | if (plus == minus) |
| 565 | res = 3; |
| 566 | if (plus != plus2) |
| 567 | res = 4; |
| 568 | |
| 569 | if (plus <= nan) |
| 570 | res = 5; |
| 571 | if (plus >= nan) |
| 572 | res = 6; |
| 573 | if (minus <= nan) |
| 574 | res = 7; |
| 575 | if (minus >= nan) |
| 576 | res = 8; |
| 577 | if (nan >= plus) |
| 578 | res = 9; |
| 579 | if (nan <= plus) |
| 580 | res = 10; |
| 581 | |
| 582 | if (nan == nan) |
| 583 | res = 1212; |
| 584 | |
| 585 | return res; |
| 586 | } |
| 587 | |
| 588 | static int testDoubleCompare(double minus, double plus, double plus2, |
| 589 | double nan) { |
| 590 | |
| 591 | int res = 4444; |
| 592 | |
| 593 | if (minus > plus) |
| 594 | return 1; |
| 595 | if (plus < minus) |
| 596 | return 2; |
| 597 | if (plus == minus) |
| 598 | return 3; |
| 599 | if (plus != plus2) |
| 600 | return 4; |
| 601 | |
| 602 | if (plus <= nan) |
| 603 | return 5; |
| 604 | if (plus >= nan) |
| 605 | return 6; |
| 606 | if (minus <= nan) |
| 607 | return 7; |
| 608 | if (minus >= nan) |
| 609 | return 8; |
| 610 | if (nan >= plus) |
| 611 | return 9; |
| 612 | if (nan <= plus) |
| 613 | return 10; |
| 614 | |
| 615 | if (nan == nan) |
| 616 | return 11; |
| 617 | return res; |
| 618 | } |
| 619 | |
| 620 | static int fibonacci(int n) { |
| 621 | if (n == 0) { |
| 622 | return 0; |
| 623 | } else if (n == 1) { |
| 624 | return 1; |
| 625 | } else { |
| 626 | return fibonacci(n - 1) + fibonacci(n - 2); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | static void throwNullPointerException() { |
| 632 | throw new NullPointerException("first throw"); |
| 633 | } |
| 634 | |
| 635 | static int throwAndCatch() { |
| 636 | try { |
| 637 | throwNullPointerException(); |
| 638 | return 1; |
| 639 | } catch (NullPointerException npe) { |
| 640 | return 0; |
| 641 | } |
| 642 | } |
| 643 | */ |
| 644 | |
| 645 | static int manyArgs(int a0, long a1, int a2, long a3, int a4, long a5, |
| 646 | int a6, int a7, double a8, float a9, double a10, short a11, int a12, |
| 647 | char a13, int a14, int a15, byte a16, boolean a17, int a18, int a19, |
| 648 | long a20, long a21, int a22, int a23, int a24, int a25, int a26) |
| 649 | { |
| 650 | if (a0 != 0) return 0; |
| 651 | if (a1 != 1L) return 1; |
| 652 | if (a2 != 2) return 2; |
| 653 | if (a3 != 3L) return 3; |
| 654 | if (a4 != 4) return 4; |
| 655 | if (a5 != 5L) return 5; |
| 656 | if (a6 != 6) return 6; |
| 657 | if (a7 != 7) return 7; |
| 658 | if (a8 != 8.0) return 8; |
| 659 | if (a9 != 9.0f) return 9; |
| 660 | if (a10 != 10.0) return 10; |
| 661 | if (a11 != (short)11) return 11; |
| 662 | if (a12 != 12) return 12; |
| 663 | if (a13 != (char)13) return 13; |
| 664 | if (a14 != 14) return 14; |
| 665 | if (a15 != 15) return 15; |
| 666 | if (a16 != (byte)-16) return 16; |
| 667 | if (a17 != true) return 17; |
| 668 | if (a18 != 18) return 18; |
| 669 | if (a19 != 19) return 19; |
| 670 | if (a20 != 20L) return 20; |
| 671 | if (a21 != 21L) return 21; |
| 672 | if (a22 != 22) return 22; |
| 673 | if (a23 != 23) return 23; |
| 674 | if (a24 != 24) return 24; |
| 675 | if (a25 != 25) return 25; |
| 676 | if (a26 != 26) return 26; |
| 677 | return -1; |
| 678 | } |
| 679 | |
| 680 | int virtualCall(int a) |
| 681 | { |
| 682 | return a * 2; |
| 683 | } |
| 684 | |
| 685 | void setFoo(int a) |
| 686 | { |
| 687 | foo_ = a; |
| 688 | } |
| 689 | |
| 690 | int getFoo() |
| 691 | { |
| 692 | return foo_; |
| 693 | } |
| 694 | |
| 695 | static int staticCall(int a) |
| 696 | { |
| 697 | IntMath foo = new IntMath(); |
| 698 | return foo.virtualCall(a); |
| 699 | } |
| 700 | |
| 701 | static int testIGetPut(int a) |
| 702 | { |
| 703 | IntMath foo = new IntMath(99); |
| 704 | IntMath foo123 = new IntMath(); |
| 705 | int z = foo.getFoo(); |
| 706 | z += a; |
| 707 | z += foo123.getFoo(); |
| 708 | foo.setFoo(z); |
| 709 | return foo.getFoo(); |
| 710 | } |
| 711 | |
| 712 | public static void main(String[] args) { |
| 713 | int res = unopTest(38); |
| 714 | if (res == 37) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 715 | System.out.println("unopTest PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 716 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 717 | System.out.println("unopTest FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 718 | } |
| 719 | res = shiftTest1(); |
| 720 | if (res == 0) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 721 | System.out.println("shiftTest1 PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 722 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 723 | System.out.println("shiftTest1 FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 724 | } |
| 725 | res = shiftTest2(); |
| 726 | if (res == 0) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 727 | System.out.println("shiftTest2 PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 728 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 729 | System.out.println("shiftTest2 FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 730 | } |
| 731 | res = unsignedShiftTest(); |
| 732 | if (res == 0) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 733 | System.out.println("unsignedShiftTest PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 734 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 735 | System.out.println("unsignedShiftTest FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 736 | } |
| 737 | res = convTest(); |
| 738 | if (res == 0) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 739 | System.out.println("convTest PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 740 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 741 | System.out.println("convTest FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 742 | } |
| 743 | res = charSubTest(); |
| 744 | if (res == 0) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 745 | System.out.println("charSubTest PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 746 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 747 | System.out.println("charSubTest FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 748 | } |
| 749 | res = intOperTest(70000, -3); |
| 750 | if (res == 0) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 751 | System.out.println("intOperTest PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 752 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 753 | System.out.println("intOperTest FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 754 | } |
| 755 | res = longOperTest(70000000000L, -3L); |
| 756 | if (res == 0) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 757 | System.out.println("longOperTest PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 758 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 759 | System.out.println("longOperTest FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 760 | } |
| 761 | long lres = longShiftTest(0xd5aa96deff00aa01L, 16); |
| 762 | if (lres == 0x96deff00aa010000L) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 763 | System.out.println("longShiftTest PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 764 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 765 | System.out.println("longShiftTest FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | res = switchTest(1); |
| 769 | if (res == 1234) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 770 | System.out.println("switchTest PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 771 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 772 | System.out.println("switchTest FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | res = testIntCompare(-5, 4, 4, 0); |
| 776 | if (res == 1111) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 777 | System.out.println("testIntCompare PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 778 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 779 | System.out.println("testIntCompare FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | res = testLongCompare(-5L, -4294967287L, 4L, 8L); |
| 783 | if (res == 2222) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 784 | System.out.println("testLongCompare PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 785 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 786 | System.out.println("testLongCompare FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | res = testFloatCompare(-5.0f, 4.0f, 4.0f, (1.0f/0.0f) / (1.0f/0.0f)); |
| 790 | if (res == 3333) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 791 | System.out.println("testFloatCompare PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 792 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 793 | System.out.println("testFloatCompare FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | res = testDoubleCompare(-5.0, 4.0, 4.0, (1.0/0.0) / (1.0/0.0)); |
| 797 | if (res == 4444) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 798 | System.out.println("testDoubleCompare PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 799 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 800 | System.out.println("testDoubleCompare FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | res = fibonacci(10); |
| 804 | if (res == 55) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 805 | System.out.println("fibonacci PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 806 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 807 | System.out.println("fibonacci FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /* |
| 811 | res = throwAndCatch(); |
| 812 | if (res == 0) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 813 | System.out.println("throwAndCatch PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 814 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 815 | System.out.println("throwAndCatch FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 816 | } |
| 817 | */ |
| 818 | |
| 819 | res = manyArgs(0, 1L, 2, 3L, 4, 5L, 6, 7, 8.0, 9.0f, 10.0, |
| 820 | (short)11, 12, (char)13, 14, 15, (byte)-16, true, 18, |
| 821 | 19, 20L, 21L, 22, 23, 24, 25, 26); |
| 822 | if (res == -1) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 823 | System.out.println("manyArgs PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 824 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 825 | System.out.println("manyArgs FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | res = staticCall(3); |
| 829 | if (res == 6) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 830 | System.out.println("virtualCall PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 831 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 832 | System.out.println("virtualCall FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | res = testIGetPut(111); |
| 836 | if (res == 333) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 837 | System.out.println("testGetPut PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 838 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 839 | System.out.println("testGetPut FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | res = staticFieldTest(404); |
| 843 | if (res == 1404) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 844 | System.out.println("staticFieldTest PASSED"); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 845 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 846 | System.out.println("staticFieldTest FAILED: " + res); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 847 | } |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 848 | |
| 849 | res = catchBlock(1000); |
| 850 | if (res == 1579) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 851 | System.out.println("catchBlock PASSED"); |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 852 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 853 | System.out.println("catchBlock FAILED: " + res); |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 854 | } |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 855 | |
| 856 | res = superTest(4141); |
| 857 | if (res == 4175) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 858 | System.out.println("superTest PASSED"); |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 859 | } else { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame^] | 860 | System.out.println("superTest FAILED: " + res); |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | class IntMathBase { |
| 866 | IntMathBase() { |
| 867 | } |
| 868 | |
| 869 | int tryThing() { |
| 870 | return 7; |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 871 | } |
| 872 | } |