@Test public void testAdd() { assertTrue(set.add(key1)); assertFalse(set.add(key1)); assertEquals(1, set.size()); assertTrue(set.contains(key1)); assertFalse(set.contains(key2)); }
/** * Run some random insertions/ deletions and compare the results against <code>java.util.HashSet * </code>. */ @Test public void testAgainstHashMap() { final java.util.Random rnd = new java.util.Random(0x11223344); final java.util.HashSet<Integer> other = new java.util.HashSet<Integer>(); for (int size = 1000; size < 20000; size += 4000) { other.clear(); set.clear(); for (int round = 0; round < size * 20; round++) { Integer key = rnd.nextInt(size); if (rnd.nextBoolean()) { assertEquals(other.add(key), set.add(key)); assertTrue(set.contains(key)); } else { assertEquals(other.remove(key), set.remove(key)); } assertEquals(other.size(), set.size()); } int[] actual = set.toArray(); int[] expected = new int[other.size()]; int i = 0; for (Integer v : other) expected[i++] = v; Arrays.sort(expected); Arrays.sort(actual); assertArrayEquals(expected, actual); } }
@Test public void testIterable() { set.add(1, 2, 2, 3, 4); set.remove(2); assertEquals(3, set.size()); int count = 0; for (IntCursor cursor : set) { count++; assertTrue(set.contains(cursor.value)); } assertEquals(count, set.size()); set.clear(); assertFalse(set.iterator().hasNext()); }