nashorn/test/script/basic/optimistic_check_type.js
author lagergren
Fri, 14 Mar 2014 14:27:32 +0100
changeset 24723 215d17db59ad
child 24750 01ea334ab39a
permissions -rw-r--r--
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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24723
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
     1
/*
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
     4
 * 
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
     7
 * published by the Free Software Foundation.
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
     8
 * 
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    13
 * accompanied this code).
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    14
 * 
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    18
 * 
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    21
 * questions.
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    22
 */
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    23
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    24
/**
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    25
 * JDK-8036987 : Implement tests that checks static types in the compiled code
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    26
 * @test
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    27
 * @run
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    28
 */
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    29
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    30
var inspect = Java.type("jdk.nashorn.test.tools.StaticTypeInspector").inspect
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    31
var a=3,b,c,z,y;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    32
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    33
// Testing arithmetic operators
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    34
print(inspect(y*z, "undefined value multiplication by undefined value"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    35
print(inspect(y/z, "undefined value division by undefined value"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    36
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    37
var x = { a: 2, b:1 }
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    38
print(inspect(x.a*x.b, "int multiplication by int"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    39
print(inspect(x.a/x.b, "int division by int without remainder"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    40
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    41
x.a = 7;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    42
x.b = 2;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    43
print(inspect(x.a/x.b, "int division by int with remainder"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    44
print(inspect(x.a%x.b, "int modulus by int"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    45
print(inspect(x.a+x.b, "int plus int"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    46
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    47
x.a = Number.MAX_VALUE;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    48
x.b = Number.MAX_VALUE;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    49
print(inspect(x.a*x.b, "max value multiplication by max value"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    50
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    51
x.a = Number.POSITIVE_INFINITY;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    52
x.b = Number.POSITIVE_INFINITY;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    53
print(inspect(x.a*x.b, "infinity multiplication by infinity"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    54
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    55
x.a = -1;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    56
x.b = Number.POSITIVE_INFINITY;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    57
print(inspect(x.a/x.b, "-1 division by infinity"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    58
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    59
x.a = Number.POSITIVE_INFINITY;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    60
x.b = 0;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    61
print(inspect(x.a/x.b, "infinity division by zero"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    62
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    63
x.a = Number.POSITIVE_INFINITY;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    64
x.b = 'Hello';
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    65
print(inspect(x.a/x.b, "infinity division by String"))
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    66
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    67
// Testing nested functions and return value 
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    68
function f() {
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    69
    var x = 2, y = 1;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    70
    function g() {
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    71
        print(inspect(x, "outer local variable"));
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    72
        print(inspect(a, "global variable"));
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    73
        print(inspect(x*y, "outer local variable multiplication by outer local variable"));
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    74
        print(inspect(a*b, "global variable multiplication by global variable undefined"));
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    75
    }
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    76
    g()
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    77
}
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    78
f()
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    79
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    80
function f1(a,b,c) {
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    81
    d = (a+b) * c;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    82
    print(inspect(c, "c"));
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    83
    print(inspect(a+b, "a+b"));
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    84
    print(inspect(d, "d"));
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    85
}
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    86
f1()
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    87
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    88
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    89
function f2(a,b) {
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    90
    d = a && b;
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    91
    print(inspect(d, "d"));
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    92
}
215d17db59ad 8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
lagergren
parents:
diff changeset
    93
f2()