diff options
author | 434b <17012133+0xricksanchez@users.noreply.github.com> | 2023-04-06 20:42:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-06 18:42:21 +0000 |
commit | 2825c5e8c8976551378fc912bdd9245fd5501e71 (patch) | |
tree | d18b81697736a343c6af05948cef1bbdf47d7426 /src/test/java/com | |
parent | 32e6c84d10e439d4cee3380e44c904bf48230983 (diff) | |
download | jazzer-api-2825c5e8c8976551378fc912bdd9245fd5501e71.tar.gz |
map mutator improvements (#690)
improve map mutator
Diffstat (limited to 'src/test/java/com')
-rw-r--r-- | src/test/java/com/code_intelligence/jazzer/mutation/mutator/collection/MapMutatorTest.java | 130 |
1 files changed, 107 insertions, 23 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 a5b4e4d8..3713d11d 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 @@ -61,34 +61,22 @@ class MapMutatorTest { assertThat(map).hasSize(1); assertThat(map.get("Key")).isEqualTo("Value"); - // Add new entry + // Add new entry with a map size of 1 try (MockPseudoRandom prng = mockPseudoRandom( - // Add new entry - true, - // New key size - 3, - // Key value - "New".getBytes(), - // New value size - 3, - // Value value - "New".getBytes())) { + // mutate entry, prng.choice() + true)) { map = mutator.mutate(map, prng); } - assertThat(map).hasSize(2); - assertThat(map.get("New")).isEqualTo("New"); + assertThat(map).hasSize(1); + assertThat(map.get("New")).isEqualTo(null); // Mutate "New" entry try (MockPseudoRandom prng = mockPseudoRandom( // Mutate entry - false, - // Index - 1, - // Mutate value - false)) { + true)) { map = mutator.mutate(map, prng); } - assertThat(map).hasSize(2); + assertThat(map).hasSize(1); assertThat(map.get("New")).isNotEqualTo("New"); } @@ -131,7 +119,7 @@ class MapMutatorTest { // Add new entry try (MockPseudoRandom prng = mockPseudoRandom( // Add new entry - true, + 0, // New key size 3, // Key value @@ -147,9 +135,9 @@ class MapMutatorTest { // Remove one as max size reached try (MockPseudoRandom prng = mockPseudoRandom( - // Add new entry - true, - // "Index" to remove + // Remove an entry + 0, + // skip keys 1)) { map = mutator.mutate(map, prng); } @@ -157,4 +145,100 @@ class MapMutatorTest { assertThat(map).containsKey("Key1"); assertThat(map).containsKey("New"); } + + @Test + void mapMutateChunks() { + AnnotatedType type = new TypeHolder<@NotNull @WithSize( + min = 2, max = 6) Map<@NotNull String, @NotNull String>>(){} + .annotatedType(); + SerializingMutator<Map<String, String>> mutator = + (SerializingMutator<Map<String, String>>) FACTORY.createOrThrow(type); + assertThat(mutator.toString()).isEqualTo("Map<String,String>"); + + // Initialize new map with min size + Map<String, String> map; + try (MockPseudoRandom prng = mockPseudoRandom( + // Initial map size + 2, + // Key 1 size + 4, + // Key 1 value + "Key1".getBytes(), + // Value size + 6, + // Value value + "Value1".getBytes(), + // Key 2 size + 4, + // Key 2 value + "Key2".getBytes(), + // Value size + 6, + // Value value + "Value2".getBytes())) { + map = mutator.init(prng); + } + assertThat(map).hasSize(2); + assertThat(map).containsEntry("Key1", "Value1"); + assertThat(map).containsEntry("Key2", "Value2"); + + // Add 2 new entries + try (MockPseudoRandom prng = mockPseudoRandom( + // grow chunk + 1, + // ChunkSize + 2, + // Key 3 size + 4, + // Key 3 value + "Key3".getBytes(), + // Value size + 6, + // Value value + "Value3".getBytes(), + // Key 4 size + 4, + // Key 4 value + "Key4".getBytes(), + // Value size + 6, + // Value value + "Value4".getBytes())) { + map = mutator.mutate(map, prng); + } + assertThat(map).hasSize(4); + assertThat(map).containsEntry("Key1", "Value1"); + assertThat(map).containsEntry("Key2", "Value2"); + assertThat(map).containsEntry("Key3", "Value3"); + assertThat(map).containsEntry("Key4", "Value4"); + + // Mutate 2 entries + try (MockPseudoRandom prng = mockPseudoRandom( + // change chunk + 5, + // ChunkOffset + 2, + // ChunkSize (ignored) + 2, + // mutateElement + true)) { + map = mutator.mutate(map, prng); + } + assertThat(map).hasSize(4); + assertThat(map).containsEntry("Key1", "Value1"); + assertThat(map).containsEntry("Key2", "Value2"); + assertThat(map.keySet().toArray()[3]).isNotEqualTo("Key4"); + + // Delete 2 entries + try (MockPseudoRandom prng = mockPseudoRandom( + // shrink chunk + 1, + // ChunkSize + 2, + // ChunkOffset + 1)) { + map = mutator.mutate(map, prng); + } + assertThat(map).hasSize(2); + } } |