test/nashorn/script/basic/optimistic_check_type.js
author alanb
Sat, 30 Nov 2019 16:21:19 +0000
changeset 59329 289000934908
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation Reviewed-by: dfuchs, chegar
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"))
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    39
var b1 = b;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    40
print(inspect(x.b ? b1 : x.a, "ternary operator"))
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    41
var b2 = b;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    42
print(inspect(c ? b2 : a, "ternary operator"))
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    43
var b3 = b;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    44
print(inspect(!c ? b3 : a, "ternary operator"))
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    45
var b4 = b;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    46
print(inspect(d ? b4 : x.c, "ternary operator"))
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    47
print(inspect(x.c ? a : c, "ternary operator"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    48
print(inspect(c ? d : a, "ternary operator"))
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    49
var b5 = b;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    50
print(inspect(c ? +a : b5, "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
    51
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    52
// Testing format methods
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    53
print(inspect(b.toFixed(2), "global double toFixed()"))
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    54
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
    55
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
    56
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    57
// Testing arrays
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    58
print(inspect(trees[1], "member object"))
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    59
trees[1] = undefined;
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    60
print(inspect(trees[1], "member undefined"))
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    61
var b6=b;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    62
print(inspect(1 in trees ? b6 : a, "conditional on array member"))
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    63
delete trees[2]
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    64
var b7=b;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    65
print(inspect(2 in trees ? b7 : a, "conditional on array member"))
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    66
print(inspect(3 in trees ? trees[2]="bay" : a, "conditional on array member"))
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    67
var b8=b;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25828
diff changeset
    68
print(inspect("oak" in trees ? b8 : 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
    69
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
    70
// Testing nested functions and return value
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    71
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
    72
    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
    73
    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
    74
        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
    75
        print(inspect(a, "global variable"));
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    76
        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
    77
        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
    78
    }
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
    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
    80
}
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
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
    82
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    83
function f2(a,b,c) {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    84
    g = (a+b) * c;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    85
    print(inspect(c, "local undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    86
    print(inspect(a+b, "local undefined addition local undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    87
    print(inspect(g, "local undefined multiplication by undefined"));
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
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
    90
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    91
function f3(a,b) {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    92
    g = a && b;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    93
    print(inspect(g, "local undefined AND local undefined"));
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    94
    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
    95
}
24750
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    96
f3()
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
function f4() {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
    99
    var x = true, y = 0;
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
   100
    function g() {
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
   101
        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
   102
        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
   103
        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
   104
        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
   105
    }
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
   106
    g()
01ea334ab39a 8037572: Add more test cases to check static types
mnunez
parents: 24723
diff changeset
   107
}
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24750
diff changeset
   108
f4()