langtools/test/tools/javac/ConstFoldTest.java
author sogoel
Fri, 05 Sep 2014 16:43:00 -0700
changeset 26528 a1a7ad15183e
parent 26100 bb7dd001d190
child 28340 6869ff192a0e
permissions -rw-r--r--
8055075: Group 9b: golden files for tests in tools/javac dir Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23791
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     1
/*
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     4
 *
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     8
 *
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    13
 * accompanied this code).
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    14
 *
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    18
 *
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    21
 * questions.
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    22
 */
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    23
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    24
/*
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    25
 * @test
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    26
 * @bug 8025505
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    27
 * @summary Constant folding deficiency
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    28
 * @library /tools/lib
23791
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    29
 * @build ToolBox
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    30
 * @run main ConstFoldTest
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    31
 */
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    32
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    33
import java.net.URL;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    34
import java.util.List;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    35
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    36
public class ConstFoldTest {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    37
    public static void main(String... args) throws Exception {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    38
        new ConstFoldTest().run();
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    39
    }
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    40
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    41
    // This is the test case. This class should end up
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    42
    // as straight-line code with no conditionals
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    43
    class CFTest {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    44
        void m() {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    45
            int x;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    46
            if (1 != 2)       x=1; else x=0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    47
            if (1 == 2)       x=1; else x=0;
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    48
            if ("" != null)   x=1; else x=0;
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    49
            if ("" == null)   x=1; else x=0;
23791
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    50
            if (null == null) x=1; else x=0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    51
            if (null != null) x=1; else x=0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    52
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    53
            x = 1 != 2        ? 1 : 0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    54
            x = 1 == 2        ? 1 : 0;
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    55
            x = "" != null    ? 1 : 0;
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    56
            x = "" == null    ? 1 : 0;
23791
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    57
            x = null == null  ? 1 : 0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    58
            x = null != null  ? 1 : 0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    59
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    60
            boolean b;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    61
            b = 1 != 2         && true;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    62
            b = 1 == 2         || true;
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    63
            b = ("" != null)   && true;
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    64
            b = ("" == null)   || true;
23791
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    65
            b = (null == null) && true;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    66
            b = (null != null) || true;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    67
        }
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    68
    }
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    69
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    70
    // All of the conditionals above should be eliminated.
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    71
    // these if* bytecodes should not be seen
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    72
    final String regex = "\\sif(?:null|nonnull|eq|ne){1}\\s";
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    73
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    74
    void run() throws Exception {
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    75
        ToolBox tb = new ToolBox();
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    76
23791
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    77
        URL url = ConstFoldTest.class.getResource("ConstFoldTest$CFTest.class");
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    78
        List<String> result = tb.new JavapTask()
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    79
                .options("-c")
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    80
                .classes(url.getFile())
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    81
                .run()
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    82
                .write(ToolBox.OutputKind.DIRECT)
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    83
                .getOutputLines(ToolBox.OutputKind.DIRECT);
23791
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    84
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23791
diff changeset
    85
        List<String> bad_codes = tb.grep(regex, result);
23791
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    86
        if (!bad_codes.isEmpty()) {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    87
            for (String code : bad_codes)
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    88
                System.out.println("Bad OpCode Found: " + code);
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    89
            throw new Exception("constant folding failed");
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    90
        }
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    91
    }
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    92
}