optimistic_check_type.js revision 822:005ac813256a
197403Sobrien/*
297403Sobrien * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3169691Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
497403Sobrien *
597403Sobrien * This code is free software; you can redistribute it and/or modify it
697403Sobrien * under the terms of the GNU General Public License version 2 only, as
797403Sobrien * published by the Free Software Foundation.
897403Sobrien *
997403Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1097403Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1197403Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1297403Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1397403Sobrien * accompanied this code).
1497403Sobrien *
1597403Sobrien * You should have received a copy of the GNU General Public License version
1697403Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1797403Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18169691Skan *
1997403Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2097403Sobrien * or visit www.oracle.com if you need additional information or have any
2197403Sobrien * questions.
2297403Sobrien */
2397403Sobrien
2497403Sobrien/**
2597403Sobrien * JDK-8036987 : Implement tests that checks static types in the compiled code
2697403Sobrien * @test
2797403Sobrien * @run
2897403Sobrien */
2997403Sobrien
3097403Sobrienvar inspect = Java.type("jdk.nashorn.test.tools.StaticTypeInspector").inspect
3197403Sobrienvar a=3,b,c,z,y;
32169691Skan
33169691Skan// Testing arithmetic operators
3497403Sobrienprint(inspect(y*z, "undefined value multiplication by undefined value"))
3597403Sobrienprint(inspect(y/z, "undefined value division by undefined value"))
3697403Sobrien
3797403Sobrienvar x = { a: 2, b:1 }
3897403Sobrienprint(inspect(x.a*x.b, "int multiplication by int"))
3997403Sobrienprint(inspect(x.a/x.b, "int division by int without remainder"))
4097403Sobrien
4197403Sobrienx.a = 7;
4297403Sobrienx.b = 2;
4397403Sobrienprint(inspect(x.a/x.b, "int division by int with remainder"))
4497403Sobrienprint(inspect(x.a%x.b, "int modulus by int"))
4597403Sobrienprint(inspect(x.a+x.b, "int plus int"))
4697403Sobrien
4797403Sobrienx.a = Number.MAX_VALUE;
4897403Sobrienx.b = Number.MAX_VALUE;
4997403Sobrienprint(inspect(x.a*x.b, "max value multiplication by max value"))
5097403Sobrien
5197403Sobrienx.a = Number.POSITIVE_INFINITY;
5297403Sobrienx.b = Number.POSITIVE_INFINITY;
5397403Sobrienprint(inspect(x.a*x.b, "infinity multiplication by infinity"))
5497403Sobrien
5597403Sobrienx.a = -1;
5697403Sobrienx.b = Number.POSITIVE_INFINITY;
5797403Sobrienprint(inspect(x.a/x.b, "-1 division by infinity"))
5897403Sobrien
5997403Sobrienx.a = Number.POSITIVE_INFINITY;
6097403Sobrienx.b = 0;
6197403Sobrienprint(inspect(x.a/x.b, "infinity division by zero"))
62132720Skan
6397403Sobrienx.a = Number.POSITIVE_INFINITY;
6497403Sobrienx.b = 'Hello';
6597403Sobrienprint(inspect(x.a/x.b, "infinity division by String"))
6697403Sobrien
6797403Sobrien// Testing nested functions and return value
6897403Sobrienfunction f() {
6997403Sobrien    var x = 2, y = 1;
7097403Sobrien    function g() {
7197403Sobrien        print(inspect(x, "outer local variable"));
7297403Sobrien        print(inspect(a, "global variable"));
7397403Sobrien        print(inspect(x*y, "outer local variable multiplication by outer local variable"));
7497403Sobrien        print(inspect(a*b, "global variable multiplication by global variable undefined"));
7597403Sobrien    }
7697403Sobrien    g()
7797403Sobrien}
7897403Sobrienf()
7997403Sobrien
8097403Sobrienfunction f1(a,b,c) {
8197403Sobrien    d = (a+b) * c;
8297403Sobrien    print(inspect(c, "c"));
8397403Sobrien    print(inspect(a+b, "a+b"));
8497403Sobrien    print(inspect(d, "d"));
8597403Sobrien}
8697403Sobrienf1()
8797403Sobrien
8897403Sobrien
8997403Sobrienfunction f2(a,b) {
9097403Sobrien    d = a && b;
9197403Sobrien    print(inspect(d, "d"));
92132720Skan}
93169691Skanf2()