Search in sources :

Example 16 with Interval

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

the class UnifiedMapWithHashingStrategyTest method trimToSize.

@Test
public void trimToSize() {
    UnifiedMapWithHashingStrategy<String, String> map = UnifiedMapWithHashingStrategy.newMap(STRING_HASHING_STRATEGY);
    MutableMap<String, String> expected = Maps.mutable.empty();
    Interval integers = Interval.fromTo(0, 250);
    integers.each(each -> {
        map.put(each.toString(), each.toString());
        expected.put(each.toString(), each.toString());
    });
    ArrayIterate.forEach(FREQUENT_COLLISIONS, each -> {
        map.put(each, each);
        expected.put(each, each);
    });
    Assert.assertEquals(expected, map);
    Assert.assertEquals(261, map.size());
    MutableList<Integer> toRemove = Lists.mutable.withAll(Interval.evensFromTo(0, 20));
    toRemove.addAll(Interval.oddsFromTo(35, 55));
    toRemove.each(each -> {
        map.remove(each.toString());
        expected.remove(each.toString());
    });
    // First assertion to verify that trim does not happen since, the table is already at the smallest required power of 2.
    Assert.assertFalse(map.trimToSize());
    Assert.assertEquals(expected, map);
    Assert.assertEquals(239, map.size());
    Interval.evensFromTo(0, 250).each(each -> {
        map.remove(each.toString());
        expected.remove(each.toString());
    });
    // Second assertion to verify that trim happens since, the table length is less than smallest required power of 2.
    Assert.assertTrue(map.trimToSize());
    Assert.assertFalse(map.trimToSize());
    Assert.assertEquals(expected, map);
    Assert.assertEquals(124, map.size());
    expected.forEachKey(each -> Assert.assertEquals(each, map.get(each)));
    integers.each(each -> {
        map.remove(each.toString());
        expected.remove(each.toString());
    });
    Assert.assertTrue(map.trimToSize());
    Assert.assertFalse(map.trimToSize());
    Assert.assertEquals(expected, map);
    expected.forEachKey(each -> Assert.assertEquals(each, map.get(each)));
    map.clear();
    expected.clear();
    Assert.assertTrue(map.trimToSize());
    Interval.zeroTo(20).each(each -> {
        map.put(each.toString(), each.toString());
        expected.put(each.toString(), each.toString());
    });
    Assert.assertFalse(map.trimToSize());
    Interval.fromTo(9, 18).each(each -> {
        map.remove(each.toString());
        expected.remove(each.toString());
    });
    Assert.assertTrue(map.trimToSize());
    Assert.assertFalse(map.trimToSize());
    Assert.assertEquals(expected, map);
    expected.forEachKey(each -> Assert.assertEquals(each, map.get(each)));
    map.clear();
    Assert.assertTrue(map.trimToSize());
    Assert.assertTrue(map.isEmpty());
    Interval.zeroTo(6).each(each -> map.put(each.toString(), each.toString()));
    // Assert that trim does not happen as long as table.size is already as smaller than required
    Assert.assertFalse(map.trimToSize());
    map.put("7", "7");
    map.removeKey("2");
    map.removeKey("3");
    // Assert that trim does not happen as long as table.size is as smaller as required
    Assert.assertFalse(map.trimToSize());
    map.removeKey("5");
    map.removeKey("7");
    Assert.assertTrue(map.trimToSize());
    // Inflate the map so that table.length increases to next power of 2 and check that trim does not happen
    map.put("2", "2");
    map.put("5", "5");
    map.put("7", "7");
    // Assert that the resized table due to put is the required size and no need to trim that.
    Assert.assertFalse(map.trimToSize());
    Interval.zeroTo(4).each(each -> map.put(each.toString(), each.toString()));
    Interval.oneTo(3).each(each -> map.removeKey(each.toString()));
    Assert.assertTrue(map.trimToSize());
    Assert.assertEquals(5, map.size());
}
Also used : Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 17 with Interval

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

the class UnifiedSetTest method trimToSize.

@Test
public void trimToSize() {
    UnifiedSet<String> set = UnifiedSet.newSet();
    MutableSet<String> expected = Sets.mutable.empty();
    Interval integers = Interval.fromTo(0, 250);
    integers.each(each -> {
        set.add(each.toString());
        expected.add(each.toString());
    });
    ArrayIterate.forEach(FREQUENT_COLLISIONS, each -> {
        set.add(each);
        expected.add(each);
    });
    Assert.assertEquals(expected, set);
    Assert.assertEquals(261, set.size());
    MutableList<Integer> toRemove = Lists.mutable.withAll(Interval.evensFromTo(0, 20));
    toRemove.addAll(Interval.oddsFromTo(35, 55));
    toRemove.each(each -> {
        set.remove(each.toString());
        expected.remove(each.toString());
    });
    // First assertion to verify that trim does not happen since, the table is already at the smallest required power of 2.
    Assert.assertFalse(set.trimToSize());
    Assert.assertEquals(239, set.size());
    Assert.assertEquals(expected, set);
    Interval.evensFromTo(0, 250).each(each -> {
        set.remove(each.toString());
        expected.remove(each.toString());
    });
    // Second assertion to verify that trim happens since, the table length is less than smallest required power of 2.
    Assert.assertTrue(set.trimToSize());
    Assert.assertFalse(set.trimToSize());
    Assert.assertEquals(expected, set);
    Assert.assertEquals(124, set.size());
    expected.each(each -> Assert.assertEquals(each, set.get(each)));
    integers.each(each -> {
        set.remove(each.toString());
        expected.remove(each.toString());
    });
    Assert.assertTrue(set.trimToSize());
    Assert.assertFalse(set.trimToSize());
    Assert.assertEquals(expected, set);
    expected.each(each -> Assert.assertEquals(each, set.get(each)));
    set.clear();
    Assert.assertTrue(set.trimToSize());
    Interval.oneTo(4).each(each -> set.add(each.toString()));
    // Assert that trim does not happen after puts
    Assert.assertFalse(set.trimToSize());
    set.remove("1");
    set.remove("2");
    Assert.assertTrue(set.trimToSize());
    set.add("1");
    // Assert that the resized table due to put is the required size and no need to trim that.
    Assert.assertFalse(set.trimToSize());
    Interval.zeroTo(4).each(each -> set.add(each.toString()));
    Interval.oneTo(3).each(each -> set.remove(each.toString()));
    Assert.assertTrue(set.trimToSize());
    Assert.assertEquals(2, set.size());
}
Also used : Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 18 with Interval

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

the class FJIterateAcceptanceTest method setUp.

@Before
public void setUp() {
    Interval interval = Interval.oneTo(20000);
    this.iterables = Lists.immutable.of(interval.toList(), interval.toList().asUnmodifiable(), interval.toList().asSynchronized(), interval.toList().toImmutable(), interval.toSet(), interval.toSet().asUnmodifiable(), interval.toSet().asSynchronized(), interval.toSet().toImmutable(), interval.toBag(), interval.toBag().asUnmodifiable(), interval.toBag().asSynchronized(), interval.toBag().toImmutable(), interval.toSortedSet(), interval.toSortedSet().asUnmodifiable(), interval.toSortedSet().asSynchronized(), interval.toSortedSet().toImmutable(), interval.toMap(Functions.getPassThru(), Functions.getPassThru()), interval.toMap(Functions.getPassThru(), Functions.getPassThru()).asUnmodifiable(), interval.toMap(Functions.getPassThru(), Functions.getPassThru()).asSynchronized(), interval.toMap(Functions.getPassThru(), Functions.getPassThru()).toImmutable(), ArrayListAdapter.<Integer>newList().withAll(interval), ArrayListAdapter.<Integer>newList().withAll(interval).asUnmodifiable(), ArrayListAdapter.<Integer>newList().withAll(interval).asSynchronized(), new CompositeFastList<Integer>().withAll(interval.toList()), new CompositeFastList<Integer>().withAll(interval.toList()).asUnmodifiable(), new CompositeFastList<Integer>().withAll(interval.toList()).asSynchronized(), new CompositeFastList<Integer>().withAll(interval.toList()).toImmutable(), ListAdapter.adapt(new LinkedList<Integer>()).withAll(interval), ListAdapter.adapt(new LinkedList<Integer>()).withAll(interval).asUnmodifiable(), ListAdapter.adapt(new LinkedList<Integer>()).withAll(interval).asSynchronized(), UnifiedSetWithHashingStrategy.<Integer>newSet(HashingStrategies.defaultStrategy()).withAll(interval), UnifiedSetWithHashingStrategy.<Integer>newSet(HashingStrategies.defaultStrategy()).withAll(interval).asUnmodifiable(), UnifiedSetWithHashingStrategy.<Integer>newSet(HashingStrategies.defaultStrategy()).withAll(interval).asSynchronized(), UnifiedSetWithHashingStrategy.<Integer>newSet(HashingStrategies.defaultStrategy()).withAll(interval).toImmutable());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompositeFastList(org.eclipse.collections.impl.list.mutable.CompositeFastList) LinkedList(java.util.LinkedList) Interval(org.eclipse.collections.impl.list.Interval) Before(org.junit.Before)

Example 19 with Interval

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

the class SetsTest method newSet.

@Test
public void newSet() {
    for (int i = 1; i <= 5; i++) {
        Interval interval = Interval.oneTo(i);
        Verify.assertEqualsAndHashCode(UnifiedSet.newSet(interval), Sets.immutable.ofAll(interval));
    }
}
Also used : Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 20 with Interval

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

the class ListsTest method newList.

@Test
public void newList() {
    for (int i = 1; i <= 11; i++) {
        Interval interval = Interval.oneTo(i);
        Verify.assertEqualsAndHashCode(FastList.newList(interval), Lists.immutable.ofAll(interval));
    }
}
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