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.Matchers;
19  import org.junit.Test;
20  
21  import java.util.Iterator;
22  import java.util.NoSuchElementException;
23  
24  import static io.atlassian.fugue.Iterables.map;
25  import static io.atlassian.fugue.Iterables.take;
26  import static io.atlassian.fugue.Iterables.takeWhile;
27  import static java.util.Arrays.asList;
28  import static java.util.Collections.emptyList;
29  import static org.hamcrest.Matchers.contains;
30  import static org.hamcrest.Matchers.emptyIterable;
31  import static org.junit.Assert.assertThat;
32  
33  public class IterablesTakeTest {
34    @Test public void takeOneFromList() {
35      assertThat(take(3, asList(1, 2, 3, 4)), contains(1, 2, 3));
36    }
37  
38    @Test public void takeOneFromNonList() {
39      assertThat(take(3, asIterable(1, 2, 3, 4)), contains(1, 2, 3));
40    }
41  
42    @Test public void takeNoneFromList() {
43      assertThat(take(0, asList(1, 2, 3, 4)), Matchers.<Integer> emptyIterable());
44    }
45  
46    @Test public void takeNoneFromNonList() {
47      assertThat(take(0, asIterable(1, 2, 3, 4)), Matchers.<Integer> emptyIterable());
48    }
49  
50    @Test public void takeAllFromList() {
51      assertThat(take(4, asList(1, 2, 3, 4)), contains(1, 2, 3, 4));
52    }
53  
54    @Test public void takeAllFromNonList() {
55      assertThat(take(4, asIterable(1, 2, 3, 4)), contains(1, 2, 3, 4));
56    }
57  
58    @Test public void takeMoreFromList() {
59      assertThat(take(12, asList(1, 2, 3, 4)), contains(1, 2, 3, 4));
60    }
61  
62    @Test public void takeMoreFromNonList() {
63      assertThat(take(12, asIterable(1, 2, 3, 4)), contains(1, 2, 3, 4));
64    }
65  
66    @Test public void takeThreeToString() {
67      assertThat(take(3, asIterable(1, 2, 3, 4)).toString(), Matchers.is("[1, 2, 3]"));
68    }
69  
70    @Test(expected = NullPointerException.class) public void takeNull() {
71      take(0, null);
72    }
73  
74    @Test(expected = IllegalArgumentException.class) public void takeNegative() {
75      take(-1, emptyList());
76    }
77  
78    @Test(expected = NoSuchElementException.class) public void takeFromListAndIteratePastEnd() {
79      final Iterator<Integer> ints = take(1, asList(1, 2)).iterator();
80      ints.next();
81      ints.next();
82    }
83  
84    @Test(expected = NoSuchElementException.class) public void takeFromNonListAndIteratePastEnd() {
85      final Iterator<Integer> ints = take(1, asIterable(1, 2)).iterator();
86      ints.next();
87      ints.next();
88    }
89  
90    @SafeVarargs static <A> Iterable<A> asIterable(final A... as) {
91      return map(asList(as), Functions.<A> identity()::apply);
92    }
93  
94    @Test public void takeWhileTest() {
95      assertThat(takeWhile(asList(1, 2, 3, 4), i -> i < 2), contains(1));
96    }
97  
98    @Test public void takeWhileAll() {
99      assertThat(takeWhile(asList(1, 2, 3, 4), i -> true), contains(1, 2, 3, 4));
100   }
101 
102   @Test public void takeWhileNone() {
103     assertThat(takeWhile(asList(1, 2, 3, 4), i -> false), emptyIterable());
104   }
105 
106   @Test(expected = NullPointerException.class) public void takeWhileNull() {
107     takeWhile(null, null);
108   }
109 
110   @Test(expected = NullPointerException.class) public void takeWhileNullWithPredicate() {
111     takeWhile(null, x -> true);
112   }
113 
114   @Test(expected = NullPointerException.class) public void takeWhileNullPredicate() {
115     takeWhile(Iterables.emptyIterable(), null);
116   }
117 }