Skip to content

Commit 5f75082

Browse files
authored
Merge pull request #341 from gliptak/coverage5
Add Stream tests
2 parents 508364c + be64662 commit 5f75082

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

core/src/test/java/fj/data/StreamTest.java

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package fj.data;
22

3+
import fj.Equal;
4+
import fj.Ord;
5+
import fj.P2;
6+
import fj.control.parallel.Strategy;
37
import org.junit.Test;
48

5-
import java.io.IOException;
6-
import java.util.ArrayList;
79
import java.util.ConcurrentModificationException;
810

9-
import static fj.data.IOFunctions.stdinReadLine;
10-
import static java.lang.System.out;
11+
import static fj.data.Stream.*;
1112
import static org.hamcrest.CoreMatchers.equalTo;
1213
import static org.hamcrest.CoreMatchers.is;
13-
import static org.junit.Assert.assertEquals;
1414
import static org.junit.Assert.assertThat;
1515

1616
/**
@@ -21,7 +21,7 @@ public class StreamTest {
2121
@Test
2222
public void infiniteStream() {
2323
Stream<Integer> s = Stream.forever(Enumerator.intEnumerator, 0).bind(Stream::single);
24-
assertEquals(List.range(0, 5), s.take(5).toList());
24+
assertThat(List.range(0, 5), is(s.take(5).toList()));
2525
}
2626

2727
@Test
@@ -48,5 +48,68 @@ public void iterableStreamWithStructureUpdate() {
4848
x = s2.head();
4949
}
5050

51+
@Test(expected=Error.class)
52+
public void testCycleNil(){
53+
cycle(Stream.nil());
54+
}
55+
56+
@Test
57+
public void testCycle() {
58+
Stream<Character> s = stream(new Character[]{'a', 'b'});
59+
assertThat(cycle(s).take(5),
60+
is(stream(new Character[]{'a', 'b', 'a', 'b', 'a'})));
61+
}
62+
63+
@Test
64+
public void testIterate() {
65+
assertThat(iterate(a -> 2 * a + 1, 1).take(5),
66+
is(stream(new Integer[]{1, 3, 7, 15, 31})));
67+
}
68+
69+
@Test
70+
public void testArrayStreamEmpty() {
71+
assertThat(arrayStream(new Integer[]{}), is(Stream.nil()));
72+
}
73+
74+
@Test(expected=Error.class)
75+
public void testNilHead() {
76+
Stream.nil().head();
77+
}
5178

79+
@Test(expected=Error.class)
80+
public void testNilTail() {
81+
Stream.nil().tail();
82+
}
83+
84+
@Test
85+
public void testArray() {
86+
Character[] a = new Character[]{'a', 'b', 'c'};
87+
Stream<Character> s = stream(a);
88+
assertThat(s.array(Character[].class), is(a));
89+
}
90+
91+
@Test
92+
public void testZipIndex() {
93+
Character[] a = new Character[]{'a', 'b', 'c'};
94+
P2<Stream<Character>, Stream<Integer>> p = unzip(stream(a).zipIndex().drop(1));
95+
assertThat(p._1(), is(stream(new Character[]{'b', 'c'})));
96+
assertThat(p._2(), is(stream(new Integer[]{1, 2})));
97+
}
98+
99+
@Test
100+
public void testMinus() {
101+
Character[] a1 = new Character[]{'a', 'b', 'c', 'd', 'e'};
102+
Stream<Character> s1 = stream(a1);
103+
Character[] a2 = new Character[]{'b', 'e'};
104+
Stream<Character> s2 = stream(a2);
105+
assertThat(s1.minus(Equal.charEqual, s2),
106+
is(stream(new Character[]{'a', 'c', 'd'})));
107+
}
108+
109+
@Test
110+
public void testSort() {
111+
Stream<Integer> s = range(Enumerator.intEnumerator, 99, -99, -1);
112+
assertThat(s.sort(Ord.intOrd, Strategy.seqStrategy()),
113+
is(s.sort(Ord.intOrd)));
114+
}
52115
}

0 commit comments

Comments
 (0)