Skip to content

Commit ed18eaf

Browse files
authored
Merge pull request #1 from functionaljava/series/4.x
Sync 2020-05-20.
2 parents de992c3 + 39896f5 commit ed18eaf

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

quickcheck/src/main/java/fj/test/Gen.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import fj.data.Array;
1010
import fj.data.List;
1111
import fj.data.Option;
12+
import fj.data.Stream;
1213
import fj.function.Effect1;
1314

1415
import static fj.Bottom.error;
@@ -497,6 +498,18 @@ public static <A> Gen<List<A>> listOf1(final Gen<A> g) {
497498
return listOf(g, 1);
498499
}
499500

501+
/**
502+
* Returns a generator of streams whose values come from the given generator.
503+
*
504+
* @param g the generator to produce values from for the returned generator
505+
* @param <A> the type of the generator
506+
*
507+
* @return A generator of streams whose values come from the given generator.
508+
*/
509+
public static <A> Gen<Stream<A>> streamOf(final Gen<A> g) {
510+
return gen(i -> r -> Stream.cons(g.gen(i, r), () -> streamOf(g).gen(i, r)));
511+
}
512+
500513
/**
501514
* Returns a generator that picks one element from the given list. If the given list is empty, then the
502515
* returned generator will never produce a value.

quickcheck/src/test/java/fj/test/GenTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package fj.test;
22

33
import fj.data.List;
4+
import fj.data.Stream;
45
import fj.function.Effect1;
56
import org.junit.Test;
67

78
import static fj.Ord.charOrd;
89
import static fj.data.List.list;
910
import static fj.data.List.range;
10-
import static fj.test.Gen.selectionOf;
1111
import static fj.test.Gen.combinationOf;
12-
import static fj.test.Gen.wordOf;
1312
import static fj.test.Gen.permutationOf;
13+
import static fj.test.Gen.pickOne;
14+
import static fj.test.Gen.selectionOf;
15+
import static fj.test.Gen.streamOf;
16+
import static fj.test.Gen.wordOf;
1417
import static fj.test.Rand.standard;
1518
import static org.junit.Assert.assertEquals;
1619
import static org.junit.Assert.assertTrue;
@@ -175,6 +178,15 @@ public void testWordOf_four() {
175178
});
176179
}
177180

181+
@Test
182+
public void testStreamOf() {
183+
final Gen<Stream<Character>> instance = streamOf(pickOne(AS));
184+
testPick(100, instance.map(stream -> stream.take(4).toList()), actual -> {
185+
assertEquals(4, actual.length());
186+
assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA))));
187+
});
188+
}
189+
178190
private static <A> void testPick(int n, Gen<List<A>> instance, Effect1<List<A>> test) {
179191
range(0, n).map(ignore -> instance.gen(0, standard)).foreachDoEffect(test);
180192
}

0 commit comments

Comments
 (0)