nashorn/test/script/basic/optimistic_check_type.js
author mnunez
Mon, 05 May 2014 14:17:20 +0200
changeset 24750 01ea334ab39a
parent 24723 215d17db59ad
child 24778 2ff5d7041566
permissions -rw-r--r--
8037572: Add more test cases to check static types Reviewed-by: attila, lagergren
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
 * @test
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    26
 * @bug 8036987, 8037572 
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    27
 * @summary Implement tests that checks static types in the compiled code 
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
    28
 * @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
    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
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 inspect = Java.type("jdk.nashorn.test.tools.StaticTypeInspector").inspect
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    32
var a=3, b=2.3, c=true, d;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    33
var x = { a: 2, b:0, c:undefined}
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    34
var trees = new Array("redwood", "bay", "cedar", "oak");
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
    35
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    36
// Testing conditional operator         
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    37
print(inspect("" ? b : x.a, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    38
print(inspect(x.b ? b : x.a, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    39
print(inspect(c ? b : a, "ternary operator"))                      
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    40
print(inspect(!c ? b : a, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    41
print(inspect(d ? b : x.c, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    42
print(inspect(x.c ? a : c, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    43
print(inspect(c ? d : a, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    44
print(inspect(c ? +a : b, "ternary operator"))  
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
    45
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    46
// Testing format methods                    
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    47
print(inspect(b.toFixed(2), "global double toFixed()"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    48
print(inspect(b.toPrecision(2)/1, "global double toPrecision() divided by 1"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    49
print(inspect(b.toExponential(2), "global double toExponential()"))
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
    50
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    51
// Testing arrays
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    52
print(inspect(trees[1], "member object"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    53
trees[1] = undefined; 
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    54
print(inspect(trees[1], "member undefined"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    55
print(inspect(1 in trees ? b : a, "conditional on array member"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    56
delete trees[2]
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    57
print(inspect(2 in trees ? b : a, "conditional on array member"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    58
print(inspect(3 in trees ? trees[2]="bay" : a, "conditional on array member"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    59
print(inspect("oak" in trees ? b : a, "conditional on array member"))
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
    60
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
// Testing nested functions and return value 
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    62
function f1() {
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
    63
    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
    64
    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
    65
        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
    66
        print(inspect(a, "global variable"));
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    67
        print(inspect(x*y, "outer local int multiplication by outer local int"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    68
        print(inspect(a*d, "global int multiplication by global undefined"));
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
    69
    }
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
    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
}
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
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
    73
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    74
function f2(a,b,c) {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    75
    g = (a+b) * c;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    76
    print(inspect(c, "local undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    77
    print(inspect(a+b, "local undefined addition local undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    78
    print(inspect(g, "local undefined multiplication by undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    79
}
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    80
f2()
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
    81
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    82
function f3(a,b) {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    83
    g = a && b;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    84
    print(inspect(g, "local undefined AND local undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    85
    print(inspect(c||g, "global true OR local undefined"));
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
    86
}
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    87
f3()
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    88
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    89
function f4() {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    90
    var x = true, y = 0;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    91
    function g() {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    92
        print(inspect(x+y, "outer local true addition local int"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    93
        print(inspect(a+x, "global int addition outer local true"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    94
        print(inspect(x*y, "outer local true multiplication by outer local int"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    95
        print(inspect(y*d, "outer local int multiplication by global undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    96
    }
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    97
    g()
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    98
}
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    99
f4()