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.map.mutable.primitive;
19
import java.util.Iterator;
20
import java.util.NoSuchElementException;
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;
30
public class ObjectBooleanHashMapKeySetTest
32
private ObjectBooleanHashMap<String> newMapWithKeysValues(String key1, boolean value1, String key2, boolean value2, String key3, boolean value3)
34
return ObjectBooleanHashMap.newWithKeysValues(key1, value1, key2, value2, key3, value3);
37
private ObjectBooleanHashMap<String> newMapWithKeysValues(String key1, boolean value1, String key2, boolean value2, String key3, boolean value3, String key4, boolean value4)
39
return ObjectBooleanHashMap.newWithKeysValues(key1, value1, key2, value2, key3, value3, key4, value4);
42
@Test(expected = UnsupportedOperationException.class)
45
this.newMapWithKeysValues("One", true, "Two", false, "Three", true).keySet().add("Four");
48
@Test(expected = UnsupportedOperationException.class)
51
this.newMapWithKeysValues("One", true, "Two", false, "Three", true).keySet().addAll(FastList.newListWith("Four"));
55
public void contains()
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));
65
Assert.assertFalse(keySet.contains(null));
67
Assert.assertFalse(keySet.contains("One"));
71
public void containsAll()
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")));
81
Assert.assertFalse(keySet.containsAll(FastList.newListWith("One", "Two", "Three", null)));
82
Assert.assertTrue(keySet.containsAll(FastList.newListWith("One", "Two", "Three")));
84
Assert.assertFalse(keySet.containsAll(FastList.newListWith("One", "Two")));
85
Assert.assertTrue(keySet.containsAll(FastList.newListWith("Three", "Two")));
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());
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);
108
Verify.assertSize(3, keySet);
109
map.put("Five", true);
110
Verify.assertSize(4, keySet);
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);
120
public void iterator()
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();
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++)
131
Assert.assertTrue(iterator.hasNext());
132
actual.add(iterator.next());
134
Assert.assertFalse(iterator.hasNext());
135
Verify.assertThrows(NoSuchElementException.class, (Runnable) iterator::next);
136
Assert.assertEquals(expected, actual);
138
Iterator<String> iterator1 = keySet.iterator();
139
for (int i = 4; i > 0; i--)
141
Assert.assertTrue(iterator1.hasNext());
144
Verify.assertThrows(IllegalStateException.class, (Runnable) iterator1::remove);
145
Verify.assertSize(i - 1, keySet);
146
Verify.assertSize(i - 1, map);
149
Assert.assertFalse(iterator1.hasNext());
150
Verify.assertEmpty(map);
151
Verify.assertEmpty(keySet);
155
public void removeFromKeySet()
157
ObjectBooleanHashMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true);
158
Assert.assertFalse(map.keySet().remove("Four"));
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());
166
public void removeNullFromKeySet()
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());
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());
181
public void removeAllFromKeySet()
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());
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());
193
public void retainAllFromKeySet()
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());
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());
205
public void clearKeySet()
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());
214
public void keySetEqualsAndHashCode()
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());
223
public void keySetToArray()
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])));
232
Assert.assertEquals(expected, HashBag.newBagWith(keySet.toArray(new String[keySet.size() + 1])));