/**
   * Check that the set is consistent, i.e all allocated slots are reachable by get(), and all
   * not-allocated contains nulls if Generic
   *
   * @param set
   */
  @After
  public void checkConsistency() {
    if (this.set != null) {
      int occupied = 0;

      final int mask = getKeys(this.set).length - 1;

      for (int i = 0; i < getKeys(this.set).length; i++) {
        if (!is_allocated(i, Intrinsics.<KType[]>cast(getKeys(this.set)))) {
          // if not allocated, generic version if patched to null for GC sake
          /*! #if ($TemplateOptions.KTypeGeneric) !*/
          TestUtils.assertEquals2(this.keyE, getKeys(this.set)[i]);
          /*! #end !*/
        } else {
          // try to reach the key by contains()
          Assert.assertTrue(this.set.contains(Intrinsics.<KType>cast(getKeys(this.set)[i])));

          occupied++;
        }
      }

      if (isAllocatedDefaultKey(this.set)) {

        // try to reach the key by contains()
        Assert.assertTrue(this.set.contains(this.keyE));

        occupied++;
      }

      Assert.assertEquals(occupied, this.set.size());
    }
  }
  @Test
  public void testForEachProcedure() {
    // Test that the container do not resize if less that the initial size

    // 1) Choose a map to build
    /*! #if ($TemplateOptions.isKType("GENERIC", "int", "long", "float", "double")) !*/
    final int NB_ELEMENTS = 2000;
    /*!
    #elseif ($TemplateOptions.isKType("short", "char"))
     int NB_ELEMENTS = 1000;
    #else
      int NB_ELEMENTS = 126;
    #end !*/

    final KTypeSet<KType> newSet = createNewSetInstance();

    newSet.add(this.keyE);

    // add a increasing number of key
    for (int i = 0; i < NB_ELEMENTS; i++) {

      final int KVpair = i;

      newSet.add(cast(KVpair));
    }

    // List the keys in the reverse-order of the internal buffer, since forEach() is iterating in
    // reverse also:
    final KTypeArrayList<KType> keyList = new KTypeArrayList<KType>();

    keyList.add(this.keyE);

    for (int i = getKeys(newSet).length - 1; i >= 0; i--) {

      if (is_allocated(i, Intrinsics.<KType[]>cast(getKeys(newSet)))) {

        keyList.add(Intrinsics.<KType>cast(getKeys(newSet)[i]));
      }
    }

    // Test forEach predicate and stop at each key in turn.
    final KTypeArrayList<KType> keyListTest = new KTypeArrayList<KType>();

    keyListTest.clear();

    // A) Run forEach(KType)

    newSet.forEach(
        new KTypeProcedure<KType>() {

          @Override
          public void apply(final KType key) {
            keyListTest.add(key);
          }
        });

    // check that keyList/keyListTest and valueList/valueListTest are identical.
    Assert.assertEquals(keyList, keyListTest);
  }
  @Test
  public void testForEachProcedureWithException() {

    // Test that the container do not resize if less that the initial size

    // 1) Choose a map to build
    /*! #if ($TemplateOptions.isKType("GENERIC", "int", "long", "float", "double")) !*/
    final int NB_ELEMENTS = 2000;
    /*!
    #elseif ($TemplateOptions.isKType("short", "char"))
     int NB_ELEMENTS = 1000;
    #else
      int NB_ELEMENTS = 126;
    #end !*/

    final KTypeSet<KType> newSet = createNewSetInstance();

    newSet.add(this.keyE);

    // add a increasing number of key
    for (int i = 0; i < NB_ELEMENTS; i++) {

      final int KVpair = i;

      newSet.add(cast(KVpair));
    }

    // List the keys in the reverse-order of the internal buffer, since forEach() is iterating in
    // reverse also:
    final KTypeArrayList<KType> keyList = new KTypeArrayList<KType>();

    keyList.add(this.keyE);

    // Test forEach predicate and stop at each key in turn.
    final KTypeArrayList<KType> keyListTest = new KTypeArrayList<KType>();

    for (int k = getKeys(newSet).length - 1; k >= 0; k--) {

      if (is_allocated(k, Intrinsics.<KType[]>cast(getKeys(newSet)))) {

        keyList.add(Intrinsics.<KType>cast(getKeys(newSet)[k]));
      }
    }

    final int size = keyList.size();

    for (int i = 0; i < size; i++) {
      final int currentPairIndexSizeToIterate = i + 1;

      keyListTest.clear();

      keyList.clear();

      keyList.add(this.keyE);

      for (int k = getKeys(newSet).length - 1; k >= 0; k--) {

        if (is_allocated(k, Intrinsics.<KType[]>cast(getKeys(newSet)))) {

          keyList.add(Intrinsics.<KType>cast(getKeys(newSet)[k]));
        }
      }

      // A) Run forEach(KType)
      try {
        newSet.forEach(
            new KTypeProcedure<KType>() {

              @Override
              public void apply(final KType key) {
                keyListTest.add(key);

                // when the stopping key/value pair is encountered, add to list and stop iteration
                if (key == keyList.get(currentPairIndexSizeToIterate - 1)) {
                  // interrupt iteration by an exception
                  throw new RuntimeException("Interrupted treatment by test");
                }
              }
            });
      } catch (final RuntimeException e) {
        if (!e.getMessage().equals("Interrupted treatment by test")) {
          throw e;
        }
      } finally {
        // despite the exception, the procedure terminates cleanly

        // check that keyList/keyListTest and valueList/valueListTest are identical for the first
        // currentPairIndexToIterate + 1 elements
        Assert.assertEquals("i = " + i, currentPairIndexSizeToIterate, keyListTest.size());

        for (int j = 0; j < currentPairIndexSizeToIterate; j++) {
          TestUtils.assertEquals2("j = " + j, keyList.get(j), keyListTest.get(j));
        }
      } // end finally
    } // end for each index
  }
  private boolean is_allocated(final int slot, final KType[] keys) {

    return !Intrinsics.<KType>isEmpty(keys[slot]);
  }