-
Notifications
You must be signed in to change notification settings - Fork 587
/
UnmodifiableSortedBagTest.java
383 lines (336 loc) · 11.8 KB
/
UnmodifiableSortedBagTest.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright (c) 2018 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.bag.sorted.mutable;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.sorted.MutableSortedBag;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.test.SerializeTestHelper;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
import org.junit.Assert;
import org.junit.Test;
/**
* JUnit test for {@link UnmodifiableSortedBag}.
*
* @since 4.2
*/
public class UnmodifiableSortedBagTest extends AbstractMutableSortedBagTestCase
{
@Override
protected <T> MutableSortedBag<T> newWith(T... elements)
{
return TreeBag.newBagWith(elements).asUnmodifiable();
}
@SafeVarargs
@Override
protected final <T> MutableSortedBag<T> newWithOccurrences(ObjectIntPair<T>... elementsWithOccurrences)
{
MutableSortedBag<T> bag = TreeBag.newBag();
for (int i = 0; i < elementsWithOccurrences.length; i++)
{
ObjectIntPair<T> itemToAdd = elementsWithOccurrences[i];
bag.addOccurrences(itemToAdd.getOne(), itemToAdd.getTwo());
}
return bag.asUnmodifiable();
}
@Override
protected <T> MutableSortedBag<T> newWith(Comparator<? super T> comparator, T... elements)
{
return TreeBag.newBagWith(comparator, elements).asUnmodifiable();
}
@Override
public void equalsAndHashCode()
{
super.equalsAndHashCode();
Verify.assertInstanceOf(UnmodifiableSortedBag.class, SerializeTestHelper.serializeDeserialize(this.newWith(Comparators.reverseNaturalOrder(), 1, 2, 3)));
Verify.assertInstanceOf(UnmodifiableSortedBag.class, SerializeTestHelper.serializeDeserialize(this.newWith(1, 2, 3)));
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void toString_with_collection_containing_self()
{
super.toString_with_collection_containing_self();
MutableCollection<Object> collection = this.newWith(1);
collection.add(collection);
String simpleName = collection.getClass().getSimpleName();
String string = collection.toString();
Assert.assertTrue(
("[1, (this " + simpleName + ")]").equals(string)
|| ("[(this " + simpleName + "), 1]").equals(string));
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void makeString_with_collection_containing_self()
{
super.makeString_with_collection_containing_self();
MutableCollection<Object> collection = this.newWith(1, 2, 3);
collection.add(collection);
Assert.assertEquals(collection.toString(), '[' + collection.makeString() + ']');
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void appendString_with_collection_containing_self()
{
super.appendString_with_collection_containing_self();
MutableCollection<Object> collection = this.newWith(1, 2, 3);
collection.add(collection);
Appendable builder = new StringBuilder();
collection.appendString(builder);
Assert.assertEquals(collection.toString(), '[' + builder.toString() + ']');
}
@Override
@Test
public void asSynchronized()
{
Verify.assertInstanceOf(SynchronizedSortedBag.class, this.newWith().asSynchronized());
}
@Override
@Test
public void asUnmodifiable()
{
Verify.assertInstanceOf(UnmodifiableSortedBag.class, this.newWith());
}
@Override
@Test
public void iterator()
{
MutableSortedBag<Integer> bag = this.newWith(-1, 0, 1, 1, 2);
Iterator<Integer> iterator = bag.iterator();
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(Integer.valueOf(-1), iterator.next());
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(Integer.valueOf(0), iterator.next());
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(Integer.valueOf(1), iterator.next());
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(Integer.valueOf(1), iterator.next());
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(Integer.valueOf(2), iterator.next());
Assert.assertFalse(iterator.hasNext());
MutableSortedBag<Integer> revBag = this.newWith(Comparators.reverseNaturalOrder(), -1, 0, 1, 1, 2);
Iterator<Integer> revIterator = revBag.iterator();
Assert.assertTrue(revIterator.hasNext());
Assert.assertEquals(Integer.valueOf(2), revIterator.next());
Assert.assertTrue(revIterator.hasNext());
Assert.assertEquals(Integer.valueOf(1), revIterator.next());
Assert.assertTrue(revIterator.hasNext());
Assert.assertEquals(Integer.valueOf(1), revIterator.next());
Assert.assertTrue(revIterator.hasNext());
Assert.assertEquals(Integer.valueOf(0), revIterator.next());
Assert.assertTrue(revIterator.hasNext());
Assert.assertEquals(Integer.valueOf(-1), revIterator.next());
Assert.assertFalse(revIterator.hasNext());
Iterator<Integer> iterator3 = this.newWith(Comparators.reverseNaturalOrder(), 2, 1, 1, 0, -1).iterator();
Verify.assertThrows(UnsupportedOperationException.class, iterator3::remove);
Assert.assertEquals(Integer.valueOf(2), iterator3.next());
Verify.assertThrows(UnsupportedOperationException.class, iterator3::remove);
}
@Override
@Test
public void iteratorRemove()
{
MutableSortedBag<Integer> bag = this.newWith(-1, 0, 1, 1, 2);
Iterator<Integer> iterator = bag.iterator();
Verify.assertThrows(UnsupportedOperationException.class, iterator::remove);
}
@Override
@Test
public void iteratorRemove2()
{
}
@Test
public void testAsUnmodifiable()
{
Verify.assertInstanceOf(UnmodifiableSortedBag.class, this.newWith().asUnmodifiable());
MutableSortedBag<Object> bag = this.newWith();
Assert.assertSame(bag, bag.asUnmodifiable());
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void removeIfWith()
{
super.removeIfWith();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void clear()
{
super.clear();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void addAll()
{
super.addAll();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void addAllIterable()
{
super.addAllIterable();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void removeIf()
{
super.removeIf();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void removeAll()
{
super.removeAll();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void removeAllIterable()
{
super.removeAllIterable();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void retainAll()
{
super.retainAll();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void retainAllIterable()
{
super.retainAllIterable();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void add()
{
super.add();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void addOccurrences()
{
super.addOccurrences();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void addOccurrences_throws()
{
super.addOccurrences_throws();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void removeObject()
{
super.removeObject();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void removeOccurrences()
{
super.removeOccurrences();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void removeOccurrences_throws()
{
super.removeOccurrences_throws();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void setOccurrences()
{
super.setOccurrences();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void setOccurrences_throws()
{
super.setOccurrences_throws();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void forEachWithOccurrences()
{
super.forEachWithOccurrences();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void with()
{
super.with();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void withAll()
{
super.withAll();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void without()
{
super.without();
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void withoutAll()
{
super.withoutAll();
}
@Override
@Test
public void testClone()
{
MutableSortedBag<Integer> set = this.newWith(1, 2, 3);
MutableSortedBag<Integer> clone = set.clone();
Assert.assertSame(set, clone);
Verify.assertSortedBagsEqual(set, clone);
}
@Override
@Test
public void collectWithOccurrences()
{
Bag<Integer> bag1 = this.newWith(3, 3, 3, 2, 2, 1);
Bag<ObjectIntPair<Integer>> actual1 =
bag1.collectWithOccurrences(PrimitiveTuples::pair, Bags.mutable.empty());
Assert.assertEquals(
Bags.immutable.with(
PrimitiveTuples.pair(Integer.valueOf(3), 3),
PrimitiveTuples.pair(Integer.valueOf(2), 2),
PrimitiveTuples.pair(Integer.valueOf(1), 1)),
actual1);
Assert.assertEquals(
Lists.mutable.with(
PrimitiveTuples.pair(Integer.valueOf(1), 1),
PrimitiveTuples.pair(Integer.valueOf(2), 2),
PrimitiveTuples.pair(Integer.valueOf(3), 3)),
bag1.collectWithOccurrences(PrimitiveTuples::pair));
Set<ObjectIntPair<Integer>> actual2 =
bag1.collectWithOccurrences(PrimitiveTuples::pair, Sets.mutable.empty());
Assert.assertEquals(
Sets.immutable.with(
PrimitiveTuples.pair(Integer.valueOf(3), 3),
PrimitiveTuples.pair(Integer.valueOf(2), 2),
PrimitiveTuples.pair(Integer.valueOf(1), 1)),
actual2);
Bag<Integer> bag2 = this.newWith(3, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 4, 5, 7);
Assert.assertEquals(
Lists.mutable.with(6, 5, 8, 5, 6, 8),
bag2.collectWithOccurrences((each, index) -> each + index));
}
}