changeset 22138:a583d7ffd285

Enhancing TCK to pass in all Java number types and see whether they result in something meaningful.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 09 Sep 2015 18:53:14 +0200
parents 1957c49a979d
children 597953a8e6f0
files truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java
diffstat 2 files changed, 88 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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 {
+    }
 }
--- 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 <code>type1</code> and <code>type2</code> and expects result of type {@link Number} 
+     * which's {@link Number#intValue()} is equivalent of <code>param1 + param2</code>. 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);