nashorn/test/script/basic/optimistic_check_type.js
author attila
Wed, 06 Aug 2014 10:42:46 +0200
changeset 25828 077046a5d726
parent 24778 2ff5d7041566
child 26503 3ed48a01100c
permissions -rw-r--r--
8044786: Some tests fail with non-optimistic compilation Reviewed-by: hannesw, jlaskey
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.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
     4
 *
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
     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.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
     8
 *
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
     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).
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    14
 *
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
    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.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    18
 *
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
    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
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    26
 * @bug 8036987, 8037572
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    27
 * @summary Implement tests that checks static types in the compiled code
25828
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
    28
 * @option --optimistic-types=true
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
    29
 * @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
    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
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
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
    33
var a=3, b=2.3, c=true, d;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    34
var x = { a: 2, b:0, c:undefined}
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    35
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
    36
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    37
// Testing conditional operator
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    38
print(inspect("" ? b : x.a, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    39
print(inspect(x.b ? b : x.a, "ternary operator"))
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    40
print(inspect(c ? b : a, "ternary operator"))
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    41
print(inspect(!c ? b : a, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    42
print(inspect(d ? b : x.c, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    43
print(inspect(x.c ? a : c, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    44
print(inspect(c ? d : a, "ternary operator"))
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    45
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
    46
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    47
// Testing format methods
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    48
print(inspect(b.toFixed(2), "global double toFixed()"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    49
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
    50
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
    51
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    52
// Testing arrays
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    53
print(inspect(trees[1], "member object"))
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    54
trees[1] = undefined;
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    55
print(inspect(trees[1], "member undefined"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    56
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
    57
delete trees[2]
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    58
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
    59
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
    60
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
    61
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    62
// Testing nested functions and return value
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    63
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
    64
    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
    65
    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
    66
        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
    67
        print(inspect(a, "global variable"));
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    68
        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
    69
        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
    70
    }
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
    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
    72
}
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
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
    74
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    75
function f2(a,b,c) {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    76
    g = (a+b) * c;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    77
    print(inspect(c, "local undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    78
    print(inspect(a+b, "local undefined addition local undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    79
    print(inspect(g, "local undefined multiplication by undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    80
}
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    81
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
    82
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    83
function f3(a,b) {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    84
    g = a && b;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    85
    print(inspect(g, "local undefined AND local undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    86
    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
    87
}
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    88
f3()
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    89
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    90
function f4() {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    91
    var x = true, y = 0;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    92
    function g() {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    93
        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
    94
        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
    95
        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
    96
        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
    97
    }
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    98
    g()
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    99
}
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
   100
f4()