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 java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.LinkedList;
24  
25  import static io.atlassian.fugue.Iterables.mergeSorted;
26  import static java.util.Collections.emptyList;
27  import static java.util.Collections.singletonList;
28  import static org.hamcrest.MatcherAssert.assertThat;
29  import static org.hamcrest.Matchers.contains;
30  import static org.hamcrest.Matchers.emptyIterableOf;
31  import static org.hamcrest.Matchers.is;
32  
33  public class IterablesMergeSortedTest {
34    @Test public void mergingEmptyIterablesGivesAnEmptyIterable() {
35      final Matcher<Iterable<String>> iterableMatcher = emptyIterableOf(String.class);
36      assertThat(mergeSorted(Arrays.asList(new ArrayList<String>(), new LinkedList<String>())), is(iterableMatcher));
37    }
38  
39    @Test public void mergingNonEmptyAndEmptyIterablesGivesTheMergedIterable() {
40      assertThat(mergeSorted(Arrays.asList(singletonList("a"), emptyList())), contains("a"));
41    }
42  
43    @Test public void mergingEmptyAndNonEmptyIterablesGivesTheMergedIterable() {
44      assertThat(mergeSorted(Arrays.asList(emptyList(), singletonList("a"))), contains("a"));
45    }
46  
47    @Test public void mergingNonEmptyIterablesInOrderGivesMergedIterable() {
48      assertThat(mergeSorted(Arrays.asList(singletonList("a"), singletonList("b"))), contains("a", "b"));
49    }
50  
51    @Test public void mergingNonEmptyIterablesOutOfOrderGivesMergedIterable() {
52      assertThat(mergeSorted(Arrays.asList(singletonList("b"), singletonList("a"))), contains("a", "b"));
53    }
54  
55    @Test public void mergingNonEmptyIterablesOutOfOrderGivesMergedIterableInOrder() {
56      assertThat(mergeSorted(Arrays.asList(Arrays.asList("b", "d"), Arrays.asList("a", "c", "e"))), contains("a", "b", "c", "d", "e"));
57    }
58  
59    @Test public void mergingManyNonEmptyIterablesOutOfOrderGivesMergedIterableInOrder() {
60      assertThat(
61        mergeSorted(Arrays.asList(Arrays.asList("b", "d"), Arrays.asList("f", "x"), Arrays.asList("c", "e"), Arrays.asList("g", "h"),
62          Arrays.asList("a", "z"))), contains("a", "b", "c", "d", "e", "f", "g", "h", "x", "z"));
63    }
64  
65    @Test public void mergedToString() {
66      assertThat(mergeSorted(Arrays.asList(Arrays.asList("b", "d"), Arrays.asList("a", "c", "e"))).toString(), is("[a, b, c, d, e]"));
67    }
68  }