comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java @ 22543:0ef597d27256

Making sure the values of random input variables are printed in case of failure
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Thu, 07 Jan 2016 15:54:23 +0100
parents f8fb609939a7
children 9aded7e1e122
comparison
equal deleted inserted replaced
22542:f8fb609939a7 22543:0ef597d27256
441 441
442 @Test 442 @Test
443 public void testPlusWithInts() throws Exception { 443 public void testPlusWithInts() throws Exception {
444 int a = RANDOM.nextInt(100); 444 int a = RANDOM.nextInt(100);
445 int b = RANDOM.nextInt(100); 445 int b = RANDOM.nextInt(100);
446 446 doPlusWithInts(a, b);
447 }
448
449 @Test
450 public void testPlusWithOneNegativeInt() throws Exception {
451 int a = -RANDOM.nextInt(100);
452 int b = RANDOM.nextInt(100);
453 doPlusWithInts(a, b);
454 }
455
456 private void doPlusWithInts(int a, int b) throws Exception {
447 PolyglotEngine.Value plus = findGlobalSymbol(plus(int.class, int.class)); 457 PolyglotEngine.Value plus = findGlobalSymbol(plus(int.class, int.class));
448 458
449 Number n = plus.execute(a, b).as(Number.class); 459 Number n = plus.execute(a, b).as(Number.class);
450 assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue(); 460 assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue();
451 } 461 }
452 462
453 @Test 463 @Test
454 public void testPlusWithBytes() throws Exception { 464 public void testPlusWithBytes() throws Exception {
455 int a = RANDOM.nextInt(100); 465 int a = RANDOM.nextInt(100);
456 int b = RANDOM.nextInt(100); 466 int b = RANDOM.nextInt(100);
457 467 doPlusWithBytes(a, b);
468 }
469
470 @Test
471 public void testPlusWithOneNegativeByte() throws Exception {
472 int a = -RANDOM.nextInt(100);
473 int b = RANDOM.nextInt(100);
474 doPlusWithBytes(a, b);
475 }
476
477 private void doPlusWithBytes(int a, int b) throws Exception {
458 PolyglotEngine.Value plus = findGlobalSymbol(plus(byte.class, byte.class)); 478 PolyglotEngine.Value plus = findGlobalSymbol(plus(byte.class, byte.class));
459 479
460 Number n = plus.execute((byte) a, (byte) b).as(Number.class); 480 Number n = plus.execute((byte) a, (byte) b).as(Number.class);
461 assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue(); 481 assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue();
462 } 482 }
463 483
464 @Test 484 @Test
465 public void testPlusWithShort() throws Exception { 485 public void testPlusWithShort() throws Exception {
466 int a = RANDOM.nextInt(100); 486 int a = RANDOM.nextInt(100);
467 int b = RANDOM.nextInt(100); 487 int b = RANDOM.nextInt(100);
468 488 doPlusWithShorts(a, b);
489 }
490
491 @Test
492 public void testPlusWithOneNegativeShort() throws Exception {
493 int a = RANDOM.nextInt(100);
494 int b = -RANDOM.nextInt(100);
495 doPlusWithShorts(a, b);
496 }
497
498 private void doPlusWithShorts(int a, int b) throws Exception {
469 PolyglotEngine.Value plus = findGlobalSymbol(plus(short.class, short.class)); 499 PolyglotEngine.Value plus = findGlobalSymbol(plus(short.class, short.class));
470 500
471 Number n = plus.execute((short) a, (short) b).as(Number.class); 501 Number n = plus.execute((short) a, (short) b).as(Number.class);
472 assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue(); 502 assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue();
473 } 503 }
474 504
475 @Test 505 @Test
476 public void testPlusWithLong() throws Exception { 506 public void testPlusWithLong() throws Exception {
477 long a = RANDOM.nextInt(100); 507 long a = RANDOM.nextInt(100);
478 long b = RANDOM.nextInt(100); 508 long b = RANDOM.nextInt(100);
479 509 doPlusWithLong(a, b);
510 }
511
512 @Test
513 public void testPlusWithLongMaxIntMinInt() throws Exception {
514 doPlusWithLong(Integer.MAX_VALUE, Integer.MIN_VALUE);
515 }
516
517 private void doPlusWithLong(long a, long b) throws Exception {
480 PolyglotEngine.Value plus = findGlobalSymbol(plus(long.class, long.class)); 518 PolyglotEngine.Value plus = findGlobalSymbol(plus(long.class, long.class));
481 519
482 Number n = plus.execute(a, b).as(Number.class); 520 Number n = plus.execute(a, b).as(Number.class);
483 assert a + b == n.longValue() : "The value is correct: (" + a + " + " + b + ") = " + n.longValue(); 521 assert a + b == n.longValue() : "The value is correct: (" + a + " + " + b + ") = " + n.longValue();
484 } 522 }
486 @Test 524 @Test
487 public void testPlusWithFloat() throws Exception { 525 public void testPlusWithFloat() throws Exception {
488 float a = RANDOM.nextFloat() * 100.0f; 526 float a = RANDOM.nextFloat() * 100.0f;
489 float b = RANDOM.nextFloat() * 100.0f; 527 float b = RANDOM.nextFloat() * 100.0f;
490 528
529 doPlusWithFloat(a, b);
530 }
531
532 private void doPlusWithFloat(float a, float b) throws Exception {
491 PolyglotEngine.Value plus = findGlobalSymbol(plus(float.class, float.class)); 533 PolyglotEngine.Value plus = findGlobalSymbol(plus(float.class, float.class));
492 534
493 Number n = plus.execute(a, b).as(Number.class); 535 Number n = plus.execute(a, b).as(Number.class);
494 assertDouble("Correct value computed", a + b, n.floatValue()); 536 assertDouble("Correct value computed: (" + a + " + " + b + ")", a + b, n.floatValue());
495 } 537 }
496 538
497 @Test 539 @Test
498 public void testPlusWithDouble() throws Exception { 540 public void testPlusWithDouble() throws Exception {
499 double a = RANDOM.nextDouble() * 100.0; 541 double a = RANDOM.nextDouble() * 100.0;
500 double b = RANDOM.nextDouble() * 100.0; 542 double b = RANDOM.nextDouble() * 100.0;
501 543 doPlusWithDouble(a, b);
502 PolyglotEngine.Value plus = findGlobalSymbol(plus(float.class, float.class)); 544 }
545
546 @Test
547 public void testPlusWithDoubleRound() throws Exception {
548 double a = RANDOM.nextInt(1000);
549 double b = RANDOM.nextInt(1000);
550
551 doPlusWithDouble(a, b);
552 }
553
554 @Test
555 public void testPlusWithDoubleMaxInt() throws Exception {
556 double a = Integer.MAX_VALUE;
557 double b = 1;
558
559 doPlusWithDouble(a, b);
560 }
561
562 private void doPlusWithDouble(double a, double b) throws Exception {
563 PolyglotEngine.Value plus = findGlobalSymbol(plus(double.class, double.class));
503 564
504 Number n = plus.execute(a, b).as(Number.class); 565 Number n = plus.execute(a, b).as(Number.class);
505 assertDouble("Correct value computed", a + b, n.doubleValue()); 566 assertDouble("Correct value computed: (" + a + " + " + b + ")", a + b, n.doubleValue());
506 } 567 }
507 568
508 @Test 569 @Test
509 public void testPlusWithIntsOnCompoundObject() throws Exception { 570 public void testPlusWithIntsOnCompoundObject() throws Exception {
510 int a = RANDOM.nextInt(100); 571 int a = RANDOM.nextInt(100);
565 626
566 byte value = (byte) RANDOM.nextInt(100); 627 byte value = (byte) RANDOM.nextInt(100);
567 628
568 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); 629 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value));
569 Number n = apply.execute(fn).as(Number.class); 630 Number n = apply.execute(fn).as(Number.class);
570 assertEquals("The same value returned", value + 10, n.byteValue()); 631 assertEquals("The same value returned (" + value + " + 10): ", value + 10, n.byteValue());
571 } 632 }
572 633
573 @Test 634 @Test
574 public void testPrimitiveReturnTypeShort() throws Exception { 635 public void testPrimitiveReturnTypeShort() throws Exception {
575 PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); 636 PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers());
576 637
577 short value = (short) RANDOM.nextInt(100); 638 short value = (short) RANDOM.nextInt(100);
578 639
579 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); 640 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value));
580 Number n = apply.execute(fn).as(Number.class); 641 Number n = apply.execute(fn).as(Number.class);
581 assertEquals("The same value returned", value + 10, n.shortValue()); 642 assertEquals("The same value returned (" + value + " + 10): ", value + 10, n.shortValue());
582 } 643 }
583 644
584 @Test 645 @Test
585 public void testPrimitiveReturnTypeInt() throws Exception { 646 public void testPrimitiveReturnTypeInt() throws Exception {
586 PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); 647 PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers());
587 648
588 int value = RANDOM.nextInt(100); 649 int value = RANDOM.nextInt(100);
589 650
590 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); 651 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value));
591 Number n = apply.execute(fn).as(Number.class); 652 Number n = apply.execute(fn).as(Number.class);
592 assertEquals("The same value returned", value + 10, n.intValue()); 653 assertEquals("The same value returned (" + value + " + 10): ", value + 10, n.intValue());
593 } 654 }
594 655
595 @Test 656 @Test
596 public void testPrimitiveReturnTypeLong() throws Exception { 657 public void testPrimitiveReturnTypeLong() throws Exception {
597 PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); 658 PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers());
598 659
599 long value = RANDOM.nextInt(1000); 660 long value = RANDOM.nextInt(1000);
600 661
601 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); 662 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value));
602 Number n = apply.execute(fn).as(Number.class); 663 Number n = apply.execute(fn).as(Number.class);
603 assertEquals("The same value returned", value + 10, n.longValue()); 664 assertEquals("The same value returned (" + value + " + 10): ", value + 10, n.longValue());
604 } 665 }
605 666
606 @Test 667 @Test
607 public void testPrimitiveReturnTypeFloat() throws Exception { 668 public void testPrimitiveReturnTypeFloat() throws Exception {
608 PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); 669 PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers());
609 670
610 float value = RANDOM.nextInt(1000) + RANDOM.nextFloat(); 671 float value = RANDOM.nextInt(1000) + RANDOM.nextFloat();
611 672
612 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); 673 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value));
613 Number n = apply.execute(fn).as(Number.class); 674 Number n = apply.execute(fn).as(Number.class);
614 assertDouble("The same value returned", value + 10, n.floatValue()); 675 assertDouble("The same value returned (" + value + " + 10): ", value + 10, n.floatValue());
615 } 676 }
616 677
617 @Test 678 @Test
618 public void testPrimitiveReturnTypeDouble() throws Exception { 679 public void testPrimitiveReturnTypeDouble() throws Exception {
619 PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers()); 680 PolyglotEngine.Value apply = findGlobalSymbol(applyNumbers());
620 681
621 double value = RANDOM.nextInt(1000) + RANDOM.nextDouble(); 682 double value = RANDOM.nextInt(1000) + RANDOM.nextDouble();
622 683
623 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); 684 TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value));
624 Number n = apply.execute(fn).as(Number.class); 685 Number n = apply.execute(fn).as(Number.class);
625 assertDouble("The same value returned", value + 10, n.doubleValue()); 686 assertDouble("The same value returned (" + value + " + 10): ", value + 10, n.doubleValue());
626 } 687 }
627 688
628 @Test 689 @Test
629 public void testPrimitiveidentityByte() throws Exception { 690 public void testPrimitiveidentityByte() throws Exception {
630 String id = identity(); 691 String id = identity();
852 913
853 assertNotSame("Two virtual machines allocated", vm1, vm2); 914 assertNotSame("Two virtual machines allocated", vm1, vm2);
854 915
855 int prev1 = 0; 916 int prev1 = 0;
856 int prev2 = 0; 917 int prev2 = 0;
918 StringBuilder log = new StringBuilder();
857 for (int i = 0; i < 10; i++) { 919 for (int i = 0; i < 10; i++) {
858 int quantum = RANDOM.nextInt(10); 920 int quantum = RANDOM.nextInt(10);
921 log.append("quantum" + i + " is " + quantum + "\n");
859 for (int j = 0; j < quantum; j++) { 922 for (int j = 0; j < quantum; j++) {
860 Object res = count1.execute().get(); 923 Object res = count1.execute().get();
861 assert res instanceof Number : "expecting number: " + res; 924 assert res instanceof Number : "expecting number: " + res + "\n" + log;
862 ++prev1; 925 ++prev1;
863 assert ((Number) res).intValue() == prev1 : "expecting " + prev1 + " but was " + res; 926 assert ((Number) res).intValue() == prev1 : "expecting " + prev1 + " but was " + res + "\n" + log;
864 } 927 }
865 for (int j = 0; j < quantum; j++) { 928 for (int j = 0; j < quantum; j++) {
866 Object res = count2.execute().get(); 929 Object res = count2.execute().get();
867 assert res instanceof Number : "expecting number: " + res; 930 assert res instanceof Number : "expecting number: " + res + "\n" + log;
868 ++prev2; 931 ++prev2;
869 assert ((Number) res).intValue() == prev2 : "expecting " + prev2 + " but was " + res; 932 assert ((Number) res).intValue() == prev2 : "expecting " + prev2 + " but was " + res + "\n" + log;
870 } 933 }
871 assert prev1 == prev2 : "At round " + i + " the same number of invocations " + prev1 + " vs. " + prev2; 934 assert prev1 == prev2 : "At round " + i + " the same number of invocations " + prev1 + " vs. " + prev2 + "\n" + log;
872 } 935 }
873 } 936 }
874 937
875 @Test 938 @Test
876 public void testGlobalObjectIsAccessible() throws Exception { 939 public void testGlobalObjectIsAccessible() throws Exception {
909 String mulCode = multiplyCode(firstVar, secondVar); 972 String mulCode = multiplyCode(firstVar, secondVar);
910 Source source = Source.fromText("TCK42:" + mimeType() + ":" + mulCode, "evaluate " + firstVar + " * " + secondVar).withMimeType("application/x-tck"); 973 Source source = Source.fromText("TCK42:" + mimeType() + ":" + mulCode, "evaluate " + firstVar + " * " + secondVar).withMimeType("application/x-tck");
911 final PolyglotEngine.Value evalSource = vm().eval(source); 974 final PolyglotEngine.Value evalSource = vm().eval(source);
912 final PolyglotEngine.Value invokeMul = evalSource.execute(firstVar, secondVar); 975 final PolyglotEngine.Value invokeMul = evalSource.execute(firstVar, secondVar);
913 Object result = invokeMul.get(); 976 Object result = invokeMul.get();
914 assertTrue("Expecting numeric result, was:" + result, result instanceof Number); 977 assertTrue("Expecting numeric result, was:" + result + " for " + firstVar + " and " + secondVar, result instanceof Number);
915 assertEquals("Right value", 42, ((Number) result).intValue()); 978 assertEquals("Right value for " + firstVar + " and " + secondVar, 42, ((Number) result).intValue());
916 } 979 }
917 980
918 @Test 981 @Test
919 public void testAddComplexNumbers() throws Exception { 982 public void testAddComplexNumbers() throws Exception {
920 String id = complexAdd(); 983 String id = complexAdd();