changeset 18221:2c68474cc893

Truffle: add a test to track an ImplicitCast bug
author Benoit Daloze <benoit.daloze@jku.at>
date Fri, 31 Oct 2014 10:44:05 +0100
parents 890d284b2771
children a8cff27ca2e1 bfa20550f0a8
files graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/BadLongOverflowSpecializationTest.java graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/TypeSystemTest.java
diffstat 2 files changed, 93 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/BadLongOverflowSpecializationTest.java	Fri Oct 31 10:44:05 2014 +0100
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2014, 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.
+ *
+ * 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.api.dsl.test;
+
+import static com.oracle.truffle.api.dsl.test.TestHelper.*;
+import static org.junit.Assert.*;
+
+import java.math.*;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.dsl.*;
+import com.oracle.truffle.api.dsl.test.BadLongOverflowSpecializationTestFactory.AddNodeFactory;
+import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode;
+import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
+
+public class BadLongOverflowSpecializationTest {
+
+    @NodeChildren({@NodeChild("left"), @NodeChild("right")})
+    abstract static class BinaryNode extends ValueNode {
+    }
+
+    abstract static class AddNode extends BinaryNode {
+        @Specialization(rewriteOn = ArithmeticException.class)
+        int add(int left, int right) {
+            return ExactMath.addExact(left, right);
+        }
+
+        @Specialization
+        long addIntWithOverflow(int left, int right) {
+            return (long) left + (long) right;
+        }
+
+        @Specialization(rewriteOn = ArithmeticException.class)
+        Object add(long left, long right) {
+            return ExactMath.addExact(left, right);
+        }
+
+        @Specialization
+        BigInteger addSlow(long left, long right) {
+            return BigInteger.valueOf(left).add(BigInteger.valueOf(right));
+        }
+    }
+
+    @Test
+    @Ignore
+    public void testAdd() {
+        TestRootNode<AddNode> node = createRoot(AddNodeFactory.getInstance());
+
+        long index = -1;
+        int baseStep = 0;
+        Object r1 = executeWith(node, index, baseStep);
+        assertEquals(-1L, r1);
+
+        long err = 5000;
+        long errStep = 2000;
+        Object r2 = executeWith(node, err, errStep);
+
+        assertEquals(Long.class, r2.getClass());
+        assertEquals(7000L, r2);
+    }
+}
--- a/graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/TypeSystemTest.java	Thu Oct 30 17:04:16 2014 +0100
+++ b/graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/TypeSystemTest.java	Fri Oct 31 10:44:05 2014 +0100
@@ -32,7 +32,7 @@
 
 public class TypeSystemTest {
 
-    @TypeSystem({int.class, double.class, boolean.class, BigInteger.class, String.class, CallTarget.class, BExtendsAbstract.class, CExtendsAbstract.class, Abstract.class, Interface.class,
+    @TypeSystem({int.class, long.class, double.class, boolean.class, BigInteger.class, String.class, CallTarget.class, BExtendsAbstract.class, CExtendsAbstract.class, Abstract.class, Interface.class,
                     Object[].class})
     static class SimpleTypes {
 
@@ -57,6 +57,11 @@
         }
 
         @ImplicitCast
+        public long castLong(int value) {
+            return value;
+        }
+
+        @ImplicitCast
         public BigInteger castBigInteger(int value) {
             return BigInteger.valueOf(value);
         }
@@ -78,6 +83,10 @@
             return SimpleTypesGen.SIMPLETYPES.expectInteger(execute(frame));
         }
 
+        public long executeLong(VirtualFrame frame) throws UnexpectedResultException {
+            return SimpleTypesGen.SIMPLETYPES.expectLong(execute(frame));
+        }
+
         public String executeString(VirtualFrame frame) throws UnexpectedResultException {
             return SimpleTypesGen.SIMPLETYPES.expectString(execute(frame));
         }