Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 69 additions & 6 deletions core/src/test/java/fj/data/StreamTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package fj.data;

import fj.Equal;
import fj.Ord;
import fj.P2;
import fj.control.parallel.Strategy;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;

import static fj.data.IOFunctions.stdinReadLine;
import static java.lang.System.out;
import static fj.data.Stream.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

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

@Test
Expand All @@ -48,5 +48,68 @@ public void iterableStreamWithStructureUpdate() {
x = s2.head();
}

@Test(expected=Error.class)
public void testCycleNil(){
cycle(Stream.nil());
}

@Test
public void testCycle() {
Stream<Character> s = stream(new Character[]{'a', 'b'});
assertThat(cycle(s).take(5),
is(stream(new Character[]{'a', 'b', 'a', 'b', 'a'})));
}

@Test
public void testIterate() {
assertThat(iterate(a -> 2 * a + 1, 1).take(5),
is(stream(new Integer[]{1, 3, 7, 15, 31})));
}

@Test
public void testArrayStreamEmpty() {
assertThat(arrayStream(new Integer[]{}), is(Stream.nil()));
}

@Test(expected=Error.class)
public void testNilHead() {
Stream.nil().head();
}

@Test(expected=Error.class)
public void testNilTail() {
Stream.nil().tail();
}

@Test
public void testArray() {
Character[] a = new Character[]{'a', 'b', 'c'};
Stream<Character> s = stream(a);
assertThat(s.array(Character[].class), is(a));
}

@Test
public void testZipIndex() {
Character[] a = new Character[]{'a', 'b', 'c'};
P2<Stream<Character>, Stream<Integer>> p = unzip(stream(a).zipIndex().drop(1));
assertThat(p._1(), is(stream(new Character[]{'b', 'c'})));
assertThat(p._2(), is(stream(new Integer[]{1, 2})));
}

@Test
public void testMinus() {
Character[] a1 = new Character[]{'a', 'b', 'c', 'd', 'e'};
Stream<Character> s1 = stream(a1);
Character[] a2 = new Character[]{'b', 'e'};
Stream<Character> s2 = stream(a2);
assertThat(s1.minus(Equal.charEqual, s2),
is(stream(new Character[]{'a', 'c', 'd'})));
}

@Test
public void testSort() {
Stream<Integer> s = range(Enumerator.intEnumerator, 99, -99, -1);
assertThat(s.sort(Ord.intOrd, Strategy.seqStrategy()),
is(s.sort(Ord.intOrd)));
}
}