001/*
002 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.jtt.lang;
024
025import java.util.*;
026import java.util.function.*;
027
028import jdk.internal.jvmci.code.*;
029import jdk.internal.jvmci.meta.*;
030import jdk.internal.jvmci.options.*;
031import jdk.internal.jvmci.options.OptionValue.*;
032
033import org.junit.*;
034
035import com.oracle.graal.compiler.common.*;
036import com.oracle.graal.compiler.test.*;
037import com.oracle.graal.nodes.*;
038
039public class LambdaEagerTest extends GraalCompilerTest {
040
041    private static final EnumSet<DeoptimizationReason> UNRESOLVED_UNREACHED = EnumSet.of(DeoptimizationReason.Unresolved, DeoptimizationReason.UnreachedCode);
042
043    private static int doBinary(IntBinaryOperator op, int x, int y) {
044        return op.applyAsInt(x, y);
045    }
046
047    private static int add(int x, int y) {
048        return x + y;
049    }
050
051    public static int nonCapturing(int x, int y) {
052        return doBinary((a, b) -> a + b, x, y);
053    }
054
055    public static int nonCapturing2(int x, int y) {
056        return doBinary(LambdaEagerTest::add, x, y);
057    }
058
059    public static int capturing(int x, int y, int z) {
060        return doBinary((a, b) -> a + b - z, x, y);
061    }
062
063    @Test
064    public void testEagerResolveNonCapturing01() {
065        Result expected = new Result(3, null);
066        testAgainstExpected(getResolvedJavaMethod("nonCapturing"), expected, UNRESOLVED_UNREACHED, 1, 2);
067    }
068
069    @Test
070    public void testEagerResolveNonCapturing02() {
071        Result expected = new Result(3, null);
072        testAgainstExpected(getResolvedJavaMethod("nonCapturing2"), expected, UNRESOLVED_UNREACHED, 1, 2);
073    }
074
075    @Test
076    public void testEagerResolveCapturing() {
077        Result expected = new Result(0, null);
078        testAgainstExpected(getResolvedJavaMethod("capturing"), expected, UNRESOLVED_UNREACHED, 1, 2, 3);
079    }
080
081    @Override
082    protected InstalledCode getCode(ResolvedJavaMethod installedCodeOwner, StructuredGraph graph, boolean forceCompile) {
083        try (OverrideScope scope = OptionValue.override(GraalOptions.InlineEverything, true)) {
084            return super.getCode(installedCodeOwner, graph, forceCompile);
085        }
086    }
087}