@Test public void shouldReturnEmptySeqWhenZipEmptyWithEmpty() throws Exception { // given final LazySeq<Integer> empty = empty(); // when final LazySeq<Integer> zipped = empty.zip(empty, (a, b) -> a + b); // then assertThat(zipped).isEmpty(); }
@Test public void shouldTrimFirstFixedSeqIfLonger() throws Exception { // given final LazySeq<String> first = of("A", "B", "C", "D"); final LazySeq<Integer> second = of(1, 2, 3); // when final LazySeq<String> zipped = first.zip(second, (a, b) -> a + b); // then assertThat(zipped).isEqualTo(of("A1", "B2", "C3")); }
@Test public void shouldZipTwoLazyFiniteSequencesOfSameSize() throws Exception { // given final LazySeq<String> first = cons("A", () -> cons("B", () -> of("C"))); final LazySeq<Integer> second = cons(1, () -> cons(2, () -> of(3))); // when final LazySeq<String> zipped = first.zip(second, (a, b) -> a + b); // then assertThat(zipped).isEqualTo(of("A1", "B2", "C3")); }
@Test public void shouldZipInfiniteWithFiniteSeq() throws Exception { // given final LazySeq<Integer> naturals = numbers(1); final LazySeq<Integer> primes = primes().take(5); // when final LazySeq<String> zipped = naturals.zip(primes, (n, p) -> n + ": " + p); // then assertThat(zipped).isEqualTo(of("1: 2", "2: 3", "3: 5", "4: 7", "5: 11")); }
@Test public void shouldTrimFirstLazyButFiniteSeqIfLonger() throws Exception { // given final LazySeq<String> first = cons("A", () -> cons("B", () -> cons("C", () -> of("D")))); final LazySeq<Integer> second = cons(1, () -> cons(2, () -> of(3))); // when final LazySeq<String> zipped = first.zip(second, (a, b) -> a + b); // then assertThat(zipped).isEqualTo(of("A1", "B2", "C3")); }
@Test public void shouldNotEvaluateTailWhenZippingTwoSequences() throws Exception { // given final LazySeq<String> first = cons("A", firstSupplier); final LazySeq<Integer> second = cons(1, secondSupplier); // when first.zip(second, (a, b) -> a + b); // then verifyZeroInteractions(firstSupplier); verifyZeroInteractions(secondSupplier); }