Path: blob/master/test/jdk/java/lang/invoke/FoldTest.java
41149 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* @test26* @bug 813988527* @run testng/othervm -ea -esa test.java.lang.invoke.FoldTest28*/2930package test.java.lang.invoke;3132import java.io.StringWriter;33import java.lang.invoke.MethodHandle;34import java.lang.invoke.MethodHandles;35import java.lang.invoke.MethodHandles.Lookup;36import java.lang.invoke.MethodType;3738import static java.lang.invoke.MethodType.methodType;3940import static org.testng.AssertJUnit.*;4142import org.testng.annotations.*;4344/**45* Tests for the new fold method handle combinator added in JEP 274.46*/47public class FoldTest {4849static final Lookup LOOKUP = MethodHandles.lookup();5051@Test52public static void testFold0a() throws Throwable {53// equivalence to foldArguments(MethodHandle,MethodHandle)54MethodHandle fold = MethodHandles.foldArguments(Fold.MH_multer, 0, Fold.MH_adder);55assertEquals(Fold.MT_folded1, fold.type());56assertEquals(720, (int) fold.invoke(3, 4, 5));57}5859@Test60public static void testFold1a() throws Throwable {61// test foldArguments for folding position 162MethodHandle fold = MethodHandles.foldArguments(Fold.MH_multer, 1, Fold.MH_adder1);63assertEquals(Fold.MT_folded1, fold.type());64assertEquals(540, (int) fold.invoke(3, 4, 5));65}6667@Test68public static void testFold0b() throws Throwable {69// test foldArguments equivalence with multiple types70MethodHandle fold = MethodHandles.foldArguments(Fold.MH_str, 0, Fold.MH_comb);71assertEquals(Fold.MT_folded2, fold.type());72assertEquals(23, (int) fold.invoke("true", true, 23));73}7475@Test76public static void testFold1b() throws Throwable {77// test folgArguments for folding position 1, with multiple types78MethodHandle fold = MethodHandles.foldArguments(Fold.MH_str, 1, Fold.MH_comb2);79assertEquals(Fold.MT_folded3, fold.type());80assertEquals(1, (int) fold.invoke(true, true, 1));81assertEquals(-1, (int) fold.invoke(true, false, -1));82}8384@Test85public static void testFoldArgumentsExample() throws Throwable {86// test the JavaDoc foldArguments-with-pos example87StringWriter swr = new StringWriter();88MethodHandle trace = LOOKUP.findVirtual(StringWriter.class, "write", methodType(void.class, String.class)).bindTo(swr);89MethodHandle cat = LOOKUP.findVirtual(String.class, "concat", methodType(String.class, String.class));90assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));91MethodHandle catTrace = MethodHandles.foldArguments(cat, 1, trace);92assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));93assertEquals("jum", swr.toString());94}9596static class Fold {9798static int adder(int a, int b, int c) {99return a + b + c;100}101102static int adder1(int a, int b) {103return a + b;104}105106static int multer(int x, int q, int r, int s) {107return x * q * r * s;108}109110static int str(boolean b1, String s, boolean b2, int x) {111return b1 && s.equals(String.valueOf(b2)) ? x : -x;112}113114static boolean comb(String s, boolean b2) {115return !s.equals(b2);116}117118static String comb2(boolean b2, int x) {119int ib = b2 ? 1 : 0;120return ib == x ? "true" : "false";121}122123static final Class<Fold> FOLD = Fold.class;124125static final MethodType MT_adder = methodType(int.class, int.class, int.class, int.class);126static final MethodType MT_adder1 = methodType(int.class, int.class, int.class);127static final MethodType MT_multer = methodType(int.class, int.class, int.class, int.class, int.class);128static final MethodType MT_str = methodType(int.class, boolean.class, String.class, boolean.class, int.class);129static final MethodType MT_comb = methodType(boolean.class, String.class, boolean.class);130static final MethodType MT_comb2 = methodType(String.class, boolean.class, int.class);131132static final MethodHandle MH_adder;133static final MethodHandle MH_adder1;134static final MethodHandle MH_multer;135static final MethodHandle MH_str;136static final MethodHandle MH_comb;137static final MethodHandle MH_comb2;138139static final MethodType MT_folded1 = methodType(int.class, int.class, int.class, int.class);140static final MethodType MT_folded2 = methodType(int.class, String.class, boolean.class, int.class);141static final MethodType MT_folded3 = methodType(int.class, boolean.class, boolean.class, int.class);142143static {144try {145MH_adder = LOOKUP.findStatic(FOLD, "adder", MT_adder);146MH_adder1 = LOOKUP.findStatic(FOLD, "adder1", MT_adder1);147MH_multer = LOOKUP.findStatic(FOLD, "multer", MT_multer);148MH_str = LOOKUP.findStatic(FOLD, "str", MT_str);149MH_comb = LOOKUP.findStatic(FOLD, "comb", MT_comb);150MH_comb2 = LOOKUP.findStatic(FOLD, "comb2", MT_comb2);151} catch (Exception e) {152throw new ExceptionInInitializerError(e);153}154}155}156157}158159160