nashorn/test/script/currently-failing/OptimisticRecompilationTest.java
changeset 24748 69787da7c664
parent 24747 c7485e5d6cf4
child 24749 1549c85f8200
equal deleted inserted replaced
24747:c7485e5d6cf4 24748:69787da7c664
     1 /*
       
     2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package jdk.nashorn.internal.runtime;
       
    26 
       
    27 import static org.testng.Assert.fail;
       
    28 
       
    29 import java.io.ByteArrayOutputStream;
       
    30 import java.io.PrintStream;
       
    31 import java.util.regex.Matcher;
       
    32 import java.util.regex.Pattern;
       
    33 import javax.script.ScriptEngine;
       
    34 import javax.script.ScriptEngineFactory;
       
    35 import javax.script.ScriptEngineManager;
       
    36 import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
       
    37 import org.testng.annotations.AfterTest;
       
    38 import org.testng.annotations.BeforeTest;
       
    39 import org.testng.annotations.Test;
       
    40 
       
    41 /**
       
    42  * @test
       
    43  * @bug 8037086
       
    44  * @run testng/othervm jdk.nashorn.internal.runtime.OptimisticRecompilationTest
       
    45  */
       
    46 public class OptimisticRecompilationTest {
       
    47 
       
    48    private ScriptEngine engine;
       
    49    private ByteArrayOutputStream stderr;
       
    50    private PrintStream prevStderr;
       
    51 
       
    52    public void runTest(String script, String expectedOutputPattern,
       
    53                        int expectedPatternOccurrence) {
       
    54       try {
       
    55          engine.eval(script);
       
    56       } catch (final Exception se) {
       
    57          se.printStackTrace();
       
    58          fail(se.getMessage());
       
    59       }
       
    60       Pattern deoptimizing = Pattern.compile(expectedOutputPattern);
       
    61       Matcher matcher = deoptimizing.matcher(stderr.toString());
       
    62       int matches = 0;
       
    63       while (matcher.find()) {
       
    64          matches++;
       
    65       }
       
    66       if (matches != expectedPatternOccurrence) {
       
    67          fail("Number of Deoptimizing recompilation is not correct, expected: "
       
    68                     + expectedPatternOccurrence + " and found: " + matches + "\n"
       
    69               + stderr);
       
    70       }
       
    71       stderr.reset();
       
    72    }
       
    73 
       
    74    private static String getRecompilationPattern(String type, String value) {
       
    75         return "\\[recompile\\]\\s*RewriteException\\s\\[programPoint\\=(\\d+)\\s"
       
    76            + "returnType=" + type + "\\s\\(" + value + "\\)";
       
    77    }
       
    78 
       
    79     @BeforeTest
       
    80     public void setupTest() {
       
    81        stderr = new ByteArrayOutputStream();
       
    82        prevStderr = System.err;
       
    83        System.setErr(new PrintStream(stderr));
       
    84        NashornScriptEngineFactory nashornFactory = null;
       
    85        ScriptEngineManager sm = new ScriptEngineManager();
       
    86        for (ScriptEngineFactory fac : sm.getEngineFactories()) {
       
    87           if (fac instanceof NashornScriptEngineFactory) {
       
    88              nashornFactory = (NashornScriptEngineFactory) fac;
       
    89              break;
       
    90           }
       
    91        }
       
    92        if (nashornFactory == null) {
       
    93           fail("Cannot find nashorn factory!");
       
    94        }
       
    95        String[] options = new String[]{"--log=recompile"};
       
    96        engine = nashornFactory.getScriptEngine(options);
       
    97     }
       
    98 
       
    99     @AfterTest
       
   100     public void setErrTest() {
       
   101        System.setErr(prevStderr);
       
   102     }
       
   103 
       
   104     @Test
       
   105     public void divisionByZeroTest() {
       
   106         //Check that one Deoptimizing recompilation and RewriteExceptions happened
       
   107         runTest("function f() {var x1 = { a: 2, b:1 }; x1.a = Number.POSITIVE_INFINITY;"
       
   108                 + " x1.b = 0; print(x1.a/x1.b);} f()",
       
   109                 getRecompilationPattern("double", "Infinity"), 1);
       
   110     }
       
   111 
       
   112     @Test
       
   113     public void divisionWithRemainderTest() {
       
   114        //Check that one Deoptimizing recompilation and RewriteException happened
       
   115         runTest("function f() {var x2 = { a: 7, b:2 }; print(x2.a/x2.b);} f()",
       
   116                getRecompilationPattern("double", "3.5"), 1);
       
   117     }
       
   118 
       
   119     @Test
       
   120     public void infinityMultiplicationTest() {
       
   121         //Check that one deoptimizing recompilation and RewriteExceptions happened
       
   122         runTest("function f() {var x3 = { a: Number.POSITIVE_INFINITY, "
       
   123                 + "b: Number.POSITIVE_INFINITY}; print(x3.a*x3.b);} f()",
       
   124                 getRecompilationPattern("double", "Infinity"), 1);
       
   125     }
       
   126 
       
   127     @Test
       
   128     public void maxValueMultiplicationTest() {
       
   129         runTest("function f() {var x4 = { a: Number.MAX_VALUE, b: Number.MAX_VALUE};"
       
   130                 + " print(x4.a*x4.b);} f()",
       
   131                 getRecompilationPattern("double", "1.7976931348623157E308"), 1);
       
   132     }
       
   133 
       
   134     @Test
       
   135     public void divisionByInfinityTest() {
       
   136         //Check that one Deoptimizing recompilation and RewriteExceptions happened
       
   137         runTest("function f() {var x5 = { a: -1, b: Number.POSITIVE_INFINITY};"
       
   138                 + " print(x5.a/x5.b);} f()",
       
   139                 getRecompilationPattern("double", "Infinity"), 1);
       
   140     }
       
   141 
       
   142     @Test
       
   143     public void divisionByStringTest() {
       
   144         //Check that one deoptimizing recompilations and RewriteExceptions happened
       
   145         runTest("function f() {var x6 = { a: Number.POSITIVE_INFINITY, b: 'Hello'};"
       
   146                 + " print(x6.a/x6.b);} f()", getRecompilationPattern("double", "Infinity"), 1);
       
   147     }
       
   148 
       
   149     @Test
       
   150     public void nestedFunctionTest() {
       
   151        //Check that one Deoptimizing recompilations and RewriteExceptions happened
       
   152         runTest("var a=3,b,c; function f() {var x7 = 2, y =1; function g(){ "
       
   153                 + "var y = x7; var z = a; z = x7*y; print(a*b); } g() } f()",
       
   154                getRecompilationPattern("object", "undefined"), 1);
       
   155     }
       
   156 
       
   157     @Test
       
   158     public void andTest() {
       
   159        //Check that one Deoptimizing recompilations and RewriteExceptions happened
       
   160        runTest("function f(a,b) { d = a && b; print(d);} f()",
       
   161                "\\[recompile\\]\\sDeoptimizing\\srecompilation\\sof\\s\'f\'", 1);
       
   162     }
       
   163 
       
   164     @Test
       
   165     public void functionTest() {
       
   166        //Check that one Deoptimizing recompilations and RewriteExceptions happened
       
   167         runTest("function f(a,b,c) { h = (a + b) * c; print(h);} f()",
       
   168                getRecompilationPattern("double", "NaN"), 1);
       
   169     }
       
   170 }