changeset 22140:92906003d607

Adding check of behavior of identity function into the TCK
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Thu, 10 Sep 2015 16:26:31 +0200
parents 597953a8e6f0
children 46384e637592 ffadd23c63c8
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, 136 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java	Wed Sep 09 19:18:44 2015 +0200
+++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java	Thu Sep 10 16:26:31 2015 +0200
@@ -69,6 +69,9 @@
                 "function plus(a, b) {\n" +
                 "  return a + b;\n" +
                 "}\n" +
+                "function identity(x) {\n" +
+                "  return x;\n" +
+                "}\n" +
                 "function apply(f) {\n" +
                 "  return f(18, 32) + 10;\n" +
                 "}\n" +
@@ -107,6 +110,11 @@
     }
 
     @Override
+    protected String identity() {
+        return "identity";
+    }
+
+    @Override
     protected String plus(Class<?> type1, Class<?> type2) {
         return "plus";
     }
@@ -160,4 +168,12 @@
     @Override
     public void testPrimitiveReturnTypeFloat() throws Exception {
     }
+
+    @Override
+    public void testPrimitiveidentityDouble() throws Exception {
+    }
+
+    @Override
+    public void testPrimitiveidentityFloat() throws Exception {
+    }
 }
--- a/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java	Wed Sep 09 19:18:44 2015 +0200
+++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java	Thu Sep 10 16:26:31 2015 +0200
@@ -127,6 +127,28 @@
     protected abstract String applyNumbers();
 
     /**
+     * Name of identity function. The identity function accepts one argument and returns it. The
+     * argument should go through without any modification, e.g. the input should result in
+     * identical output.
+     *
+     * @return name of globally exported symbol
+     */
+    protected String identity() {
+        final long introduced = 1441894042844L;
+        long wait = (System.currentTimeMillis() - introduced) / 3600;
+        if (wait < 100) {
+            wait = 100;
+        }
+        LOG.log(Level.SEVERE, "identity() method not overriden. Waiting for {0} ms", wait);
+        try {
+            Thread.sleep(wait);
+        } catch (InterruptedException ex) {
+            LOG.log(Level.SEVERE, null, ex);
+        }
+        return null;
+    }
+
+    /**
      * Name of a function that counts number of its invocations in current {@link TruffleVM}
      * context. Your function should somehow keep a counter to remember number of its invocations
      * and always increment it. The first invocation should return <code>1</code>, the second
@@ -413,6 +435,103 @@
     }
 
     @Test
+    public void testPrimitiveidentityByte() throws Exception {
+        String id = identity();
+        if (id == null) {
+            return;
+        }
+        TruffleVM.Symbol apply = findGlobalSymbol(id);
+
+        byte value = (byte) RANDOM.nextInt(100);
+
+        Number n = (Number) apply.invoke(null, value).get();
+        assertEquals("The same value returned", value, n.byteValue());
+    }
+
+    @Test
+    public void testPrimitiveidentityShort() throws Exception {
+        String id = identity();
+        if (id == null) {
+            return;
+        }
+        TruffleVM.Symbol apply = findGlobalSymbol(id);
+
+        short value = (short) RANDOM.nextInt(100);
+        Number n = (Number) apply.invoke(null, value).get();
+        assertEquals("The same value returned", value, n.shortValue());
+    }
+
+    @Test
+    public void testPrimitiveidentityInt() throws Exception {
+        String id = identity();
+        if (id == null) {
+            return;
+        }
+        TruffleVM.Symbol apply = findGlobalSymbol(id);
+
+        int value = RANDOM.nextInt(100);
+
+        Number n = (Number) apply.invoke(null, value).get();
+        assertEquals("The same value returned", value, n.intValue());
+    }
+
+    @Test
+    public void testPrimitiveidentityLong() throws Exception {
+        String id = identity();
+        if (id == null) {
+            return;
+        }
+        TruffleVM.Symbol apply = findGlobalSymbol(id);
+
+        long value = RANDOM.nextInt(1000);
+
+        Number n = (Number) apply.invoke(null, value).get();
+        assertEquals("The same value returned", value, n.longValue());
+    }
+
+    @Test
+    public void testPrimitiveidentityFloat() throws Exception {
+        String id = identity();
+        if (id == null) {
+            return;
+        }
+        TruffleVM.Symbol apply = findGlobalSymbol(id);
+
+        float value = RANDOM.nextInt(1000) + RANDOM.nextFloat();
+
+        Number n = (Number) apply.invoke(null, value).get();
+        assertEquals("The same value returned", value, n.floatValue(), 0.01);
+    }
+
+    @Test
+    public void testPrimitiveidentityDouble() throws Exception {
+        String id = identity();
+        if (id == null) {
+            return;
+        }
+        TruffleVM.Symbol apply = findGlobalSymbol(id);
+
+        double value = RANDOM.nextInt(1000) + RANDOM.nextDouble();
+
+        Number n = (Number) apply.invoke(null, value).get();
+        assertEquals("The same value returned", value, n.doubleValue(), 0.01);
+    }
+
+    @Test
+    public void testPrimitiveIdentityForeignObject() throws Exception {
+        String id = identity();
+        if (id == null) {
+            return;
+        }
+        TruffleVM.Symbol apply = findGlobalSymbol(id);
+
+        TruffleObject fn = JavaInterop.asTruffleFunction(LongBinaryOperation.class, new MaxMinObject(true));
+
+        Object ret = apply.invoke(null, fn).get();
+        assertSame("The same value returned", fn, ret);
+    }
+
+    @Test
     public void testCoExistanceOfMultipleLanguageInstances() throws Exception {
         final String countMethod = countInvocations();
         TruffleVM.Symbol count1 = findGlobalSymbol(countMethod);
@@ -454,7 +573,7 @@
         final String compoundObjectName = compoundObject();
         if (compoundObjectName == null) {
             final long introduced = 1441616302340L;
-            long wait = (System.currentTimeMillis() - introduced) / 3600;
+            long wait = (System.currentTimeMillis() - introduced) / 36000;
             if (wait < 100) {
                 wait = 100;
             }