1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.atlassian.fugue;
17
18 import org.junit.Test;
19
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.NoSuchElementException;
26 import java.util.function.Function;
27 import java.util.function.Predicate;
28
29 import static io.atlassian.fugue.Iterables.all;
30 import static io.atlassian.fugue.Iterables.any;
31 import static io.atlassian.fugue.Iterables.emptyIterable;
32 import static io.atlassian.fugue.Iterables.findFirst;
33 import static io.atlassian.fugue.Iterables.map;
34 import static io.atlassian.fugue.Iterables.join;
35 import static io.atlassian.fugue.Iterables.partition;
36 import static io.atlassian.fugue.Iterables.rangeTo;
37 import static io.atlassian.fugue.Iterables.rangeUntil;
38 import static java.util.Arrays.asList;
39 import static java.util.Collections.emptyList;
40 import static java.util.Collections.singletonList;
41 import static org.hamcrest.MatcherAssert.assertThat;
42 import static org.hamcrest.Matchers.contains;
43 import static org.hamcrest.Matchers.everyItem;
44 import static org.hamcrest.Matchers.is;
45 import static org.hamcrest.Matchers.nullValue;
46
47 public class IterablesTest {
48
49 private final Predicate<Integer> grepOne = input -> new Integer(1).equals(input);
50 private final Option<Integer> none = Option.<Integer> none();
51
52 @Test public void emptyIterableIteratorHasNext() {
53 assertThat(emptyIterable().iterator().hasNext(), is(false));
54 }
55
56 @Test(expected = NoSuchElementException.class) public void emptyIterableIteratorNext() {
57 emptyIterable().iterator().next();
58 }
59
60 @Test(expected = UnsupportedOperationException.class) public void emptyIterableIteratorRemove() {
61 emptyIterable().iterator().remove();
62 }
63
64 @Test public void emptyIterablesAreEqual() {
65 assertThat(emptyIterable(), is(emptyIterable()));
66 }
67
68 @Test public void emptyIterablesToString() {
69 assertThat(emptyIterable().toString(), is("[]"));
70 }
71
72 @Test public void findFirstEmpty() {
73 assertThat(findFirst(emptyList(), grepOne), is(Option.<Integer> none()));
74 }
75
76 @Test public void findFirstAbsent() {
77 assertThat(findFirst(singletonList(2), grepOne), is(none));
78 }
79
80 @Test public void findFirstSingle() {
81 assertThat(findFirst(singletonList(1), grepOne), is(Option.some(1)));
82 }
83
84 @Test public void findFirstWhenNotFirstElement() {
85 assertThat(findFirst(asList(2, 1), grepOne), is(Option.some(1)));
86 }
87
88 @Test public void findFirstMultipleMatches() {
89 final Pair<Integer, Integer> expected = Pair.pair(1, 1);
90 final List<Pair<Integer, Integer>> ts = Arrays.asList(expected, Pair.pair(2, 2), Pair.pair(1, 3), Pair.pair(2, 4));
91
92 final Option<Pair<Integer, Integer>> found = findFirst(ts, input -> input.left().equals(1));
93
94 assertThat(found, is(Option.some(expected)));
95 }
96
97 @Test public void rangeToSingle() {
98 assertThat(rangeTo(1, 5), is(contains(1, 2, 3, 4, 5)));
99 }
100
101 @Test public void rangeToSingleNegative() {
102 assertThat(rangeTo(5, 1), is(contains(5, 4, 3, 2, 1)));
103 }
104
105 @Test public void rangeUntilSingle() {
106 assertThat(rangeUntil(1, 5), is(contains(1, 2, 3, 4)));
107 }
108
109 @Test public void rangeUntilSingleNegative() {
110 assertThat(rangeUntil(5, 1), is(contains(5, 4, 3, 2)));
111 }
112
113 @Test public void rangeToStep() {
114 assertThat(rangeTo(1, 5, 2), is(contains(1, 3, 5)));
115 }
116
117 @Test public void rangeUntilStep() {
118 assertThat(rangeUntil(1, 5, 2), is(contains(1, 3)));
119 }
120
121 @Test public void rangeToNegativeStep() {
122 assertThat(rangeTo(8, -1, -3), is(contains(8, 5, 2, -1)));
123 }
124
125 @Test public void rangeUntilNegativeStep() {
126 assertThat(rangeUntil(8, -1, -3), is(contains(8, 5, 2)));
127 }
128
129 @Test public void rangeToEqual() {
130 assertThat(rangeTo(1, 1), is(contains(1)));
131 }
132
133 @Test(expected = IllegalArgumentException.class) public void rangeToNegative() {
134 assertThat(rangeTo(1, 2, -1), is(contains(1)));
135 }
136
137 @Test(expected = IllegalArgumentException.class) public void rangeToBackwardsPositive() {
138 assertThat(rangeTo(2, 1, 1), is(contains(1)));
139 }
140
141 @Test(expected = IllegalArgumentException.class) public void rangeToZero() {
142 assertThat(rangeTo(1, 2, 0), is(contains(1)));
143 }
144
145 @Test(expected = IllegalArgumentException.class) public void rangeUntilNegative() {
146 assertThat(rangeUntil(1, 2, -1), is(contains(1)));
147 }
148
149 @Test(expected = IllegalArgumentException.class) public void rangeUntilBackwardsPositive() {
150 assertThat(rangeUntil(2, 1, 1), is(contains(1)));
151 }
152
153 @Test(expected = IllegalArgumentException.class) public void rangeUntilZero() {
154 assertThat(rangeUntil(1, 2, 0), is(contains(1)));
155 }
156
157 @Test public void partitionSimple() {
158 final Pair<Iterable<Integer>, Iterable<Integer>> part = partition(asList(1, 2, 3, 4), i -> i > 2);
159 assertThat(part.left(), contains(3, 4));
160 assertThat(part.right(), contains(1, 2));
161 }
162
163 @Test public void flatMapConcatenates() {
164 final Iterable<String> result = Iterables.flatMap(asList("123", "ABC"), CharSplitter::new);
165 assertThat(result, contains("1", "2", "3", "A", "B", "C"));
166 }
167
168 @Test public void findFirstFunctionWorks() {
169 assertThat(findFirst(Predicate.isEqual(3)).apply(asList(1, 2, 3)), is(Option.some(3)));
170 }
171
172 @Test public void findFirstFunctionFails() {
173 assertThat(findFirst(Predicate.isEqual(3)).apply(asList(1, 2, 4)), is(Option.<Integer> none()));
174 }
175
176 @Test(expected = InvocationTargetException.class) public void nonInstantiable() throws Exception {
177 Eithers.getOrThrow(UtilityFunctions.<Iterables> defaultCtor().apply(Iterables.class));
178 }
179
180 @Test public void revMap() {
181 final Iterable<Function<Integer, Integer>> fs = asList(from -> from + 1, from -> from + 2, from -> from * from);
182 assertThat(Iterables.revMap(fs, 3), contains(4, 5, 9));
183 }
184
185 @Test public void flattenCollapses() {
186 final Iterable<Iterable<Integer>> iterables = asList(singletonList(1), singletonList(2));
187 assertThat(join(iterables), contains(1, 2));
188 }
189
190 @Test public void findAnyMatching() {
191 assertThat(any(Arrays.asList(1, 2, 3), ii -> ii > 2), is(true));
192 }
193
194 @Test public void findAnyNoMatching() {
195 assertThat(any(Arrays.asList(1, 2, 3), ii -> ii < 0), is(false));
196 }
197
198 @Test public void findAnyEmpty() {
199 assertThat(any(Collections.<Integer> emptyList(), ii -> ii < 0), is(false));
200 }
201
202 @Test public void findAllMatching() {
203 assertThat(all(Arrays.asList(1, 2, 3), ii -> ii > 0), is(true));
204 }
205
206 @Test public void findAllNoMatching() {
207 assertThat(all(Arrays.asList(1, 2, 3), ii -> ii < 2), is(false));
208 }
209
210 @Test public void findAllEmpty() {
211 assertThat(all(Collections.<Integer> emptyList(), ii -> ii < 0), is(true));
212 }
213
214 @Test public void mapChangesIterable() {
215 assertThat(map(Arrays.asList(1, 2, 3), i -> i + 1), contains(2, 3, 4));
216 }
217
218 @Test public void mappingNull() {
219 assertThat(map(Arrays.asList(1, 2, 3), i -> null), everyItem(nullValue()));
220 }
221
222
223
224
225 static class CharSplitter implements Iterable<String> {
226 private final CharSequence from;
227
228 CharSplitter(final CharSequence from) {
229 this.from = from;
230 }
231
232 @Override public Iterator<String> iterator() {
233 return new Iterators.Abstract<String>() {
234 int index = 0;
235
236 @Override protected String computeNext() {
237 if (index >= from.length()) {
238 return endOfData();
239 }
240 return from.subSequence(index, ++index).toString();
241 }
242 };
243 }
244 }
245 }