-
Notifications
You must be signed in to change notification settings - Fork 593
/
DoubletonSetTest.java
executable file
·225 lines (199 loc) · 6.67 KB
/
DoubletonSetTest.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (c) 2016 Goldman Sachs.
* 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.impl.set.fixed;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.multimap.Multimap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Twin;
import org.eclipse.collections.impl.block.factory.Procedures;
import org.eclipse.collections.impl.block.factory.Procedures2;
import org.eclipse.collections.impl.block.function.NegativeIntervalFunction;
import org.eclipse.collections.impl.block.procedure.CollectionAddProcedure;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.multimap.set.UnifiedSetMultimap;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.tuple.Tuples;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.eclipse.collections.impl.factory.Iterables.mSet;
/**
* JUnit test for {@link DoubletonSet}.
*/
public class DoubletonSetTest extends AbstractMemoryEfficientMutableSetTestCase
{
private DoubletonSet<String> set;
@Before
public void setUp()
{
this.set = new DoubletonSet<>("1", "2");
}
@Override
protected MutableSet<String> classUnderTest()
{
return new DoubletonSet<>("1", "2");
}
@Override
protected MutableSet<String> classUnderTestWithNull()
{
return new DoubletonSet<>(null, "2");
}
@Test
public void nonUniqueWith()
{
Twin<String> twin1 = Tuples.twin("1", "1");
Twin<String> twin2 = Tuples.twin("2", "2");
DoubletonSet<Twin<String>> set = new DoubletonSet<>(twin1, twin2);
Twin<String> twin3 = Tuples.twin("1", "1");
set.with(twin3);
Assert.assertSame(set.getFirst(), twin1);
Twin<String> twin4 = Tuples.twin("2", "2");
set.with(twin4);
Assert.assertSame(set.getLast(), twin2);
}
@Override
@Test
public void equalsAndHashCode()
{
super.equalsAndHashCode();
MutableSet<String> one = Sets.fixedSize.of("1", "2");
Verify.assertEqualsAndHashCode(one, mSet("1", "2"));
Verify.assertPostSerializedEqualsAndHashCode(one);
}
@Test
public void remove()
{
try
{
this.set.remove("1");
Assert.fail("Cannot remove from DoubletonSet");
}
catch (UnsupportedOperationException ignored)
{
this.assertUnchanged();
}
}
@Test
public void addDuplicate()
{
try
{
this.set.add("1");
Assert.fail("Cannot add to DoubletonSet");
}
catch (UnsupportedOperationException ignored)
{
this.assertUnchanged();
}
}
@Test
public void add()
{
try
{
this.set.add("3");
Assert.fail("Cannot add to DoubletonSet");
}
catch (UnsupportedOperationException ignored)
{
this.assertUnchanged();
}
}
@Test
public void addingAllToOtherSet()
{
MutableSet<String> newSet = UnifiedSet.newSet(Sets.fixedSize.of("1", "2"));
newSet.add("3");
Verify.assertContainsAll(newSet, "1", "2", "3");
}
private void assertUnchanged()
{
Verify.assertSize(2, this.set);
Verify.assertContainsAll(this.set, "1", "2");
Verify.assertNotContains("3", this.set);
}
@Override
@Test
public void testClone()
{
Verify.assertShallowClone(this.set);
MutableSet<String> cloneSet = this.set.clone();
Assert.assertNotSame(cloneSet, this.set);
Assert.assertEquals(UnifiedSet.newSetWith("1", "2"), cloneSet);
}
@Test
public void newEmpty()
{
MutableSet<String> newEmpty = this.set.newEmpty();
Verify.assertInstanceOf(UnifiedSet.class, newEmpty);
Verify.assertEmpty(newEmpty);
}
@Test
public void getLast()
{
Assert.assertEquals("2", this.set.getLast());
}
@Test
public void forEach()
{
MutableList<String> result = Lists.mutable.of();
MutableSet<String> source = Sets.fixedSize.of("1", "2");
source.forEach(CollectionAddProcedure.on(result));
Assert.assertEquals(FastList.newListWith("1", "2"), result);
}
@Test
public void forEachWithIndex()
{
int[] indexSum = new int[1];
MutableList<String> result = Lists.mutable.of();
MutableSet<String> source = Sets.fixedSize.of("1", "2");
source.forEachWithIndex((each, index) -> {
result.add(each);
indexSum[0] += index;
});
Assert.assertEquals(FastList.newListWith("1", "2"), result);
Assert.assertEquals(1, indexSum[0]);
}
@Test
public void forEachWith()
{
MutableList<String> result = Lists.mutable.of();
MutableSet<String> source = Sets.fixedSize.of("1", "2");
source.forEachWith(Procedures2.fromProcedure(CollectionAddProcedure.on(result)), null);
Assert.assertEquals(FastList.newListWith("1", "2"), result);
}
@Test
public void getFirstGetLast()
{
MutableSet<String> source = Sets.fixedSize.of("1", "2");
Assert.assertEquals("1", source.getFirst());
Assert.assertEquals("2", source.getLast());
}
@Override
@Test
public void groupByEach()
{
super.groupByEach();
MutableSet<Integer> set = Sets.fixedSize.of(1, 2);
MutableMultimap<Integer, Integer> expected = UnifiedSetMultimap.newMultimap();
set.forEach(Procedures.cast(value -> expected.putAll(-value, Interval.fromTo(value, set.size()))));
Multimap<Integer, Integer> actual =
set.groupByEach(new NegativeIntervalFunction());
Assert.assertEquals(expected, actual);
Multimap<Integer, Integer> actualWithTarget =
set.groupByEach(new NegativeIntervalFunction(), UnifiedSetMultimap.newMultimap());
Assert.assertEquals(expected, actualWithTarget);
}
}