View Javadoc
1   /*
2      Copyright 2011 Atlassian
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package io.atlassian.fugue;
17  
18  import org.hamcrest.Matcher;
19  import org.junit.Test;
20  
21  import static io.atlassian.fugue.Iterables.zip;
22  import static io.atlassian.fugue.Iterables.zipWith;
23  import static io.atlassian.fugue.Iterables.zipWithIndex;
24  import static io.atlassian.fugue.Pair.pair;
25  import static java.util.Arrays.asList;
26  import static org.hamcrest.Matchers.contains;
27  import static org.junit.Assert.assertThat;
28  
29  public class IterablesZipTest {
30    @Test public void zipFromLists() {
31      assertThat(zip(asList(4, 3, 2, 1), asList(1, 2, 3, 4)), genericContains(pair(4, 1), pair(3, 2), pair(2, 3), pair(1, 4)));
32    }
33  
34    @SuppressWarnings("unchecked") @Test public void zipFromLongerFirstList() {
35      assertThat(zip(asList(4, 2, 3, 1, 12), asList(1, 2, 3, 4)), genericContains(pair(4, 1), pair(2, 2), pair(3, 3), pair(1, 4)));
36    }
37  
38    @SuppressWarnings("unchecked") @Test public void zipFromLongerLastList() {
39      assertThat(zip(asList(4, 3, 2, 1), asList(6, 2, 5, 4, 5, 6)), genericContains(pair(4, 6), pair(3, 2), pair(2, 5), pair(1, 4)));
40    }
41  
42    @Test public void zipWithFrom() {
43      assertThat(zipWith(UtilityFunctions.product).apply(asList(4, 3, 2, 8), asList(2, 4, 6, 8)), contains(8, 12, 12, 64));
44    }
45  
46    @Test public void testZipWithIndex() {
47      @SuppressWarnings("unchecked")
48      final Matcher<Iterable<? extends Pair<String, Integer>>> containsPairs = contains(pair("a", 0), pair("b", 1), pair("c", 2));
49      assertThat(zipWithIndex(asList("a", "b", "c")), containsPairs);
50    }
51  
52    @SuppressWarnings("unchecked") @SafeVarargs private final <A> Matcher<Iterable<? extends A>> genericContains(final A... as) {
53      return contains(as);
54    }
55  }