forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamTest.java
More file actions
52 lines (43 loc) · 1.54 KB
/
StreamTest.java
File metadata and controls
52 lines (43 loc) · 1.54 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
package fj.data;
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 org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
/**
* Created by Zheka Kozlov on 27.05.2015.
*/
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());
}
@Test
public void testToString() {
Stream<Integer> range = Stream.range(1);
assertThat(range.toString(), is(equalTo("Cons(1, ?)")));
}
/**
* This test demonstrates the known problem of creating streams from mutable structures.
*
* Some of the ways streams created in this way can fail is:
* - weak stream references getting garbage collected
* - underlying mutable data structure changes
* - iterator gets updated (e.g. iterator used to create 2 different streams).
*/
@Test(expected = ConcurrentModificationException.class)
public void iterableStreamWithStructureUpdate() {
java.util.List<Integer> list = List.list(1, 2, 3).toJavaList();
Stream<Integer> s1 = Stream.iterableStream(list);
int x = s1.head();
list.remove(1);
Stream<Integer> s2 = s1.tail()._1();
x = s2.head();
}
}