@Test public void testRemove() { IntOpenHashSet s = new IntOpenHashSet(Hash.DEFAULT_INITIAL_SIZE); for (int i = 0; i < 100; i++) assertTrue(s.add(i)); for (int i = 0; i < 100; i++) assertFalse(s.remove(100 + i)); for (int i = 50; i < 150; i++) assertTrue(Integer.toString(i % 100), s.remove(i % 100)); }
@Test public void testRemove0() { IntOpenHashSet s = new IntOpenHashSet(Hash.DEFAULT_INITIAL_SIZE); for (int i = -1; i <= 1; i++) assertTrue(s.add(i)); assertTrue(s.remove(0)); IntIterator iterator = s.iterator(); IntOpenHashSet z = new IntOpenHashSet(); z.add(iterator.nextInt()); z.add(iterator.nextInt()); assertFalse(iterator.hasNext()); assertEquals(new IntOpenHashSet(new int[] {-1, 1}), z); s = new IntOpenHashSet(Hash.DEFAULT_INITIAL_SIZE); for (int i = -1; i <= 1; i++) assertTrue(s.add(i)); iterator = s.iterator(); while (iterator.hasNext()) if (iterator.nextInt() == 0) iterator.remove(); assertFalse(s.contains(0)); iterator = s.iterator(); int[] content = new int[2]; content[0] = iterator.nextInt(); content[1] = iterator.nextInt(); assertFalse(iterator.hasNext()); Arrays.sort(content); assertArrayEquals(new int[] {-1, 1}, content); }
@SuppressWarnings({"unchecked", "boxing"}) private static void test(int n, float f) throws IOException, ClassNotFoundException { int c; IntOpenHashSet m = new IntOpenHashSet(Hash.DEFAULT_INITIAL_SIZE, f); java.util.Set t = new java.util.HashSet(); /* First of all, we fill t with random data. */ for (int i = 0; i < Math.ceil(f * n); i++) t.add((Integer.valueOf(genKey()))); /* Now we add to m the same data */ m.addAll(t); checkTable(m); assertTrue("Error: !m.equals(t) after insertion", m.equals(t)); assertTrue("Error: !t.equals(m) after insertion", t.equals(m)); printProbes(m); /* Now we check that m actually holds that data. */ for (java.util.Iterator i = t.iterator(); i.hasNext(); ) { Object e = i.next(); assertTrue( "Error: m and t differ on a key (" + e + ") after insertion (iterating on t)", m.contains(e)); } /* Now we check that m actually holds that data, but iterating on m. */ c = 0; for (java.util.Iterator i = m.iterator(); i.hasNext(); ) { Object e = i.next(); c++; assertTrue( "Error: m and t differ on a key (" + e + ") after insertion (iterating on m)", t.contains(e)); } assertEquals( "Error: m has only " + c + " keys instead of " + t.size() + " after insertion (iterating on m)", c, t.size()); /* * Now we check that inquiries about random data give the same answer in m and t. For m we * use the polymorphic method. */ for (int i = 0; i < n; i++) { int T = genKey(); assertEquals( "Error: divergence in keys between t and m (polymorphic method)", m.contains(T), t.contains((Integer.valueOf(T)))); } /* * Again, we check that inquiries about random data give the same answer in m and t, but for * m we use the standard method. */ for (int i = 0; i < n; i++) { int T = genKey(); assertFalse( "Error: divergence between t and m (standard method)", m.contains((Integer.valueOf(T))) != t.contains((Integer.valueOf(T)))); } /* Now we put and remove random data in m and t, checking that the result is the same. */ for (int i = 0; i < 20 * n; i++) { int T = genKey(); assertFalse( "Error: divergence in add() between t and m", m.add((Integer.valueOf(T))) != t.add((Integer.valueOf(T)))); T = genKey(); assertFalse( "Error: divergence in remove() between t and m", m.remove((Integer.valueOf(T))) != t.remove((Integer.valueOf(T)))); } checkTable(m); assertTrue("Error: !m.equals(t) after removal", m.equals(t)); assertTrue("Error: !t.equals(m) after removal", t.equals(m)); /* Now we check that m actually holds that data. */ for (java.util.Iterator i = t.iterator(); i.hasNext(); ) { Object e = i.next(); assertFalse( "Error: m and t differ on a key (" + e + ") after removal (iterating on t)", !m.contains(e)); } /* Now we check that m actually holds that data, but iterating on m. */ for (java.util.Iterator i = m.iterator(); i.hasNext(); ) { Object e = i.next(); assertFalse( "Error: m and t differ on a key (" + e + ") after removal (iterating on m)", !t.contains(e)); } /* Now we make m into an array, make it again a set and check it is OK. */ int a[] = m.toIntArray(); assertTrue( "Error: toArray() output (or array-based constructor) is not OK", new IntOpenHashSet(a).equals(m)); /* Now we check cloning. */ assertTrue("Error: m does not equal m.clone()", m.equals(m.clone())); assertTrue("Error: m.clone() does not equal m", m.clone().equals(m)); int h = m.hashCode(); /* Now we save and read m. */ java.io.File ff = new java.io.File("it.unimi.dsi.fastutil.test"); java.io.OutputStream os = new java.io.FileOutputStream(ff); java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(os); oos.writeObject(m); oos.close(); java.io.InputStream is = new java.io.FileInputStream(ff); java.io.ObjectInputStream ois = new java.io.ObjectInputStream(is); m = (IntOpenHashSet) ois.readObject(); ois.close(); ff.delete(); assertEquals("Error: hashCode() changed after save/read", h, m.hashCode()); printProbes(m); checkTable(m); /* Now we check that m actually holds that data, but iterating on m. */ for (java.util.Iterator i = m.iterator(); i.hasNext(); ) { Object e = i.next(); assertFalse("Error: m and t differ on a key (" + e + ") after save/read", !t.contains(e)); } /* Now we put and remove random data in m and t, checking that the result is the same. */ for (int i = 0; i < 20 * n; i++) { int T = genKey(); assertFalse( "Error: divergence in add() between t and m after save/read", m.add((Integer.valueOf(T))) != t.add((Integer.valueOf(T)))); T = genKey(); assertFalse( "Error: divergence in remove() between t and m after save/read", m.remove((Integer.valueOf(T))) != t.remove((Integer.valueOf(T)))); } assertTrue("Error: !m.equals(t) after post-save/read removal", m.equals(t)); assertTrue("Error: !t.equals(m) after post-save/read removal", t.equals(m)); /* Now we take out of m everything, and check that it is empty. */ for (java.util.Iterator i = m.iterator(); i.hasNext(); ) { i.next(); i.remove(); } assertFalse("Error: m is not empty (as it should be)", !m.isEmpty()); m = new IntOpenHashSet(n, f); t.clear(); /* Now we torture-test the hash table. This part is implemented only for integers and longs. */ for (int i = n; i-- != 0; ) m.add(i); t.addAll(m); printProbes(m); checkTable(m); for (int i = n; i-- != 0; ) assertEquals( "Error: m and t differ on a key during torture-test insertion.", m.add(i), t.add((Integer.valueOf(i)))); assertTrue("Error: !m.equals(t) after torture-test insertion", m.equals(t)); assertTrue("Error: !t.equals(m) after torture-test insertion", t.equals(m)); for (int i = n; i-- != 0; ) assertEquals( "Error: m and t differ on a key during torture-test insertion.", m.remove(i), t.remove((Integer.valueOf(i)))); assertTrue("Error: !m.equals(t) after torture-test removal", m.equals(t)); assertTrue("Error: !t.equals(m) after torture-test removal", t.equals(m)); assertTrue("Error: !m.equals(m.clone()) after torture-test removal", m.equals(m.clone())); assertTrue("Error: !m.clone().equals(m) after torture-test removal", m.clone().equals(m)); return; }