@Test
 public void forEachWith() {
   MutableList<String> result = Lists.mutable.of();
   MutableSet<String> source = Sets.fixedSize.of("1", "2", "3", "4");
   source.forEachWith(Procedures2.fromProcedure(CollectionAddProcedure.on(result)), null);
   Assert.assertEquals(FastList.newListWith("1", "2", "3", "4"), result);
 }
  @Test
  public void serialization() {
    int size = COLLISIONS.size();
    for (int i = 1; i < size; i++) {
      MutableSet<Integer> set =
          UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, SIZE)
              .withAll(COLLISIONS.subList(0, i));
      Verify.assertPostSerializedEqualsAndHashCode(set);
      set.add(null);
      Verify.assertPostSerializedEqualsAndHashCode(set);
    }

    UnifiedSetWithHashingStrategy<Integer> nullBucketZero =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY, null, COLLISION_1, COLLISION_2);
    Verify.assertPostSerializedEqualsAndHashCode(nullBucketZero);

    UnifiedSetWithHashingStrategy<Integer> simpleSetWithNull =
        UnifiedSetWithHashingStrategy.newSetWith(INTEGER_HASHING_STRATEGY, null, 1, 2);
    Verify.assertPostSerializedEqualsAndHashCode(simpleSetWithNull);

    UnifiedSetWithHashingStrategy<Person> people =
        UnifiedSetWithHashingStrategy.newSet(LAST_NAME_HASHING_STRATEGY, PEOPLE);
    Verify.assertPostSerializedEqualsAndHashCode(people);
    // Testing the hashingStrategy is serialized correctly by making sure it is still hashing by
    // last name
    Verify.assertSetsEqual(LAST_NAME_HASHED_SET.castToSet(), people.withAll(PEOPLE.castToList()));
  }
  @Override
  @Test
  public void retainAll() {
    super.retainAll();

    MutableSet<Object> setWithNull = this.newWith((Object) null);
    Assert.assertFalse(setWithNull.retainAll(FastList.newListWith((Object) null)));
    Assert.assertEquals(UnifiedSet.newSetWith((Object) null), setWithNull);
  }
 @Test
 public void testUnifiedSetKeySetToArrayDest() {
   MutableSet<Integer> set = MultiReaderUnifiedSet.newSetWith(1, 2, 3, 4);
   // deliberately to small to force the method to allocate one of the correct size
   Integer[] dest = new Integer[2];
   Integer[] result = set.toArray(dest);
   Verify.assertSize(4, result);
   Arrays.sort(result);
   Assert.assertArrayEquals(new Integer[] {1, 2, 3, 4}, result);
 }
 @Test
 public void forEachWithIndex() {
   int[] indexSum = new int[1];
   MutableList<String> result = Lists.mutable.of();
   MutableSet<String> source = Sets.fixedSize.of("1", "2", "3", "4");
   source.forEachWithIndex(
       (each, index) -> {
         result.add(each);
         indexSum[0] += index;
       });
   Assert.assertEquals(FastList.newListWith("1", "2", "3", "4"), result);
   Assert.assertEquals(6, indexSum[0]);
 }
  @Override
  @Test
  public void toArray() {
    super.toArray();

    int size = COLLISIONS.size();
    for (int i = 1; i < size; i++) {
      MutableSet<Integer> set =
          UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, SIZE)
              .withAll(COLLISIONS.subList(0, i));
      Object[] objects = set.toArray();
      Assert.assertEquals(set, UnifiedSet.newSetWith(objects));
    }

    MutableSet<Integer> deepChain =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY,
            COLLISION_1,
            COLLISION_2,
            COLLISION_3,
            COLLISION_4,
            COLLISION_5,
            COLLISION_6);
    Assert.assertArrayEquals(
        new Integer[] {
          COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6
        },
        deepChain.toArray());

    MutableSet<Integer> minimumChain =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY, COLLISION_1, COLLISION_2);
    minimumChain.remove(COLLISION_2);
    Assert.assertArrayEquals(new Integer[] {COLLISION_1}, minimumChain.toArray());

    MutableSet<Integer> set =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY, COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
    Integer[] target = {
      Integer.valueOf(1),
      Integer.valueOf(1),
      Integer.valueOf(1),
      Integer.valueOf(1),
      Integer.valueOf(1),
      Integer.valueOf(1)
    };
    Integer[] actual = set.toArray(target);
    ArrayIterate.sort(actual, actual.length, Comparators.safeNullsHigh(Integer::compareTo));
    Assert.assertArrayEquals(
        new Integer[] {COLLISION_1, 1, COLLISION_2, COLLISION_3, COLLISION_4, null}, actual);
  }
  @Test
  public void setKeyPreservation() {
    Key key = new Key("key");

    Key duplicateKey1 = new Key("key");
    MutableSet<Key> set1 =
        UnifiedSetWithHashingStrategy.newSet(HashingStrategies.<Key>defaultStrategy())
            .with(key, duplicateKey1);
    Verify.assertSize(1, set1);
    Verify.assertContains(key, set1);
    Assert.assertSame(key, set1.getFirst());

    Key duplicateKey2 = new Key("key");
    MutableSet<Key> set2 =
        UnifiedSetWithHashingStrategy.newSet(HashingStrategies.<Key>defaultStrategy())
            .with(key, duplicateKey1, duplicateKey2);
    Verify.assertSize(1, set2);
    Verify.assertContains(key, set2);
    Assert.assertSame(key, set2.getFirst());

    Key duplicateKey3 = new Key("key");
    MutableSet<Key> set3 =
        UnifiedSetWithHashingStrategy.newSet(HashingStrategies.<Key>defaultStrategy())
            .with(key, new Key("not a dupe"), duplicateKey3);
    Verify.assertSize(2, set3);
    Verify.assertContainsAll(set3, key, new Key("not a dupe"));
    Assert.assertSame(key, set3.detect(key::equals));
  }
 @SuppressWarnings("unchecked")
 @Override
 public <V> StateMachine<T> notify(final V state, final T value) {
   if (strict) {
     Assert.isTrue(states.contains(state), "State " + state + " has not been defined");
   }
   Event<T> ev = Event.wrap(value instanceof Event ? ((Event<T>) value).getData() : value);
   observable.notify(
       state,
       ev,
       new Consumer<Event<T>>() {
         @Override
         public void accept(final Event<T> ev) {
           transition(state, ev.getData());
         }
       });
   return this;
 }
  private static void assertUnifiedSetEqualsAndHashCode(int shift) {
    MutableSet<CollidingInt> set1 = MultiReaderUnifiedSet.newSet();
    Set<CollidingInt> set2 = new HashSet<CollidingInt>();
    MutableSet<CollidingInt> set3 = MultiReaderUnifiedSet.newSet();
    MutableSet<CollidingInt> set4 = MultiReaderUnifiedSet.newSet();

    int size = 100000;
    for (int i = 0; i < size; i++) {
      set1.add(new CollidingInt(i, shift));
      set2.add(new CollidingInt(i, shift));
      set3.add(new CollidingInt(i, shift));
      set4.add(new CollidingInt(size - i - 1, shift));
    }

    Assert.assertEquals(set1, set2);
    Assert.assertEquals(set1.hashCode(), set2.hashCode());
    Verify.assertSetsEqual(set1, set3);
    Verify.assertEqualsAndHashCode(set1, set3);
    Verify.assertSetsEqual(set2, set4);
    Assert.assertEquals(set4, set2);
    Assert.assertEquals(set2.hashCode(), set4.hashCode());
  }
 @Override
 public <V> StateMachine<T> on(final V state, final Function<T, T> fn) {
   if (strict) {
     Assert.isTrue(states.contains(state), "State " + state + " has not been defined");
   }
   observable.on(
       (state instanceof Selector ? (Selector) state : Selectors.object(state)),
       new Consumer<Event<T>>() {
         @SuppressWarnings("unchecked")
         @Override
         public void accept(final Event<T> data) {
           try {
             data.setData(fn.apply(data.getData()));
           } catch (Throwable t) {
             observable.notify(
                 t.getClass(), Event.wrap(new StateException(t, state, data.getData())));
           }
         }
       });
   return this;
 }
 @Test
 public void getFirstGetLast() {
   MutableSet<String> list5 = Sets.fixedSize.of("1", "2", "3", "4");
   Assert.assertEquals("1", list5.getFirst());
   Assert.assertEquals("4", list5.getLast());
 }
 @Test
 public void testClone() {
   MutableSet<String> set = this.newWith();
   MutableSet<String> clone = set.clone();
   Assert.assertSame(clone, set);
 }
 @Override
 public <V> boolean contains(V state) {
   return states.contains(state);
 }
 @Override
 public <V> StateMachine<T> define(V state) {
   states.add(state);
   return this;
 }
 @Test
 public void addingAllToOtherSet() {
   MutableSet<String> newSet = UnifiedSet.newSet(Sets.fixedSize.of("1", "2", "3", "4"));
   newSet.add("5");
   Verify.assertContainsAll(newSet, "1", "2", "3", "4", "5");
 }
  @Test
  public void iterator_remove() {
    int size = MORE_COLLISIONS.size();
    for (int i = 0; i < size; i++) {
      MutableSet<Integer> actual =
          UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, SIZE)
              .withAll(MORE_COLLISIONS);
      Iterator<Integer> iterator = actual.iterator();
      for (int j = 0; j <= i; j++) {
        Assert.assertTrue(iterator.hasNext());
        iterator.next();
      }
      iterator.remove();

      MutableSet<Integer> expected =
          UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, MORE_COLLISIONS);
      expected.remove(MORE_COLLISIONS.get(i));
      Assert.assertEquals(expected, actual);
    }

    // remove the last element from within a 2-level long chain that is fully populated
    MutableSet<Integer> set =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY,
            COLLISION_1,
            COLLISION_2,
            COLLISION_3,
            COLLISION_4,
            COLLISION_5,
            COLLISION_6,
            COLLISION_7);
    Iterator<Integer> iterator1 = set.iterator();
    for (int i = 0; i < 7; i++) {
      iterator1.next();
    }
    iterator1.remove();
    Assert.assertEquals(
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY,
            COLLISION_1,
            COLLISION_2,
            COLLISION_3,
            COLLISION_4,
            COLLISION_5,
            COLLISION_6),
        set);

    // remove the second-to-last element from a 2-level long chain that that has one empty slot
    Iterator<Integer> iterator2 = set.iterator();
    for (int i = 0; i < 6; i++) {
      iterator2.next();
    }
    iterator2.remove();
    Assert.assertEquals(
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY,
            COLLISION_1,
            COLLISION_2,
            COLLISION_3,
            COLLISION_4,
            COLLISION_5),
        set);

    // Testing removing the last element in a fully populated chained bucket
    UnifiedSetWithHashingStrategy<Integer> set2 =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY, COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
    Iterator<Integer> iterator3 = set2.iterator();
    for (int i = 0; i < 3; ++i) {
      iterator3.next();
    }
    iterator3.next();
    iterator3.remove();
    Verify.assertSetsEqual(UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3), set2);
  }
 @Test
 public void testNewEmpty() {
   MutableSet<String> set = this.unmodifiableSet.newEmpty();
   set.add(LED_ZEPPELIN);
   Verify.assertContains(LED_ZEPPELIN, set);
 }