-
Notifications
You must be signed in to change notification settings - Fork 584
/
ListIterableTestCase.java
158 lines (134 loc) · 6.35 KB
/
ListIterableTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright (c) 2020 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.test.list;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.HashingStrategies;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.test.OrderedIterableWithDuplicatesTestCase;
import org.junit.Test;
import static org.eclipse.collections.impl.test.Verify.assertThrows;
import static org.eclipse.collections.test.IterableTestCase.assertEquals;
public interface ListIterableTestCase extends OrderedIterableWithDuplicatesTestCase, TransformsToListTrait
{
@Override
<T> ListIterable<T> newWith(T... elements);
@Override
default <T> ListIterable<T> getExpectedFiltered(T... elements)
{
return Lists.immutable.with(elements);
}
@Override
default <T> MutableList<T> newMutableForFilter(T... elements)
{
return Lists.mutable.with(elements);
}
@Override
default <T> ListIterable<T> getExpectedTransformed(T... elements)
{
return Lists.immutable.with(elements);
}
@Test
default void ListIterable_forEachWithIndex()
{
RichIterable<Integer> integers = this.newWith(1, 2, 3);
MutableCollection<Pair<Integer, Integer>> result = Lists.mutable.with();
integers.forEachWithIndex((each, index) -> result.add(Tuples.pair(each, index)));
assertEquals(
Lists.immutable.with(Tuples.pair(1, 0), Tuples.pair(2, 1), Tuples.pair(3, 2)),
result);
}
@Test
default void ListIterable_forEachInBoth()
{
MutableList<Pair<Integer, String>> result = Lists.mutable.empty();
ListIterable<Integer> integers = this.newWith(1, 2, 3);
ImmutableList<String> strings = this.newWith("1", "2", "3").toImmutable();
integers.forEachInBoth(strings,
(integer, string) -> result.add(Tuples.pair(integer, string)));
assertEquals(
Lists.immutable.with(Tuples.pair(1, "1"), Tuples.pair(2, "2"), Tuples.pair(3, "3")),
result);
}
@Test
default void ListIterable_indexOf()
{
ListIterable<Integer> integers = this.newWith(1, 2, 2, 3, 3, 3, 4, 4, 4, 4);
assertEquals(3, integers.indexOf(3));
assertEquals(-1, integers.indexOf(0));
assertEquals(-1, integers.indexOf(null));
ListIterable<Integer> integers2 = this.newWith(1, 2, 2, null, 3, 3, 3, null, 4, 4, 4, 4);
assertEquals(3, integers2.indexOf(null));
}
@Test
default void ListIterable_lastIndexOf()
{
ListIterable<Integer> integers = this.newWith(1, 2, 2, 3, 3, 3, 4, 4, 4, 4);
assertEquals(5, integers.lastIndexOf(3));
assertEquals(-1, integers.lastIndexOf(0));
assertEquals(-1, integers.lastIndexOf(null));
ListIterable<Integer> integers2 = this.newWith(1, 2, 2, null, 3, 3, 3, null, 4, 4, 4, 4);
assertEquals(7, integers2.lastIndexOf(null));
}
@Test
default void OrderedIterable_forEach_from_to()
{
ListIterable<Integer> integers = this.newWith(4, 4, 4, 4, 3, 3, 3, 2, 2, 1);
MutableList<Integer> result = Lists.mutable.empty();
integers.forEach(5, 7, result::add);
assertEquals(Lists.immutable.with(3, 3, 2), result);
MutableList<Integer> result2 = Lists.mutable.empty();
integers.forEach(0, 9, result2::add);
assertEquals(Lists.immutable.with(4, 4, 4, 4, 3, 3, 3, 2, 2, 1), result2);
ListIterable<Integer> integers2 = this.newWith(4, 4, 4, 4, 3, 3, 3);
MutableList<Integer> result3 = Lists.mutable.empty();
integers2.forEach(5, 6, result3::add);
assertEquals(Lists.immutable.with(3, 3), result3);
MutableList<Integer> result4 = Lists.mutable.empty();
integers2.forEach(3, 3, result4::add);
assertEquals(Lists.immutable.with(4), result4);
MutableList<Integer> result5 = Lists.mutable.empty();
integers2.forEach(4, 4, result5::add);
assertEquals(Lists.immutable.with(3), result5);
MutableList<Integer> result6 = Lists.mutable.empty();
integers2.forEach(5, 5, result6::add);
assertEquals(Lists.immutable.with(3), result6);
MutableList<Integer> result7 = Lists.mutable.empty();
integers2.forEach(6, 6, result7::add);
assertEquals(Lists.immutable.with(3), result7);
assertThrows(IndexOutOfBoundsException.class, () -> integers.forEach(-1, 0, result::add));
assertThrows(IndexOutOfBoundsException.class, () -> integers.forEach(0, -1, result::add));
assertThrows(IndexOutOfBoundsException.class, () -> integers.forEach(0, 10, result::add));
assertThrows(IndexOutOfBoundsException.class, () -> integers.forEach(10, 0, result::add));
}
@Test
default void OrderedIterable_forEach_from_to_reverse_order()
{
ListIterable<Integer> integers = this.newWith(4, 4, 4, 4, 3, 3, 3, 2, 2, 1);
MutableList<Integer> result = Lists.mutable.empty();
integers.forEach(7, 5, result::add);
assertEquals(Lists.immutable.with(2, 3, 3), result);
}
@Test
default void ListIterable_distinct()
{
ListIterable<String> letters = this.newWith("A", "a", "b", "c", "B", "D", "e", "e", "E", "D").distinct(HashingStrategies.fromFunction(String::toLowerCase));
ListIterable<String> expected = FastList.newListWith("A", "b", "c", "D", "e");
assertEquals(letters, expected);
ListIterable<String> empty = this.<String>newWith().distinct(HashingStrategies.fromFunction(String::toLowerCase));
assertEquals(empty, this.newWith());
}
}