This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
LoopCombinatorTest.java
1382 lines (1205 loc) · 61.6 KB
/
LoopCombinatorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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
* @bug 8139885
* @bug 8150635
* @bug 8150956
* @bug 8150957
* @bug 8151179
* @bug 8152667
* @bug 8153637
* @bug 8154751
* @bug 8154754
* @bug 8167974
* @run testng/othervm -ea -esa test.java.lang.invoke.LoopCombinatorTest
*/
package test.java.lang.invoke;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.util.*;
import static java.lang.invoke.MethodType.methodType;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.*;
/**
* Tests for the loop combinators introduced in JEP 274.
*/
public class LoopCombinatorTest {
static final Lookup LOOKUP = MethodHandles.lookup();
@Test
public static void testLoopFac() throws Throwable {
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 loop = MethodHandles.loop(counterClause, accumulatorClause);
assertEquals(Fac.MT_fac, loop.type());
assertEquals(120, loop.invoke(5));
}
@Test
public static void 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
public static void 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
public static void 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
public static void 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
public static void 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
public static void 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
public static void 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
public static void 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
public static void 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));
}
@DataProvider
static Object[][] negativeTestData() {
MethodHandle i0 = MethodHandles.constant(int.class, 0);
MethodHandle ii = MethodHandles.dropArguments(i0, 0, int.class, int.class);
MethodHandle id = MethodHandles.dropArguments(i0, 0, int.class, double.class);
MethodHandle i3 = MethodHandles.dropArguments(i0, 0, int.class, int.class, int.class);
List<MethodHandle> inits = Arrays.asList(ii, id, i3);
List<Class<?>> ints3 = Arrays.asList(int.class, int.class, int.class);
List<Class<?>> ints4 = Arrays.asList(int.class, int.class, int.class, int.class);
List<MethodHandle> finis = Arrays.asList(Fac.MH_fin, Fac.MH_inc, Counted.MH_step);
List<MethodHandle> preds1 = Arrays.asList(null, null, null);
List<MethodHandle> preds2 = Arrays.asList(null, Fac.MH_fin, null);
MethodHandle eek = MethodHandles.dropArguments(i0, 0, int.class, int.class, double.class);
List<MethodHandle> nesteps = Arrays.asList(Fac.MH_inc, eek, Fac.MH_dot);
List<MethodHandle> nepreds = Arrays.asList(null, Fac.MH_pred, null);
List<MethodHandle> nefinis = Arrays.asList(null, Fac.MH_fin, null);
List<MethodHandle> lvsteps = Arrays.asList(LoopWithVirtuals.MH_inc, LoopWithVirtuals.MH_mult);
List<MethodHandle> lvpreds = Arrays.asList(null, LoopWithVirtuals.MH_pred);
List<MethodHandle> lvfinis = Arrays.asList(null, LoopWithVirtuals.MH_fin);
return new Object[][] {
{null, "null or no clauses passed"},
{new MethodHandle[][]{}, "null or no clauses passed"},
{new MethodHandle[][]{{null, Fac.MH_inc}, {Fac.MH_one, null, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}},
"All loop clauses must be represented as MethodHandle arrays with at most 4 elements."},
{new MethodHandle[][]{{null, Fac.MH_inc}, null}, "null clauses are not allowed"},
{new MethodHandle[][]{{Fac.MH_zero, Fac.MH_dot}},
"clause 0: init and step return types must match: int != void"},
{new MethodHandle[][]{{ii}, {id}, {i3}},
"found non-effectively identical init parameter type lists: " + inits +
" (common suffix: " + ints3 + ")"},
{new MethodHandle[][]{{null, Fac.MH_inc, null, Fac.MH_fin}, {null, Fac.MH_inc, null, Fac.MH_inc},
{null, Counted.MH_start, null, Counted.MH_step}},
"found non-identical finalizer return types: " + finis + " (return type: int)"},
{new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, null, Fac.MH_fin},
{null, Fac.MH_dot}}, "no predicate found: " + preds1},
{new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, Fac.MH_fin, Fac.MH_fin},
{null, Fac.MH_dot}}, "predicates must have boolean return type: " + preds2},
{new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, eek, Fac.MH_pred, Fac.MH_fin},
{null, Fac.MH_dot}},
"found non-effectively identical parameter type lists:\nstep: " + nesteps +
"\npred: " + nepreds + "\nfini: " + nefinis + " (common parameter sequence: " + ints3 + ")"},
{new MethodHandle[][]{{null, LoopWithVirtuals.MH_inc},
{LoopWithVirtuals.MH_one, LoopWithVirtuals.MH_mult, LoopWithVirtuals.MH_pred, LoopWithVirtuals.MH_fin}},
"found non-effectively identical parameter type lists:\nstep: " + lvsteps +
"\npred: " + lvpreds + "\nfini: " + lvfinis + " (common parameter sequence: " + ints4 + ")"}
};
}
static final MethodHandle MH_loop;
static {
try {
MH_loop = LOOKUP.findStatic(MethodHandles.class, "loop", methodType(MethodHandle.class, MethodHandle[][].class));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new ExceptionInInitializerError(e);
}
}
@Test(dataProvider = "negativeTestData")
public static void testLoopNegative(MethodHandle[][] clauses, String expectedMessage) throws Throwable {
boolean caught = false;
try {
MH_loop.invokeWithArguments((Object[]) clauses);
} catch (IllegalArgumentException iae) {
assertEquals(expectedMessage, iae.getMessage());
caught = true;
}
assertTrue(caught);
}
@Test(dataProvider = "whileLoopTestData")
public static void testWhileLoop(MethodHandle MH_zero,
MethodHandle MH_pred,
MethodHandle MH_step,
String messageOrNull) throws Throwable {
// int i = 0; while (i < limit) { ++i; } return i; => limit
try {
MethodHandle loop = MethodHandles.whileLoop(MH_zero, MH_pred, MH_step);
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;
assertEqualsFIXME(messageOrNull, iae.getMessage());
}
}
static void 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)
;
return new 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) return null; // special case for optional args
Class<?> argType = mh.type().parameterType(argPos);
Object zero;
try {
zero = MethodHandles.zero(argType).invoke();
} catch (Throwable ex) {
throw new 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
public static void 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")
public static void 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
public static void testDoWhileBadInit() throws Throwable {
boolean caught = false;
try {
While w = new While();
MethodHandle loop = MethodHandles.doWhileLoop(MethodHandles.empty(methodType(char.class)),
While.MH_voidBody.bindTo(w),
While.MH_voidPred.bindTo(w));
} catch (IllegalArgumentException iae) {
assertEquals("loop initializer must match: ()char != (int)void", iae.getMessage());
caught = true;
}
assertTrue(caught);
}
@Test
public static void testWhileZip() throws Throwable {
MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zipInitZip, While.MH_zipStep, While.MH_zipPred);
assertEquals(While.MT_zip, loop.type());
List<String> a = Arrays.asList("a", "b", "c", "d");
List<String> b = Arrays.asList("e", "f", "g", "h");
List<String> zipped = Arrays.asList("a", "e", "b", "f", "c", "g", "d", "h");
assertEquals(zipped, (List<String>) loop.invoke(a.iterator(), b.iterator()));
}
@Test
public static void testWhileBadInit() throws Throwable {
boolean caught = false;
try {
While w = new While();
MethodHandle loop = MethodHandles.whileLoop(MethodHandles.empty(methodType(void.class, char.class)),
While.MH_voidPred.bindTo(w),
While.MH_voidBody.bindTo(w));
} catch (IllegalArgumentException iae) {
assertEquals("loop initializer must match: (char)void != (int)void", iae.getMessage());
caught = true;
}
assertTrue(caught);
}
@Test
public static void testWhileVoidInit() throws Throwable {
While w = new While();
int v = 5;
MethodHandle loop = MethodHandles.whileLoop(While.MH_voidInit.bindTo(w), While.MH_voidPred.bindTo(w),
While.MH_voidBody.bindTo(w));
assertEquals(While.MT_void, loop.type());
loop.invoke(v);
assertEquals(v, w.i);
}
@Test
public static void testDoWhileVoidInit() throws Throwable {
While w = new While();
int v = 5;
MethodHandle loop = MethodHandles.doWhileLoop(While.MH_voidInit.bindTo(w), While.MH_voidBody.bindTo(w),
While.MH_voidPred.bindTo(w));
assertEquals(While.MT_void, loop.type());
loop.invoke(v);
assertEquals(v, w.i);
}
@DataProvider
static Object[][] nullArgs() {
MethodHandle c = MethodHandles.constant(int.class, 1);
return new Object[][]{{null, c}, {c, null}};
}
@Test(dataProvider = "nullArgs", expectedExceptions = NullPointerException.class)
public static void testWhileNullArgs(MethodHandle pred, MethodHandle body) {
MethodHandles.whileLoop(null, pred, body);
}
@Test(dataProvider = "nullArgs", expectedExceptions = NullPointerException.class)
public static void testDoWhileNullArgs(MethodHandle body, MethodHandle pred) {
MethodHandles.whileLoop(null, body, pred);
}
@Test
public static void 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
public static void 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
public static void 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 = new int[]{0};
loop.invoke(a);
assertEquals(13, a[0]);
}
@Test
public static void testCountedPrintingLoop() throws Throwable {
MethodHandle fit5 = MethodHandles.constant(int.class, 5);
MethodHandle loop = MethodHandles.countedLoop(fit5, null, Counted.MH_printHello);
assertEquals(Counted.MT_countedPrinting, loop.type());
loop.invoke();
}
@Test(expectedExceptions = NullPointerException.class)
public static void testCountedLoopNullBody() throws Throwable {
MethodHandle h5 = MethodHandles.constant(int.class, 5);
MethodHandle h13 = MethodHandles.constant(int.class, 13);
MethodHandle loop = MethodHandles.countedLoop(h5, h13, null);
assertEquals(methodType(int.class), loop.type());
assertEquals(13, loop.invoke());
}
@Test(expectedExceptions = NullPointerException.class)
public static void testCountedLoopNullIterations() throws Throwable {
MethodHandle loop = MethodHandles.countedLoop(null, null, null);
assertEquals(methodType(void.class), loop.type());
loop.invoke();
}
@Test(expectedExceptions = NullPointerException.class)
public static void testCountedLoopNullInitAndBody() throws Throwable {
MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5), null, null);
assertEquals(methodType(void.class), loop.type());
loop.invoke();
}
@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...)}
return new 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")
public static void 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(dataProvider = "countedLoopBodyParameters")
public static void testCountedLoopBodyParametersNullInit(MethodType countType, MethodType initType, MethodType bodyType) throws Throwable {
testCountedLoopBodyParameters(countType, null, bodyType);
}
@Test
public static void testCountedLoopStateInitializedToNull() throws Throwable {
MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5),
MethodHandles.empty(methodType(String.class)), Counted.MH_stateBody);
assertEquals(Counted.MT_bodyDeterminesState, loop.type());
assertEquals("sssssnull01234", loop.invoke());
}
@Test
public static void testCountedLoopArgsDefinedByIterations() throws Throwable {
MethodHandle iterations =
MethodHandles.dropArguments(MethodHandles.constant(int.class, 3), 0, String.class);
MethodHandle loop = MethodHandles.countedLoop(iterations,
MethodHandles.empty(iterations.type().changeReturnType(String.class)), Counted.MH_append);
assertEquals(Counted.MT_iterationsDefineArgs, loop.type());
assertEquals("hello012", loop.invoke("hello"));
}
@Test
public static void 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
public static void 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
public static void testCountedLoopEmpty() throws Throwable {
// for (int i = 0; i < 5; ++i) { /* empty */ }
MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5), null,
MethodHandles.empty(methodType(void.class, int.class)));
assertEquals(methodType(void.class), loop.type());
loop.invoke();
}
@Test
public static void testCountedRangeLoopEmpty() throws Throwable {
// for (int i = -5; i < 5; ++i) { /* empty */ }
MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, -5),
MethodHandles.constant(int.class, 5), null, MethodHandles.empty(methodType(void.class, int.class)));
assertEquals(methodType(void.class), loop.type());
loop.invoke();
}
@DataProvider
static Object[][] countedLoopNegativeData() {
MethodHandle dummy = MethodHandles.zero(void.class);
MethodHandle one = MethodHandles.constant(int.class, 1);
MethodHandle oneString = MethodHandles.dropArguments(one, 0, String.class);
MethodHandle oneDouble = MethodHandles.dropArguments(one, 0, double.class);
return new Object[][]{
{dummy, one, dummy, dummy, String.format("start/end must return int %s, %s", dummy, one)},
{one, dummy, dummy, dummy, String.format("start/end must return int %s, %s", one, dummy)},
{oneString, oneDouble, dummy, dummy,
String.format("start and end parameter types must match: %s != %s", oneString.type(),
oneDouble.type())},
{oneString, oneString, dummy, dummy,
String.format("start/end and init parameter types must match: %s != %s", oneString.type(),
dummy.type())},
{one, one, null, dummy, String.format("actual and expected body signatures must match: %s != %s",
dummy.type(), dummy.type().appendParameterTypes(int.class))}
};
}
@Test(dataProvider = "countedLoopNegativeData")
public static void testCountedLoopNegative(MethodHandle start, MethodHandle end, MethodHandle init,
MethodHandle body, String msg) {
if (true) return; //%%%FIXME%%%%
boolean caught = false;
try {
MethodHandles.countedLoop(start, end, init, body);
} catch (IllegalArgumentException iae) {
assertEquals(msg, iae.getMessage());
caught = true;
}
assertTrue(caught);
}
@Test
public static void testIterateSum() throws Throwable {
// Integer[] a = new Integer[]{1,2,3,4,5,6}; int sum = 0; for (int e : a) { sum += e; } return sum; => 21
MethodHandle loop = MethodHandles.iteratedLoop(Iterate.MH_sumIterator, Iterate.MH_sumInit, Iterate.MH_sumStep);
assertEquals(Iterate.MT_sum, loop.type());
assertEquals(21, loop.invoke(new Integer[]{1, 2, 3, 4, 5, 6}));
}
@DataProvider
static Object[][] iteratorInits() {
return new Object[][]{{Iterate.MH_iteratorFromList}, {Iterate.MH_iteratorFromIterable}, {null}};
}
@Test(dataProvider = "iteratorInits")
public static void 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")
public static void 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")
public static void 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")
public static void 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(expectedExceptions = NullPointerException.class)
public static void testIterateNullBody() {
MethodHandles.iteratedLoop(MethodHandles.empty(methodType(Iterator.class, int.class)),
MethodHandles.identity(int.class), null);
}
@DataProvider
static Object[][] wrongIteratorTypes() {
return new Object[][]{{void.class}, {Object.class}, {Iterable.class}};
}
@Test(dataProvider = "wrongIteratorTypes")
public static void 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")
public static void 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"));
}
@DataProvider
static Object[][] iterateParameters() {
MethodType i = methodType(int.class);
MethodType sil_v = methodType(void.class, String.class, int.class, List.class);
MethodType isl_i = methodType(int.class, int.class, String.class, List.class);
MethodType isli_i = methodType(int.class, int.class, String.class, List.class, int.class);
MethodType sl_v = methodType(void.class, String.class, List.class);
MethodType sli_v = methodType(void.class, String.class, List.class, int.class);
MethodType l_it = methodType(Iterator.class, List.class);
MethodType li_i = methodType(int.class, List.class, int.class);
MethodType li_it = methodType(Iterator.class, List.class, int.class);
MethodType il_it = methodType(Iterator.class, int.class, List.class);
MethodType l_i = methodType(int.class, List.class);
return new Object[][]{
{l_it, null, sl_v, ""},
{l_it, l_i, isl_i, ""},
{l_it, null, sl_v, ""},
{li_it, li_i, isli_i, ""},
{null, null, sil_v, "inferred first loop argument must inherit from Iterable: int"},
{il_it, null, sil_v, ""},
{li_it, null, sli_v, ""},
{sl_v, null, sl_v, "iteratedLoop first argument must have Iterator return type"},
{li_it, l_it, sl_v,
String.format("iterator and init parameter lists must match: %s != %s", li_it, l_it)},
{li_it, li_i, isl_i,
String.format("body types (regard parameter types after index 0, and result type) must match: %s != %s",
isl_i, isl_i.dropParameterTypes(0, 1).appendParameterTypes(int.class))}
};
}
@Test(dataProvider = "iterateParameters")
public static void testIterateParameters(MethodType it, MethodType in, MethodType bo, String msg) {
boolean negative = !msg.isEmpty();
MethodHandle iterator = it == null ? null : MethodHandles.empty(it);
MethodHandle init = in == null ? null : MethodHandles.empty(in);
boolean caught = false;
MethodHandle loop = null;
try {
loop = MethodHandles.iteratedLoop(iterator, init, MethodHandles.empty(bo));
} catch (Throwable t) {
if (!negative) {
throw t;
}
assertEqualsFIXME(msg, t.getMessage());
caught = true;
}
if (negative) {
assertTrue(caught);
} else {
MethodType lt = loop.type();
if (it == null && in == null) {
assertEquals(bo.dropParameterTypes(0, 1), lt);
} else if (it == null) {
if (in.parameterCount() == 0) {
assertEquals(bo.dropParameterTypes(0, in.returnType() == void.class ? 1 : 2), lt);
} else {
assertEquals(methodType(bo.returnType(), in.parameterArray()), lt);
}
} else if (in == null) {
assertEquals(methodType(bo.returnType(), it.parameterArray()), lt);
} else if (it.parameterCount() > in.parameterCount()) {
assertEquals(methodType(bo.returnType(), it.parameterArray()), lt);
} else if (it.parameterCount() < in.parameterCount()) {
assertEquals(methodType(bo.returnType(), in.parameterArray()), lt);
} else {
// both it, in present; with equal parameter list lengths
assertEquals(it.parameterList(), lt.parameterList());
assertEquals(in.parameterList(), lt.parameterList());
assertEquals(bo.returnType(), lt.returnType());
}
}
}
@Test
public static void testIteratorSubclass() throws Throwable {
MethodHandle loop = MethodHandles.iteratedLoop(MethodHandles.empty(methodType(BogusIterator.class, List.class)),
null, MethodHandles.empty(methodType(void.class, String.class, List.class)));
assertEquals(methodType(void.class, List.class), loop.type());
}
static class BogusIterator implements Iterator {
@Override
public boolean hasNext() {
return false;
}
@Override
public Object next() {
return null;
}
}
static class Empty {
static void f() { }
static boolean pred() {
return false;
}
static int c() {
return 23;
}
static final Class<Empty> EMPTY = Empty.class;
static final MethodType MT_f = methodType(void.class);
static final MethodType MT_pred = methodType(boolean.class);
static final MethodType MT_c = methodType(int.class);
static final MethodHandle MH_f;
static final MethodHandle MH_pred;
static final MethodHandle MH_c;
static {
try {
MH_f = LOOKUP.findStatic(EMPTY, "f", MT_f);
MH_pred = LOOKUP.findStatic(EMPTY, "pred", MT_pred);
MH_c = LOOKUP.findStatic(EMPTY, "c", MT_c);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
}
static class Fac {
static int zero(int k) {
return 0;
}
static int one(int k) {
return 1;
}
static boolean pred(int i, int acc, int k) {
return i < k;
}
static int inc(int i, int acc, int k) {
return i + 1;
}
static int mult(int i, int acc, int k) {
return i * acc;
}
static void dot(int i, int acc, int k) {
System.out.print('.');
}
static int fin(int i, int acc, int k) {
return acc;
}
static final Class<Fac> FAC = Fac.class;
static final MethodType MT_init = methodType(int.class, int.class);
static final MethodType MT_fn = methodType(int.class, int.class, int.class, int.class);
static final MethodType MT_dot = methodType(void.class, int.class, int.class, int.class);
static final MethodType MT_pred = methodType(boolean.class, int.class, int.class, int.class);
static final MethodHandle MH_zero;
static final MethodHandle MH_one;
static final MethodHandle MH_pred;
static final MethodHandle MH_inc;
static final MethodHandle MH_mult;
static final MethodHandle MH_dot;
static final MethodHandle MH_fin;
static final MethodType MT_fac = methodType(int.class, int.class);
static {
try {
MH_zero = LOOKUP.findStatic(FAC, "zero", MT_init);
MH_one = LOOKUP.findStatic(FAC, "one", MT_init);
MH_pred = LOOKUP.findStatic(FAC, "pred", MT_pred);
MH_inc = LOOKUP.findStatic(FAC, "inc", MT_fn);
MH_mult = LOOKUP.findStatic(FAC, "mult", MT_fn);
MH_dot = LOOKUP.findStatic(FAC, "dot", MT_dot);
MH_fin = LOOKUP.findStatic(FAC, "fin", MT_fn);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
}
static class Loop {
static int inc(int i, int k) {
return i + 1;
}
static boolean pred(int i, int k) {
return i < k;
}
static int fin(int i, int k) {
return k;
}
static final Class<Loop> LOOP = Loop.class;
static final MethodType MT_inc = methodType(int.class, int.class, int.class);
static final MethodType MT_pred = methodType(boolean.class, int.class, int.class);
static final MethodType MT_fin = methodType(int.class, int.class, int.class);
static final MethodHandle MH_inc;
static final MethodHandle MH_pred;
static final MethodHandle MH_fin;
static final MethodType MT_loop = methodType(int.class, int.class);
static {
try {