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