add stream() and parallelStream()
Both methods return an IntStream.
This commit is contained in:
@@ -7,8 +7,10 @@ import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -474,4 +476,58 @@ public class IntListTest {
|
||||
list.set(1, 0);
|
||||
Assert.assertNotEquals(list, clone);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
Assert.assertEquals("[]", new IntList().toString());
|
||||
|
||||
final IntList list = new IntList();
|
||||
list.addAll(-2, -1, 0, 1, 2);
|
||||
Assert.assertEquals("[-2, -1, 0, 1, 2]", list.toString());
|
||||
Assert.assertEquals("same result as Arrays.toString()", Arrays.toString(list.toArray()), list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSequentialStream() {
|
||||
|
||||
{
|
||||
final IntList list = new IntList();
|
||||
list.addAll(0, 1, 2, 3, 4, 5, 6);
|
||||
final IntStream stream = list.stream();
|
||||
Assert.assertEquals(list.size(), stream.count());
|
||||
}
|
||||
{
|
||||
final IntList list = new IntList();
|
||||
list.addAll(0, 1, 2, 3, 4, 5);
|
||||
final IntStream stream = list.stream();
|
||||
Assert.assertEquals(15, stream.sum());
|
||||
}
|
||||
{
|
||||
final IntList emptyList = new IntList();
|
||||
Assert.assertEquals(0, emptyList.stream().count());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParallelStream() {
|
||||
final IntList list = new IntList();
|
||||
|
||||
final int size = 1000;
|
||||
final int[] ints = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
ints[i] = i;
|
||||
}
|
||||
|
||||
list.addAll(ints);
|
||||
|
||||
final ConcurrentLinkedQueue<Integer> processingOrder = new ConcurrentLinkedQueue<>();
|
||||
final List<Integer> actualList = list.parallelStream()//
|
||||
.peek(e -> processingOrder.add(e))//
|
||||
.boxed()//
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.assertEquals("should be sequential, when using collect", actualList.toString(), list.toString());
|
||||
Assert.assertNotEquals("should use parallelism during computation", processingOrder.toString(),
|
||||
list.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user