~ubuntu-branches/ubuntu/wily/gs-collections/wily

« back to all changes in this revision

Viewing changes to unit-tests/src/test/java/com/gs/collections/impl/set/fixed/FixedSizeSetFactoryTest.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2015-07-23 12:42:30 UTC
  • Revision ID: package-import@ubuntu.com-20150723124230-2rjvfv6elyn2m7d4
Tags: upstream-5.1.0
ImportĀ upstreamĀ versionĀ 5.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014 Goldman Sachs.
 
3
 *
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
 
 
17
package com.gs.collections.impl.set.fixed;
 
18
 
 
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;
 
34
 
 
35
public class FixedSizeSetFactoryTest
 
36
{
 
37
    private FixedSizeSetFactory setFactory;
 
38
 
 
39
    @Before
 
40
    public void setUp()
 
41
    {
 
42
        this.setFactory = new FixedSizeSetFactoryImpl();
 
43
    }
 
44
 
 
45
    @Test
 
46
    public void testCreateWith3Args()
 
47
    {
 
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");
 
52
    }
 
53
 
 
54
    @Test
 
55
    public void testCreateWith4Args()
 
56
    {
 
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");
 
63
    }
 
64
 
 
65
    private void assertCreateSet(FixedSizeSet<String> undertest, String... expected)
 
66
    {
 
67
        Assert.assertEquals(UnifiedSet.newSetWith(expected), undertest);
 
68
        Verify.assertInstanceOf(FixedSizeSet.class, undertest);
 
69
    }
 
70
 
 
71
    @Test
 
72
    public void keyPreservation()
 
73
    {
 
74
        Key key = new Key("key");
 
75
 
 
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());
 
81
 
 
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());
 
87
 
 
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));
 
93
 
 
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));
 
99
 
 
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));
 
104
 
 
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));
 
109
    }
 
110
 
 
111
    @Test
 
112
    public void create1()
 
113
    {
 
114
        FixedSizeSet<String> set = Sets.fixedSize.of("1");
 
115
        Verify.assertSize(1, set);
 
116
        Verify.assertContains("1", set);
 
117
    }
 
118
 
 
119
    @Test
 
120
    public void create2()
 
121
    {
 
122
        FixedSizeSet<String> set = Sets.fixedSize.of("1", "2");
 
123
        Assert.assertEquals(UnifiedSet.newSetWith("1", "2"), set);
 
124
    }
 
125
 
 
126
    @Test
 
127
    public void create3()
 
128
    {
 
129
        FixedSizeSet<String> set = Sets.fixedSize.of("1", "2", "3");
 
130
        Assert.assertEquals(UnifiedSet.newSetWith("1", "2", "3"), set);
 
131
    }
 
132
 
 
133
    @Test
 
134
    public void create4()
 
135
    {
 
136
        FixedSizeSet<String> set = Sets.fixedSize.of("1", "2", "3", "4");
 
137
        Assert.assertEquals(UnifiedSet.newSetWith("1", "2", "3", "4"), set);
 
138
    }
 
139
 
 
140
    @Test
 
141
    public void createWithDuplicates()
 
142
    {
 
143
        FixedSizeSet<String> set1 = Sets.fixedSize.of("1", "1");
 
144
        Assert.assertEquals(UnifiedSet.newSetWith("1"), set1);
 
145
 
 
146
        FixedSizeSet<String> set2 = Sets.fixedSize.of("1", "1", "1");
 
147
        Assert.assertEquals(UnifiedSet.newSetWith("1"), set2);
 
148
 
 
149
        FixedSizeSet<String> set3 = Sets.fixedSize.of("2", "3", "2");
 
150
        Assert.assertEquals(UnifiedSet.newSetWith("2", "3"), set3);
 
151
 
 
152
        FixedSizeSet<String> set4 = Sets.fixedSize.of("3", "4", "4");
 
153
        Assert.assertEquals(UnifiedSet.newSetWith("3", "4"), set4);
 
154
 
 
155
        FixedSizeSet<String> set5 = Sets.fixedSize.of("4", "4", "4", "4");
 
156
        Assert.assertEquals(UnifiedSet.newSetWith("4"), set5);
 
157
 
 
158
        FixedSizeSet<String> set6 = Sets.fixedSize.of("4", "3", "4", "4");
 
159
        Assert.assertEquals(UnifiedSet.newSetWith("4", "3"), set6);
 
160
 
 
161
        FixedSizeSet<String> set7 = Sets.fixedSize.of("4", "2", "3", "4");
 
162
        Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set7);
 
163
 
 
164
        FixedSizeSet<String> set8 = Sets.fixedSize.of("2", "3", "4", "4");
 
165
        Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set8);
 
166
 
 
167
        FixedSizeSet<String> set9 = Sets.fixedSize.of("2", "4", "3", "4");
 
168
        Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set9);
 
169
 
 
170
        FixedSizeSet<String> set10 = Sets.fixedSize.of("2", "4", "3", "4");
 
171
        Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set10);
 
172
 
 
173
        FixedSizeSet<String> set11 = Sets.fixedSize.of("4", "3", "4", "2");
 
174
        Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set11);
 
175
 
 
176
        FixedSizeSet<String> set12 = Sets.fixedSize.of("3", "4", "4", "2");
 
177
        Assert.assertEquals(UnifiedSet.newSetWith("4", "3", "2"), set12);
 
178
    }
 
179
 
 
180
    @Test
 
181
    public void createSet()
 
182
    {
 
183
        MutableSet<String> set1 = Sets.fixedSize.of();
 
184
        Verify.assertEmpty(set1);
 
185
 
 
186
        MutableSet<String> set2 = Sets.fixedSize.of();
 
187
        Verify.assertEmpty(set2);
 
188
 
 
189
        Assert.assertSame(Sets.fixedSize.of(), Sets.fixedSize.of());
 
190
    }
 
191
 
 
192
    @Test
 
193
    public void forEach()
 
194
    {
 
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);
 
199
    }
 
200
 
 
201
    @Test
 
202
    public void forEachWithIndex()
 
203
    {
 
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) -> {
 
208
            result.add(each);
 
209
            indexSum[0] += index;
 
210
        });
 
211
        Assert.assertEquals(6, indexSum[0]);
 
212
        Assert.assertEquals(FastList.newListWith("1", "2", "3", "4"), result);
 
213
    }
 
214
 
 
215
    @Test
 
216
    public void forEachWith()
 
217
    {
 
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);
 
222
    }
 
223
 
 
224
    @Test
 
225
    public void ofAllSizeZero()
 
226
    {
 
227
        MutableSet<Integer> set = Sets.fixedSize.ofAll(FastList.<Integer>newList());
 
228
        Assert.assertEquals(UnifiedSet.<Integer>newSetWith(), set);
 
229
        Verify.assertInstanceOf(FixedSizeSet.class, set);
 
230
    }
 
231
 
 
232
    @Test
 
233
    public void ofAllSizeOne()
 
234
    {
 
235
        MutableSet<Integer> set = Sets.fixedSize.ofAll(FastList.newListWith(1));
 
236
        Assert.assertEquals(UnifiedSet.newSetWith(1), set);
 
237
        Verify.assertInstanceOf(FixedSizeSet.class, set);
 
238
    }
 
239
 
 
240
    @Test
 
241
    public void ofAllSizeTwo()
 
242
    {
 
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);
 
246
    }
 
247
 
 
248
    @Test
 
249
    public void ofAllSizeThree()
 
250
    {
 
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);
 
254
    }
 
255
 
 
256
    @Test
 
257
    public void ofAllSizeFour()
 
258
    {
 
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);
 
262
    }
 
263
 
 
264
    @Test
 
265
    public void ofAllSizeFive()
 
266
    {
 
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);
 
270
    }
 
271
}