-
Notifications
You must be signed in to change notification settings - Fork 587
/
abstractImmutablePrimitiveBagTestCase.stg
309 lines (274 loc) · 13.9 KB
/
abstractImmutablePrimitiveBagTestCase.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
import "copyrightAndOthers.stg"
import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"
isTest() ::= "true"
skipBoolean() ::="true"
targetPath() ::= "org/eclipse/collections/impl/bag/immutable/primitive"
fileName(primitive) ::= "AbstractImmutable<primitive.name>BagTestCase"
class(primitive) ::= <<
<body(primitive.type, primitive.name, primitive.wrapperName)>
>>
body(type, name, wrapperName) ::= <<
<copyrightAndOthers()>
package org.eclipse.collections.impl.bag.immutable.primitive;
import java.util.NoSuchElementException;
import org.eclipse.collections.api.bag.ImmutableBag;
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.list.ImmutableList;
import org.eclipse.collections.api.tuple.primitive.<name>IntPair;
import org.eclipse.collections.api.iterator.<name>Iterator;
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.<name>HashBag;
import org.eclipse.collections.impl.block.factory.primitive.<name>Predicates;
import org.eclipse.collections.impl.collection.immutable.primitive.AbstractImmutable<name>CollectionTestCase;
import org.eclipse.collections.impl.factory.primitive.<name>Bags;
import org.eclipse.collections.impl.list.mutable.primitive.<name>ArrayList;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
import org.junit.Assert;
import org.junit.Test;
import org.eclipse.collections.impl.factory.primitive.<name>Sets;
import org.eclipse.collections.api.set.primitive.Immutable<name>Set;
/**
* JUnit test for {@link Immutable<name>Bag}.
* This file was automatically generated from template file abstractImmutablePrimitiveBagTestCase.stg.
*/
public abstract class AbstractImmutable<name>BagTestCase extends AbstractImmutable<name>CollectionTestCase
{
@Override
protected abstract Immutable<name>Bag classUnderTest();
@Override
protected Immutable<name>Bag newWith(<type>... elements)
{
return <name>Bags.immutable.of(elements);
}
@Override
protected Mutable<name>Bag newMutableCollectionWith(<type>... elements)
{
return <name>HashBag.newBagWith(elements);
}
@Override
protected ImmutableBag\<<wrapperName>\> newObjectCollectionWith(<wrapperName>... elements)
{
return HashBag.newBagWith(elements).toImmutable();
}
@Test
public void sizeDistinct()
{
Assert.assertEquals(0L, this.newWith().sizeDistinct());
Assert.assertEquals(1L, this.newWith(<(literal.(type))("1")>).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()
{
Immutable<name>Bag bag = this.newWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">);
Immutable<name>Bag filtered = bag.selectByOccurrences(i -> i > 2);
Assert.assertEquals(<name>HashBag.newBagWith(<["3", "3", "3"]:(literal.(type))(); separator=", ">), filtered);
}
@Test
public void selectDuplicates()
{
Immutable<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());
}
@Test
public void selectUnique()
{
Immutable<name>Bag bag = this.newWith(<["1", "2", "2", "3", "3", "3", "3", "4", "5", "5", "6"]:(literal.(type))(); separator=", ">);
Immutable<name>Set expected = <name>Sets.immutable.with(<["1", "4", "6"]:(literal.(type))(); separator=", ">);
Immutable<name>Set actual = bag.selectUnique();
Assert.assertEquals(expected, actual);
}
protected Immutable<name>Bag newWithOccurrences(<name>IntPair... elementsWithOccurrences)
{
Mutable<name>Bag bag = <name>Bags.mutable.empty();
for (int i = 0; i \< elementsWithOccurrences.length; i++)
{
<name>IntPair itemToAdd = elementsWithOccurrences[i];
bag.addOccurrences(itemToAdd.getOne(), itemToAdd.getTwo());
}
return bag.toImmutable();
}
@Test
public void topOccurrences()
{
Immutable<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));
ImmutableList\<<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()
{
Immutable<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));
ImmutableList\<<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 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 <type>Iterator()
{
Immutable<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);
}
@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();
Immutable<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();
Immutable<name>Bag bag1 = this.newWith(<["0", "1", "1", "2", "2", "2"]:(literal.(type))(); separator=", ">);
Immutable<name>Bag bag2 = this.newWith(<["0", "2", "1", "2", "1", "2"]:(literal.(type))(); separator=", ">);
Immutable<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);
Assert.assertNotEquals(this.newWith(), <name>ArrayList.newListWith());
Assert.assertNotEquals(this.newWith(<(literal.(type))("1")>), <name>ArrayList.newListWith(<(literal.(type))("1")>));
}
@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());
Immutable<name>Bag expected = this.classUnderTest();
Assert.assertSame(expected, expected.toImmutable());
}
@Test
public void toStringOfItemToCount()
{
Immutable<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));
}
}
>>