# HG changeset patch # User Jaroslav Tulach # Date 1441817594 -7200 # Node ID a583d7ffd285c4905fef3658432c8bd297edb84e # Parent 1957c49a979d369203a01c795c75956ecb555d2b Enhancing TCK to pass in all Java number types and see whether they result in something meaningful. diff -r 1957c49a979d -r a583d7ffd285 truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java --- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Sep 09 16:03:49 2015 +0200 +++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Sep 09 18:53:14 2015 +0200 @@ -107,7 +107,7 @@ } @Override - protected String plusInt() { + protected String plus(Class type1, Class type2) { return "plus"; } @@ -140,4 +140,16 @@ protected String countInvocations() { return "count"; } + + // + // Ignore tests working on floats and double + // + + @Override + public void testPlusWithDouble() throws Exception { + } + + @Override + public void testPlusWithFloat() throws Exception { + } } diff -r 1957c49a979d -r a583d7ffd285 truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java --- a/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java Wed Sep 09 16:03:49 2015 +0200 +++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java Wed Sep 09 18:53:14 2015 +0200 @@ -96,7 +96,25 @@ * * @return name of globally exported symbol */ - protected abstract String plusInt(); + protected String plusInt() { + throw new UnsupportedOperationException("Override plus(Class,Class) method!"); + } + + /** + * Name of function to add two numbers together. The symbol will be invoked with two parameters + * of type1 and type2 and expects result of type {@link Number} + * which's {@link Number#intValue()} is equivalent of param1 + param2. As some + * languages may have different operations for different types of numbers, the actual types are + * passed to the method and the implementation can decide to return different symbol based on + * the parameters. + * + * @param type1 one of byte, short, int, long, float, double class + * @param type2 one of byte, short, int, long, float, double class + * @return name of globally exported symbol + */ + protected String plus(Class type1, Class type2) { + return plusInt(); + } /** * Name of a function in your language to perform a callback to foreign function. Your function @@ -213,13 +231,68 @@ int a = RANDOM.nextInt(100); int b = RANDOM.nextInt(100); - TruffleVM.Symbol plus = findGlobalSymbol(plusInt()); + TruffleVM.Symbol plus = findGlobalSymbol(plus(int.class, int.class)); Number n = plus.invoke(null, a, b).as(Number.class); assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue(); } @Test + public void testPlusWithBytes() throws Exception { + int a = RANDOM.nextInt(100); + int b = RANDOM.nextInt(100); + + TruffleVM.Symbol plus = findGlobalSymbol(plus(byte.class, byte.class)); + + Number n = plus.invoke(null, (byte) a, (byte) b).as(Number.class); + assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue(); + } + + @Test + public void testPlusWithShort() throws Exception { + int a = RANDOM.nextInt(100); + int b = RANDOM.nextInt(100); + + TruffleVM.Symbol plus = findGlobalSymbol(plus(short.class, short.class)); + + Number n = plus.invoke(null, (short) a, (short) b).as(Number.class); + assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue(); + } + + @Test + public void testPlusWithLong() throws Exception { + long a = RANDOM.nextInt(100); + long b = RANDOM.nextInt(100); + + TruffleVM.Symbol plus = findGlobalSymbol(plus(long.class, long.class)); + + Number n = plus.invoke(null, a, b).as(Number.class); + assert a + b == n.longValue() : "The value is correct: (" + a + " + " + b + ") = " + n.longValue(); + } + + @Test + public void testPlusWithFloat() throws Exception { + float a = RANDOM.nextFloat(); + float b = RANDOM.nextFloat(); + + TruffleVM.Symbol plus = findGlobalSymbol(plus(float.class, float.class)); + + Number n = plus.invoke(null, a, b).as(Number.class); + assertEquals("Correct value computed", a + b, n.floatValue(), 0.01f); + } + + @Test + public void testPlusWithDouble() throws Exception { + double a = RANDOM.nextDouble(); + double b = RANDOM.nextDouble(); + + TruffleVM.Symbol plus = findGlobalSymbol(plus(float.class, float.class)); + + Number n = plus.invoke(null, a, b).as(Number.class); + assertEquals("Correct value computed", a + b, n.doubleValue(), 0.01); + } + + @Test public void testPlusWithIntsOnCompoundObject() throws Exception { int a = RANDOM.nextInt(100); int b = RANDOM.nextInt(100);