-
Notifications
You must be signed in to change notification settings - Fork 587
/
abstractMutablePrimitiveBagTestCase.stg
394 lines (350 loc) · 17.3 KB
/
abstractMutablePrimitiveBagTestCase.stg
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
384
385
386
387
388
389
390
391
392
393
394
import "copyrightAndOthers.stg"
import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"
isTest() ::= "true"
targetPath() ::= "org/eclipse/collections/impl/bag/mutable/primitive"
fileName(primitive) ::= "AbstractMutable<primitive.name>BagTestCase"
class(primitive) ::= <<
<body(primitive.type, primitive.name, primitive.wrapperName)>
>>
body(type, name, wrapperName) ::= <<
<copyrightAndOthers()>
package org.eclipse.collections.impl.bag.mutable.primitive;
import java.util.NoSuchElementException;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.Immutable<name>Bag;
import org.eclipse.collections.api.bag.primitive.Mutable<name>Bag;
import org.eclipse.collections.api.iterator.<name>Iterator;
import org.eclipse.collections.api.iterator.Mutable<name>Iterator;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.tuple.primitive.<name>IntPair;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.block.factory.primitive.<name>Predicates;
import org.eclipse.collections.impl.collection.mutable.primitive.AbstractMutable<name>CollectionTestCase;
import org.eclipse.collections.impl.list.mutable.primitive.<name>ArrayList;
import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;
/**
* JUnit test for {@link Mutable<name>Bag}.
* This file was automatically generated from template file abstractMutablePrimitiveBagTestCase.stg.
*/
public abstract class AbstractMutable<name>BagTestCase extends AbstractMutable<name>CollectionTestCase
{
@Override
protected abstract Mutable<name>Bag classUnderTest();
@Override
protected abstract Mutable<name>Bag newWith(<type>... elements);
@Override
protected Mutable<name>Bag newMutableCollectionWith(<type>... elements)
{
return <name>HashBag.newBagWith(elements);
}
@Override
protected MutableBag\<<wrapperName>\> newObjectCollectionWith(<wrapperName>... elements)
{
return HashBag.newBagWith(elements);
}
@Test
public void sizeDistinct()
{
Assert.assertEquals(0L, this.newWith().sizeDistinct());
Assert.assertEquals(3L, this.newWith(<["0", "1", "2"]:(literal.(type))(); separator=", ">).sizeDistinct());
Assert.assertEquals(3L, this.newWith(<["0", "1", "1", "2", "2", "2"]:(literal.(type))(); separator=", ">).sizeDistinct());
}
@Test
public void selectByOccurrences()
{
Mutable<name>Bag bag = this.newWith(<["100", "100", "100", "50", "50"]:(literal.(type))(); separator=", ">);
Mutable<name>Bag filtered = bag.selectByOccurrences(i -> i > 2);
Assert.assertEquals(<name>HashBag.newBagWith(<["100", "100", "100"]:(literal.(type))(); separator=", ">), filtered);
}
@Test
public void selectDuplicates()
{
Mutable<name>Bag bag = this.newWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">);
Assert.assertEquals(<name>HashBag.newBagWith(<["2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">), bag.selectDuplicates());
}
protected Mutable<name>Bag newWithOccurrences(<name>IntPair... elementsWithOccurrences)
{
Mutable<name>Bag bag = this.newWith();
for (int i = 0; i \< elementsWithOccurrences.length; i++)
{
<name>IntPair itemToAdd = elementsWithOccurrences[i];
bag.addOccurrences(itemToAdd.getOne(), itemToAdd.getTwo());
}
return bag;
}
@Test
public void topOccurrences()
{
Mutable<name>Bag bag = this.newWithOccurrences(
PrimitiveTuples.pair(<(literal.(type))("1")>, 1),
PrimitiveTuples.pair(<(literal.(type))("2")>, 2),
PrimitiveTuples.pair(<(literal.(type))("3")>, 3),
PrimitiveTuples.pair(<(literal.(type))("4")>, 4),
PrimitiveTuples.pair(<(literal.(type))("5")>, 5),
PrimitiveTuples.pair(<(literal.(type))("6")>, 6),
PrimitiveTuples.pair(<(literal.(type))("7")>, 7),
PrimitiveTuples.pair(<(literal.(type))("8")>, 8),
PrimitiveTuples.pair(<(literal.(type))("9")>, 9),
PrimitiveTuples.pair(<(literal.(type))("10")>, 10));
MutableList\<<name>IntPair> top5 = bag.topOccurrences(5);
Verify.assertSize(5, top5);
Assert.assertEquals(10, top5.getFirst().getTwo());
Assert.assertEquals(6, top5.getLast().getTwo());
Verify.assertSize(0, this.newWith(<(literal.(type))("1")>).topOccurrences(0));
Verify.assertSize(0, this.newWith().topOccurrences(5));
Verify.assertSize(3, this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">).topOccurrences(5));
Verify.assertSize(3, this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">).topOccurrences(1));
Verify.assertSize(3, this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">).topOccurrences(2));
Verify.assertSize(3, this.newWith(<["1", "1", "2", "3"]:(literal.(type))(); separator=", ">).topOccurrences(2));
Verify.assertSize(2, this.newWith(<["1", "1", "2", "2", "3"]:(literal.(type))(); separator=", ">).topOccurrences(1));
Verify.assertSize(3, this.newWith(<["1", "1", "2", "2", "3", "3"]:(literal.(type))(); separator=", ">).topOccurrences(1));
Verify.assertSize(0, this.newWith().topOccurrences(0));
Verify.assertThrows(IllegalArgumentException.class, () -> this.newWith().topOccurrences(-1));
}
@Test
public void bottomOccurrences()
{
Mutable<name>Bag bag = this.newWithOccurrences(
PrimitiveTuples.pair(<(literal.(type))("1")>, 1),
PrimitiveTuples.pair(<(literal.(type))("2")>, 2),
PrimitiveTuples.pair(<(literal.(type))("3")>, 3),
PrimitiveTuples.pair(<(literal.(type))("4")>, 4),
PrimitiveTuples.pair(<(literal.(type))("5")>, 5),
PrimitiveTuples.pair(<(literal.(type))("6")>, 6),
PrimitiveTuples.pair(<(literal.(type))("7")>, 7),
PrimitiveTuples.pair(<(literal.(type))("8")>, 8),
PrimitiveTuples.pair(<(literal.(type))("9")>, 9),
PrimitiveTuples.pair(<(literal.(type))("10")>, 10));
MutableList\<<name>IntPair> bottom5 = bag.bottomOccurrences(5);
Verify.assertSize(5, bottom5);
Assert.assertEquals(1, bottom5.getFirst().getTwo());
Assert.assertEquals(5, bottom5.getLast().getTwo());
Verify.assertSize(0, this.newWith(<(literal.(type))("1")>).bottomOccurrences(0));
Verify.assertSize(0, this.newWith().bottomOccurrences(5));
Verify.assertSize(3, this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">).bottomOccurrences(5));
Verify.assertSize(3, this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">).bottomOccurrences(1));
Verify.assertSize(3, this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">).bottomOccurrences(2));
Verify.assertSize(2, this.newWith(<["1", "1", "2", "3"]:(literal.(type))(); separator=", ">).bottomOccurrences(2));
Verify.assertSize(3, this.newWith(<["1", "1", "2", "2", "3"]:(literal.(type))(); separator=", ">).bottomOccurrences(2));
Verify.assertSize(3, this.newWith(<["1", "1", "2", "2", "3", "3"]:(literal.(type))(); separator=", ">).bottomOccurrences(1));
Verify.assertSize(0, this.newWith().bottomOccurrences(0));
Verify.assertThrows(IllegalArgumentException.class, () -> this.newWith().bottomOccurrences(-1));
}
@Test
public void addOccurrences()
{
Mutable<name>Bag bag = this.newWith();
bag.addOccurrences(<(literal.(type))("100")>, 3);
Assert.assertEquals(<name>HashBag.newBagWith(<["100", "100", "100"]:(literal.(type))(); separator=", ">), bag);
bag.addOccurrences(<(literal.(type))("100")>, 2);
Assert.assertEquals(<name>HashBag.newBagWith(<["100", "100", "100", "100", "100"]:(literal.(type))(); separator=", ">), bag);
bag.addOccurrences(<(literal.(type))("100")>, 0);
Assert.assertEquals(<name>HashBag.newBagWith(<["100", "100", "100", "100", "100"]:(literal.(type))(); separator=", ">), bag);
}
@Test(expected = IllegalArgumentException.class)
public void addOccurrences_throws()
{
this.newWith().addOccurrences(<(literal.(type))("100")>, -1);
}
@Test
public void removeOccurrences()
{
Mutable<name>Bag bag = this.newWith();
Assert.assertFalse(bag.removeOccurrences(<(literal.(type))("100")>, 2));
bag.addOccurrences(<(literal.(type))("100")>, 5);
Assert.assertTrue(bag.removeOccurrences(<(literal.(type))("100")>, 2));
Assert.assertEquals(<name>HashBag.newBagWith(<["100", "100", "100"]:(literal.(type))(); separator=", ">), bag);
Assert.assertFalse(bag.removeOccurrences(<(literal.(type))("100")>, 0));
Assert.assertEquals(<name>HashBag.newBagWith(<["100", "100", "100"]:(literal.(type))(); separator=", ">), bag);
Assert.assertTrue(bag.removeOccurrences(<(literal.(type))("100")>, 5));
Assert.assertEquals(new <name>HashBag(), bag);
Assert.assertFalse(bag.removeOccurrences(<(literal.(type))("100")>, 5));
Assert.assertEquals(new <name>HashBag(), bag);
}
@Test(expected = IllegalArgumentException.class)
public void removeOccurrences_throws()
{
this.newWith().removeOccurrences(<(literal.(type))("100")>, -1);
}
@Test
public void forEachWithOccurrences()
{
StringBuilder stringBuilder = new StringBuilder();
this.newWith(<["1", "1", "2"]:(literal.(type))(); separator=", ">).forEachWithOccurrences((<type> argument1, int argument2) -> stringBuilder.append(argument1).append(argument2));
String string = stringBuilder.toString();
Assert.assertTrue("<(toStringLiteral.(type))("1")>2<(toStringLiteral.(type))("2")>1".equals(string)
|| "<(toStringLiteral.(type))("2")>1<(toStringLiteral.(type))("1")>2".equals(string));
}
@Test
@Override
public void add()
{
super.add();
Mutable<name>Bag bag = this.newWith();
Assert.assertTrue(bag.add(<(literal.(type))("100")>));
Assert.assertEquals(<name>HashBag.newBagWith(<(literal.(type))("100")>), bag);
Assert.assertTrue(bag.add(<(literal.(type))("100")>));
Assert.assertEquals(<name>HashBag.newBagWith(<["100", "100"]:(literal.(type))(); separator=", ">), bag);
}
@Test
@Override
public void addAllIterable()
{
super.addAllIterable();
Mutable<name>Bag bag = this.newWith();
Assert.assertTrue(bag.addAll(<name>ArrayList.newListWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">)));
Assert.assertFalse(bag.addAll(new <name>ArrayList()));
Assert.assertEquals(<name>HashBag.newBagWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">), bag);
Assert.assertTrue(bag.addAll(<name>HashBag.newBagWith(<["4", "4", "4", "4"]:(literal.(type))(); separator=", ">)));
Assert.assertEquals(<name>HashBag.newBagWith(<["1", "2", "2", "3", "3", "3", "4", "4", "4", "4"]:(literal.(type))(); separator=", ">), bag);
}
@Test
@Override
public void remove()
{
super.remove();
Mutable<name>Bag bag = this.newWith();
Assert.assertFalse(bag.remove(<(literal.(type))("100")>));
Verify.assertSize(0, bag);
Assert.assertEquals(new <name>HashBag(), bag);
Assert.assertTrue(bag.add(<(literal.(type))("100")>));
Verify.assertSize(1, bag);
Assert.assertTrue(bag.add(<(literal.(type))("100")>));
Verify.assertSize(2, bag);
Assert.assertTrue(bag.remove(<(literal.(type))("100")>));
Verify.assertSize(1, bag);
Assert.assertEquals(<name>HashBag.newBagWith(<(literal.(type))("100")>), bag);
Assert.assertTrue(bag.remove(<(literal.(type))("100")>));
Verify.assertSize(0, bag);
Assert.assertEquals(new <name>HashBag(), bag);
}
@Test
@Override
public void <type>Iterator()
{
Mutable<name>Bag bag = this.newWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">);
<name>ArrayList list = <name>ArrayList.newListWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">);
<name>Iterator iterator = bag.<type>Iterator();
for (int i = 0; i \< 6; i++)
{
Assert.assertTrue(iterator.hasNext());
Assert.assertTrue(list.remove(iterator.next()));
}
Verify.assertEmpty(list);
Assert.assertFalse(iterator.hasNext());
Verify.assertThrows(NoSuchElementException.class, iterator::next);
}
@Override
@Test
public void <type>Iterator_with_remove()
{
super.<type>Iterator_with_remove();
Mutable<name>Bag bag = this.newWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">);
Mutable<name>Iterator iterator = bag.<type>Iterator();
for (int i = 0; i \< 6; i++)
{
Assert.assertTrue(iterator.hasNext());
iterator.next();
iterator.remove();
}
Verify.assertEmpty(bag);
Verify.assertThrows(NoSuchElementException.class, iterator::next);
}
@Test
@Override
public void forEach()
{
super.forEach();
<wideType.(type)>[] sum = new <wideType.(type)>[1];
this.newWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">).forEach((<type> each) -> sum[0] += each);
Assert.assertEquals(14L, sum[0]<(delta.(type))>);
}
@Test
@Override
public void count()
{
super.count();
Mutable<name>Bag bag = this.newWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">);
Assert.assertEquals(5L, bag.count(<name>Predicates.greaterThan(<(literal.(type))("1")>)));
Assert.assertEquals(1L, bag.count(<name>Predicates.lessThan(<(literal.(type))("2")>)));
Assert.assertEquals(0L, bag.count(<name>Predicates.greaterThan(<(literal.(type))("4")>)));
}
@Test
@Override
public void sum()
{
super.sum();
Assert.assertEquals(<(wideLiteral.(type))("14")>, this.newWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">).sum()<wideDelta.(type)>);
}
@Test
@Override
public void testEquals()
{
super.testEquals();
Mutable<name>Bag bag1 = this.newWith(<["0", "1", "1", "2", "2", "2"]:(literal.(type))(); separator=", ">);
Mutable<name>Bag bag2 = this.newWith(<["0", "2", "1", "2", "1", "2"]:(literal.(type))(); separator=", ">);
Mutable<name>Bag bag3 = this.newWith(<["0", "1", "2", "2", "2"]:(literal.(type))(); separator=", ">);
Verify.assertEqualsAndHashCode(bag1, bag2);
Assert.assertNotEquals(bag1, bag3);
Assert.assertNotEquals(bag2, bag3);
}
@Test
@Override
public void testToString()
{
super.testToString();
Assert.assertEquals("[<["1", "1", "1"]:(toStringLiteral.(type))(); separator=", ">]", this.newWith(<["1", "1", "1"]:(literal.(type))(); separator=", ">).toString());
}
@Test
@Override
public void makeString()
{
super.makeString();
Assert.assertEquals("<["1", "1", "1"]:(toStringLiteral.(type))(); separator=", ">", this.newWith(<["1", "1", "1"]:(literal.(type))(); separator=", ">).makeString());
}
@Test
@Override
public void appendString()
{
super.appendString();
StringBuilder appendable1 = new StringBuilder();
this.newWith(<["1", "1", "1"]:(literal.(type))(); separator=", ">).appendString(appendable1);
Assert.assertEquals("<["1", "1", "1"]:(toStringLiteral.(type))(); separator=", ">", appendable1.toString());
}
@Test
@Override
public void toList()
{
super.toList();
Assert.assertEquals(<name>ArrayList.newListWith(<["1", "1", "1"]:(literal.(type))(); separator=", ">), this.newWith(<["1", "1", "1"]:(literal.(type))(); separator=", ">).toList());
}
@Test
@Override
public void toSortedList()
{
super.toSortedList();
Assert.assertEquals(<name>ArrayList.newListWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">), this.newWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">).toSortedList());
}
@Test
public void toImmutable()
{
Assert.assertEquals(this.classUnderTest(), this.classUnderTest().toImmutable());
Assert.assertNotSame(this.classUnderTest(), this.classUnderTest().toImmutable());
Verify.assertInstanceOf(Immutable<name>Bag.class, this.classUnderTest().toImmutable());
}
@Test
public void toStringOfItemToCount()
{
Mutable<name>Bag empty = this.newWith();
Assert.assertEquals("{}", empty.toStringOfItemToCount());
Assert.assertEquals("{" + <(literal.(type))("100")> + "=3}", this.newWith(<["100", "100", "100"]:(literal.(type))(); separator=", ">).toStringOfItemToCount());
String actual = this.newWith(<["100", "101", "101"]:(literal.(type))(); separator=", ">).toStringOfItemToCount();
Assert.assertTrue(("{" + <(literal.(type))("100")> + "=1, " + <(literal.(type))("101")> + "=2}").equals(actual) || ("{" + <(literal.(type))("101")> + "=2, " + <(literal.(type))("100")> + "=1}").equals(actual));
}
}
>>