Search in sources :

Example 21 with Interval

use of org.eclipse.collections.impl.list.Interval in project eclipse-collections by eclipse.

the class TakeIteratorTest method iterator.

@Test
public void iterator() {
    Interval list = Interval.oneTo(5);
    Iterator<Integer> iterator1 = new TakeIterator<>(list.iterator(), 2);
    assertElements(iterator1, 2);
    Iterator<Integer> iterator2 = new TakeIterator<>(list, 5);
    assertElements(iterator2, 5);
    Iterator<Integer> iterator3 = new TakeIterator<>(list, 10);
    assertElements(iterator3, 5);
    Iterator<Integer> iterator4 = new TakeIterator<>(list, 0);
    assertElements(iterator4, 0);
    Iterator<Integer> iterator5 = new TakeIterator<>(Lists.fixedSize.of(), 0);
    assertElements(iterator5, 0);
}
Also used : Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 22 with Interval

use of org.eclipse.collections.impl.list.Interval in project eclipse-collections by eclipse.

the class MultiReaderHashBagTest method concurrentWrite.

@Test
public void concurrentWrite() {
    MultiReaderHashBag<Integer> numbers = this.newWith();
    Interval interval = Interval.oneTo(100);
    ParallelIterate.forEach(interval, each -> {
        numbers.add(each);
        Verify.assertSize(1, numbers.select(each::equals));
        numbers.add(each);
        Assert.assertEquals(2, numbers.count(each::equals));
        numbers.add(each);
        Integer[] removed = new Integer[1];
        numbers.withWriteLockAndDelegate(bag -> {
            Iterator<Integer> iterator = bag.iterator();
            removed[0] = iterator.next();
            bag.remove(removed[0]);
            bag.add(removed[0]);
        });
        numbers.add(each);
        Assert.assertEquals(4, numbers.count(each::equals));
    }, 1);
    interval.forEach(Procedures.cast(each -> Assert.assertEquals(4, numbers.occurrencesOf(each))));
}
Also used : PartitionMutableCollection(org.eclipse.collections.api.partition.PartitionMutableCollection) Function(org.eclipse.collections.api.block.function.Function) Verify(org.eclipse.collections.impl.test.Verify) AtomicReference(java.util.concurrent.atomic.AtomicReference) MutableBag(org.eclipse.collections.api.bag.MutableBag) FastList(org.eclipse.collections.impl.list.mutable.FastList) Function2(org.eclipse.collections.api.block.function.Function2) Interval(org.eclipse.collections.impl.list.Interval) ImmutableBag(org.eclipse.collections.api.bag.ImmutableBag) SerializeTestHelper(org.eclipse.collections.impl.test.SerializeTestHelper) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) ParallelIterate(org.eclipse.collections.impl.parallel.ParallelIterate) Iterator(java.util.Iterator) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) IntegerPredicates(org.eclipse.collections.impl.block.factory.IntegerPredicates) MultiReaderMutableCollectionTestCase(org.eclipse.collections.impl.set.mutable.MultiReaderMutableCollectionTestCase) Bags(org.eclipse.collections.impl.factory.Bags) Procedures(org.eclipse.collections.impl.block.factory.Procedures) IntPredicates(org.eclipse.collections.impl.block.factory.primitive.IntPredicates) Test(org.junit.Test) Sets(org.eclipse.collections.impl.factory.Sets) AddFunction(org.eclipse.collections.impl.block.function.AddFunction) Assert(org.junit.Assert) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 23 with Interval

use of org.eclipse.collections.impl.list.Interval in project eclipse-collections by eclipse.

the class MultiReaderHashBagTest method parallelCollect.

@Test
public void parallelCollect() {
    MultiReaderHashBag<String> numbers = this.newWith();
    Interval interval = Interval.oneTo(50000);
    ParallelIterate.collect(interval, String::valueOf, numbers, true);
    Assert.assertEquals(numbers, interval.collect(String::valueOf).toBag());
}
Also used : Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 24 with Interval

use of org.eclipse.collections.impl.list.Interval in project eclipse-collections by eclipse.

the class ImmutableArrayBagTest method newWith.

@Override
@Test
public void newWith() {
    super.newWith();
    int maximumUsefulArrayBagSize = ImmutableArrayBag.MAXIMUM_USEFUL_ARRAY_BAG_SIZE;
    Verify.assertInstanceOf(ImmutableArrayBag.class, Bags.immutable.ofAll(Interval.oneTo(maximumUsefulArrayBagSize - 1)).newWith(maximumUsefulArrayBagSize));
    Verify.assertInstanceOf(ImmutableHashBag.class, Bags.immutable.ofAll(Interval.oneTo(maximumUsefulArrayBagSize)).newWith(maximumUsefulArrayBagSize + 1));
    Interval items = Interval.oneTo(maximumUsefulArrayBagSize);
    Verify.assertInstanceOf(ImmutableHashBag.class, new ImmutableArrayBag<>(items.toArray(), items.toIntArray()).newWith(maximumUsefulArrayBagSize + 1));
}
Also used : Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 25 with Interval

use of org.eclipse.collections.impl.list.Interval in project eclipse-collections by eclipse.

the class ImmutableArrayBagTest method testNewBag.

@Test
public void testNewBag() {
    for (int i = 1; i <= ImmutableArrayBag.MAXIMUM_USEFUL_ARRAY_BAG_SIZE + 1; i++) {
        Interval interval = Interval.oneTo(i);
        Verify.assertEqualsAndHashCode(HashBag.newBag(interval), Bags.immutable.ofAll(interval));
    }
    Verify.assertThrows(IllegalArgumentException.class, () -> new ImmutableArrayBag<>(new Integer[] { 2, 3 }, new int[] { 2 }));
}
Also used : Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Aggregations

Interval (org.eclipse.collections.impl.list.Interval)32 Test (org.junit.Test)23 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 LinkedList (java.util.LinkedList)10 CompositeFastList (org.eclipse.collections.impl.list.mutable.CompositeFastList)10 Before (org.junit.Before)10 Function (org.eclipse.collections.api.block.function.Function)9 FastList (org.eclipse.collections.impl.list.mutable.FastList)9 Verify (org.eclipse.collections.impl.test.Verify)9 Assert (org.junit.Assert)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 MutableList (org.eclipse.collections.api.list.MutableList)8 MutableMap (org.eclipse.collections.api.map.MutableMap)8 BigInteger (java.math.BigInteger)7 Function2 (org.eclipse.collections.api.block.function.Function2)7 ImmutableList (org.eclipse.collections.api.list.ImmutableList)7 Predicates (org.eclipse.collections.impl.block.factory.Predicates)7 BigDecimal (java.math.BigDecimal)6 Arrays (java.util.Arrays)6