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