8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
Summary: This uses the inspection framework for generated code to ensure optimisim.
Reviewed-by: attila, lagergren
Contributed-by: matherey.nunez@oracle.com
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/optimistic_check_type.js Fri Mar 14 14:27:32 2014 +0100
@@ -0,0 +1,93 @@
+/*
+ * 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.
+ */
+
+/**
+ * JDK-8036987 : Implement tests that checks static types in the compiled code
+ * @test
+ * @run
+ */
+
+var inspect = Java.type("jdk.nashorn.test.tools.StaticTypeInspector").inspect
+var a=3,b,c,z,y;
+
+// Testing arithmetic operators
+print(inspect(y*z, "undefined value multiplication by undefined value"))
+print(inspect(y/z, "undefined value division by undefined value"))
+
+var x = { a: 2, b:1 }
+print(inspect(x.a*x.b, "int multiplication by int"))
+print(inspect(x.a/x.b, "int division by int without remainder"))
+
+x.a = 7;
+x.b = 2;
+print(inspect(x.a/x.b, "int division by int with remainder"))
+print(inspect(x.a%x.b, "int modulus by int"))
+print(inspect(x.a+x.b, "int plus int"))
+
+x.a = Number.MAX_VALUE;
+x.b = Number.MAX_VALUE;
+print(inspect(x.a*x.b, "max value multiplication by max value"))
+
+x.a = Number.POSITIVE_INFINITY;
+x.b = Number.POSITIVE_INFINITY;
+print(inspect(x.a*x.b, "infinity multiplication by infinity"))
+
+x.a = -1;
+x.b = Number.POSITIVE_INFINITY;
+print(inspect(x.a/x.b, "-1 division by infinity"))
+
+x.a = Number.POSITIVE_INFINITY;
+x.b = 0;
+print(inspect(x.a/x.b, "infinity division by zero"))
+
+x.a = Number.POSITIVE_INFINITY;
+x.b = 'Hello';
+print(inspect(x.a/x.b, "infinity division by String"))
+
+// Testing nested functions and return value
+function f() {
+ var x = 2, y = 1;
+ function g() {
+ print(inspect(x, "outer local variable"));
+ print(inspect(a, "global variable"));
+ print(inspect(x*y, "outer local variable multiplication by outer local variable"));
+ print(inspect(a*b, "global variable multiplication by global variable undefined"));
+ }
+ g()
+}
+f()
+
+function f1(a,b,c) {
+ d = (a+b) * c;
+ print(inspect(c, "c"));
+ print(inspect(a+b, "a+b"));
+ print(inspect(d, "d"));
+}
+f1()
+
+
+function f2(a,b) {
+ d = a && b;
+ print(inspect(d, "d"));
+}
+f2()
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/optimistic_check_type.js.EXPECTED Fri Mar 14 14:27:32 2014 +0100
@@ -0,0 +1,20 @@
+undefined value multiplication by undefined value: double
+undefined value division by undefined value: double
+int multiplication by int: int
+int division by int without remainder: int
+int division by int with remainder: double
+int modulus by int: int
+int plus int: int
+max value multiplication by max value: double
+infinity multiplication by infinity: double
+-1 division by infinity: double
+infinity division by zero: double
+infinity division by String: double
+outer local variable: int
+global variable: int
+outer local variable multiplication by outer local variable: int
+global variable multiplication by global variable undefined: double
+c: undefined
+a+b: double
+d: double
+d: undefined
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/src/jdk/nashorn/test/tools/StaticTypeInspector.java Fri Mar 14 14:27:32 2014 +0100
@@ -0,0 +1,54 @@
+/*
+ * 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. 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 jdk.nashorn.test.tools;
+
+import jdk.nashorn.internal.runtime.Undefined;
+
+public class StaticTypeInspector {
+
+ public static String inspect(boolean x, String w) {
+ return w + ": boolean";
+ }
+
+ public static String inspect(int x, String w) {
+ return w + ": int";
+ }
+
+ public static String inspect(long x, String w) {
+ return w + ": long";
+ }
+
+ public static String inspect(double x, String w) {
+ return w + ": double";
+ }
+
+ public static String inspect(Undefined x, String w) {
+ return w + ": undefined";
+ }
+
+ public static String inspect(Object x, String w) {
+ return w + ": object";
+ }
+}
\ No newline at end of file