Tue, 16 Aug 2016 21:41:28 -0700
Added tag jdk8u102-b33 for changeset 36e3c21b5fca
attila@963 | 1 | /* |
attila@963 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
attila@963 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
attila@963 | 4 | * |
attila@963 | 5 | * This code is free software; you can redistribute it and/or modify it |
attila@963 | 6 | * under the terms of the GNU General Public License version 2 only, as |
attila@963 | 7 | * published by the Free Software Foundation. |
attila@963 | 8 | * |
attila@963 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
attila@963 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
attila@963 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
attila@963 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
attila@963 | 13 | * accompanied this code). |
attila@963 | 14 | * |
attila@963 | 15 | * You should have received a copy of the GNU General Public License version |
attila@963 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
attila@963 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
attila@963 | 18 | * |
attila@963 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
attila@963 | 20 | * or visit www.oracle.com if you need additional information or have any |
attila@963 | 21 | * questions. |
attila@963 | 22 | */ |
attila@963 | 23 | |
attila@963 | 24 | /** |
attila@963 | 25 | * @test |
attila@963 | 26 | * @bug 8036987, 8037572 |
attila@963 | 27 | * @summary Implement tests that checks static types in the compiled code |
attila@963 | 28 | * @option --optimistic-types=true |
attila@963 | 29 | * @run |
attila@963 | 30 | */ |
attila@963 | 31 | |
attila@963 | 32 | var inspect = Java.type("jdk.nashorn.test.tools.StaticTypeInspector").inspect |
attila@963 | 33 | var a=3, b=2.3, c=true, d; |
attila@963 | 34 | var x = { a: 2, b:0, c:undefined} |
attila@963 | 35 | var trees = new Array("redwood", "bay", "cedar", "oak"); |
attila@963 | 36 | |
attila@963 | 37 | // Testing conditional operator |
attila@963 | 38 | print(inspect("" ? b : x.a, "ternary operator")) |
attila@994 | 39 | var b1 = b; |
attila@994 | 40 | print(inspect(x.b ? b1 : x.a, "ternary operator")) |
attila@994 | 41 | var b2 = b; |
attila@994 | 42 | print(inspect(c ? b2 : a, "ternary operator")) |
attila@994 | 43 | var b3 = b; |
attila@994 | 44 | print(inspect(!c ? b3 : a, "ternary operator")) |
attila@994 | 45 | var b4 = b; |
attila@994 | 46 | print(inspect(d ? b4 : x.c, "ternary operator")) |
attila@963 | 47 | print(inspect(x.c ? a : c, "ternary operator")) |
attila@963 | 48 | print(inspect(c ? d : a, "ternary operator")) |
attila@994 | 49 | var b5 = b; |
attila@994 | 50 | print(inspect(c ? +a : b5, "ternary operator")) |
attila@963 | 51 | |
attila@963 | 52 | // Testing format methods |
attila@963 | 53 | print(inspect(b.toFixed(2), "global double toFixed()")) |
attila@963 | 54 | print(inspect(b.toPrecision(2)/1, "global double toPrecision() divided by 1")) |
attila@963 | 55 | print(inspect(b.toExponential(2), "global double toExponential()")) |
attila@963 | 56 | |
attila@963 | 57 | // Testing arrays |
attila@963 | 58 | print(inspect(trees[1], "member object")) |
attila@963 | 59 | trees[1] = undefined; |
attila@963 | 60 | print(inspect(trees[1], "member undefined")) |
attila@994 | 61 | var b6=b; |
attila@994 | 62 | print(inspect(1 in trees ? b6 : a, "conditional on array member")) |
attila@963 | 63 | delete trees[2] |
attila@994 | 64 | var b7=b; |
attila@994 | 65 | print(inspect(2 in trees ? b7 : a, "conditional on array member")) |
attila@963 | 66 | print(inspect(3 in trees ? trees[2]="bay" : a, "conditional on array member")) |
attila@994 | 67 | var b8=b; |
attila@994 | 68 | print(inspect("oak" in trees ? b8 : a, "conditional on array member")) |
attila@963 | 69 | |
attila@963 | 70 | // Testing nested functions and return value |
attila@963 | 71 | function f1() { |
attila@963 | 72 | var x = 2, y = 1; |
attila@963 | 73 | function g() { |
attila@963 | 74 | print(inspect(x, "outer local variable")); |
attila@963 | 75 | print(inspect(a, "global variable")); |
attila@963 | 76 | print(inspect(x*y, "outer local int multiplication by outer local int")); |
attila@963 | 77 | print(inspect(a*d, "global int multiplication by global undefined")); |
attila@963 | 78 | } |
attila@963 | 79 | g() |
attila@963 | 80 | } |
attila@963 | 81 | f1() |
attila@963 | 82 | |
attila@963 | 83 | function f2(a,b,c) { |
attila@963 | 84 | g = (a+b) * c; |
attila@963 | 85 | print(inspect(c, "local undefined")); |
attila@963 | 86 | print(inspect(a+b, "local undefined addition local undefined")); |
attila@963 | 87 | print(inspect(g, "local undefined multiplication by undefined")); |
attila@963 | 88 | } |
attila@963 | 89 | f2() |
attila@963 | 90 | |
attila@963 | 91 | function f3(a,b) { |
attila@963 | 92 | g = a && b; |
attila@963 | 93 | print(inspect(g, "local undefined AND local undefined")); |
attila@963 | 94 | print(inspect(c||g, "global true OR local undefined")); |
attila@963 | 95 | } |
attila@963 | 96 | f3() |
attila@963 | 97 | |
attila@963 | 98 | function f4() { |
attila@963 | 99 | var x = true, y = 0; |
attila@963 | 100 | function g() { |
attila@963 | 101 | print(inspect(x+y, "outer local true addition local int")); |
attila@963 | 102 | print(inspect(a+x, "global int addition outer local true")); |
attila@963 | 103 | print(inspect(x*y, "outer local true multiplication by outer local int")); |
attila@963 | 104 | print(inspect(y*d, "outer local int multiplication by global undefined")); |
attila@963 | 105 | } |
attila@963 | 106 | g() |
attila@963 | 107 | } |
attila@963 | 108 | f4() |