forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArbitrary.java
More file actions
1654 lines (1545 loc) · 71.5 KB
/
Arbitrary.java
File metadata and controls
1654 lines (1545 loc) · 71.5 KB
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
package fj.test;
import fj.Effect;
import fj.F;
import fj.F2;
import fj.F3;
import fj.F4;
import fj.F5;
import fj.F6;
import fj.F7;
import fj.F8;
import fj.Function;
import static fj.Function.compose;
import static fj.P.p;
import fj.P1;
import fj.P2;
import fj.P3;
import fj.P4;
import fj.P5;
import fj.P6;
import fj.P7;
import fj.P8;
import fj.data.Array;
import fj.data.Either;
import static fj.data.Either.left;
import static fj.data.Either.right;
import static fj.data.Enumerator.charEnumerator;
import fj.data.List;
import static fj.data.List.asString;
import static fj.data.List.list;
import fj.data.Option;
import static fj.data.Option.some;
import fj.data.Stream;
import static fj.data.Stream.range;
import static fj.test.Gen.choose;
import static fj.test.Gen.elements;
import static fj.test.Gen.fail;
import static fj.test.Gen.frequency;
import static fj.test.Gen.listOf;
import static fj.test.Gen.oneOf;
import static fj.test.Gen.promote;
import static fj.test.Gen.sized;
import static fj.test.Gen.value;
import static java.lang.Math.abs;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Date;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Locale;
import static java.util.Locale.getAvailableLocales;
import java.util.PriorityQueue;
import java.util.Properties;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.WeakHashMap;
import static java.util.EnumSet.copyOf;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;
/**
* The type used to generate arbitrary values of the given type parameter (<code>A</code>). Common
* arbitrary implementations are provided.
*
* @version %build.number%
*/
public final class Arbitrary<A> {
/**
* The generator associated with this arbitrary.
*/
@SuppressWarnings({"PublicField"})
public final Gen<A> gen;
private Arbitrary(final Gen<A> gen) {
this.gen = gen;
}
/**
* Constructs and arbitrary with the given generator.
*
* @param g The generator to construct an arbitrary with.
* @return A new arbitrary that uses the given generator.
*/
public static <A> Arbitrary<A> arbitrary(final Gen<A> g) {
return new Arbitrary<A>(g);
}
/**
* An arbitrary for functions.
*
* @param c The coarbitrary for the function domain.
* @param a The arbitrary for the function codomain.
* @return An arbitrary for functions.
*/
public static <A, B> Arbitrary<F<A, B>> arbF(final Coarbitrary<A> c, final Arbitrary<B> a) {
return arbitrary(promote(new F<A, Gen<B>>() {
public Gen<B> f(final A x) {
return c.coarbitrary(x, a.gen);
}
}));
}
/**
* An arbitrary for functions.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for functions.
*/
public static <A, B> Arbitrary<F<A, B>> arbFInvariant(final Arbitrary<B> a) {
return arbitrary(a.gen.map(Function.<A, B>constant()));
}
/**
* An arbitrary for function-2.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-2.
*/
public static <A, B, C> Arbitrary<F2<A, B, C>> arbF2(final Coarbitrary<A> ca, final Coarbitrary<B> cb,
final Arbitrary<C> a) {
return arbitrary(arbF(ca, arbF(cb, a)).gen.map(Function.<A, B, C>uncurryF2()));
}
/**
* An arbitrary for function-2.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-2.
*/
public static <A, B, C> Arbitrary<F2<A, B, C>> arbF2Invariant(final Arbitrary<C> a) {
return arbitrary(a.gen.map(
compose(Function.<A, B, C>uncurryF2(), compose(Function.<A, F<B, C>>constant(), Function.<B, C>constant()))));
}
/**
* An arbitrary for function-3.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-3.
*/
public static <A, B, C, D> Arbitrary<F3<A, B, C, D>> arbF3(final Coarbitrary<A> ca, final Coarbitrary<B> cb,
final Coarbitrary<C> cc, final Arbitrary<D> a) {
return arbitrary(arbF(ca, arbF(cb, arbF(cc, a))).gen.map(Function.<A, B, C, D>uncurryF3()));
}
/**
* An arbitrary for function-3.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-3.
*/
public static <A, B, C, D> Arbitrary<F3<A, B, C, D>> arbF3Invariant(final Arbitrary<D> a) {
return arbitrary(a.gen.map(compose(Function.<A, B, C, D>uncurryF3(), compose(Function.<A, F<B, F<C, D>>>constant(),
compose(
Function.<B, F<C, D>>constant(),
Function.<C, D>constant())))));
}
/**
* An arbitrary for function-4.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param cd A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-4.
*/
public static <A, B, C, D, E> Arbitrary<F4<A, B, C, D, E>> arbF4(final Coarbitrary<A> ca, final Coarbitrary<B> cb,
final Coarbitrary<C> cc, final Coarbitrary<D> cd,
final Arbitrary<E> a) {
return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, a)))).gen.map(Function.<A, B, C, D, E>uncurryF4()));
}
/**
* An arbitrary for function-4.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-4.
*/
public static <A, B, C, D, E> Arbitrary<F4<A, B, C, D, E>> arbF4Invariant(final Arbitrary<E> a) {
return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E>uncurryF4(),
compose(Function.<A, F<B, F<C, F<D, E>>>>constant(),
compose(Function.<B, F<C, F<D, E>>>constant(),
compose(Function.<C, F<D, E>>constant(),
Function.<D, E>constant()))))));
}
/**
* An arbitrary for function-5.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param cd A coarbitrary for the part of the domain of the function.
* @param ce A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-5.
*/
public static <A, B, C, D, E, F$> Arbitrary<F5<A, B, C, D, E, F$>> arbF5(final Coarbitrary<A> ca,
final Coarbitrary<B> cb,
final Coarbitrary<C> cc,
final Coarbitrary<D> cd,
final Coarbitrary<E> ce,
final Arbitrary<F$> a) {
return arbitrary(
arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, a))))).gen.map(Function.<A, B, C, D, E, F$>uncurryF5()));
}
/**
* An arbitrary for function-5.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-5.
*/
public static <A, B, C, D, E, F$> Arbitrary<F5<A, B, C, D, E, F$>> arbF5Invariant(final Arbitrary<F$> a) {
return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$>uncurryF5(),
compose(Function.<A, F<B, F<C, F<D, F<E, F$>>>>>constant(),
compose(Function.<B, F<C, F<D, F<E, F$>>>>constant(),
compose(Function.<C, F<D, F<E, F$>>>constant(),
compose(Function.<D, F<E, F$>>constant(),
Function.<E, F$>constant())))))));
}
/**
* An arbitrary for function-6.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param cd A coarbitrary for the part of the domain of the function.
* @param ce A coarbitrary for the part of the domain of the function.
* @param cf A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-6.
*/
public static <A, B, C, D, E, F$, G> Arbitrary<F6<A, B, C, D, E, F$, G>> arbF6(final Coarbitrary<A> ca,
final Coarbitrary<B> cb,
final Coarbitrary<C> cc,
final Coarbitrary<D> cd,
final Coarbitrary<E> ce,
final Coarbitrary<F$> cf,
final Arbitrary<G> a) {
return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, a)))))).gen.map(
Function.<A, B, C, D, E, F$, G>uncurryF6()));
}
/**
* An arbitrary for function-6.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-6.
*/
public static <A, B, C, D, E, F$, G> Arbitrary<F6<A, B, C, D, E, F$, G>> arbF6Invariant(final Arbitrary<G> a) {
return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$, G>uncurryF6(),
compose(Function.<A, F<B, F<C, F<D, F<E, F<F$, G>>>>>>constant(),
compose(Function.<B, F<C, F<D, F<E, F<F$, G>>>>>constant(),
compose(Function.<C, F<D, F<E, F<F$, G>>>>constant(),
compose(Function.<D, F<E, F<F$, G>>>constant(),
compose(Function.<E, F<F$, G>>constant(),
Function.<F$, G>constant()))))))));
}
/**
* An arbitrary for function-7.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param cd A coarbitrary for the part of the domain of the function.
* @param ce A coarbitrary for the part of the domain of the function.
* @param cf A coarbitrary for the part of the domain of the function.
* @param cg A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-7.
*/
public static <A, B, C, D, E, F$, G, H> Arbitrary<F7<A, B, C, D, E, F$, G, H>> arbF7(final Coarbitrary<A> ca,
final Coarbitrary<B> cb,
final Coarbitrary<C> cc,
final Coarbitrary<D> cd,
final Coarbitrary<E> ce,
final Coarbitrary<F$> cf,
final Coarbitrary<G> cg,
final Arbitrary<H> a) {
return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, arbF(cg, a))))))).gen.map(
Function.<A, B, C, D, E, F$, G, H>uncurryF7()));
}
/**
* An arbitrary for function-7.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-7.
*/
public static <A, B, C, D, E, F$, G, H> Arbitrary<F7<A, B, C, D, E, F$, G, H>> arbF7Invariant(final Arbitrary<H> a) {
return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$, G, H>uncurryF7(),
compose(Function.<A, F<B, F<C, F<D, F<E, F<F$, F<G, H>>>>>>>constant(),
compose(Function.<B, F<C, F<D, F<E, F<F$, F<G, H>>>>>>constant(),
compose(Function.<C, F<D, F<E, F<F$, F<G, H>>>>>constant(),
compose(Function.<D, F<E, F<F$, F<G, H>>>>constant(),
compose(Function.<E, F<F$, F<G, H>>>constant(),
compose(Function.<F$, F<G, H>>constant(),
Function.<G, H>constant())))))))));
}
/**
* An arbitrary for function-8.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param cd A coarbitrary for the part of the domain of the function.
* @param ce A coarbitrary for the part of the domain of the function.
* @param cf A coarbitrary for the part of the domain of the function.
* @param cg A coarbitrary for the part of the domain of the function.
* @param ch A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-8.
*/
public static <A, B, C, D, E, F$, G, H, I> Arbitrary<F8<A, B, C, D, E, F$, G, H, I>> arbF8(final Coarbitrary<A> ca,
final Coarbitrary<B> cb,
final Coarbitrary<C> cc,
final Coarbitrary<D> cd,
final Coarbitrary<E> ce,
final Coarbitrary<F$> cf,
final Coarbitrary<G> cg,
final Coarbitrary<H> ch,
final Arbitrary<I> a) {
return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, arbF(cg, arbF(ch, a)))))))).gen.map(
Function.<A, B, C, D, E, F$, G, H, I>uncurryF8()));
}
/**
* An arbitrary for function-8.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-8.
*/
public static <A, B, C, D, E, F$, G, H, I> Arbitrary<F8<A, B, C, D, E, F$, G, H, I>> arbF8Invariant(
final Arbitrary<I> a) {
return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$, G, H, I>uncurryF8(),
compose(Function.<A, F<B, F<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>>>constant(),
compose(Function.<B, F<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>>constant(),
compose(Function.<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>constant(),
compose(
Function.<D, F<E, F<F$, F<G, F<H, I>>>>>constant(),
compose(Function.<E, F<F$, F<G, F<H, I>>>>constant(),
compose(
Function.<F$, F<G, F<H, I>>>constant(),
compose(Function.<G, F<H, I>>constant(),
Function.<H, I>constant()))))))))));
}
/**
* An arbitrary implementation for boolean values.
*/
public static final Arbitrary<Boolean> arbBoolean = arbitrary(elements(true, false));
/**
* An arbitrary implementation for integer values.
*/
public static final Arbitrary<Integer> arbInteger = arbitrary(sized(new F<Integer, Gen<Integer>>() {
public Gen<Integer> f(final Integer i) {
return choose(-i, i);
}
}));
/**
* An arbitrary implementation for integer values that checks boundary values <code>(0, 1, -1,
* max, min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link
* #arbInteger} the remainder of the time (93%).
*/
public static final Arbitrary<Integer> arbIntegerBoundaries = arbitrary(sized(new F<Integer, Gen<Integer>>() {
@SuppressWarnings("unchecked")
public Gen<Integer> f(final Integer i) {
return frequency(list(p(1, value(0)),
p(1, value(1)),
p(1, value(-1)),
p(1, value(Integer.MAX_VALUE)),
p(1, value(Integer.MIN_VALUE)),
p(1, value(Integer.MAX_VALUE - 1)),
p(1, value(Integer.MIN_VALUE + 1)),
p(93, arbInteger.gen)));
}
}));
/**
* An arbitrary implementation for long values.
*/
public static final Arbitrary<Long> arbLong =
arbitrary(arbInteger.gen.bind(arbInteger.gen, new F<Integer, F<Integer, Long>>() {
public F<Integer, Long> f(final Integer i1) {
return new F<Integer, Long>() {
public Long f(final Integer i2) {
return (long) i1 << 32L & i2;
}
};
}
}));
/**
* An arbitrary implementation for long values that checks boundary values <code>(0, 1, -1, max,
* min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbLong}
* the remainder of the time (93%).
*/
public static final Arbitrary<Long> arbLongBoundaries = arbitrary(sized(new F<Integer, Gen<Long>>() {
@SuppressWarnings("unchecked")
public Gen<Long> f(final Integer i) {
return frequency(list(p(1, value(0L)),
p(1, value(1L)),
p(1, value(-1L)),
p(1, value(Long.MAX_VALUE)),
p(1, value(Long.MIN_VALUE)),
p(1, value(Long.MAX_VALUE - 1L)),
p(1, value(Long.MIN_VALUE + 1L)),
p(93, arbLong.gen)));
}
}));
/**
* An arbitrary implementation for byte values.
*/
public static final Arbitrary<Byte> arbByte = arbitrary(arbInteger.gen.map(new F<Integer, Byte>() {
public Byte f(final Integer i) {
return (byte) i.intValue();
}
}));
/**
* An arbitrary implementation for byte values that checks boundary values <code>(0, 1, -1, max,
* min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbByte}
* the remainder of the time (93%).
*/
public static final Arbitrary<Byte> arbByteBoundaries = arbitrary(sized(new F<Integer, Gen<Byte>>() {
@SuppressWarnings("unchecked")
public Gen<Byte> f(final Integer i) {
return frequency(list(p(1, value((byte) 0)),
p(1, value((byte) 1)),
p(1, value((byte) -1)),
p(1, value(Byte.MAX_VALUE)),
p(1, value(Byte.MIN_VALUE)),
p(1, value((byte) (Byte.MAX_VALUE - 1))),
p(1, value((byte) (Byte.MIN_VALUE + 1))),
p(93, arbByte.gen)));
}
}));
/**
* An arbitrary implementation for short values.
*/
public static final Arbitrary<Short> arbShort = arbitrary(arbInteger.gen.map(new F<Integer, Short>() {
public Short f(final Integer i) {
return (short) i.intValue();
}
}));
/**
* An arbitrary implementation for short values that checks boundary values <code>(0, 1, -1, max,
* min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbShort}
* the remainder of the time (93%).
*/
public static final Arbitrary<Short> arbShortBoundaries = arbitrary(sized(new F<Integer, Gen<Short>>() {
@SuppressWarnings("unchecked")
public Gen<Short> f(final Integer i) {
return frequency(list(p(1, value((short) 0)),
p(1, value((short) 1)),
p(1, value((short) -1)),
p(1, value(Short.MAX_VALUE)),
p(1, value(Short.MIN_VALUE)),
p(1, value((short) (Short.MAX_VALUE - 1))),
p(1, value((short) (Short.MIN_VALUE + 1))),
p(93, arbShort.gen)));
}
}));
/**
* An arbitrary implementation for character values.
*/
public static final Arbitrary<Character> arbCharacter = arbitrary(choose(0, 65536).map(new F<Integer, Character>() {
public Character f(final Integer i) {
return (char) i.intValue();
}
}));
/**
* An arbitrary implementation for character values that checks boundary values <code>(max, min,
* max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbCharacter}
* the remainder of the time (96%).
*/
public static final Arbitrary<Character> arbCharacterBoundaries = arbitrary(sized(new F<Integer, Gen<Character>>() {
@SuppressWarnings("unchecked")
public Gen<Character> f(final Integer i) {
return frequency(list(p(1, value(Character.MIN_VALUE)),
p(1, value((char) (Character.MIN_VALUE + 1))),
p(1, value(Character.MAX_VALUE)),
p(1, value((char) (Character.MAX_VALUE - 1))),
p(95, arbCharacter.gen)));
}
}));
/**
* An arbitrary implementation for double values.
*/
public static final Arbitrary<Double> arbDouble = arbitrary(sized(new F<Integer, Gen<Double>>() {
public Gen<Double> f(final Integer i) {
return choose((double) -i, i);
}
}));
/**
* An arbitrary implementation for double values that checks boundary values <code>(0, 1, -1, max,
* min, min (normal), NaN, -infinity, infinity, max - 1)</code> with a frequency of 1% each then
* generates from {@link #arbDouble} the remainder of the time (91%).
*/
public static final Arbitrary<Double> arbDoubleBoundaries = arbitrary(sized(new F<Integer, Gen<Double>>() {
@SuppressWarnings("unchecked")
public Gen<Double> f(final Integer i) {
return frequency(list(p(1, value(0D)),
p(1, value(1D)),
p(1, value(-1D)),
p(1, value(Double.MAX_VALUE)),
p(1, value(Double.MIN_VALUE)),
p(1, value(Double.NaN)),
p(1, value(Double.NEGATIVE_INFINITY)),
p(1, value(Double.POSITIVE_INFINITY)),
p(1, value(Double.MAX_VALUE - 1D)),
p(91, arbDouble.gen)));
}
}));
/**
* An arbitrary implementation for float values.
*/
public static final Arbitrary<Float> arbFloat = arbitrary(arbDouble.gen.map(new F<Double, Float>() {
public Float f(final Double d) {
return (float) d.doubleValue();
}
}));
/**
* An arbitrary implementation for float values that checks boundary values <code>(0, 1, -1, max,
* min, NaN, -infinity, infinity, max - 1)</code> with a frequency of 1% each then generates from
* {@link #arbFloat} the remainder of the time (91%).
*/
public static final Arbitrary<Float> arbFloatBoundaries = arbitrary(sized(new F<Integer, Gen<Float>>() {
@SuppressWarnings("unchecked")
public Gen<Float> f(final Integer i) {
return frequency(list(p(1, value(0F)),
p(1, value(1F)),
p(1, value(-1F)),
p(1, value(Float.MAX_VALUE)),
p(1, value(Float.MIN_VALUE)),
p(1, value(Float.NaN)),
p(1, value(Float.NEGATIVE_INFINITY)),
p(1, value(Float.POSITIVE_INFINITY)),
p(1, value(Float.MAX_VALUE - 1F)),
p(91, arbFloat.gen)));
}
}));
/**
* An arbitrary implementation for string values.
*/
public static final Arbitrary<String> arbString =
arbitrary(arbList(arbCharacter).gen.map(new F<List<Character>, String>() {
public String f(final List<Character> cs) {
return asString(cs);
}
}));
/**
* An arbitrary implementation for string values with characters in the US-ASCII range.
*/
public static final Arbitrary<String> arbUSASCIIString =
arbitrary(arbList(arbCharacter).gen.map(new F<List<Character>, String>() {
public String f(final List<Character> cs) {
return asString(cs.map(new F<Character, Character>() {
public Character f(final Character c) {
return (char) (c % 128);
}
}));
}
}));
/**
* An arbitrary implementation for string values with alpha-numeric characters.
*/
public static final Arbitrary<String> arbAlphaNumString =
arbitrary(arbList(arbitrary(elements(range(charEnumerator, 'a', 'z').append(
range(charEnumerator, 'A', 'Z')).append(
range(charEnumerator, '0', '9')).toArray().array(Character[].class)))).gen.map(asString()));
/**
* An arbitrary implementation for string buffer values.
*/
public static final Arbitrary<StringBuffer> arbStringBuffer =
arbitrary(arbString.gen.map(new F<String, StringBuffer>() {
public StringBuffer f(final String s) {
return new StringBuffer(s);
}
}));
/**
* An arbitrary implementation for string builder values.
*/
public static final Arbitrary<StringBuilder> arbStringBuilder =
arbitrary(arbString.gen.map(new F<String, StringBuilder>() {
public StringBuilder f(final String s) {
return new StringBuilder(s);
}
}));
/**
* Returns an arbitrary implementation for generators.
*
* @param aa an arbitrary implementation for the type over which the generator is defined.
* @return An arbitrary implementation for generators.
*/
public static <A> Arbitrary<Gen<A>> arbGen(final Arbitrary<A> aa) {
return arbitrary(sized(new F<Integer, Gen<Gen<A>>>() {
@SuppressWarnings({"IfMayBeConditional"})
public Gen<Gen<A>> f(final Integer i) {
if (i == 0)
return fail();
else
return aa.gen.map(new F<A, Gen<A>>() {
public Gen<A> f(final A a) {
return value(a);
}
}).resize(i - 1);
}
}));
}
/**
* Returns an arbitrary implementation for optional values.
*
* @param aa an arbitrary implementation for the type over which the optional value is defined.
* @return An arbitrary implementation for optional values.
*/
public static <A> Arbitrary<Option<A>> arbOption(final Arbitrary<A> aa) {
return arbitrary(sized(new F<Integer, Gen<Option<A>>>() {
public Gen<Option<A>> f(final Integer i) {
return i == 0 ?
value(Option.<A>none()) :
aa.gen.map(new F<A, Option<A>>() {
public Option<A> f(final A a) {
return some(a);
}
}).resize(i - 1);
}
}));
}
/**
* Returns an arbitrary implementation for the disjoint union.
*
* @param aa An arbitrary implementation for the type over which one side of the disjoint union is
* defined.
* @param ab An arbitrary implementation for the type over which one side of the disjoint union is
* defined.
* @return An arbitrary implementation for the disjoint union.
*/
@SuppressWarnings({"unchecked"})
public static <A, B> Arbitrary<Either<A, B>> arbEither(final Arbitrary<A> aa, final Arbitrary<B> ab) {
final Gen<Either<A, B>> left = aa.gen.map(new F<A, Either<A, B>>() {
public Either<A, B> f(final A a) {
return left(a);
}
});
final Gen<Either<A, B>> right = ab.gen.map(new F<B, Either<A, B>>() {
public Either<A, B> f(final B b) {
return right(b);
}
});
return arbitrary(oneOf(list(left, right)));
}
/**
* Returns an arbitrary implementation for lists.
*
* @param aa An arbitrary implementation for the type over which the list is defined.
* @return An arbitrary implementation for lists.
*/
public static <A> Arbitrary<List<A>> arbList(final Arbitrary<A> aa) {
return arbitrary(listOf(aa.gen));
}
/**
* Returns an arbitrary implementation for streams.
*
* @param aa An arbitrary implementation for the type over which the stream is defined.
* @return An arbitrary implementation for streams.
*/
public static <A> Arbitrary<Stream<A>> arbStream(final Arbitrary<A> aa) {
return arbitrary(arbList(aa).gen.map(new F<List<A>, Stream<A>>() {
public Stream<A> f(final List<A> as) {
return as.toStream();
}
}));
}
/**
* Returns an arbitrary implementation for arrays.
*
* @param aa An arbitrary implementation for the type over which the array is defined.
* @return An arbitrary implementation for arrays.
*/
public static <A> Arbitrary<Array<A>> arbArray(final Arbitrary<A> aa) {
return arbitrary(arbList(aa).gen.map(new F<List<A>, Array<A>>() {
public Array<A> f(final List<A> as) {
return as.toArray();
}
}));
}
/**
* Returns an arbitrary implementation for throwables.
*
* @param as An arbitrary used for the throwable message.
* @return An arbitrary implementation for throwables.
*/
public static Arbitrary<Throwable> arbThrowable(final Arbitrary<String> as) {
return arbitrary(as.gen.map(new F<String, Throwable>() {
public Throwable f(final String msg) {
return new Throwable(msg);
}
}));
}
/**
* An arbitrary implementation for throwables.
*/
public static final Arbitrary<Throwable> arbThrowable = arbThrowable(arbString);
// BEGIN java.util
/**
* Returns an arbitrary implementation for array lists.
*
* @param aa An arbitrary implementation for the type over which the array list is defined.
* @return An arbitrary implementation for array lists.
*/
public static <A> Arbitrary<ArrayList<A>> arbArrayList(final Arbitrary<A> aa) {
return arbitrary(arbArray(aa).gen.map(new F<Array<A>, ArrayList<A>>() {
public ArrayList<A> f(final Array<A> a) {
return new ArrayList<A>(a.toCollection());
}
}));
}
/**
* An arbitrary implementation for bit sets.
*/
public static final Arbitrary<BitSet> arbBitSet =
arbitrary(arbList(arbBoolean).gen.map(new F<List<Boolean>, BitSet>() {
public BitSet f(final List<Boolean> bs) {
final BitSet s = new BitSet(bs.length());
bs.zipIndex().foreach(new Effect<P2<Boolean, Integer>>() {
public void e(final P2<Boolean, Integer> bi) {
s.set(bi._2(), bi._1());
}
});
return s;
}
}));
/**
* An arbitrary implementation for calendars.
*/
public static final Arbitrary<Calendar> arbCalendar = arbitrary(arbLong.gen.map(new F<Long, Calendar>() {
public Calendar f(final Long i) {
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(i);
return c;
}
}));
/**
* An arbitrary implementation for dates.
*/
public static final Arbitrary<Date> arbDate = arbitrary(arbLong.gen.map(new F<Long, Date>() {
public Date f(final Long i) {
return new Date(i);
}
}));
/**
* Returns an arbitrary implementation for a Java enumeration.
*
* @param clazz The type of enum to return an arbtrary of.
* @return An arbitrary for instances of the supplied enum type.
*/
public static <A extends Enum<A>> Arbitrary<A> arbEnumValue(final Class<A> clazz) {
return arbitrary(Gen.elements(clazz.getEnumConstants()));
}
/**
* Returns an arbitrary implementation for enum maps.
*
* @param ak An arbitrary implementation for the type over which the enum map's keys are defined.
* @param av An arbitrary implementation for the type over which the enum map's values are
* defined.
* @return An arbitrary implementation for enum maps.
*/
public static <K extends Enum<K>, V> Arbitrary<EnumMap<K, V>> arbEnumMap(final Arbitrary<K> ak,
final Arbitrary<V> av) {
return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, EnumMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public EnumMap<K, V> f(final Hashtable<K, V> ht) {
return new EnumMap<K, V>(ht);
}
}));
}
/**
* Returns an arbitrary implementation for enum sets.
*
* @param aa An arbitrary implementation for the type over which the enum set is defined.
* @return An arbitrary implementation for enum sets.
*/
public static <A extends Enum<A>> Arbitrary<EnumSet<A>> arbEnumSet(final Arbitrary<A> aa) {
return arbitrary(arbArray(aa).gen.map(new F<Array<A>, EnumSet<A>>() {
public EnumSet<A> f(final Array<A> a) {
return copyOf(a.toCollection());
}
}));
}
/**
* An arbitrary implementation for gregorian calendars.
*/
public static final Arbitrary<GregorianCalendar> arbGregorianCalendar =
arbitrary(arbLong.gen.map(new F<Long, GregorianCalendar>() {
public GregorianCalendar f(final Long i) {
final GregorianCalendar c = new GregorianCalendar();
c.setTimeInMillis(i);
return c;
}
}));
/**
* Returns an arbitrary implementation for hash maps.
*
* @param ak An arbitrary implementation for the type over which the hash map's keys are defined.
* @param av An arbitrary implementation for the type over which the hash map's values are
* defined.
* @return An arbitrary implementation for hash maps.
*/
public static <K, V> Arbitrary<HashMap<K, V>> arbHashMap(final Arbitrary<K> ak, final Arbitrary<V> av) {
return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, HashMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public HashMap<K, V> f(final Hashtable<K, V> ht) {
return new HashMap<K, V>(ht);
}
}));
}
/**
* Returns an arbitrary implementation for hash sets.
*
* @param aa An arbitrary implementation for the type over which the hash set is defined.
* @return An arbitrary implementation for hash sets.
*/
public static <A> Arbitrary<HashSet<A>> arbHashSet(final Arbitrary<A> aa) {
return arbitrary(arbArray(aa).gen.map(new F<Array<A>, HashSet<A>>() {
public HashSet<A> f(final Array<A> a) {
return new HashSet<A>(a.toCollection());
}
}));
}
/**
* Returns an arbitrary implementation for hash tables.
*
* @param ak An arbitrary implementation for the type over which the hash table's keys are
* defined.
* @param av An arbitrary implementation for the type over which the hash table's values are
* defined.
* @return An arbitrary implementation for hash tables.
*/
public static <K, V> Arbitrary<Hashtable<K, V>> arbHashtable(final Arbitrary<K> ak, final Arbitrary<V> av) {
return arbitrary(arbList(ak).gen.bind(arbList(av).gen, new F<List<K>, F<List<V>, Hashtable<K, V>>>() {
public F<List<V>, Hashtable<K, V>> f(final List<K> ks) {
return new F<List<V>, Hashtable<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public Hashtable<K, V> f(final List<V> vs) {
final Hashtable<K, V> t = new Hashtable<K, V>();
ks.zip(vs).foreach(new Effect<P2<K, V>>() {
public void e(final P2<K, V> kv) {
t.put(kv._1(), kv._2());
}
});
return t;
}
};
}
}));
}
/**
* Returns an arbitrary implementation for identity hash maps.
*
* @param ak An arbitrary implementation for the type over which the identity hash map's keys are
* defined.
* @param av An arbitrary implementation for the type over which the identity hash map's values
* are defined.
* @return An arbitrary implementation for identity hash maps.
*/
public static <K, V> Arbitrary<IdentityHashMap<K, V>> arbIdentityHashMap(final Arbitrary<K> ak,
final Arbitrary<V> av) {
return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, IdentityHashMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public IdentityHashMap<K, V> f(final Hashtable<K, V> ht) {
return new IdentityHashMap<K, V>(ht);
}
}));
}
/**
* Returns an arbitrary implementation for linked hash maps.
*
* @param ak An arbitrary implementation for the type over which the linked hash map's keys are
* defined.
* @param av An arbitrary implementation for the type over which the linked hash map's values are
* defined.
* @return An arbitrary implementation for linked hash maps.
*/
public static <K, V> Arbitrary<LinkedHashMap<K, V>> arbLinkedHashMap(final Arbitrary<K> ak, final Arbitrary<V> av) {
return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, LinkedHashMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public LinkedHashMap<K, V> f(final Hashtable<K, V> ht) {
return new LinkedHashMap<K, V>(ht);
}
}));
}
/**
* Returns an arbitrary implementation for hash sets.
*
* @param aa An arbitrary implementation for the type over which the hash set is defined.
* @return An arbitrary implementation for hash sets.
*/
public static <A> Arbitrary<LinkedHashSet<A>> arbLinkedHashSet(final Arbitrary<A> aa) {
return arbitrary(arbArray(aa).gen.map(new F<Array<A>, LinkedHashSet<A>>() {
public LinkedHashSet<A> f(final Array<A> a) {
return new LinkedHashSet<A>(a.toCollection());
}
}));
}
/**
* Returns an arbitrary implementation for linked lists.
*
* @param aa An arbitrary implementation for the type over which the linked list is defined.
* @return An arbitrary implementation for linked lists.
*/
public static <A> Arbitrary<LinkedList<A>> arbLinkedList(final Arbitrary<A> aa) {
return arbitrary(arbArray(aa).gen.map(new F<Array<A>, LinkedList<A>>() {
public LinkedList<A> f(final Array<A> a) {
return new LinkedList<A>(a.toCollection());
}
}));
}
/**
* Returns an arbitrary implementation for priority queues.
*
* @param aa An arbitrary implementation for the type over which the priority queue is defined.