Exemplo n.º 1
0
  @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);
  }
Exemplo n.º 2
0
  @Test
  public void testPooledIteratorFullIteratorLoop() {
    // A) for-each loop interrupted

    // must accommodate even the smallest primitive type
    // so that the iteration do not break before it should...
    final int TEST_SIZE = 126;
    final long TEST_ROUNDS = 5000;

    final KTypeSet<KType> testContainer = createSetWithOrderedData(TEST_SIZE);

    final long checksum =
        testContainer.forEach(
                new KTypeProcedure<KType>() {

                  long count;

                  @Override
                  public void apply(final KType value) {
                    this.count += castType(value);
                  }
                })
            .count;

    long testValue = 0;
    final int startingPoolSize = getEntryPoolSize(testContainer);

    for (int round = 0; round < TEST_ROUNDS; round++) {
      // Classical iterator loop, with manually allocated Iterator
      final int initialPoolSize = getEntryPoolSize(testContainer);

      final AbstractIterator<KTypeCursor<KType>> loopIterator =
          (AbstractIterator<KTypeCursor<KType>>) testContainer.iterator();

      Assert.assertEquals(initialPoolSize - 1, getEntryPoolSize(testContainer));

      testValue = 0;
      while (loopIterator.hasNext()) {
        testValue += castType(loopIterator.next().value);
      } // end IteratorLoop

      // iterator is returned automatically to its pool, by normal iteration termination
      Assert.assertEquals(initialPoolSize, getEntryPoolSize(testContainer));

      // checksum
      Assert.assertEquals(checksum, testValue);
    } // end for rounds

    // pool initial size is untouched anyway
    Assert.assertEquals(startingPoolSize, getEntryPoolSize(testContainer));
  }
Exemplo n.º 3
0
  @Test
  public void testPooledIteratorForEach() {
    // A) Unbroken for-each loop
    // must accommodate even the smallest primitive type
    // so that the iteration do not break before it should...
    final int TEST_SIZE = 126;
    final long TEST_ROUNDS = 5000;

    final KTypeSet<KType> testContainer = createSetWithOrderedData(TEST_SIZE);

    final long checksum =
        testContainer.forEach(
                new KTypeProcedure<KType>() {

                  long count;

                  @Override
                  public void apply(final KType value) {

                    this.count += castType(value);
                  }
                })
            .count;

    long testValue = 0;
    final long initialPoolSize = getEntryPoolSize(testContainer);

    for (int round = 0; round < TEST_ROUNDS; round++) {
      // for-each in test :
      testValue = 0;
      for (final KTypeCursor<KType> cursor : testContainer) {
        // we consume 1 iterator for this loop
        Assert.assertEquals(initialPoolSize - 1, getEntryPoolSize(testContainer));

        testValue += castType(cursor.value);
      }

      // check checksum the iteration
      Assert.assertEquals(checksum, testValue);

      // iterator is returned to its pool
      Assert.assertEquals(initialPoolSize, getEntryPoolSize(testContainer));
    } // end for rounds
  }
Exemplo n.º 4
0
  @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
  }
Exemplo n.º 5
0
  @Test
  public void testPooledIteratorExceptionIteratorLoop() {
    // must accommodate even the smallest primitive type
    // so that the iteration do not break before it should...
    final int TEST_SIZE = 126;
    final long TEST_ROUNDS = 5000;

    final KTypeSet<KType> testContainer = createSetWithOrderedData(TEST_SIZE);

    final long checksum =
        testContainer.forEach(
                new KTypeProcedure<KType>() {

                  long count;

                  @Override
                  public void apply(final KType value) {
                    this.count += castType(value);
                  }
                })
            .count;

    final int startingPoolSize = getEntryPoolSize(testContainer);

    int count = 0;
    AbstractIterator<KTypeCursor<KType>> loopIterator = null;

    for (int round = 0; round < TEST_ROUNDS; round++) {
      try {
        loopIterator = (AbstractIterator<KTypeCursor<KType>>) testContainer.iterator();

        Assert.assertEquals(startingPoolSize - 1, getEntryPoolSize(testContainer));

        int guard = 0;
        count = 0;
        while (loopIterator.hasNext()) {
          guard += castType(loopIterator.next().value);

          // brutally interrupt in the middle some of the loops, but not all
          if (round > TEST_ROUNDS / 2 && count > TEST_SIZE / 2) {
            throw new Exception("Oups some problem in the loop occured");
          }
          count++;
        } // end while

        // iterator is returned to its pool in case of normal loop termination
        Assert.assertEquals(startingPoolSize, getEntryPoolSize(testContainer));
        Assert.assertEquals(checksum, guard);

      } catch (final Exception e) {
        // iterator is NOT returned to its pool because of the exception
        Assert.assertEquals(startingPoolSize - 1, getEntryPoolSize(testContainer));

        // manual return to the pool then
        loopIterator.release();

        // now the pool is restored
        Assert.assertEquals(startingPoolSize, getEntryPoolSize(testContainer));
      }
    } // end for rounds

    // pool initial size is untouched anyway
    Assert.assertEquals(startingPoolSize, getEntryPoolSize(testContainer));
  }