예제 #1
0
  @Test
  public void testRemove() {
    set.add(0, 1, 2, 3, 4);

    assertTrue(set.remove(2));
    assertFalse(set.remove(2));
    assertEquals(4, set.size());
    assertSortedListEquals(set.toArray(), 0, 1, 3, 4);
  }
예제 #2
0
  /**
   * 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);
    }
  }
예제 #3
0
  @Test
  public void testClone() {
    set.add(1, 2, 3);

    IntDoubleLinkedSet cloned = set.clone();
    cloned.remove(1);

    assertSortedListEquals(set.toArray(), 1, 2, 3);
    assertSortedListEquals(cloned.toArray(), 2, 3);
  }
예제 #4
0
  @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());
  }