changeset 22488:fa7b15454c66

Add ComplexNumber test to TCK
author Matthias Grimmer <grimmer@ssw.jku.at>
date Thu, 10 Dec 2015 12:18:27 +0100
parents 50baaa7da8e8
children 28227895fa35
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/ComplexNumber.java truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java
diffstat 3 files changed, 161 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java	Fri Dec 04 14:32:24 2015 +0100
+++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java	Thu Dec 10 12:18:27 2015 +0100
@@ -99,6 +99,10 @@
                 "}\n" +
                 "function returnsNull() {\n" +
                 "}\n" +
+                "function complexAdd(a, b) {\n" +
+                "  a.real = a.real + b.real;\n" +
+                "  a.imaginary = a.imaginary + b.imaginary;\n" +
+                "}\n" +
                 "function compoundObject() {\n" +
                 "  obj = new();\n" +
                 "  obj.fourtyTwo = fourtyTwo;\n" +
@@ -159,6 +163,11 @@
     }
 
     @Override
+    protected String complexAdd() {
+        return "complexAdd";
+    }
+
+    @Override
     protected String multiplyCode(String firstName, String secondName) {
         // @formatter:off
         return
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/ComplexNumber.java	Thu Dec 10 12:18:27 2015 +0100
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.tck;
+
+import com.oracle.truffle.api.CallTarget;
+import com.oracle.truffle.api.Truffle;
+import com.oracle.truffle.api.frame.VirtualFrame;
+import com.oracle.truffle.api.interop.ForeignAccess;
+import com.oracle.truffle.api.interop.ForeignAccess.Factory;
+import com.oracle.truffle.api.interop.Message;
+import com.oracle.truffle.api.interop.TruffleObject;
+import com.oracle.truffle.api.nodes.RootNode;
+
+public class ComplexNumber implements TruffleObject {
+
+    public static final String REAL_IDENTIFIER = "real";
+    public static final String IMAGINARY_IDENTIFIER = "imaginary";
+
+    private final double[] data = new double[2];
+
+    public ComplexNumber(double real, double imaginary) {
+        data[0] = real;
+        data[1] = imaginary;
+    }
+
+    public ForeignAccess getForeignAccess() {
+        return ForeignAccess.create(new ComplexForeignAccessFactory());
+    }
+
+    private static int identifierToIndex(String identifier) {
+        switch (identifier) {
+            case REAL_IDENTIFIER:
+                return 0;
+            case IMAGINARY_IDENTIFIER:
+                return 1;
+            default:
+                throw new IllegalArgumentException();
+        }
+    }
+
+    public void set(String identifier, double value) {
+        data[identifierToIndex(identifier)] = value;
+    }
+
+    public double get(String identifier) {
+        return data[identifierToIndex(identifier)];
+    }
+
+    private static class ComplexForeignAccessFactory implements Factory {
+
+        public boolean canHandle(TruffleObject obj) {
+            return obj instanceof ComplexNumber;
+        }
+
+        public CallTarget accessMessage(Message tree) {
+            if (Message.IS_NULL.equals(tree)) {
+                return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
+            } else if (Message.IS_EXECUTABLE.equals(tree)) {
+                return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
+            } else if (Message.IS_BOXED.equals(tree)) {
+                return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
+            } else if (Message.HAS_SIZE.equals(tree)) {
+                return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
+            } else if (Message.READ.equals(tree)) {
+                return Truffle.getRuntime().createCallTarget(new ComplexReadNode());
+            } else if (Message.WRITE.equals(tree)) {
+                return Truffle.getRuntime().createCallTarget(new ComplexWriteNode());
+            } else {
+                throw new IllegalArgumentException(tree.toString() + " not supported");
+            }
+        }
+    }
+
+    private static class ComplexWriteNode extends RootNode {
+        protected ComplexWriteNode() {
+            super(TckLanguage.class, null, null);
+        }
+
+        @Override
+        public Object execute(VirtualFrame frame) {
+            ComplexNumber complex = (ComplexNumber) ForeignAccess.getReceiver(frame);
+            String identifier = (String) ForeignAccess.getArguments(frame).get(0);
+            Number value = (Number) ForeignAccess.getArguments(frame).get(1);
+            complex.set(identifier, value.doubleValue());
+            return value;
+        }
+    }
+
+    private static class ComplexReadNode extends RootNode {
+        protected ComplexReadNode() {
+            super(TckLanguage.class, null, null);
+        }
+
+        @Override
+        public Object execute(VirtualFrame frame) {
+            ComplexNumber complex = (ComplexNumber) ForeignAccess.getReceiver(frame);
+            String identifier = (String) ForeignAccess.getArguments(frame).get(0);
+            return complex.get(identifier);
+        }
+
+    }
+}
--- a/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java	Fri Dec 04 14:32:24 2015 +0100
+++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java	Thu Dec 10 12:18:27 2015 +0100
@@ -145,6 +145,17 @@
     }
 
     /**
+     * Name of a function that adds up two complex numbers. The function accepts two arguments and
+     * provides no return value. The arguments are complex numbers with members called real and
+     * imaginary. The first argument contains the result of the addition.
+     *
+     * @return name of globally exported symbol
+     */
+    protected String complexAdd() {
+        throw new UnsupportedOperationException("complexAdd() method not implemented");
+    }
+
+    /**
      * Name of a function to return global object. The function can be executed without providing
      * any arguments and should return global object of the language, if the language supports it.
      * Global object is the one accessible via
@@ -768,6 +779,23 @@
         assertEquals("Right value", 42, ((Number) result).intValue());
     }
 
+    @Test
+    public void testAddComplexNumbers() throws Exception {
+        String id = complexAdd();
+        if (id == null) {
+            return;
+        }
+        PolyglotEngine.Value apply = findGlobalSymbol(id);
+
+        ComplexNumber a = new ComplexNumber(32, 10);
+        ComplexNumber b = new ComplexNumber(10, 32);
+
+        apply.execute(a, b);
+
+        assertEquals(42.0, a.get(ComplexNumber.REAL_IDENTIFIER), 0.1);
+        assertEquals(42.0, a.get(ComplexNumber.IMAGINARY_IDENTIFIER), 0.1);
+    }
+
     private PolyglotEngine.Value findGlobalSymbol(String name) throws Exception {
         PolyglotEngine.Value s = vm().findGlobalSymbol(name);
         assert s != null : "Symbol " + name + " is not found!";