SSL LoopCombinatorTest.java
Interaktion und PortierbarkeitJAVA
/* * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
@Test publicstaticvoid testLoopFacNullInit() throws Throwable { // null initializer for counter, should initialize to 0
MethodHandle[] counterClause = new MethodHandle[]{null, Fac.MH_inc};
MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
assertEquals(Fac.MT_fac, loop.type());
assertEquals(120, loop.invoke(5));
}
@Test publicstaticvoid testLoopNullInit() throws Throwable { // null initializer for counter, should initialize to 0, one-clause loop
MethodHandle[] counterClause = new MethodHandle[]{null, Loop.MH_inc, Loop.MH_pred, Loop.MH_fin};
MethodHandle loop = MethodHandles.loop(counterClause);
assertEquals(Loop.MT_loop, loop.type());
assertEquals(10, loop.invoke(10));
}
@Test publicstaticvoid testLoopVoid1() throws Throwable { // construct a post-checked loop that only does one iteration and has a void body and void local state
MethodHandle loop = MethodHandles.loop(new MethodHandle[]{Empty.MH_f, Empty.MH_f, Empty.MH_pred, null});
assertEquals(MethodType.methodType(void.class), loop.type());
loop.invoke();
}
@Test publicstaticvoid testLoopVoid2() throws Throwable { // construct a post-checked loop that only does one iteration and has a void body and void local state, // initialized implicitly from the step type
MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, null});
assertEquals(MethodType.methodType(void.class), loop.type());
loop.invoke();
}
@Test publicstaticvoid testLoopVoid3() throws Throwable { // construct a post-checked loop that only does one iteration and has a void body and void local state, // and that has a void finalizer
MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, Empty.MH_f});
assertEquals(MethodType.methodType(void.class), loop.type());
loop.invoke();
}
@Test publicstaticvoid testLoopFacWithVoidState() throws Throwable { // like testLoopFac, but with additional void state that outputs a dot
MethodHandle[] counterClause = new MethodHandle[]{Fac.MH_zero, Fac.MH_inc};
MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
MethodHandle[] dotClause = new MethodHandle[]{null, Fac.MH_dot};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause, dotClause);
assertEquals(Fac.MT_fac, loop.type());
assertEquals(120, loop.invoke(5));
}
@Test publicstaticvoid testLoopVoidInt() throws Throwable { // construct a post-checked loop that only does one iteration and has a void body and void local state, // and that returns a constant
MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, Empty.MH_c});
assertEquals(MethodType.methodType(int.class), loop.type());
assertEquals(23, loop.invoke());
}
@Test publicstaticvoid testLoopWithVirtuals() throws Throwable { // construct a loop (to calculate factorial) that uses a mix of static and virtual methods
MethodHandle[] counterClause = new MethodHandle[]{null, LoopWithVirtuals.permute(LoopWithVirtuals.MH_inc)};
MethodHandle[] accumulatorClause = new MethodHandle[]{ // init function must indicate the loop arguments (there is no other means to determine them)
MethodHandles.dropArguments(LoopWithVirtuals.MH_one, 0, LoopWithVirtuals.class),
LoopWithVirtuals.permute(LoopWithVirtuals.MH_mult),
LoopWithVirtuals.permute(LoopWithVirtuals.MH_pred),
LoopWithVirtuals.permute(LoopWithVirtuals.MH_fin)
};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
assertEquals(LoopWithVirtuals.MT_loop, loop.type());
assertEquals(120, loop.invoke(new LoopWithVirtuals(), 5));
}
@Test publicstaticvoid testLoopOmitPred() throws Throwable { // construct a loop to calculate factorial that omits a predicate
MethodHandle[] counterClause = new MethodHandle[]{null, Fac.MH_inc, null, Fac.MH_fin};
MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
assertEquals(Fac.MT_fac, loop.type());
assertEquals(120, loop.invoke(5));
}
staticvoid assertEqualsFIXME(String expect, String actual) { if (!expect.equals(actual)) { // just issue a warning
System.out.println("*** "+actual+"\n != "+expect);
}
}
@DataProvider static Object[][] whileLoopTestData() {
MethodHandle
zeroI = While.MH_zero,
zeroX = snip(zeroI),
zeroIB = slap(zeroI, byte.class),
predII = While.MH_pred,
predIX = snip(predII),
predIIB = slap(predII, byte.class),
stepII = While.MH_step,
stepIX = snip(stepII),
stepIIB = slap(stepII, byte.class)
; returnnew Object[][] { // normal while loop clauses, perhaps with effectively-identical reductions
{zeroI, predII, stepII, null},
{zeroX, predII, stepII, null},
{null, predII, stepII, null}, // expanded while loop clauses
{zeroIB, predIIB, stepIIB, null},
{zeroI, predIIB, stepIIB, null},
{null, predIIB, stepIIB, null},
{zeroIB, predII, stepIIB, null},
{zeroX, predII, stepIIB, null},
{null, predII, stepIIB, null}, // short step clauses cause errors
{zeroI, predII, stepIX, "loop predicate must match: (int,int)boolean != (int)boolean"},
{zeroIB, predIX, stepIX, "loop initializer must match: (int,byte)int != ()int"}, // bad body type
{zeroI, predII, tweak(stepII, -1, char.class), "body function must match: (int,int)char != (char,int,int)char"},
{zeroI, predII, tweak(stepII, 0, char.class), "body function must match: (char,int)int != (int,char,int)int"}, // bad pred type
{zeroI, tweak(predII, -1, char.class), stepII, "loop predicate must match: (int,int)char != (int,int)boolean"},
{zeroI, tweak(predII, 0, char.class), stepII, "loop predicate must match: (char,int)boolean != (int,int)boolean"}, // bad init type
{tweak(zeroI, -1, char.class), predII, stepII, "loop initializer must match: (int)char != (int)int"},
{tweak(zeroI, 0, char.class), predII, stepII, "loop initializer must match: (char)int != (int)int"},
};
}
// tweak the type of an MH static MethodHandle tweak(MethodHandle mh, int argPos, Class<?> type) {
MethodType mt = mh.type(); if (argPos == -1)
mt = mt.changeReturnType(type); else
mt = mt.changeParameterType(argPos, type); return MethodHandles.explicitCastArguments(mh, mt);
} // snip off an MH argument, hard-wiring to zero static MethodHandle snip(MethodHandle mh, int argPos) { if (argPos < 0) returnnull; // special case for optional args Class<?> argType = mh.type().parameterType(argPos);
Object zero; try {
zero = MethodHandles.zero(argType).invoke();
} catch (Throwable ex) { thrownew AssertionError(ex);
} return MethodHandles.insertArguments(mh, argPos, zero);
} static MethodHandle snip(MethodHandle mh) { return snip(mh, mh.type().parameterCount()-1);
} // slap on an extra type on the end of the MH static MethodHandle slap(MethodHandle mh, Class<?> addType) { return MethodHandles.dropArguments(mh, mh.type().parameterCount(), addType);
}
@Test publicstaticvoid testWhileLoopNoIteration() throws Throwable { // a while loop that never executes its body because the predicate evaluates to false immediately
MethodHandle loop = MethodHandles.whileLoop(While.MH_initString, While.MH_predString, While.MH_stepString);
assertEquals(While.MT_string, loop.type());
assertEquals("a", loop.invoke());
}
@Test(dataProvider = "whileLoopTestData") publicstaticvoid testDoWhileLoop(MethodHandle MH_zero,
MethodHandle MH_pred,
MethodHandle MH_step,
String messageOrNull) throws Throwable { // int i = 0; do { ++i; } while (i < limit); return i; => limit try {
MethodHandle loop = MethodHandles.doWhileLoop(MH_zero, MH_step, MH_pred); assert messageOrNull == null; if (MH_step.type().equals(While.MH_step.type()))
assertEquals(While.MT_while, loop.type());
assertEquals(MH_step.type().dropParameterTypes(0, 1), loop.type()); while (loop.type().parameterCount() > 1) loop = snip(loop);
assertEquals(23, loop.invoke(23));
} catch (IllegalArgumentException iae) { assert messageOrNull != null; if (!messageOrNull.equals(iae.getMessage())) { // just issue a warning
System.out.println("*** "+messageOrNull+"\n != "+iae.getMessage());
}
}
}
@Test publicstaticvoid testCountedLoop() throws Throwable { // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s; => a variation on a well known theme
MethodHandle fit13 = MethodHandles.dropArguments(MethodHandles.constant(int.class, 13), 0, String.class);
MethodHandle loop = MethodHandles.countedLoop(fit13, Counted.MH_start, Counted.MH_step);
assertEquals(Counted.MT_counted, loop.type());
assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
}
@Test publicstaticvoid testCountedLoopVoidInit() throws Throwable {
MethodHandle fit5 = MethodHandles.constant(int.class, 5); for (int i = 0; i < 8; i++) {
MethodHandle zero = MethodHandles.zero(void.class);
MethodHandle init = fit5;
MethodHandle body = Counted.MH_printHello; boolean useNull = (i & 1) != 0, addInitArg = (i & 2) != 0, addBodyArg = (i & 4) != 0; if (useNull) zero = null; if (addInitArg) init = MethodHandles.dropArguments(init, 0, int.class); if (addBodyArg) body = MethodHandles.dropArguments(body, 1, int.class);
System.out.println("testCountedLoopVoidInit i="+i+" : "+Arrays.asList(init, zero, body));
MethodHandle loop = MethodHandles.countedLoop(init, zero, body);
MethodType expectedType = Counted.MT_countedPrinting; if (addInitArg || addBodyArg)
expectedType = expectedType.insertParameterTypes(0, int.class);
assertEquals(expectedType, loop.type()); if (addInitArg || addBodyArg)
loop.invoke(99); else
loop.invoke();
}
}
@Test publicstaticvoid testCountedArrayLoop() throws Throwable { // int[] a = new int[]{0}; for (int i = 0; i < 13; ++i) { ++a[0]; } => a[0] == 13
MethodHandle fit13 = MethodHandles.dropArguments(MethodHandles.constant(int.class, 13), 0, int[].class);
MethodHandle loop = MethodHandles.countedLoop(fit13, null, Counted.MH_stepUpdateArray);
assertEquals(Counted.MT_arrayCounted, loop.type()); int[] a = newint[]{0};
loop.invoke(a);
assertEquals(13, a[0]);
}
@DataProvider static Object[][] countedLoopBodyParameters() { Class<?> V = String.class, I = int.class, A = List.class; // return types are of these forms: // {count = int(A...), init = V(A...), body = V(V, I, A...)} returnnew Object[][] { // body leads determining A...
{methodType(I), methodType(V), methodType(V, V, I)},
{methodType(I), methodType(V), methodType(V, V, I, A)},
{methodType(I,A), methodType(V), methodType(V, V, I, A)},
{methodType(I), methodType(V,A), methodType(V, V, I, A)}, // body leads, with void V
{methodType(I), methodType(void.class), methodType(void.class, I)},
{methodType(I), methodType(void.class), methodType(void.class, I, A)},
{methodType(I,A), methodType(void.class), methodType(void.class, I, A)},
{methodType(I), methodType(void.class,A), methodType(void.class, I, A)}, // count leads determining A..., but only if body drops all A...
{methodType(I,A), methodType(V), methodType(V, V, I)},
{methodType(I,A), methodType(V,A), methodType(V, V, I)}, // count leads, with void V
{methodType(I,A), methodType(void.class), methodType(void.class, I)},
{methodType(I,A), methodType(void.class,A), methodType(void.class, I)},
};
}
@Test(dataProvider = "countedLoopBodyParameters") publicstaticvoid testCountedLoopBodyParameters(MethodType countType, MethodType initType, MethodType bodyType) throws Throwable {
MethodHandle loop = MethodHandles.countedLoop(
MethodHandles.empty(countType),
initType == null ? null : MethodHandles.empty(initType),
MethodHandles.empty(bodyType)); // The rule: If body takes the minimum number of parameters, then take what countType offers. // The initType has to just roll with whatever the other two agree on. int innerParams = (bodyType.returnType() == void.class ? 1 : 2);
MethodType expectType = bodyType.dropParameterTypes(0, innerParams); if (expectType.parameterCount() == 0)
expectType = expectType.insertParameterTypes(0, countType.parameterList());
assertEquals(expectType, loop.type());
}
@Test publicstaticvoid testCountedRangeLoop() throws Throwable { // String s = "Lambdaman!"; for (int i = -5; i < 8; ++i) { s = "na " + s; } return s; => a well known theme
MethodHandle fitm5 = MethodHandles.dropArguments(Counted.MH_m5, 0, String.class);
MethodHandle fit8 = MethodHandles.dropArguments(Counted.MH_8, 0, String.class);
MethodHandle loop = MethodHandles.countedLoop(fitm5, fit8, Counted.MH_start, Counted.MH_step);
assertEquals(Counted.MT_counted, loop.type());
assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
}
@Test publicstaticvoid testCountedLoopCounterInit() throws Throwable { // int x = 0; for (int i = 0; i < 5; ++i) { x += i; } return x; => 10 // (only if counter's first value in body is 0)
MethodHandle iter = MethodHandles.constant(int.class, 5);
MethodHandle init = MethodHandles.constant(int.class, 0);
MethodHandle body = Counted.MH_addCounter;
MethodHandle loop = MethodHandles.countedLoop(iter, init, body);
assertEquals(Counted.MT_counterInit, loop.type());
assertEquals(10, loop.invoke());
}
@Test(dataProvider = "iteratorInits") publicstaticvoid testIterateReverse(MethodHandle iterator) throws Throwable { // this test uses List as its loop state type; don't try to change that if (iterator != null)
iterator = iterator.asType(iterator.type().changeParameterType(0, List.class)); for (int i = 0; i < 4; i++) {
MethodHandle init = Iterate.MH_reverseInit, body = Iterate.MH_reverseStep; boolean snipInit = (i & 1) != 0, snipBody = (i & 2) != 0; if (snipInit) init = snip(init); if (snipBody) body = snip(body); if (!snipInit && snipBody && iterator == null) { // Body does not determine (A...), so the default guy just picks Iterable. // If body insisted on (List), the default guy would adjust himself. // Init has no authority to change the (A...), so must patch init. // All according to plan!
init = slap(snip(init), Iterable.class);
}
System.out.println("testIterateReverse i="+i+" : "+Arrays.asList(iterator, init, body));
MethodHandle loop = MethodHandles.iteratedLoop(iterator, init, body);
MethodType expectedType = Iterate.MT_reverse; if (iterator == null && i >= 2)
expectedType = expectedType.changeParameterType(0, Iterable.class);
assertEquals(expectedType, loop.type());
List<String> list = Arrays.asList("a", "b", "c", "d", "e");
List<String> reversedList = Arrays.asList("e", "d", "c", "b", "a");
assertEquals(reversedList, (List<String>) loop.invoke(list));
}
}
@Test(dataProvider = "iteratorInits") publicstaticvoid testIterateLength(MethodHandle iterator) throws Throwable {
MethodHandle body = Iterate.MH_lengthStep;
MethodHandle init = Iterate.MH_lengthInit;
MethodType expectedType = Iterate.MT_length; int barity = body.type().parameterCount(); Class<?> iteratorSource = iterator == null ? null : iterator.type().parameterType(0); if (iterator != null && iteratorSource != body.type().parameterType(barity-1)) { // adjust body to accept the other type
body = body.asType(body.type().changeParameterType(barity-1, iteratorSource));
init = init.asType(init.type().changeParameterType(0, iteratorSource));
expectedType = expectedType.changeParameterType(0, iteratorSource);
} for (;; init = snip(init)) {
System.out.println("testIterateLength.init = "+init);
MethodHandle loop = MethodHandles.iteratedLoop(iterator, init, body);
assertEquals(expectedType, loop.type());
List<Double> list = Arrays.asList(23.0, 148.0, 42.0);
assertEquals(list.size(), (int) loop.invoke(list)); if (init == null) break;
}
}
@Test(dataProvider = "iteratorInits") publicstaticvoid testIterateMap(MethodHandle iterator) throws Throwable {
MethodHandle body = Iterate.MH_mapStep;
MethodHandle init = Iterate.MH_mapInit;
MethodType expectedType = Iterate.MT_map; int barity = body.type().parameterCount(); Class<?> iteratorSource = iterator == null ? null : iterator.type().parameterType(0); if (iterator != null && iteratorSource != body.type().parameterType(barity-1)) { // adjust body to accept the other type
body = body.asType(body.type().changeParameterType(barity-1, iteratorSource));
init = init.asType(init.type().changeParameterType(0, iteratorSource));
expectedType = expectedType.changeParameterType(0, iteratorSource);
} for (; init != null; init = snip(init)) {
System.out.println("testIterateMap.init = "+init);
MethodHandle loop = MethodHandles.iteratedLoop(iterator, init, body);
assertEquals(expectedType, loop.type());
List<String> list = Arrays.asList("Hello", "world", "!");
List<String> upList = Arrays.asList("HELLO", "WORLD", "!");
assertEquals(upList, (List<String>) loop.invoke(list));
}
}
@Test(dataProvider = "iteratorInits") publicstaticvoid testIteratePrint(MethodHandle iterator) throws Throwable {
MethodHandle body = Iterate.MH_printStep;
MethodType expectedType = Iterate.MT_print; int barity = body.type().parameterCount(); Class<?> iteratorSource = iterator == null ? null : iterator.type().parameterType(0); if (iterator != null && iteratorSource != body.type().parameterType(barity-1)) { // adjust body to accept the other type
body = body.asType(body.type().changeParameterType(barity-1, iteratorSource));
expectedType = expectedType.changeParameterType(0, iteratorSource);
}
MethodHandle loop = MethodHandles.iteratedLoop(iterator, null, body);
assertEquals(expectedType, loop.type());
loop.invoke(Arrays.asList("hello", "world"));
}
@Test(dataProvider = "wrongIteratorTypes") publicstaticvoid testIterateVoidIterator(Class<?> it) { boolean caught = false;
MethodType v = methodType(it); try {
MethodHandles.iteratedLoop(MethodHandles.empty(v), null, MethodHandles.empty(v));
} catch(IllegalArgumentException iae) {
assertEqualsFIXME("iteratedLoop first argument must have Iterator return type", iae.getMessage());
caught = true;
}
assertTrue(caught);
}
@Test(dataProvider = "iteratorInits") publicstaticvoid testIterateVoidInit(MethodHandle iterator) throws Throwable { // this test uses List as its loop state type; don't try to change that if (iterator != null)
iterator = iterator.asType(iterator.type().changeParameterType(0, List.class));
MethodHandle loop = MethodHandles.iteratedLoop(iterator, Iterate.MH_voidInit, Iterate.MH_printStep);
assertEquals(Iterate.MT_print, loop.type());
loop.invoke(Arrays.asList("hello", "world"));
}
static MethodHandle permute(MethodHandle h) { // The handles representing virtual methods need to be rearranged to match the required order of arguments // (loop-local state comes first, then loop arguments). As the receiver comes first in the signature but is // a loop argument, it must be moved to the appropriate position in the signature. return MethodHandles.permuteArguments(h,
methodType(h.type().returnType(), int.class, int.class, LOOP_WITH_VIRTUALS, int.class), 2, 0, 1, 3);
}
}
staticclassWhile {
staticint zero(int limit) { return 0;
}
staticboolean pred(int i, int limit) { return i < limit;
}
staticint step(int i, int limit) { return i + 1;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.