~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/map/mutable/primitive/ObjectBooleanHashMapKeySetTest.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.map.mutable.primitive;
 
18
 
 
19
import java.util.Iterator;
 
20
import java.util.NoSuchElementException;
 
21
import java.util.Set;
 
22
 
 
23
import com.gs.collections.impl.bag.mutable.HashBag;
 
24
import com.gs.collections.impl.list.mutable.FastList;
 
25
import com.gs.collections.impl.set.mutable.UnifiedSet;
 
26
import com.gs.collections.impl.test.Verify;
 
27
import org.junit.Assert;
 
28
import org.junit.Test;
 
29
 
 
30
public class ObjectBooleanHashMapKeySetTest
 
31
{
 
32
    private ObjectBooleanHashMap<String> newMapWithKeysValues(String key1, boolean value1, String key2, boolean value2, String key3, boolean value3)
 
33
    {
 
34
        return ObjectBooleanHashMap.newWithKeysValues(key1, value1, key2, value2, key3, value3);
 
35
    }
 
36
 
 
37
    private ObjectBooleanHashMap<String> newMapWithKeysValues(String key1, boolean value1, String key2, boolean value2, String key3, boolean value3, String key4, boolean value4)
 
38
    {
 
39
        return ObjectBooleanHashMap.newWithKeysValues(key1, value1, key2, value2, key3, value3, key4, value4);
 
40
    }
 
41
 
 
42
    @Test(expected = UnsupportedOperationException.class)
 
43
    public void add()
 
44
    {
 
45
        this.newMapWithKeysValues("One", true, "Two", false, "Three", true).keySet().add("Four");
 
46
    }
 
47
 
 
48
    @Test(expected = UnsupportedOperationException.class)
 
49
    public void addAll()
 
50
    {
 
51
        this.newMapWithKeysValues("One", true, "Two", false, "Three", true).keySet().addAll(FastList.newListWith("Four"));
 
52
    }
 
53
 
 
54
    @Test
 
55
    public void contains()
 
56
    {
 
57
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false);
 
58
        Set<String> keySet = map.keySet();
 
59
        Assert.assertTrue(keySet.contains("One"));
 
60
        Assert.assertTrue(keySet.contains("Two"));
 
61
        Assert.assertTrue(keySet.contains("Three"));
 
62
        Assert.assertFalse(keySet.contains("Four"));
 
63
        Assert.assertTrue(keySet.contains(null));
 
64
        keySet.remove(null);
 
65
        Assert.assertFalse(keySet.contains(null));
 
66
        map.removeKey("One");
 
67
        Assert.assertFalse(keySet.contains("One"));
 
68
    }
 
69
 
 
70
    @Test
 
71
    public void containsAll()
 
72
    {
 
73
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false);
 
74
        Set<String> keySet = map.keySet();
 
75
        Assert.assertTrue(keySet.containsAll(FastList.newListWith("One", "Two")));
 
76
        Assert.assertTrue(keySet.containsAll(FastList.newListWith("One", "Two", "Three", null)));
 
77
        Assert.assertTrue(keySet.containsAll(FastList.newListWith(null, null)));
 
78
        Assert.assertFalse(keySet.containsAll(FastList.newListWith("One", "Four")));
 
79
        Assert.assertFalse(keySet.containsAll(FastList.newListWith("Five", "Four")));
 
80
        keySet.remove(null);
 
81
        Assert.assertFalse(keySet.containsAll(FastList.newListWith("One", "Two", "Three", null)));
 
82
        Assert.assertTrue(keySet.containsAll(FastList.newListWith("One", "Two", "Three")));
 
83
        map.removeKey("One");
 
84
        Assert.assertFalse(keySet.containsAll(FastList.newListWith("One", "Two")));
 
85
        Assert.assertTrue(keySet.containsAll(FastList.newListWith("Three", "Two")));
 
86
    }
 
87
 
 
88
    @Test
 
89
    public void isEmpty()
 
90
    {
 
91
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false);
 
92
        Set<String> keySet = map.keySet();
 
93
        Assert.assertFalse(keySet.isEmpty());
 
94
        ObjectBooleanHashMap<String> map1 = ObjectBooleanHashMap.newMap();
 
95
        Set<String> keySet1 = map1.keySet();
 
96
        Assert.assertTrue(keySet1.isEmpty());
 
97
        map1.put("One", true);
 
98
        Assert.assertFalse(keySet1.isEmpty());
 
99
    }
 
100
 
 
101
    @Test
 
102
    public void size()
 
103
    {
 
104
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false);
 
105
        Set<String> keySet = map.keySet();
 
106
        Verify.assertSize(4, keySet);
 
107
        map.remove("One");
 
108
        Verify.assertSize(3, keySet);
 
109
        map.put("Five", true);
 
110
        Verify.assertSize(4, keySet);
 
111
 
 
112
        ObjectBooleanHashMap<String> map1 = ObjectBooleanHashMap.newMap();
 
113
        Set<String> keySet1 = map1.keySet();
 
114
        Verify.assertSize(0, keySet1);
 
115
        map1.put(null, true);
 
116
        Verify.assertSize(1, keySet1);
 
117
    }
 
118
 
 
119
    @Test
 
120
    public void iterator()
 
121
    {
 
122
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false);
 
123
        Set<String> keySet = map.keySet();
 
124
        Iterator<String> iterator = keySet.iterator();
 
125
 
 
126
        HashBag<String> expected = HashBag.newBagWith("One", "Two", "Three", null);
 
127
        HashBag<String> actual = HashBag.newBag();
 
128
        Verify.assertThrows(IllegalStateException.class, (Runnable) iterator::remove);
 
129
        for (int i = 0; i < 4; i++)
 
130
        {
 
131
            Assert.assertTrue(iterator.hasNext());
 
132
            actual.add(iterator.next());
 
133
        }
 
134
        Assert.assertFalse(iterator.hasNext());
 
135
        Verify.assertThrows(NoSuchElementException.class, (Runnable) iterator::next);
 
136
        Assert.assertEquals(expected, actual);
 
137
 
 
138
        Iterator<String> iterator1 = keySet.iterator();
 
139
        for (int i = 4; i > 0; i--)
 
140
        {
 
141
            Assert.assertTrue(iterator1.hasNext());
 
142
            iterator1.next();
 
143
            iterator1.remove();
 
144
            Verify.assertThrows(IllegalStateException.class, (Runnable) iterator1::remove);
 
145
            Verify.assertSize(i - 1, keySet);
 
146
            Verify.assertSize(i - 1, map);
 
147
        }
 
148
 
 
149
        Assert.assertFalse(iterator1.hasNext());
 
150
        Verify.assertEmpty(map);
 
151
        Verify.assertEmpty(keySet);
 
152
    }
 
153
 
 
154
    @Test
 
155
    public void removeFromKeySet()
 
156
    {
 
157
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true);
 
158
        Assert.assertFalse(map.keySet().remove("Four"));
 
159
 
 
160
        Assert.assertTrue(map.keySet().remove("Two"));
 
161
        Assert.assertEquals(ObjectBooleanHashMap.newWithKeysValues("One", true, "Three", true), map);
 
162
        Assert.assertEquals(UnifiedSet.newSetWith("One", "Three"), map.keySet());
 
163
    }
 
164
 
 
165
    @Test
 
166
    public void removeNullFromKeySet()
 
167
    {
 
168
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true);
 
169
        Assert.assertFalse(map.keySet().remove(null));
 
170
        Assert.assertEquals(ObjectBooleanHashMap.newWithKeysValues("One", true, "Two", false, "Three", true), map);
 
171
        Assert.assertEquals(UnifiedSet.newSetWith("One", "Two", "Three"), map.keySet());
 
172
 
 
173
        map.put(null, true);
 
174
        Assert.assertEquals(UnifiedSet.newSetWith("One", "Two", "Three", null), map.keySet());
 
175
        Assert.assertTrue(map.keySet().remove(null));
 
176
        Assert.assertEquals(ObjectBooleanHashMap.newWithKeysValues("One", true, "Two", false, "Three", true), map);
 
177
        Assert.assertEquals(UnifiedSet.newSetWith("One", "Two", "Three"), map.keySet());
 
178
    }
 
179
 
 
180
    @Test
 
181
    public void removeAllFromKeySet()
 
182
    {
 
183
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true);
 
184
        Assert.assertFalse(map.keySet().removeAll(FastList.newListWith("Four")));
 
185
        Assert.assertEquals(UnifiedSet.newSetWith("One", "Two", "Three"), map.keySet());
 
186
 
 
187
        Assert.assertTrue(map.keySet().removeAll(FastList.newListWith("Two", "Four")));
 
188
        Assert.assertEquals(ObjectBooleanHashMap.newWithKeysValues("One", true, "Three", true), map);
 
189
        Assert.assertEquals(UnifiedSet.newSetWith("One", "Three"), map.keySet());
 
190
    }
 
191
 
 
192
    @Test
 
193
    public void retainAllFromKeySet()
 
194
    {
 
195
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true);
 
196
        Assert.assertFalse(map.keySet().retainAll(FastList.newListWith("One", "Two", "Three", "Four")));
 
197
        Assert.assertEquals(UnifiedSet.newSetWith("One", "Two", "Three"), map.keySet());
 
198
 
 
199
        Assert.assertTrue(map.keySet().retainAll(FastList.newListWith("One", "Three")));
 
200
        Assert.assertEquals(ObjectBooleanHashMap.newWithKeysValues("One", true, "Three", true), map);
 
201
        Assert.assertEquals(UnifiedSet.newSetWith("One", "Three"), map.keySet());
 
202
    }
 
203
 
 
204
    @Test
 
205
    public void clearKeySet()
 
206
    {
 
207
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true);
 
208
        map.keySet().clear();
 
209
        Verify.assertEmpty(map);
 
210
        Verify.assertEmpty(map.keySet());
 
211
    }
 
212
 
 
213
    @Test
 
214
    public void keySetEqualsAndHashCode()
 
215
    {
 
216
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false);
 
217
        Verify.assertEqualsAndHashCode(UnifiedSet.newSetWith("One", "Two", "Three", null), map.keySet());
 
218
        Assert.assertNotEquals(UnifiedSet.newSetWith("One", "Two", "Three"), map.keySet());
 
219
        Assert.assertNotEquals(FastList.newListWith("One", "Two", "Three", null), map.keySet());
 
220
    }
 
221
 
 
222
    @Test
 
223
    public void keySetToArray()
 
224
    {
 
225
        ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true);
 
226
        HashBag<String> expected = HashBag.newBagWith("One", "Two", "Three");
 
227
        Set<String> keySet = map.keySet();
 
228
        Assert.assertEquals(expected, HashBag.newBagWith(keySet.toArray()));
 
229
        Assert.assertEquals(expected, HashBag.newBagWith(keySet.toArray(new String[keySet.size()])));
 
230
        Assert.assertEquals(expected, HashBag.newBagWith(keySet.toArray(new String[0])));
 
231
        expected.add(null);
 
232
        Assert.assertEquals(expected, HashBag.newBagWith(keySet.toArray(new String[keySet.size() + 1])));
 
233
    }
 
234
}