OpenJDK / amber / amber
changeset 56888:f83e03003147 pattern-runtime
Updates to pattern match tests
author | briangoetz |
---|---|
date | Fri, 21 Jun 2019 10:34:47 -0400 |
parents | d85aaf1ffd89 |
children | d47c05ad75fe 96ad823df459 |
files | test/jdk/java/lang/lang-runtime/PatternHandleTest.java test/jdk/java/lang/lang-runtime/boottest/java.base/java/lang/runtime/CarrierTest.java |
diffstat | 2 files changed, 101 insertions(+), 94 deletions(-) [+] |
line wrap: on
line diff
--- a/test/jdk/java/lang/lang-runtime/PatternHandleTest.java Thu Jun 20 22:35:17 2019 +0200 +++ b/test/jdk/java/lang/lang-runtime/PatternHandleTest.java Fri Jun 21 10:34:47 2019 -0400 @@ -35,6 +35,7 @@ import org.testng.annotations.Test; +import static java.util.Map.entry; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotSame; @@ -82,14 +83,21 @@ static final TryMatchInvoker<List> listInvoker = (MethodHandle mh, List x) -> mh.invokeExact(x); static final TryMatchInvoker<TestClass> testClassInvoker = (MethodHandle mh, TestClass x) -> mh.invokeExact(x); static final TryMatchInvoker<TestClass2> testClass2Invoker = (MethodHandle mh, TestClass2 x) -> mh.invokeExact(x); - static final TryMatchInvoker rawObjectInvoker = objectInvoker; - static final TryMatchInvoker rawStringInvoker = stringInvoker; - static final TryMatchInvoker rawIntegerInvoker = integerInvoker; - static final TryMatchInvoker rawIntInvoker = intInvoker; + + static final Map<Class<?>, TryMatchInvoker<?>> invokers + = Map.ofEntries(entry(Object.class, objectInvoker), + entry(Number.class, numberInvoker), + entry(Integer.class, integerInvoker), + entry(int.class, intInvoker), + entry(String.class, stringInvoker), + entry(List.class, listInvoker), + entry(TestClass.class, testClassInvoker), + entry(TestClass2.class, testClass2Invoker)); interface Throwing { public void run() throws Throwable; } + static void assertThrows(Class<? extends Throwable> exception, Throwing r) { try { r.run(); @@ -101,16 +109,16 @@ } } - static<T> void assertMatch(MatchKind expected, - PatternHandle e, - TryMatchInvoker<T> invoker, - T target, - Object... expectedBindings) throws Throwable { + static void assertMatch(MatchKind expected, + PatternHandle e, + Object target, + Object... expectedBindings) throws Throwable { int count = e.descriptor().parameterCount(); Object[] bindings = new Object[count]; Object carrier; try { - carrier = invoker.tryMatch(e.tryMatch(), target); + TryMatchInvoker inv = invokers.get(e.descriptor().returnType()); + carrier = inv.tryMatch(e.tryMatch(), target); } catch (Throwable t) { carrier = null; @@ -245,49 +253,49 @@ PatternHandle TYPE_STRING_NULLABLE = PatternHandles.ofTypeNullable(String.class); public void testType() throws Throwable { - assertMatch(MatchKind.MATCH_SELF, TYPE_STRING, stringInvoker, "Foo", "Foo"); - assertMatch(MatchKind.NO_MATCH, TYPE_STRING, stringInvoker, null); - assertMatch(MatchKind.ERROR, TYPE_STRING, rawStringInvoker, List.of()); - assertMatch(MatchKind.ERROR, TYPE_STRING, rawStringInvoker, 3); + assertMatch(MatchKind.MATCH_SELF, TYPE_STRING, "Foo", "Foo"); + assertMatch(MatchKind.NO_MATCH, TYPE_STRING, null); + assertMatch(MatchKind.ERROR, TYPE_STRING, List.of()); + assertMatch(MatchKind.ERROR, TYPE_STRING, 3); - assertMatch(MatchKind.MATCH_SELF, TYPE_LIST, listInvoker, List.of(3), List.of(3)); - assertMatch(MatchKind.MATCH_SELF, TYPE_LIST, listInvoker, List.of(), List.of()); - assertMatch(MatchKind.MATCH_SELF, TYPE_LIST, listInvoker, new ArrayList<>(), List.of()); - assertMatch(MatchKind.NO_MATCH, TYPE_LIST, listInvoker, null); + assertMatch(MatchKind.MATCH_SELF, TYPE_LIST, List.of(3), List.of(3)); + assertMatch(MatchKind.MATCH_SELF, TYPE_LIST, List.of(), List.of()); + assertMatch(MatchKind.MATCH_SELF, TYPE_LIST, new ArrayList<>(), List.of()); + assertMatch(MatchKind.NO_MATCH, TYPE_LIST, null); - assertMatch(MatchKind.MATCH_SELF, TYPE_INTEGER, integerInvoker, 3, 3); - assertMatch(MatchKind.MATCH_SELF, TYPE_NUMBER, numberInvoker, 3, 3); - assertMatch(MatchKind.MATCH_SELF, TYPE_OBJECT, objectInvoker, 3, 3); - assertMatch(MatchKind.NO_MATCH, TYPE_OBJECT, objectInvoker, null); + assertMatch(MatchKind.MATCH_SELF, TYPE_INTEGER, 3, 3); + assertMatch(MatchKind.MATCH_SELF, TYPE_NUMBER, 3, 3); + assertMatch(MatchKind.MATCH_SELF, TYPE_OBJECT, 3, 3); + assertMatch(MatchKind.NO_MATCH, TYPE_OBJECT, null); - assertMatch(MatchKind.ERROR, TYPE_INTEGER, rawIntegerInvoker, 3.14f); - assertMatch(MatchKind.ERROR, TYPE_INTEGER, rawIntegerInvoker, "foo"); + assertMatch(MatchKind.ERROR, TYPE_INTEGER, 3.14f); + assertMatch(MatchKind.ERROR, TYPE_INTEGER, "foo"); } public void testPrimitiveType() throws Throwable { - assertMatch(MatchKind.MATCH_SELF, TYPE_INT, intInvoker, 3, 3); - assertMatch(MatchKind.ERROR, TYPE_INT, rawIntInvoker, 3.14f); + assertMatch(MatchKind.MATCH_SELF, TYPE_INT, 3, 3); + assertMatch(MatchKind.ERROR, TYPE_INT, 3.14f); PatternHandle asObject = PatternHandles.adaptTarget(TYPE_INT, Object.class); - assertMatch(MatchKind.MATCH_SELF, asObject, objectInvoker, 3, 3); - assertMatch(MatchKind.NO_MATCH, asObject, objectInvoker, 3.14f); - assertMatch(MatchKind.NO_MATCH, asObject, objectInvoker, null); + assertMatch(MatchKind.MATCH_SELF, asObject, 3, 3); + assertMatch(MatchKind.NO_MATCH, asObject, 3.14f); + assertMatch(MatchKind.NO_MATCH, asObject, null); PatternHandle asInteger = PatternHandles.adaptTarget(TYPE_INT, Integer.class); - assertMatch(MatchKind.MATCH_SELF, asInteger, integerInvoker, 3, 3); - assertMatch(MatchKind.NO_MATCH, asInteger, integerInvoker, null); - assertMatch(MatchKind.ERROR, asInteger, rawIntegerInvoker, 3.14f); + assertMatch(MatchKind.MATCH_SELF, asInteger, 3, 3); + assertMatch(MatchKind.NO_MATCH, asInteger, null); + assertMatch(MatchKind.ERROR, asInteger, 3.14f); } public void testNullableType() throws Throwable { - assertMatch(MatchKind.MATCH_SELF, TYPE_STRING_NULLABLE, stringInvoker, "Foo", "Foo"); - assertMatch(MatchKind.MATCH, TYPE_STRING_NULLABLE, stringInvoker, null, (Object) null); - assertMatch(MatchKind.ERROR, TYPE_STRING_NULLABLE, rawStringInvoker, 3); + assertMatch(MatchKind.MATCH_SELF, TYPE_STRING_NULLABLE, "Foo", "Foo"); + assertMatch(MatchKind.MATCH, TYPE_STRING_NULLABLE, null, (Object) null); + assertMatch(MatchKind.ERROR, TYPE_STRING_NULLABLE, 3); PatternHandle asObjectNullable = PatternHandles.adaptTarget(TYPE_STRING_NULLABLE, Object.class); - assertMatch(MatchKind.MATCH_SELF, asObjectNullable, objectInvoker, "Foo", "Foo"); - assertMatch(MatchKind.MATCH, asObjectNullable, objectInvoker, null, (Object) null); - assertMatch(MatchKind.NO_MATCH, asObjectNullable, objectInvoker, 3); + assertMatch(MatchKind.MATCH_SELF, asObjectNullable, "Foo", "Foo"); + assertMatch(MatchKind.MATCH, asObjectNullable, null, (Object) null); + assertMatch(MatchKind.NO_MATCH, asObjectNullable, 3); } public void testAdapt() throws Throwable { @@ -299,60 +307,60 @@ assertEquals(n.descriptor().returnType(), Integer.class); assertEquals(w.descriptor().returnType(), Object.class); - assertMatch(MatchKind.MATCH_SELF, e, numberInvoker, 1, 1); - assertMatch(MatchKind.MATCH_SELF, n, integerInvoker, 1, 1); - assertMatch(MatchKind.MATCH_SELF, w, objectInvoker, 1, 1); + assertMatch(MatchKind.MATCH_SELF, e, 1, 1); + assertMatch(MatchKind.MATCH_SELF, n, 1, 1); + assertMatch(MatchKind.MATCH_SELF, w, 1, 1); - assertMatch(MatchKind.MATCH_SELF, e, numberInvoker, 3.14f, 3.14f); - assertMatch(MatchKind.ERROR, n, rawIntegerInvoker, 3.14f); - assertMatch(MatchKind.MATCH_SELF, w, objectInvoker, 3.14f, 3.14f); + assertMatch(MatchKind.MATCH_SELF, e, 3.14f, 3.14f); + assertMatch(MatchKind.ERROR, n, 3.14f); + assertMatch(MatchKind.MATCH_SELF, w, 3.14f, 3.14f); - assertMatch(MatchKind.MATCH, e, numberInvoker, null, (Object) null); - assertMatch(MatchKind.MATCH, n, integerInvoker, null, (Object) null); - assertMatch(MatchKind.MATCH, w, objectInvoker, null, (Object) null); + assertMatch(MatchKind.MATCH, e, null, (Object) null); + assertMatch(MatchKind.MATCH, n, null, (Object) null); + assertMatch(MatchKind.MATCH, w, null, (Object) null); e = PatternHandles.ofType(Number.class); n = PatternHandles.adaptTarget(e, Integer.class); w = PatternHandles.adaptTarget(e, Object.class); - assertMatch(MatchKind.MATCH_SELF, e, numberInvoker, 1, 1); - assertMatch(MatchKind.MATCH_SELF, n, integerInvoker, 1, 1); - assertMatch(MatchKind.MATCH_SELF, w, objectInvoker, 1, 1); - assertMatch(MatchKind.NO_MATCH, e, numberInvoker, null); - assertMatch(MatchKind.NO_MATCH, n, integerInvoker, null); - assertMatch(MatchKind.NO_MATCH, w, objectInvoker, null); + assertMatch(MatchKind.MATCH_SELF, e, 1, 1); + assertMatch(MatchKind.MATCH_SELF, n, 1, 1); + assertMatch(MatchKind.MATCH_SELF, w, 1, 1); + assertMatch(MatchKind.NO_MATCH, e, null); + assertMatch(MatchKind.NO_MATCH, n, null); + assertMatch(MatchKind.NO_MATCH, w, null); PatternHandle widenNarrow = PatternHandles.adaptTarget(PatternHandles.adaptTarget(TYPE_STRING, Object.class), String.class); - assertMatch(MatchKind.MATCH_SELF, widenNarrow, stringInvoker, "Foo", "Foo"); - assertMatch(MatchKind.NO_MATCH, widenNarrow, stringInvoker, null); - assertMatch(MatchKind.ERROR, widenNarrow, rawStringInvoker, List.of()); - assertMatch(MatchKind.ERROR, widenNarrow, rawStringInvoker, 3); + assertMatch(MatchKind.MATCH_SELF, widenNarrow, "Foo", "Foo"); + assertMatch(MatchKind.NO_MATCH, widenNarrow, null); + assertMatch(MatchKind.ERROR, widenNarrow, List.of()); + assertMatch(MatchKind.ERROR, widenNarrow, 3); PatternHandle widenNarrowNullable = PatternHandles.adaptTarget(PatternHandles.adaptTarget(TYPE_STRING_NULLABLE, Object.class), String.class); - assertMatch(MatchKind.MATCH_SELF, widenNarrowNullable, stringInvoker, "Foo", "Foo"); - assertMatch(MatchKind.MATCH, widenNarrowNullable, stringInvoker, null, (Object) null); - assertMatch(MatchKind.ERROR, widenNarrowNullable, rawStringInvoker, List.of()); - assertMatch(MatchKind.ERROR, widenNarrowNullable, rawStringInvoker, 3); + assertMatch(MatchKind.MATCH_SELF, widenNarrowNullable, "Foo", "Foo"); + assertMatch(MatchKind.MATCH, widenNarrowNullable, null, (Object) null); + assertMatch(MatchKind.ERROR, widenNarrowNullable, List.of()); + assertMatch(MatchKind.ERROR, widenNarrowNullable, 3); } public void testConstant() throws Throwable { PatternHandle constantFoo = PatternHandles.ofConstant("foo"); - assertMatch(MatchKind.MATCH, constantFoo, stringInvoker, "foo"); - assertMatch(MatchKind.NO_MATCH, constantFoo, stringInvoker, "bar"); - assertMatch(MatchKind.ERROR, constantFoo, rawStringInvoker, 3); - assertMatch(MatchKind.NO_MATCH, constantFoo, stringInvoker, null); + assertMatch(MatchKind.MATCH, constantFoo, "foo"); + assertMatch(MatchKind.NO_MATCH, constantFoo, "bar"); + assertMatch(MatchKind.ERROR, constantFoo, 3); + assertMatch(MatchKind.NO_MATCH, constantFoo, null); PatternHandle constantThree = PatternHandles.ofConstant(3); - assertMatch(MatchKind.MATCH, constantThree, integerInvoker, 3); - assertMatch(MatchKind.NO_MATCH, constantThree, integerInvoker, 4); - assertMatch(MatchKind.NO_MATCH, constantThree, integerInvoker, null); + assertMatch(MatchKind.MATCH, constantThree, 3); + assertMatch(MatchKind.NO_MATCH, constantThree, 4); + assertMatch(MatchKind.NO_MATCH, constantThree, null); } public void testNullConstant() throws Throwable { PatternHandle constantNull = PatternHandles.ofConstant(null); - assertMatch(MatchKind.MATCH, constantNull, objectInvoker, null); - assertMatch(MatchKind.NO_MATCH, constantNull, objectInvoker, "foo"); - assertMatch(MatchKind.NO_MATCH, constantNull, objectInvoker, 3); + assertMatch(MatchKind.MATCH, constantNull, null); + assertMatch(MatchKind.NO_MATCH, constantNull, "foo"); + assertMatch(MatchKind.NO_MATCH, constantNull, 3); } public void testProjections() throws Throwable { @@ -361,38 +369,38 @@ PatternHandles.ofEagerProjection(TestClass.class, TestClass.COMPONENT_MHS), MatchKind.MATCH_CARRIER); for (var ps : m.entrySet()) { for (var entry : TestClass.INSTANCES.entrySet()) { - assertMatch(ps.getValue(), ps.getKey(), testClassInvoker, entry.getKey(), entry.getValue()); + assertMatch(ps.getValue(), ps.getKey(), entry.getKey(), entry.getValue()); } - assertMatch(MatchKind.NO_MATCH, ps.getKey(), testClassInvoker, null); + assertMatch(MatchKind.NO_MATCH, ps.getKey(), null); PatternHandle asObject = PatternHandles.adaptTarget(ps.getKey(), Object.class); for (var entry : TestClass.INSTANCES.entrySet()) - assertMatch(ps.getValue(), asObject, objectInvoker, entry.getKey(), entry.getValue()); - assertMatch(MatchKind.NO_MATCH, asObject, objectInvoker, null); + assertMatch(ps.getValue(), asObject, entry.getKey(), entry.getValue()); + assertMatch(MatchKind.NO_MATCH, asObject, null); PatternHandle asTestClassAgain = PatternHandles.adaptTarget(asObject, TestClass.class); for (var entry : TestClass.INSTANCES.entrySet()) - assertMatch(ps.getValue(), asTestClassAgain, testClassInvoker, entry.getKey(), entry.getValue()); - assertMatch(MatchKind.NO_MATCH, asTestClassAgain, testClassInvoker, null); + assertMatch(ps.getValue(), asTestClassAgain, entry.getKey(), entry.getValue()); + assertMatch(MatchKind.NO_MATCH, asTestClassAgain, null); } } public void testDigest() throws Throwable { PatternHandle e = PatternHandles.ofImperative(TestClass.TYPE, TestClass.DIGESTER); for (var entry : TestClass.INSTANCES.entrySet()) - assertMatch(MatchKind.MATCH_CARRIER, e, testClassInvoker, entry.getKey(), entry.getValue()); - assertMatch(MatchKind.NO_MATCH, e, testClassInvoker, null); + assertMatch(MatchKind.MATCH_CARRIER, e, entry.getKey(), entry.getValue()); + assertMatch(MatchKind.NO_MATCH, e, null); } public void testDigestPartial() throws Throwable { PatternHandle e = PatternHandles.ofImperative(TestClass.TYPE, TestClass.DIGESTER_PARTIAL); for (var entry : TestClass.INSTANCES.entrySet()) { if (entry.getKey().matches()) - assertMatch(MatchKind.MATCH_CARRIER, e, testClassInvoker, entry.getKey(), entry.getValue()); + assertMatch(MatchKind.MATCH_CARRIER, e, entry.getKey(), entry.getValue()); else - assertMatch(MatchKind.NO_MATCH, e, testClassInvoker, entry.getKey()); + assertMatch(MatchKind.NO_MATCH, e, entry.getKey()); } - assertMatch(MatchKind.NO_MATCH, e, testClassInvoker, null); + assertMatch(MatchKind.NO_MATCH, e, null); } public void testCompose() throws Throwable { @@ -409,21 +417,21 @@ public void testDropBindings() throws Throwable { PatternHandle e = PatternHandles.ofEagerProjection(TestClass.class, TestClass.COMPONENT_MHS); - assertMatch(MatchKind.MATCH_CARRIER, e, testClassInvoker, TestClass.INSTANCE_A, + assertMatch(MatchKind.MATCH_CARRIER, e, TestClass.INSTANCE_A, TestClass.COMPONENTS_A); - assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, 0), testClassInvoker, TestClass.INSTANCE_A, + assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, 0), TestClass.INSTANCE_A, 3, 4L, (byte) 5); - assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, 0, 0), testClassInvoker, TestClass.INSTANCE_A, + assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, 0, 0), TestClass.INSTANCE_A, 3, 4L, (byte) 5); - assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, 3), testClassInvoker, TestClass.INSTANCE_A, + assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, 3), TestClass.INSTANCE_A, "foo", 3, 4L); - assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, 0, 1, 2, 3), testClassInvoker, TestClass.INSTANCE_A); + assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, 0, 1, 2, 3), TestClass.INSTANCE_A); assertThrows(IndexOutOfBoundsException.class, - () -> assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, -1), testClassInvoker, TestClass.INSTANCE_A, + () -> assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, -1), TestClass.INSTANCE_A, 3, 4L, (byte) 5)); assertThrows(IndexOutOfBoundsException.class, - () -> assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, 4), testClassInvoker, TestClass.INSTANCE_A, + () -> assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(e, 4), TestClass.INSTANCE_A, 3, 4L, (byte) 5)); } @@ -432,14 +440,14 @@ PatternHandle TC2_STRING = PatternHandles.nested(TC2, TYPE_STRING); PatternHandle TC2_OBJECT = PatternHandles.nested(TC2, TYPE_OBJECT); - assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(TC2_STRING, 0), testClass2Invoker, new TestClass2("foo"), + assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(TC2_STRING, 0), new TestClass2("foo"), "foo"); - assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(TC2_OBJECT, 0), testClass2Invoker, new TestClass2("foo"), + assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(TC2_OBJECT, 0), new TestClass2("foo"), "foo"); - assertMatch(MatchKind.NO_MATCH, PatternHandles.dropBindings(TC2_STRING, 0), testClass2Invoker, new TestClass2(List.of(3)), + assertMatch(MatchKind.NO_MATCH, PatternHandles.dropBindings(TC2_STRING, 0), new TestClass2(List.of(3)), "foo"); - assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(PatternHandles.nested(TC2, TC2_STRING), 0, 1), testClass2Invoker, new TestClass2(new TestClass2("foo")), + assertMatch(MatchKind.MATCH_CARRIER, PatternHandles.dropBindings(PatternHandles.nested(TC2, TC2_STRING), 0, 1), new TestClass2(new TestClass2("foo")), "foo"); } }
--- a/test/jdk/java/lang/lang-runtime/boottest/java.base/java/lang/runtime/CarrierTest.java Thu Jun 20 22:35:17 2019 +0200 +++ b/test/jdk/java/lang/lang-runtime/boottest/java.base/java/lang/runtime/CarrierTest.java Fri Jun 21 10:34:47 2019 -0400 @@ -24,7 +24,6 @@ */ package java.lang.runtime; -import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodType; import java.util.Arrays; import java.util.Collections;