View Javadoc
1   package io.atlassian.fugue;
2   
3   import org.hamcrest.Matcher;
4   import org.junit.Test;
5   
6   import java.util.Arrays;
7   import java.util.Iterator;
8   import java.util.Optional;
9   
10  import static io.atlassian.fugue.Iterables.unzip;
11  import static io.atlassian.fugue.Pair.pair;
12  import static io.atlassian.fugue.Pair.zip;
13  import static java.util.Optional.empty;
14  import static org.hamcrest.Matchers.contains;
15  import static org.hamcrest.Matchers.is;
16  import static org.hamcrest.Matchers.iterableWithSize;
17  import static org.junit.Assert.assertThat;
18  
19  public class PairsTest {
20    @Test public void zipped() {
21      final Iterable<Integer> ints = Arrays.asList(1, 2, 3);
22      final Iterable<String> strings = Arrays.asList("1", "2", "3", "4");
23      @SuppressWarnings("unchecked")
24      final Matcher<Iterable<? extends Pair<Integer, String>>> contains = contains(pair(1, "1"), pair(2, "2"), pair(3, "3"));
25      assertThat(zip(ints, strings), contains);
26    }
27  
28    @Test public void zippedDiscardsLongest() {
29      final Iterable<Integer> ints = Arrays.asList(1, 2, 3, 4, 5);
30      final Iterable<String> strings = Arrays.asList("1", "2", "3");
31      assertThat(zip(ints, strings), iterableWithSize(3));
32    }
33  
34    @Test(expected = UnsupportedOperationException.class) public void zippedUnmodifiable() {
35      final Iterable<Integer> ints = Arrays.asList(1, 2, 3, 4, 5);
36      final Iterable<String> strings = Arrays.asList("1", "2", "3");
37      final Iterator<Pair<Integer, String>> zip = zip(ints, strings).iterator();
38      zip.next();
39      zip.remove();
40    }
41  
42    @Test public void unzipLeft() {
43      final Iterable<Pair<Integer, String>> pairs = Arrays.asList(pair(1, "1"), pair(2, "2"), pair(3, "3"), pair(4, "4"));
44      final Pair<Iterable<Integer>, Iterable<String>> ls = unzip(pairs);
45      assertThat(ls.left(), contains(1, 2, 3, 4));
46    }
47  
48    @Test public void unzipRight() {
49      final Iterable<Pair<Integer, String>> pairs = Arrays.asList(pair(1, "1"), pair(2, "2"), pair(3, "3"), pair(4, "4"));
50      final Pair<Iterable<Integer>, Iterable<String>> ls = unzip(pairs);
51      assertThat(ls.right(), contains("1", "2", "3", "4"));
52    }
53  
54    @SuppressWarnings("OptionalUsedAsFieldOrParameterType") private static <A, B> void assertZip(final Optional<A> o1, final Optional<B> o2,
55      final Optional<Pair<A, B>> expected) {
56      final Optional<Pair<A, B>> zip = zip(o1, o2);
57      assertThat(zip, is(expected));
58    }
59  
60    @Test public void zippingTwoEmptiesGivesAnEmpty() {
61      assertZip(empty(), empty(), empty());
62    }
63  
64    @Test public void zippingAnEmptyWithAFullGivesAnEmpty() {
65      assertZip(empty(), Optional.of(42), empty());
66    }
67  
68    @Test public void zippingAFullWithAnEmptyGivesAnEmpty() {
69      assertZip(Optional.of(42), empty(), empty());
70    }
71  
72    @Test public void zippingAFullWithAFullGivesAPair() {
73      assertZip(Optional.of("a"), Optional.of("b"), Optional.of(pair("a", "b")));
74    }
75  }