@Test public void testRemoveAbsent() { Map<Integer, Fraction> generated = generateAbsent(); OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field); int mapSize = map.size(); for (Map.Entry<Integer, Fraction> mapEntry : generated.entrySet()) { map.remove(mapEntry.getKey()); Assert.assertEquals(mapSize, map.size()); Assert.assertTrue(field.getZero().equals(map.get(mapEntry.getKey()))); } }
@Test public void testRemove() { OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field); int mapSize = javaMap.size(); Assert.assertEquals(mapSize, map.size()); for (Map.Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) { map.remove(mapEntry.getKey()); Assert.assertEquals(--mapSize, map.size()); Assert.assertTrue(field.getZero().equals(map.get(mapEntry.getKey()))); } /* Ensure that put and get still work correctly after removals */ assertPutAndGet(map); }
@Test public void testContainsKey() { OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field); for (Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) { Assert.assertTrue(map.containsKey(mapEntry.getKey())); } for (Map.Entry<Integer, Fraction> mapEntry : generateAbsent().entrySet()) { Assert.assertFalse(map.containsKey(mapEntry.getKey())); } for (Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) { int key = mapEntry.getKey(); Assert.assertTrue(map.containsKey(key)); map.remove(key); Assert.assertFalse(map.containsKey(key)); } }
/** * Similar to testPutKeysWithCollisions() but exercises the codepaths in a slightly different * manner. */ @Test public void testPutKeysWithCollision2() { OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<Fraction>(field); int key1 = 837989881; Fraction value1 = new Fraction(1); map.put(key1, value1); int key2 = 476463321; map.put(key2, value1); Assert.assertEquals(2, map.size()); Assert.assertEquals(value1, map.get(key2)); map.remove(key1); Fraction value2 = new Fraction(2); map.put(key2, value2); Assert.assertEquals(1, map.size()); Assert.assertEquals(value2, map.get(key2)); }
/** * Regression test for a bug in findInsertionIndex where the hashing in the second probing loop * was inconsistent with the first causing duplicate keys after the right sequence of puts and * removes. */ @Test public void testPutKeysWithCollisions() { OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<Fraction>(field); int key1 = -1996012590; Fraction value1 = new Fraction(1); map.put(key1, value1); int key2 = 835099822; map.put(key2, value1); int key3 = 1008859686; map.put(key3, value1); Assert.assertEquals(value1, map.get(key3)); Assert.assertEquals(3, map.size()); map.remove(key2); Fraction value2 = new Fraction(2); map.put(key3, value2); Assert.assertEquals(value2, map.get(key3)); Assert.assertEquals(2, map.size()); }
@Test public void testRemoveFromEmpty() { OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<Fraction>(field); Assert.assertTrue(field.getZero().equals(map.remove(50))); }