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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.jtt.lang;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.InlineEverything;
  26 
  27 import java.util.EnumSet;
  28 import java.util.function.IntBinaryOperator;
  29 
  30 import org.graalvm.compiler.core.test.GraalCompilerTest;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.options.OptionValues;
  33 import org.junit.Test;
  34 
  35 import jdk.vm.ci.code.InstalledCode;
  36 import jdk.vm.ci.meta.DeoptimizationReason;
  37 import jdk.vm.ci.meta.ResolvedJavaMethod;
  38 
  39 public class LambdaEagerTest extends GraalCompilerTest {
  40 
  41     private static final EnumSet<DeoptimizationReason> UNRESOLVED_UNREACHED = EnumSet.of(DeoptimizationReason.Unresolved, DeoptimizationReason.UnreachedCode);
  42 
  43     private static int doBinary(IntBinaryOperator op, int x, int y) {
  44         return op.applyAsInt(x, y);
  45     }
  46 
  47     private static int add(int x, int y) {
  48         return x + y;
  49     }
  50 
  51     public static int nonCapturing(int x, int y) {
  52         return doBinary((a, b) -> a + b, x, y);
  53     }
  54 
  55     public static int nonCapturing2(int x, int y) {
  56         return doBinary(LambdaEagerTest::add, x, y);
  57     }
  58 
  59     public static int capturing(int x, int y, int z) {
  60         return doBinary((a, b) -> a + b - z, x, y);
  61     }
  62 
  63     @Test
  64     public void testEagerResolveNonCapturing01() {
  65         Result expected = new Result(3, null);
  66         testAgainstExpected(getResolvedJavaMethod("nonCapturing"), expected, UNRESOLVED_UNREACHED, 1, 2);
  67     }
  68 
  69     @Test
  70     public void testEagerResolveNonCapturing02() {
  71         Result expected = new Result(3, null);
  72         testAgainstExpected(getResolvedJavaMethod("nonCapturing2"), expected, UNRESOLVED_UNREACHED, 1, 2);
  73     }
  74 
  75     @Test
  76     public void testEagerResolveCapturing() {
  77         Result expected = new Result(0, null);
  78         testAgainstExpected(getResolvedJavaMethod("capturing"), expected, UNRESOLVED_UNREACHED, 1, 2, 3);
  79     }
  80 
  81     @Override
  82     @SuppressWarnings("try")
  83     protected InstalledCode getCode(ResolvedJavaMethod installedCodeOwner, StructuredGraph graph, boolean forceCompile, boolean installAsDefault, OptionValues options) {
  84         assert graph == null;
  85         return super.getCode(installedCodeOwner, graph, forceCompile, installAsDefault, new OptionValues(options, InlineEverything, true));
  86     }
  87 }