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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations