SeqBuffer { Seq<T> seq() { return Seq.seq(new BufferSpliterator()); } private  SeqBuffer(Spliterator<T> source);   }
@Test public void testBufferingSpliteratorFollowedByNonBufferingOne() { SeqBuffer<Integer> buffer = buffer(); assertEquals(list, buffer.seq().toList()); assertEquals(list, buffer.seq().toList()); }
@Test public void testTwoBufferingSpliteratorsConsumedOneAfterAnother() { SeqBuffer<Integer> buffer = buffer(); Seq<Integer> seq1 = buffer.seq(); Seq<Integer> seq2 = buffer.seq(); assertEquals(list, seq1.toList()); assertEquals(list, seq2.toList()); assertEquals(list, buffer.seq().toList()); }
@Test public void testTwoBufferingSpliteratorsConsumedInParallel() { SeqBuffer<Integer> buffer = buffer(); List<Tuple2<Integer, Integer>> expected = Seq.zip(list, list).toList(); List<Tuple2<Integer, Integer>> actual = Seq.zip(buffer.seq(), buffer.seq()).toList(); assertEquals(expected, actual); assertEquals(list, buffer.seq().toList()); }
@Test public void testTwoBufferedSpliteratorsConsumedInterchangeably() { SeqBuffer<Integer> buffer = buffer(); Spliterator<Integer> x = buffer.seq().spliterator(); assertTrue(x.tryAdvance(i -> verifyInt(1, i))); assertTrue(x.tryAdvance(i -> verifyInt(2, i))); Spliterator<Integer> y = buffer.seq().spliterator(); assertTrue(y.tryAdvance(i -> verifyInt(1, i))); assertTrue(x.tryAdvance(i -> verifyInt(3, i))); assertTrue(y.tryAdvance(i -> verifyInt(2, i))); assertTrue(y.tryAdvance(i -> verifyInt(3, i))); assertTrue(y.tryAdvance(i -> verifyInt(4, i))); assertTrue(x.tryAdvance(i -> verifyInt(4, i))); assertTrue(x.tryAdvance(i -> verifyInt(5, i))); assertTrue(y.tryAdvance(i -> verifyInt(5, i))); assertFalse(x.tryAdvance(AssertionError::new)); assertFalse(y.tryAdvance(AssertionError::new)); assertEquals(list, buffer.seq().toList()); }