aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com
diff options
context:
space:
mode:
authorNorbert Schneider <norbert.schneider@code-intelligence.com>2023-05-05 11:29:45 +0200
committerNorbert Schneider <mail@bertschneider.de>2023-05-19 16:17:07 +0200
commit258897a900ecd337eba51f1f887cc09ec29a4602 (patch)
tree73982b8c70746fa6c0e18c56d5a270bc76c6aaf1 /src/test/java/com
parentaa61c89e00146d65a27c7c99ea7254eabe0b80b0 (diff)
downloadjazzer-api-258897a900ecd337eba51f1f887cc09ec29a4602.tar.gz
mutator: Map cross over
Diffstat (limited to 'src/test/java/com')
-rw-r--r--src/test/java/com/code_intelligence/jazzer/mutation/mutator/collection/MapMutatorTest.java148
1 files changed, 148 insertions, 0 deletions
diff --git a/src/test/java/com/code_intelligence/jazzer/mutation/mutator/collection/MapMutatorTest.java b/src/test/java/com/code_intelligence/jazzer/mutation/mutator/collection/MapMutatorTest.java
index c6004d87..4c2c14f9 100644
--- a/src/test/java/com/code_intelligence/jazzer/mutation/mutator/collection/MapMutatorTest.java
+++ b/src/test/java/com/code_intelligence/jazzer/mutation/mutator/collection/MapMutatorTest.java
@@ -17,8 +17,10 @@
package com.code_intelligence.jazzer.mutation.mutator.collection;
import static com.code_intelligence.jazzer.mutation.support.TestSupport.asMap;
+import static com.code_intelligence.jazzer.mutation.support.TestSupport.asMutableList;
import static com.code_intelligence.jazzer.mutation.support.TestSupport.mockPseudoRandom;
import static com.google.common.truth.Truth.assertThat;
+import static java.util.Collections.emptyMap;
import com.code_intelligence.jazzer.mutation.annotation.NotNull;
import com.code_intelligence.jazzer.mutation.annotation.WithSize;
@@ -29,6 +31,7 @@ import com.code_intelligence.jazzer.mutation.mutator.lang.LangMutators;
import com.code_intelligence.jazzer.mutation.support.TestSupport.MockPseudoRandom;
import com.code_intelligence.jazzer.mutation.support.TypeHolder;
import java.lang.reflect.AnnotatedType;
+import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
@@ -37,6 +40,12 @@ class MapMutatorTest {
public static final MutatorFactory FACTORY =
new ChainedMutatorFactory(LangMutators.newFactory(), CollectionMutators.newFactory());
+ private static SerializingMutator<Map<Integer, Integer>> defaultTestMapMutator() {
+ AnnotatedType type =
+ new TypeHolder<@NotNull Map<@NotNull Integer, @NotNull Integer>>() {}.annotatedType();
+ return (SerializingMutator<Map<Integer, Integer>>) FACTORY.createOrThrow(type);
+ }
+
@Test
void mapInitInsert() {
AnnotatedType type =
@@ -204,4 +213,143 @@ class MapMutatorTest {
}
assertThat(map).containsExactly(false, true, true, true).inOrder();
}
+
+ @Test
+ void testCrossOverEmptyMaps() {
+ SerializingMutator<@NotNull Map<@NotNull Integer, @NotNull Integer>> mutator =
+ defaultTestMapMutator();
+
+ try (MockPseudoRandom prng = mockPseudoRandom()) {
+ Map<Integer, Integer> map = mutator.crossOver(emptyMap(), emptyMap(), prng);
+ assertThat(map).isEmpty();
+ }
+ }
+
+ @Test
+ void testCrossOverInsertChunk() {
+ SerializingMutator<@NotNull Map<@NotNull Integer, @NotNull Integer>> mutator =
+ defaultTestMapMutator();
+
+ Map<Integer, Integer> map = asMap(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6);
+ Map<Integer, Integer> otherMap = asMap(1, 1, 2, 2, 3, 3, 40, 40, 50, 50, 60, 60);
+
+ try (MockPseudoRandom prng = mockPseudoRandom(
+ // insert action
+ 0,
+ // chunk size
+ 3,
+ // from chunk offset, will skip first element of chunk as it is already present in map
+ 3,
+ // to chunk offset, unused
+ 0)) {
+ map = mutator.crossOver(map, otherMap, prng);
+ assertThat(map)
+ .containsExactly(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 40, 40, 50, 50, 60, 60)
+ .inOrder();
+ }
+ }
+
+ @Test
+ void testCrossOverOverwriteChunk() {
+ SerializingMutator<@NotNull Map<@NotNull Integer, @NotNull Integer>> mutator =
+ defaultTestMapMutator();
+
+ Map<Integer, Integer> map = asMap(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6);
+ Map<Integer, Integer> otherMap = asMap(1, 1, 2, 2, 3, 3, 40, 40, 50, 50, 60, 60);
+
+ try (MockPseudoRandom prng = mockPseudoRandom(
+ // overwrite action
+ 1,
+ // chunk size
+ 3,
+ // from chunk offset
+ 2,
+ // to chunk offset, will not change first element as values are equal
+ 2)) {
+ map = mutator.crossOver(map, otherMap, prng);
+ assertThat(map).containsExactly(1, 1, 2, 2, 3, 3, 4, 40, 5, 50, 6, 6).inOrder();
+ }
+ }
+
+ @Test
+ void testCrossOverCrossOverChunkKeys() {
+ AnnotatedType type =
+ new TypeHolder<@NotNull Map<@NotNull List<@NotNull Integer>, @NotNull Integer>>() {
+ }.annotatedType();
+ SerializingMutator<@NotNull Map<@NotNull List<@NotNull Integer>, @NotNull Integer>> mutator =
+ (SerializingMutator<@NotNull Map<@NotNull List<@NotNull Integer>, @NotNull Integer>>)
+ FACTORY.createOrThrow(type);
+
+ Map<List<Integer>, Integer> map = asMap(asMutableList(1), 1, asMutableList(2), 2,
+ asMutableList(3), 3, asMutableList(4), 4, asMutableList(5), 5, asMutableList(6), 6);
+ Map<List<Integer>, Integer> otherMap = asMap(asMutableList(1), 1, asMutableList(2), 2,
+ asMutableList(3), 3, asMutableList(40), 4, asMutableList(50), 5, asMutableList(60), 6);
+
+ try (MockPseudoRandom prng = mockPseudoRandom(
+ // cross over action
+ 2,
+ // keys
+ true,
+ // chunk size
+ 3,
+ // from chunk offset
+ 2,
+ // to chunk offset,
+ // first keys ("3") are equal and will be overwritten
+ 2,
+ // first key, delegate to list cross over, overwrite 1 entry at offset 0 from offset 0
+ 1, 1, 0, 0,
+ // second key, delegate to list cross over, overwrite 1 entry at offset 0 from offset 0
+ 1, 1, 0, 0,
+ // third key, delegate to list cross over, overwrite 1 entry at offset 0 from offset 0
+ 1, 1, 0, 0)) {
+ map = mutator.crossOver(map, otherMap, prng);
+ assertThat(map)
+ .containsExactly(asMutableList(1), 1, asMutableList(2), 2, asMutableList(6), 6,
+ // Overwritten keys after here
+ asMutableList(3), 3, asMutableList(40), 4, asMutableList(50), 5)
+ .inOrder();
+ }
+ }
+
+ @Test
+ void testCrossOverCrossOverChunkValues() {
+ AnnotatedType type =
+ new TypeHolder<@NotNull Map<@NotNull Integer, @NotNull List<@NotNull Integer>>>() {
+ }.annotatedType();
+ SerializingMutator<@NotNull Map<@NotNull Integer, @NotNull List<@NotNull Integer>>> mutator =
+ (SerializingMutator<@NotNull Map<@NotNull Integer, @NotNull List<@NotNull Integer>>>)
+ FACTORY.createOrThrow(type);
+
+ Map<Integer, List<Integer>> map = asMap(1, asMutableList(1), 2, asMutableList(2), 3,
+ asMutableList(3), 4, asMutableList(4), 5, asMutableList(5), 6, asMutableList(6));
+ Map<Integer, List<Integer>> otherMap = asMap(1, asMutableList(1), 2, asMutableList(2), 3,
+ asMutableList(30), 40, asMutableList(40), 50, asMutableList(50), 60, asMutableList(60));
+
+ try (
+ MockPseudoRandom prng = mockPseudoRandom(
+ // cross over action
+ 2,
+ // values
+ false,
+ // chunk size
+ 3,
+ // from chunk offset
+ 2,
+ // to chunk offset,
+ 2,
+ // first value, delegate to list cross over, overwrite 1 entry at offset 0 from offset 0
+ 1, 1, 0, 0,
+ // second value, delegate to list cross over, overwrite 1 entry at offset 0 from offset
+ // 0
+ 1, 1, 0, 0,
+ // third value, delegate to list cross over, overwrite 1 entry at offset 0 from offset 0
+ 1, 1, 0, 0)) {
+ map = mutator.crossOver(map, otherMap, prng);
+ assertThat(map)
+ .containsExactly(1, asMutableList(1), 2, asMutableList(2), 3, asMutableList(30), 4,
+ asMutableList(40), 5, asMutableList(50), 6, asMutableList(6))
+ .inOrder();
+ }
+ }
}