LambdaEagerTest.java revision 12968:4d8a004e5c6d
1207753Smm/*
2207753Smm * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3207753Smm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4207753Smm *
5207753Smm * This code is free software; you can redistribute it and/or modify it
6207753Smm * under the terms of the GNU General Public License version 2 only, as
7207753Smm * published by the Free Software Foundation.
8207753Smm *
9207753Smm * This code is distributed in the hope that it will be useful, but WITHOUT
10207753Smm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11207753Smm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12207753Smm * version 2 for more details (a copy is included in the LICENSE file that
13207753Smm * accompanied this code).
14207753Smm *
15207753Smm * You should have received a copy of the GNU General Public License version
16207753Smm * 2 along with this work; if not, write to the Free Software Foundation,
17207753Smm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18207753Smm *
19207753Smm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20207753Smm * or visit www.oracle.com if you need additional information or have any
21207753Smm * questions.
22207753Smm */
23207753Smmpackage org.graalvm.compiler.jtt.lang;
24207753Smm
25207753Smmimport static org.graalvm.compiler.core.common.GraalOptions.InlineEverything;
26207753Smm
27207753Smmimport java.util.EnumSet;
28207753Smmimport java.util.function.IntBinaryOperator;
29207753Smm
30207753Smmimport org.graalvm.compiler.core.test.GraalCompilerTest;
31207753Smmimport org.graalvm.compiler.nodes.StructuredGraph;
32207753Smmimport org.graalvm.compiler.options.OptionValues;
33207753Smmimport org.junit.Test;
34207753Smm
35207753Smmimport jdk.vm.ci.code.InstalledCode;
36207753Smmimport jdk.vm.ci.meta.DeoptimizationReason;
37207753Smmimport jdk.vm.ci.meta.ResolvedJavaMethod;
38207753Smm
39207753Smmpublic class LambdaEagerTest extends GraalCompilerTest {
40207753Smm
41207753Smm    private static final EnumSet<DeoptimizationReason> UNRESOLVED_UNREACHED = EnumSet.of(DeoptimizationReason.Unresolved, DeoptimizationReason.UnreachedCode);
42207753Smm
43207753Smm    private static int doBinary(IntBinaryOperator op, int x, int y) {
44207753Smm        return op.applyAsInt(x, y);
45207753Smm    }
46207753Smm
47207753Smm    private static int add(int x, int y) {
48207753Smm        return x + y;
49207753Smm    }
50207753Smm
51207753Smm    public static int nonCapturing(int x, int y) {
52207753Smm        return doBinary((a, b) -> a + b, x, y);
53207753Smm    }
54207753Smm
55207753Smm    public static int nonCapturing2(int x, int y) {
56207753Smm        return doBinary(LambdaEagerTest::add, x, y);
57207753Smm    }
58207753Smm
59207753Smm    public static int capturing(int x, int y, int z) {
60207753Smm        return doBinary((a, b) -> a + b - z, x, y);
61207753Smm    }
62207753Smm
63207753Smm    @Test
64207753Smm    public void testEagerResolveNonCapturing01() {
65207753Smm        Result expected = new Result(3, null);
66207753Smm        testAgainstExpected(getResolvedJavaMethod("nonCapturing"), expected, UNRESOLVED_UNREACHED, 1, 2);
67207753Smm    }
68207753Smm
69207753Smm    @Test
70207753Smm    public void testEagerResolveNonCapturing02() {
71207753Smm        Result expected = new Result(3, null);
72207753Smm        testAgainstExpected(getResolvedJavaMethod("nonCapturing2"), expected, UNRESOLVED_UNREACHED, 1, 2);
73207753Smm    }
74207753Smm
75207753Smm    @Test
76207753Smm    public void testEagerResolveCapturing() {
77207753Smm        Result expected = new Result(0, null);
78207753Smm        testAgainstExpected(getResolvedJavaMethod("capturing"), expected, UNRESOLVED_UNREACHED, 1, 2, 3);
79207753Smm    }
80207753Smm
81207753Smm    @Override
82207753Smm    @SuppressWarnings("try")
83207753Smm    protected InstalledCode getCode(ResolvedJavaMethod installedCodeOwner, StructuredGraph graph, boolean forceCompile, boolean installAsDefault, OptionValues options) {
84207753Smm        assert graph == null;
85207753Smm        return super.getCode(installedCodeOwner, graph, forceCompile, installAsDefault, new OptionValues(options, InlineEverything, true));
86207753Smm    }
87207753Smm}
88207753Smm