aboutsummaryrefslogtreecommitdiffstats
path: root/store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/RestoreFuzzTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/RestoreFuzzTest.java')
-rw-r--r--store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/RestoreFuzzTest.java108
1 files changed, 108 insertions, 0 deletions
diff --git a/store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/RestoreFuzzTest.java b/store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/RestoreFuzzTest.java
new file mode 100644
index 00000000..7d9f5372
--- /dev/null
+++ b/store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/RestoreFuzzTest.java
@@ -0,0 +1,108 @@
1package org.eclipse.viatra.solver.data.map.tests.fuzz;
2
3import static org.junit.jupiter.api.Assertions.fail;
4
5import java.util.HashMap;
6import java.util.Map;
7import java.util.Random;
8import java.util.stream.Stream;
9
10import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
11import org.eclipse.viatra.solver.data.map.VersionedMapStore;
12import org.eclipse.viatra.solver.data.map.VersionedMapStoreImpl;
13import org.eclipse.viatra.solver.data.map.internal.VersionedMapImpl;
14import org.eclipse.viatra.solver.data.map.tests.fuzz.utils.FuzzTestUtils;
15import org.eclipse.viatra.solver.data.map.tests.utils.MapTestEnvironment;
16import org.junit.jupiter.api.Tag;
17import org.junit.jupiter.api.Timeout;
18import org.junit.jupiter.params.ParameterizedTest;
19import org.junit.jupiter.params.provider.Arguments;
20import org.junit.jupiter.params.provider.MethodSource;
21
22class RestoreFuzzTest {
23 private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency,
24 boolean evilHash) {
25 String[] values = MapTestEnvironment.prepareValues(maxValue);
26 ContinousHashProvider<Integer> chp = MapTestEnvironment.prepareHashProvider(evilHash);
27
28 VersionedMapStore<Integer, String> store = new VersionedMapStoreImpl<Integer, String>(chp, values[0]);
29
30 iterativeRandomPutsAndCommitsThenRestore(scenario, store, steps, maxKey, values, seed, commitFrequency);
31 }
32
33 private void iterativeRandomPutsAndCommitsThenRestore(String scenario, VersionedMapStore<Integer, String> store,
34 int steps, int maxKey, String[] values, int seed, int commitFrequency) {
35 // 1. build a map with versions
36 Random r = new Random(seed);
37 VersionedMapImpl<Integer, String> versioned = (VersionedMapImpl<Integer, String>) store.createMap();
38 Map<Integer, Long> index2Version = new HashMap<>();
39
40 for (int i = 0; i < steps; i++) {
41 int index = i + 1;
42 int nextKey = r.nextInt(maxKey);
43 String nextValue = values[r.nextInt(values.length)];
44 try {
45 versioned.put(nextKey, nextValue);
46 } catch (Exception exception) {
47 exception.printStackTrace();
48 fail(scenario + ":" + index + ": exception happened: " + exception);
49 }
50 if (index % commitFrequency == 0) {
51 long version = versioned.commit();
52 index2Version.put(i, version);
53 }
54 MapTestEnvironment.printStatus(scenario, index, steps, "building");
55 }
56 // 2. create a non-versioned and
57 VersionedMapImpl<Integer, String> reference = (VersionedMapImpl<Integer, String>) store.createMap();
58 r = new Random(seed);
59
60 for (int i = 0; i < steps; i++) {
61 int index = i + 1;
62 int nextKey = r.nextInt(maxKey);
63 String nextValue = values[r.nextInt(values.length)];
64 try {
65 reference.put(nextKey, nextValue);
66 } catch (Exception exception) {
67 exception.printStackTrace();
68 fail(scenario + ":" + index + ": exception happened: " + exception);
69 }
70 if (index % commitFrequency == 0) {
71 versioned.restore(index2Version.get(i));
72 MapTestEnvironment.compareTwoMaps(scenario + ":" + index, reference, versioned);
73 }
74 MapTestEnvironment.printStatus(scenario, index, steps, "comparison");
75 }
76
77 }
78
79 @ParameterizedTest(name = "Restore {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
80 @MethodSource
81 @Timeout(value = 10)
82 @Tag("smoke")
83 void parametrizedFastFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
84 boolean evilHash) {
85 runFuzzTest("RestoreS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
86 commitFrequency, evilHash);
87 }
88
89 static Stream<Arguments> parametrizedFastFuzz() {
90 return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT }, new Object[] { 3, 32, 32 * 32 },
91 new Object[] { 2, 3 }, new Object[] { 1, 10, 100 }, new Object[] { 1, 2, 3 },
92 new Object[] { false, true });
93 }
94
95 @ParameterizedTest(name = "Restore {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
96 @MethodSource
97 @Tag("smoke")
98 @Tag("slow")
99 void parametrizedSlowFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
100 boolean evilHash) {
101 runFuzzTest("RestoreS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
102 commitFrequency, evilHash);
103 }
104
105 static Stream<Arguments> parametrizedSlowFuzz() {
106 return FuzzTestUtils.changeStepCount(RestoreFuzzTest.parametrizedFastFuzz(), 1);
107 }
108}