/* * Copyright 2015 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gs.collections.impl.map.mutable.primitive; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Set; import com.gs.collections.api.map.primitive.MutableObjectBooleanMap; import com.gs.collections.impl.bag.mutable.HashBag; import com.gs.collections.impl.list.mutable.FastList; import com.gs.collections.impl.set.mutable.UnifiedSet; import com.gs.collections.impl.test.Verify; import org.junit.Assert; import org.junit.Test; public abstract class ObjectBooleanHashMapKeySetTestCase { public abstract MutableObjectBooleanMap<String> newMapWithKeysValues(String key1, boolean value1); public abstract MutableObjectBooleanMap<String> newMapWithKeysValues(String key1, boolean value1, String key2, boolean value2); public abstract MutableObjectBooleanMap<String> newMapWithKeysValues(String key1, boolean value1, String key2, boolean value2, String key3, boolean value3); public abstract MutableObjectBooleanMap<String> newMapWithKeysValues(String key1, boolean value1, String key2, boolean value2, String key3, boolean value3, String key4, boolean value4); public abstract MutableObjectBooleanMap<String> newEmptyMap(); @Test(expected = UnsupportedOperationException.class) public void add() { this.newMapWithKeysValues("One", true, "Two", false, "Three", true).keySet().add("Four"); } @Test(expected = UnsupportedOperationException.class) public void addAll() { this.newMapWithKeysValues("One", true, "Two", false, "Three", true).keySet().addAll(FastList.newListWith("Four")); } @Test public void contains() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false); Set<String> keySet = map.keySet(); Assert.assertTrue(keySet.contains("One")); Assert.assertTrue(keySet.contains("Two")); Assert.assertTrue(keySet.contains("Three")); Assert.assertFalse(keySet.contains("Four")); Assert.assertTrue(keySet.contains(null)); keySet.remove(null); Assert.assertFalse(keySet.contains(null)); map.removeKey("One"); Assert.assertFalse(keySet.contains("One")); } @Test public void containsAll() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false); Set<String> keySet = map.keySet(); Assert.assertTrue(keySet.containsAll(FastList.newListWith("One", "Two"))); Assert.assertTrue(keySet.containsAll(FastList.newListWith("One", "Two", "Three", null))); Assert.assertTrue(keySet.containsAll(FastList.newListWith(null, null))); Assert.assertFalse(keySet.containsAll(FastList.newListWith("One", "Four"))); Assert.assertFalse(keySet.containsAll(FastList.newListWith("Five", "Four"))); keySet.remove(null); Assert.assertFalse(keySet.containsAll(FastList.newListWith("One", "Two", "Three", null))); Assert.assertTrue(keySet.containsAll(FastList.newListWith("One", "Two", "Three"))); map.removeKey("One"); Assert.assertFalse(keySet.containsAll(FastList.newListWith("One", "Two"))); Assert.assertTrue(keySet.containsAll(FastList.newListWith("Three", "Two"))); } @Test public void isEmpty() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false); Set<String> keySet = map.keySet(); Assert.assertFalse(keySet.isEmpty()); MutableObjectBooleanMap<String> map1 = this.newEmptyMap(); Set<String> keySet1 = map1.keySet(); Assert.assertTrue(keySet1.isEmpty()); map1.put("One", true); Assert.assertFalse(keySet1.isEmpty()); } @Test public void size() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false); Set<String> keySet = map.keySet(); Verify.assertSize(4, keySet); map.remove("One"); Verify.assertSize(3, keySet); map.put("Five", true); Verify.assertSize(4, keySet); MutableObjectBooleanMap<String> map1 = this.newEmptyMap(); Set<String> keySet1 = map1.keySet(); Verify.assertSize(0, keySet1); map1.put(null, true); Verify.assertSize(1, keySet1); } @Test public void iterator() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false); Set<String> keySet = map.keySet(); Iterator<String> iterator = keySet.iterator(); HashBag<String> expected = HashBag.newBagWith("One", "Two", "Three", null); HashBag<String> actual = HashBag.newBag(); Verify.assertThrows(IllegalStateException.class, iterator::remove); for (int i = 0; i < 4; i++) { Assert.assertTrue(iterator.hasNext()); actual.add(iterator.next()); } Assert.assertFalse(iterator.hasNext()); Verify.assertThrows(NoSuchElementException.class, (Runnable) iterator::next); Assert.assertEquals(expected, actual); Iterator<String> iterator1 = keySet.iterator(); for (int i = 4; i > 0; i--) { Assert.assertTrue(iterator1.hasNext()); iterator1.next(); iterator1.remove(); Verify.assertThrows(IllegalStateException.class, iterator1::remove); Verify.assertSize(i - 1, keySet); Verify.assertSize(i - 1, map); } Assert.assertFalse(iterator1.hasNext()); Verify.assertEmpty(map); Verify.assertEmpty(keySet); } @Test public void removeFromKeySet() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true); Assert.assertFalse(map.keySet().remove("Four")); Assert.assertTrue(map.keySet().remove("Two")); Assert.assertEquals(this.newMapWithKeysValues("One", true, "Three", true), map); Assert.assertEquals(UnifiedSet.newSetWith("One", "Three"), map.keySet()); } @Test public void removeNullFromKeySet() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true); Assert.assertFalse(map.keySet().remove(null)); Assert.assertEquals(this.newMapWithKeysValues("One", true, "Two", false, "Three", true), map); Assert.assertEquals(UnifiedSet.newSetWith("One", "Two", "Three"), map.keySet()); map.put(null, true); Assert.assertEquals(UnifiedSet.newSetWith("One", "Two", "Three", null), map.keySet()); Assert.assertTrue(map.keySet().remove(null)); Assert.assertEquals(this.newMapWithKeysValues("One", true, "Two", false, "Three", true), map); Assert.assertEquals(UnifiedSet.newSetWith("One", "Two", "Three"), map.keySet()); } @Test public void removeAllFromKeySet() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true); Assert.assertFalse(map.keySet().removeAll(FastList.newListWith("Four"))); Assert.assertEquals(UnifiedSet.newSetWith("One", "Two", "Three"), map.keySet()); Assert.assertTrue(map.keySet().removeAll(FastList.newListWith("Two", "Four"))); Assert.assertEquals(this.newMapWithKeysValues("One", true, "Three", true), map); Assert.assertEquals(UnifiedSet.newSetWith("One", "Three"), map.keySet()); } @Test public void retainAllFromKeySet() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true); Assert.assertFalse(map.keySet().retainAll(FastList.newListWith("One", "Two", "Three", "Four"))); Assert.assertEquals(UnifiedSet.newSetWith("One", "Two", "Three"), map.keySet()); Assert.assertTrue(map.keySet().retainAll(FastList.newListWith("One", "Three"))); Assert.assertEquals(this.newMapWithKeysValues("One", true, "Three", true), map); Assert.assertEquals(UnifiedSet.newSetWith("One", "Three"), map.keySet()); } @Test public void clearKeySet() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true); map.keySet().clear(); Verify.assertEmpty(map); Verify.assertEmpty(map.keySet()); } @Test public void keySetEqualsAndHashCode() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true, null, false); Verify.assertEqualsAndHashCode(UnifiedSet.newSetWith("One", "Two", "Three", null), map.keySet()); Assert.assertNotEquals(UnifiedSet.newSetWith("One", "Two", "Three"), map.keySet()); Assert.assertNotEquals(FastList.newListWith("One", "Two", "Three", null), map.keySet()); } @Test public void keySetToArray() { MutableObjectBooleanMap<String> map = this.newMapWithKeysValues("One", true, "Two", false, "Three", true); HashBag<String> expected = HashBag.newBagWith("One", "Two", "Three"); Set<String> keySet = map.keySet(); Assert.assertEquals(expected, HashBag.newBagWith(keySet.toArray())); Assert.assertEquals(expected, HashBag.newBagWith(keySet.toArray(new String[keySet.size()]))); Assert.assertEquals(expected, HashBag.newBagWith(keySet.toArray(new String[0]))); expected.add(null); Assert.assertEquals(expected, HashBag.newBagWith(keySet.toArray(new String[keySet.size() + 1]))); } }