Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jan 18, 2025
1 parent e9d420b commit 99ac957
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.function.BiConsumer;

import static kala.collection.internal.champ.ChampNode.improve;

Expand Down Expand Up @@ -265,6 +266,11 @@ public boolean containsKey(K key) {
return withTree(rootNode.removed(key, keyUnimprovedHash, improve(keyUnimprovedHash), 0));
}

@Override
public void forEach(@NotNull BiConsumer<? super K, ? super V> consumer) {
rootNode.forEach(consumer);
}

private static final class Factory<K, V> implements MapFactory<K, V, ChampMapBuilder<K, V>, ImmutableChampMap<K, V>> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public BitmapIndexedChampMapNode<K, V> copyAndMigrateFromNodeToInline(int bitpos
}

@Override
public void forEach(BiConsumer<K, V> consumer) {
public void forEach(BiConsumer<? super K, ? super V> consumer) {
var iN = payloadArity(); // arity doesn't change during this operation
var i = 0;
while (i < iN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static <K, V> BitmapIndexedChampMapNode<K, V> empty() {

public abstract int size();

public abstract void forEach(BiConsumer<K, V> consumer);
public abstract void forEach(BiConsumer<? super K, ? super V> consumer);

public abstract <U> ChampMapNode<K, U> transform(BiFunction<K, V, U> function);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public int getHash(int index) {
}

@Override
public void forEach(BiConsumer<K, V> consumer) {
public void forEach(BiConsumer<? super K, ? super V> consumer) {
content.forEach(Consumers.tupled(consumer));
}

Expand Down
43 changes: 43 additions & 0 deletions src/test/template/kala/collection/MapLikeTestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import kala.control.Option;
import kala.tuple.Tuple;
import kala.tuple.Tuple2;
import kala.value.primitive.BooleanVar;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.BiConsumer;

import static kala.ExtendedAssertions.assertMapEntries;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -255,4 +257,45 @@ default void removedTest() {
assertMapEntries(List.of(entry(0, "0"), entry(2, "2"), entry(3, "3"), entry(4, "4")), testMap.removed(1));
assertMapEntries(List.of(entry(0, "0"), entry(1, "1"), entry(2, "2"), entry(3, "3"), entry(4, "4")), testMap.removed(5));
}

@Test
default void forEachTest() {
{
final var empty = this.<Integer, String>ofEntries();
final var holder = new BooleanVar(false);
empty.forEach((k, v) -> holder.value = true);
assertFalse(holder.value);
}

class Helper<K, V> implements BiConsumer<K, V>, AutoCloseable {
final java.util.HashMap<K, V> values = new java.util.HashMap<>();

@Override
public void accept(K k, V v) {
assertTrue(values.remove(k, v), () -> "Value not removed: key=" + k + ", value=" + v);
}

public void close() {
assertTrue(values.isEmpty(), () -> "The remaining values: " + values);
}
}

try (var helper = new Helper<Integer, String>()) {
final int n = 5;
for (int i = 0; i < n; i++) {
helper.values.put(i, Integer.toString(i));
}

testMap(n).forEach(helper);
}

try (var helper = new Helper<HashCollisionsValue, Integer>()) {
final int n = 10000;
for (int i = 0; i < n; i++) {
helper.values.put(new HashCollisionsValue(i), i);
}

testMapHashCollisions(n).forEach(helper);
}
}
}

0 comments on commit 99ac957

Please sign in to comment.