Search in sources :

Example 6 with Counter

use of org.eclipse.collections.impl.Counter in project eclipse-collections by eclipse.

the class AbstractMutableSetTestCase method forEachWith.

@Override
@Test
public void forEachWith() {
    super.forEachWith();
    Object sentinel = new Object();
    int size = MORE_COLLISIONS.size();
    for (int i = 1; i < size; i++) {
        MutableSet<Integer> set = this.newWith();
        set.addAll(MORE_COLLISIONS.subList(0, i));
        MutableSet<Integer> result = UnifiedSet.newSet();
        set.forEachWith((argument1, argument2) -> {
            Assert.assertSame(sentinel, argument2);
            result.add(argument1);
        }, sentinel);
        Assert.assertEquals(set, result);
    }
    // test iterating on a bucket with only one element
    MutableSet<Integer> set = this.newWith(COLLISION_1, COLLISION_2);
    set.remove(COLLISION_2);
    Counter counter = new Counter();
    set.forEachWith((argument1, argument2) -> argument2.increment(), counter);
    Assert.assertEquals(1, counter.getCount());
}
Also used : Counter(org.eclipse.collections.impl.Counter) Test(org.junit.Test)

Example 7 with Counter

use of org.eclipse.collections.impl.Counter in project eclipse-collections by eclipse.

the class UnmodifiableMutableCollectionTest method iterator.

@Test
public void iterator() {
    Counter counter = new Counter();
    for (Object each : this.unmodifiableCollection) {
        counter.increment();
    }
    Assert.assertEquals(4, counter.getCount());
}
Also used : Counter(org.eclipse.collections.impl.Counter) Test(org.junit.Test)

Example 8 with Counter

use of org.eclipse.collections.impl.Counter in project eclipse-collections by eclipse.

the class AbstractMutableSortedBagTestCase method forEachWithOccurrences.

@Override
@Test
public void forEachWithOccurrences() {
    super.forEachWithOccurrences();
    MutableSortedBag<Integer> bag = this.newWith(Collections.reverseOrder(), 3, 3, 3, 2, 2, 1);
    MutableList<Integer> actualItems = FastList.newList();
    MutableList<Integer> actualIndexes = FastList.newList();
    bag.forEachWithOccurrences((each, index) -> {
        actualItems.add(each);
        actualIndexes.add(index);
    });
    Assert.assertEquals(FastList.newListWith(3, 2, 1), actualItems);
    Assert.assertEquals(FastList.newListWith(3, 2, 1), actualIndexes);
    MutableSortedBag<Integer> bag2 = this.newWith();
    bag2.addOccurrences(1, 10);
    bag2.addOccurrences(2, 10);
    bag2.addOccurrences(3, 10);
    IntegerSum sum = new IntegerSum(0);
    Counter counter = new Counter();
    bag2.forEachWithOccurrences((each, occurrences) -> {
        counter.increment();
        sum.add(each * occurrences * counter.getCount());
    });
    Assert.assertEquals(140, sum.getIntSum());
    bag2.removeOccurrences(2, 1);
    IntegerSum sum2 = new IntegerSum(0);
    bag2.forEachWithOccurrences((each, occurrences) -> sum2.add(each * occurrences));
    Assert.assertEquals(58, sum2.getIntSum());
    bag2.removeOccurrences(1, 3);
    IntegerSum sum3 = new IntegerSum(0);
    bag2.forEachWithOccurrences((each, occurrences) -> sum3.add(each * occurrences));
    Assert.assertEquals(55, sum3.getIntSum());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Counter(org.eclipse.collections.impl.Counter) Test(org.junit.Test)

Example 9 with Counter

use of org.eclipse.collections.impl.Counter in project eclipse-collections by eclipse.

the class AbstractMutableSetTestCase method tap.

@Override
@Test
public void tap() {
    super.tap();
    int size = MORE_COLLISIONS.size();
    for (int i = 1; i < size; i++) {
        MutableList<Integer> tapResult = Lists.mutable.of();
        MutableSet<Integer> set = this.newWith();
        set.addAll(MORE_COLLISIONS.subList(0, i));
        Assert.assertSame(set, set.tap(tapResult::add));
        Assert.assertEquals(set.toList(), tapResult);
    }
    // test iterating on a bucket with only one element
    MutableSet<Integer> set = this.newWith(COLLISION_1, COLLISION_2);
    set.remove(COLLISION_2);
    Counter counter = new Counter();
    Assert.assertSame(set, set.tap(x -> counter.increment()));
    Assert.assertEquals(1, counter.getCount());
}
Also used : Arrays(java.util.Arrays) ArrayAdapter(org.eclipse.collections.impl.list.fixed.ArrayAdapter) Function(org.eclipse.collections.api.block.function.Function) UnsortedSetIterable(org.eclipse.collections.api.set.UnsortedSetIterable) Iterables.mList(org.eclipse.collections.impl.factory.Iterables.mList) Verify(org.eclipse.collections.impl.test.Verify) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) AbstractCollectionTestCase(org.eclipse.collections.impl.collection.mutable.AbstractCollectionTestCase) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) IntegerWithCast(org.eclipse.collections.impl.IntegerWithCast) TreeBag(org.eclipse.collections.impl.bag.sorted.mutable.TreeBag) Interval(org.eclipse.collections.impl.list.Interval) NoSuchElementException(java.util.NoSuchElementException) Pair(org.eclipse.collections.api.tuple.Pair) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Counter(org.eclipse.collections.impl.Counter) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) Iterator(java.util.Iterator) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) MutableSortedBag(org.eclipse.collections.api.bag.sorted.MutableSortedBag) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Test(org.junit.Test) Iterate(org.eclipse.collections.impl.utility.Iterate) Iterables.iSet(org.eclipse.collections.impl.factory.Iterables.iSet) Lists(org.eclipse.collections.impl.factory.Lists) Assert(org.junit.Assert) Collections(java.util.Collections) Counter(org.eclipse.collections.impl.Counter) Test(org.junit.Test)

Example 10 with Counter

use of org.eclipse.collections.impl.Counter in project eclipse-collections by eclipse.

the class AbstractMutableSetTestCase method forEachWithIndex.

@Override
@Test
public void forEachWithIndex() {
    super.forEachWithIndex();
    int size = MORE_COLLISIONS.size();
    for (int i = 1; i < size; i++) {
        MutableSet<Integer> set = this.newWith();
        set.addAll(MORE_COLLISIONS.subList(0, i));
        MutableSet<Integer> result = UnifiedSet.newSet();
        MutableList<Integer> indexes = Lists.mutable.of();
        set.forEachWithIndex((each, index) -> {
            result.add(each);
            indexes.add(index);
        });
        Assert.assertEquals(set, result);
        Assert.assertEquals(Interval.zeroTo(i - 1), indexes);
    }
    // test iterating on a bucket with only one element
    UnifiedSet<Integer> set = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2);
    set.remove(COLLISION_2);
    Counter counter = new Counter();
    set.forEachWithIndex((each, index) -> counter.increment());
    Assert.assertEquals(1, counter.getCount());
}
Also used : Counter(org.eclipse.collections.impl.Counter) Test(org.junit.Test)

Aggregations

Counter (org.eclipse.collections.impl.Counter)25 Test (org.junit.Test)10 MutableList (org.eclipse.collections.api.list.MutableList)6 NoSuchElementException (java.util.NoSuchElementException)5 Function (org.eclipse.collections.api.block.function.Function)5 Predicates (org.eclipse.collections.impl.block.factory.Predicates)5 Procedures (org.eclipse.collections.impl.block.factory.Procedures)5 FastList (org.eclipse.collections.impl.list.mutable.FastList)5 Collections (java.util.Collections)4 Iterator (java.util.Iterator)4 RichIterable (org.eclipse.collections.api.RichIterable)4 TreeBag (org.eclipse.collections.impl.bag.sorted.mutable.TreeBag)4 Lists (org.eclipse.collections.impl.factory.Lists)4 Interval (org.eclipse.collections.impl.list.Interval)4 Assert (org.junit.Assert)4 Map (java.util.Map)3 LazyIterable (org.eclipse.collections.api.LazyIterable)3 MutableSortedBag (org.eclipse.collections.api.bag.sorted.MutableSortedBag)3 MutableSortedMap (org.eclipse.collections.api.map.sorted.MutableSortedMap)3 TreeSortedMap (org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap)3