2
* Copyright 2014 Goldman Sachs.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.gs.collections.impl.set.fixed;
19
import com.gs.collections.api.factory.set.FixedSizeSetFactory;
20
import com.gs.collections.api.list.MutableList;
21
import com.gs.collections.api.set.FixedSizeSet;
22
import com.gs.collections.api.set.MutableSet;
23
import com.gs.collections.impl.block.factory.Procedures2;
24
import com.gs.collections.impl.block.procedure.CollectionAddProcedure;
25
import com.gs.collections.impl.factory.Lists;
26
import com.gs.collections.impl.factory.Sets;
27
import com.gs.collections.impl.list.mutable.FastList;
28
import com.gs.collections.impl.set.mutable.UnifiedSet;
29
import com.gs.collections.impl.test.Verify;
30
import com.gs.collections.impl.test.domain.Key;
31
import org.junit.Assert;
32
import org.junit.Before;
33
import org.junit.Test;
35
public class FixedSizeSetFactoryTest
37
private FixedSizeSetFactory setFactory;
42
this.setFactory = new FixedSizeSetFactoryImpl();
46
public void testCreateWith3Args()
48
this.assertCreateSet(this.setFactory.of("a", "a"), "a");
49
this.assertCreateSet(this.setFactory.of("a", "a", "c"), "a", "c");
50
this.assertCreateSet(this.setFactory.of("a", "b", "a"), "a", "b");
51
this.assertCreateSet(this.setFactory.of("a", "b", "b"), "a", "b");
55
public void testCreateWith4Args()
57
this.assertCreateSet(this.setFactory.of("a", "a", "c", "d"), "a", "c", "d");
58
this.assertCreateSet(this.setFactory.of("a", "b", "a", "d"), "a", "b", "d");
59
this.assertCreateSet(this.setFactory.of("a", "b", "c", "a"), "a", "b", "c");
60
this.assertCreateSet(this.setFactory.of("a", "b", "b", "d"), "a", "b", "d");
61
this.assertCreateSet(this.setFactory.of("a", "b", "c", "b"), "a", "b", "c");
62
this.assertCreateSet(this.setFactory.of("a", "b", "c", "c"), "a", "b", "c");
65
private void assertCreateSet(FixedSizeSet<String> undertest, String... expected)
67
Assert.assertEquals(UnifiedSet.newSetWith(expected), undertest);
68
Verify.assertInstanceOf(FixedSizeSet.class, undertest);
72
public void keyPreservation()
74
Key key = new Key("key");
76
Key duplicateKey1 = new Key("key");
77
MutableSet<Key> set1 = this.setFactory.of(key, duplicateKey1);
78
Verify.assertSize(1, set1);
79
Verify.assertContains(key, set1);
80
Assert.assertSame(key, set1.getFirst());
82
Key duplicateKey2 = new Key("key");
83
MutableSet<Key> set2 = this.setFactory.of(key, duplicateKey1, duplicateKey2);
84
Verify.assertSize(1, set2);
85
Verify.assertContains(key, set2);
86
Assert.assertSame(key, set1.getFirst());
88
Key duplicateKey3 = new Key("key");
89
MutableSet<Key> set3 = this.setFactory.of(key, new Key("not a dupe"), duplicateKey3);
90
Verify.assertSize(2, set3);
91
Verify.assertContainsAll(set3, key, new Key("not a dupe"));
92
Assert.assertSame(key, set3.detect(key::equals));
94
Key duplicateKey4 = new Key("key");
95
MutableSet<Key> set4 = this.setFactory.of(key, new Key("not a dupe"), duplicateKey3, duplicateKey4);
96
Verify.assertSize(2, set4);
97
Verify.assertContainsAll(set4, key, new Key("not a dupe"));
98
Assert.assertSame(key, set4.detect(key::equals));
100
MutableSet<Key> set5 = this.setFactory.of(key, new Key("not a dupe"), new Key("me neither"), duplicateKey4);
101
Verify.assertSize(3, set5);
102
Verify.assertContainsAll(set5, key, new Key("not a dupe"), new Key("me neither"));
103
Assert.assertSame(key, set5.detect(key::equals));
105
MutableSet<Key> set6 = this.setFactory.of(key, duplicateKey2, duplicateKey3, duplicateKey4);
106
Verify.assertSize(1, set6);
107
Verify.assertContains(key, set6);
108
Assert.assertSame(key, set6.detect(key::equals));
112
public void create1()
114
FixedSizeSet<String> set = Sets.fixedSize.of("1");
115
Verify.assertSize(1, set);
116
Verify.assertContains("1", set);
120
public void create2()
122
FixedSizeSet<String> set = Sets.fixedSize.of("1", "2");
123
Assert.assertEquals(UnifiedSet.newSetWith("1", "2"), set);
127
public void create3()
129
FixedSizeSet<String> set = Sets.fixedSize.of("1", "2", "3");
130
Assert.assertEquals(UnifiedSet.newSetWith("1", "2", "3"), set);
134
public void create4()
136
FixedSizeSet<String> set = Sets.fixedSize.of("1", "2", "3", "4");
137
Assert.assertEquals(UnifiedSet.newSetWith("1", "2", "3", "4"), set);
141
public void createWithDuplicates()
143
FixedSizeSet<String> set1 = Sets.fixedSize.of("1", "1");
144
Assert.assertEquals(UnifiedSet.newSetWith("1"), set1);
146
FixedSizeSet<String> set2 = Sets.fixedSize.of("1", "1", "1");
147
Assert.assertEquals(UnifiedSet.newSetWith("1"), set2);
149
FixedSizeSet<String> set3 = Sets.fixedSize.of("2", "3", "2");
150
Assert.assertEquals(UnifiedSet.newSetWith("2", "3"), set3);
152
FixedSizeSet<String> set4 = Sets.fixedSize.of("3", "4", "4");
153
Assert.assertEquals(UnifiedSet.newSetWith("3", "4"), set4);
155
FixedSizeSet<String> set5 = Sets.fixedSize.of("4", "4", "4", "4");
156
Assert.assertEquals(UnifiedSet.newSetWith("4"), set5);
158
FixedSizeSet<String> set6 = Sets.fixedSize.of("4", "3", "4", "4");
159
Assert.assertEquals(UnifiedSet.newSetWith("4", "3"), set6);
161
FixedSizeSet<String> set7 = Sets.fixedSize.of("4", "2", "3", "4");
162
Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set7);
164
FixedSizeSet<String> set8 = Sets.fixedSize.of("2", "3", "4", "4");
165
Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set8);
167
FixedSizeSet<String> set9 = Sets.fixedSize.of("2", "4", "3", "4");
168
Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set9);
170
FixedSizeSet<String> set10 = Sets.fixedSize.of("2", "4", "3", "4");
171
Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set10);
173
FixedSizeSet<String> set11 = Sets.fixedSize.of("4", "3", "4", "2");
174
Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set11);
176
FixedSizeSet<String> set12 = Sets.fixedSize.of("3", "4", "4", "2");
177
Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set12);
181
public void createSet()
183
MutableSet<String> set1 = Sets.fixedSize.of();
184
Verify.assertEmpty(set1);
186
MutableSet<String> set2 = Sets.fixedSize.of();
187
Verify.assertEmpty(set2);
189
Assert.assertSame(Sets.fixedSize.of(), Sets.fixedSize.of());
193
public void forEach()
195
MutableList<String> result = Lists.mutable.of();
196
MutableSet<String> source = Sets.fixedSize.of("1", "2", "3", "4");
197
source.forEach(CollectionAddProcedure.on(result));
198
Assert.assertEquals(FastList.newListWith("1", "2", "3", "4"), result);
202
public void forEachWithIndex()
204
int[] indexSum = new int[1];
205
MutableList<String> result = Lists.mutable.of();
206
MutableSet<String> source = Sets.fixedSize.of("1", "2", "3", "4");
207
source.forEachWithIndex((each, index) -> {
209
indexSum[0] += index;
211
Assert.assertEquals(6, indexSum[0]);
212
Assert.assertEquals(FastList.newListWith("1", "2", "3", "4"), result);
216
public void forEachWith()
218
MutableList<String> result = Lists.mutable.of();
219
MutableSet<String> source = Sets.fixedSize.of("1", "2", "3", "4");
220
source.forEachWith(Procedures2.fromProcedure(CollectionAddProcedure.on(result)), null);
221
Assert.assertEquals(FastList.newListWith("1", "2", "3", "4"), result);
225
public void ofAllSizeZero()
227
MutableSet<Integer> set = Sets.fixedSize.ofAll(FastList.<Integer>newList());
228
Assert.assertEquals(UnifiedSet.<Integer>newSetWith(), set);
229
Verify.assertInstanceOf(FixedSizeSet.class, set);
233
public void ofAllSizeOne()
235
MutableSet<Integer> set = Sets.fixedSize.ofAll(FastList.newListWith(1));
236
Assert.assertEquals(UnifiedSet.newSetWith(1), set);
237
Verify.assertInstanceOf(FixedSizeSet.class, set);
241
public void ofAllSizeTwo()
243
MutableSet<Integer> set = Sets.fixedSize.ofAll(FastList.newListWith(1, 2));
244
Assert.assertEquals(UnifiedSet.newSetWith(1, 2), set);
245
Verify.assertInstanceOf(FixedSizeSet.class, set);
249
public void ofAllSizeThree()
251
MutableSet<Integer> set = Sets.fixedSize.ofAll(FastList.newListWith(1, 2, 3));
252
Assert.assertEquals(UnifiedSet.newSetWith(1, 2, 3), set);
253
Verify.assertInstanceOf(FixedSizeSet.class, set);
257
public void ofAllSizeFour()
259
MutableSet<Integer> set = Sets.fixedSize.ofAll(FastList.newListWith(1, 2, 3, 4));
260
Assert.assertEquals(UnifiedSet.newSetWith(1, 2, 3, 4), set);
261
Verify.assertInstanceOf(FixedSizeSet.class, set);
265
public void ofAllSizeFive()
267
MutableSet<Integer> set = Sets.fixedSize.ofAll(FastList.newListWith(1, 2, 3, 4, 5));
268
Assert.assertEquals(UnifiedSet.newSetWith(1, 2, 3, 4, 5), set);
269
Verify.assertInstanceOf(UnifiedSet.class, set);