aboutsummaryrefslogtreecommitdiffstats
path: root/store/src/test/java/tools/refinery/data/map/tests
diff options
context:
space:
mode:
Diffstat (limited to 'store/src/test/java/tools/refinery/data/map/tests')
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/CommitFuzzTest.java96
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/ContentEqualsFuzzTest.java143
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/DiffCursorFuzzTest.java117
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/MultiThreadFuzzTest.java97
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/MultiThreadTestRunnable.java101
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/MutableFuzzTest.java92
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/MutableImmutableCompareFuzzTest.java89
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/RestoreFuzzTest.java109
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/SharedStoreFuzzTest.java113
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/utils/FuzzTestUtils.java64
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/fuzz/utils/FuzzTestUtilsTest.java33
-rw-r--r--store/src/test/java/tools/refinery/data/map/tests/utils/MapTestEnvironment.java213
12 files changed, 1267 insertions, 0 deletions
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/CommitFuzzTest.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/CommitFuzzTest.java
new file mode 100644
index 00000000..d744a79d
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/CommitFuzzTest.java
@@ -0,0 +1,96 @@
1package tools.refinery.data.map.tests.fuzz;
2
3import static org.junit.jupiter.api.Assertions.fail;
4
5import java.util.Random;
6import java.util.stream.Stream;
7
8import org.junit.jupiter.api.Tag;
9import org.junit.jupiter.api.Timeout;
10import org.junit.jupiter.params.ParameterizedTest;
11import org.junit.jupiter.params.provider.Arguments;
12import org.junit.jupiter.params.provider.MethodSource;
13
14import tools.refinery.data.map.ContinousHashProvider;
15import tools.refinery.data.map.VersionedMapStore;
16import tools.refinery.data.map.VersionedMapStoreImpl;
17import tools.refinery.data.map.internal.VersionedMapImpl;
18import tools.refinery.data.map.tests.fuzz.utils.FuzzTestUtils;
19import tools.refinery.data.map.tests.utils.MapTestEnvironment;
20
21class CommitFuzzTest {
22 private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency,
23 boolean evilHash) {
24 String[] values = MapTestEnvironment.prepareValues(maxValue);
25 ContinousHashProvider<Integer> chp = MapTestEnvironment.prepareHashProvider(evilHash);
26
27 VersionedMapStore<Integer, String> store = new VersionedMapStoreImpl<Integer, String>(chp, values[0]);
28 VersionedMapImpl<Integer, String> sut = (VersionedMapImpl<Integer, String>) store.createMap();
29 MapTestEnvironment<Integer, String> e = new MapTestEnvironment<Integer, String>(sut);
30
31 Random r = new Random(seed);
32
33 iterativeRandomPutsAndCommits(scenario, steps, maxKey, values, e, r, commitFrequency);
34 }
35
36 private void iterativeRandomPutsAndCommits(String scenario, int steps, int maxKey, String[] values,
37 MapTestEnvironment<Integer, String> e, Random r, int commitFrequency) {
38 int stopAt = -1;
39 for (int i = 0; i < steps; i++) {
40 int index = i + 1;
41 int nextKey = r.nextInt(maxKey);
42 String nextValue = values[r.nextInt(values.length)];
43 if (index == stopAt) {
44 System.out.println("issue!");
45 System.out.println("State before:");
46 e.printComparison();
47 e.sut.prettyPrint();
48 System.out.println("Next: put(" + nextKey + "," + nextValue + ")");
49 }
50 try {
51 e.put(nextKey, nextValue);
52 if (index == stopAt) {
53 e.sut.prettyPrint();
54 }
55 e.checkEquivalence(scenario + ":" + index);
56 } catch (Exception exception) {
57 exception.printStackTrace();
58 fail(scenario + ":" + index + ": exception happened: " + exception);
59 }
60 MapTestEnvironment.printStatus(scenario, index, steps, null);
61 if (index % commitFrequency == 0) {
62 e.sut.commit();
63 }
64 }
65 }
66
67 @ParameterizedTest(name = "Commit {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
68 @MethodSource
69 @Timeout(value = 10)
70 @Tag("fuzz")
71 void parametrizedFastFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
72 boolean evilHash) {
73 runFuzzTest("CommitS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
74 commitFrequency, evilHash);
75 }
76
77 static Stream<Arguments> parametrizedFastFuzz() {
78 return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT }, new Object[] { 3, 32, 32 * 32 },
79 new Object[] { 2, 3 }, new Object[] { 1, 10, 100 }, new Object[] { 1, 2, 3 },
80 new Object[] { false, true });
81 }
82
83 @ParameterizedTest(name = "Commit {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
84 @MethodSource
85 @Tag("fuzz")
86 @Tag("slow")
87 void parametrizedSlowFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
88 boolean evilHash) {
89 runFuzzTest("CommitS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
90 commitFrequency, evilHash);
91 }
92
93 static Stream<Arguments> parametrizedSlowFuzz() {
94 return FuzzTestUtils.changeStepCount(parametrizedFastFuzz(), 1);
95 }
96}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/ContentEqualsFuzzTest.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/ContentEqualsFuzzTest.java
new file mode 100644
index 00000000..1f6f9609
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/ContentEqualsFuzzTest.java
@@ -0,0 +1,143 @@
1package tools.refinery.data.map.tests.fuzz;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.fail;
5
6import java.util.AbstractMap.SimpleEntry;
7import java.util.Collections;
8import java.util.LinkedList;
9import java.util.List;
10import java.util.Random;
11import java.util.stream.Stream;
12
13import org.junit.jupiter.api.Tag;
14import org.junit.jupiter.api.Timeout;
15import org.junit.jupiter.params.ParameterizedTest;
16import org.junit.jupiter.params.provider.Arguments;
17import org.junit.jupiter.params.provider.MethodSource;
18
19import tools.refinery.data.map.ContinousHashProvider;
20import tools.refinery.data.map.Cursor;
21import tools.refinery.data.map.VersionedMap;
22import tools.refinery.data.map.VersionedMapStore;
23import tools.refinery.data.map.VersionedMapStoreImpl;
24import tools.refinery.data.map.internal.VersionedMapImpl;
25import tools.refinery.data.map.tests.fuzz.utils.FuzzTestUtils;
26import tools.refinery.data.map.tests.utils.MapTestEnvironment;
27
28class ContentEqualsFuzzTest {
29 private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency,
30 boolean evilHash) {
31 String[] values = MapTestEnvironment.prepareValues(maxValue);
32 ContinousHashProvider<Integer> chp = MapTestEnvironment.prepareHashProvider(evilHash);
33
34 Random r = new Random(seed);
35
36 iterativeRandomPutsAndCommitsThenCompare(scenario, chp, steps, maxKey, values, r, commitFrequency);
37 }
38
39 private void iterativeRandomPutsAndCommitsThenCompare(String scenario, ContinousHashProvider<Integer> chp, int steps, int maxKey, String[] values, Random r, int commitFrequency) {
40
41 VersionedMapStore<Integer, String> store1 = new VersionedMapStoreImpl<Integer, String>(chp, values[0]);
42 VersionedMap<Integer, String> sut1 = store1.createMap();
43
44 // Fill one map
45 for (int i = 0; i < steps; i++) {
46 int index1 = i + 1;
47 int nextKey = r.nextInt(maxKey);
48 String nextValue = values[r.nextInt(values.length)];
49 try {
50 sut1.put(nextKey, nextValue);
51 } catch (Exception exception) {
52 exception.printStackTrace();
53 fail(scenario + ":" + index1 + ": exception happened: " + exception);
54 }
55 MapTestEnvironment.printStatus(scenario, index1, steps, "Fill");
56 if (index1 % commitFrequency == 0) {
57 sut1.commit();
58 }
59 }
60
61 // Get the content of the first map
62 List<SimpleEntry<Integer, String>> content = new LinkedList<>();
63 Cursor<Integer, String> cursor = sut1.getAll();
64 while (cursor.move()) {
65 content.add(new SimpleEntry<>(cursor.getKey(), cursor.getValue()));
66 }
67
68 // Randomize the order of the content
69 Collections.shuffle(content, r);
70
71 VersionedMapStore<Integer, String> store2 = new VersionedMapStoreImpl<Integer, String>(chp, values[0]);
72 VersionedMap<Integer, String> sut2 = store2.createMap();
73 int index2 = 1;
74 for (SimpleEntry<Integer, String> entry : content) {
75 sut2.put(entry.getKey(), entry.getValue());
76 if(index2++%commitFrequency == 0)
77 sut2.commit();
78 }
79
80 // Check the integrity of the maps
81 ((VersionedMapImpl<Integer,String>) sut1).checkIntegrity();
82 ((VersionedMapImpl<Integer,String>) sut2).checkIntegrity();
83
84// // Compare the two maps
85 // By size
86 assertEquals(sut1.getSize(), content.size());
87 assertEquals(sut2.getSize(), content.size());
88
89
90
91 // By cursors
92 Cursor<Integer, String> cursor1 = sut1.getAll();
93 Cursor<Integer, String> cursor2 = sut2.getAll();
94 int index3 = 1;
95 boolean canMove = true;
96 do{
97 boolean canMove1 = cursor1.move();
98 boolean canMove2 = cursor2.move();
99 assertEquals(canMove1, canMove2, scenario + ":" + index3 +" Cursors stopped at different times!");
100 assertEquals(cursor1.getKey(), cursor2.getKey(), scenario + ":" + index3 +" Cursors have different keys!");
101 assertEquals(cursor1.getValue(), cursor2.getValue(), scenario + ":" + index3 +" Cursors have different values!");
102
103 canMove = canMove1;
104 MapTestEnvironment.printStatus(scenario, index3++, content.size(), "Compare");
105 } while (canMove);
106
107 // By hashcode
108 assertEquals(sut1.hashCode(), sut2.hashCode(), "Hash codes are not equal!");
109
110 // By equals
111 assertEquals(sut1, sut2, "Maps are not equals");
112 }
113
114 @ParameterizedTest(name = "Compare {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
115 @MethodSource
116 @Timeout(value = 10)
117 @Tag("fuzz")
118 void parametrizedFastFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
119 boolean evilHash) {
120 runFuzzTest("CompareS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
121 commitFrequency, evilHash);
122 }
123
124 static Stream<Arguments> parametrizedFastFuzz() {
125 return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT }, new Object[] { 3, 32, 32 * 32 },
126 new Object[] { 2, 3 }, new Object[] { 1, 10, 100 }, new Object[] { 1, 2, 3 },
127 new Object[] { false, true });
128 }
129
130 @ParameterizedTest(name = "Compare {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
131 @MethodSource
132 @Tag("fuzz")
133 @Tag("slow")
134 void parametrizedSlowFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
135 boolean evilHash) {
136 runFuzzTest("CompareS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
137 commitFrequency, evilHash);
138 }
139
140 static Stream<Arguments> parametrizedSlowFuzz() {
141 return FuzzTestUtils.changeStepCount(parametrizedFastFuzz(), 1);
142 }
143}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/DiffCursorFuzzTest.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/DiffCursorFuzzTest.java
new file mode 100644
index 00000000..fd663a7c
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/DiffCursorFuzzTest.java
@@ -0,0 +1,117 @@
1package tools.refinery.data.map.tests.fuzz;
2
3import static org.junit.jupiter.api.Assertions.fail;
4
5import java.util.Random;
6import java.util.stream.Stream;
7
8import org.junit.jupiter.api.Tag;
9import org.junit.jupiter.api.Timeout;
10import org.junit.jupiter.params.ParameterizedTest;
11import org.junit.jupiter.params.provider.Arguments;
12import org.junit.jupiter.params.provider.MethodSource;
13
14import tools.refinery.data.map.ContinousHashProvider;
15import tools.refinery.data.map.DiffCursor;
16import tools.refinery.data.map.VersionedMapStore;
17import tools.refinery.data.map.VersionedMapStoreImpl;
18import tools.refinery.data.map.internal.VersionedMapImpl;
19import tools.refinery.data.map.tests.fuzz.utils.FuzzTestUtils;
20import tools.refinery.data.map.tests.utils.MapTestEnvironment;
21
22class DiffCursorFuzzTest {
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 iterativeRandomPutsAndCommitsThenDiffcursor(scenario, store, steps, maxKey, values, seed, commitFrequency);
30 }
31
32 private void iterativeRandomPutsAndCommitsThenDiffcursor(String scenario, VersionedMapStore<Integer, String> store,
33 int steps, int maxKey, String[] values, int seed, int commitFrequency) {
34 // 1. build a map with versions
35 Random r = new Random(seed);
36 VersionedMapImpl<Integer, String> versioned = (VersionedMapImpl<Integer, String>) store.createMap();
37 int largestCommit = -1;
38
39 for (int i = 0; i < steps; i++) {
40 int index = i + 1;
41 int nextKey = r.nextInt(maxKey);
42 String nextValue = values[r.nextInt(values.length)];
43 try {
44 versioned.put(nextKey, nextValue);
45 } catch (Exception exception) {
46 exception.printStackTrace();
47 fail(scenario + ":" + index + ": exception happened: " + exception);
48 }
49 if (index % commitFrequency == 0) {
50 long version = versioned.commit();
51 largestCommit = (int) version;
52 }
53 if (index % 10000 == 0)
54 System.out.println(scenario + ":" + index + "/" + steps + " building finished");
55 }
56 // 2. create a non-versioned map,
57 VersionedMapImpl<Integer, String> moving = (VersionedMapImpl<Integer, String>) store.createMap();
58 Random r2 = new Random(seed + 1);
59
60 final int diffTravelFrequency = commitFrequency * 2;
61 for (int i = 0; i < steps; i++) {
62 int index = i + 1;
63 if (index % diffTravelFrequency == 0) {
64 // difftravel
65 long travelToVersion = r2.nextInt(largestCommit + 1);
66 DiffCursor<Integer, String> diffCursor = moving.getDiffCursor(travelToVersion);
67 moving.putAll(diffCursor);
68
69 } else {
70 // random puts
71 int nextKey = r2.nextInt(maxKey);
72 String nextValue = values[r2.nextInt(values.length)];
73 try {
74 moving.put(nextKey, nextValue);
75 } catch (Exception exception) {
76 exception.printStackTrace();
77 fail(scenario + ":" + index + ": exception happened: " + exception);
78 }
79 if (index % commitFrequency == 0) {
80 versioned.commit();
81 }
82 if (index % 10000 == 0)
83 System.out.println(scenario + ":" + index + "/" + steps + " building finished");
84 }
85 }
86
87 }
88
89 @ParameterizedTest(name = "Mutable-Immutable Compare {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
90 @MethodSource
91 @Timeout(value = 10)
92 @Tag("fuzz")
93 void parametrizedFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
94 boolean evilHash) {
95 runFuzzTest("MutableImmutableCompareS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps,
96 noKeys, noValues, commitFrequency, evilHash);
97 }
98
99 static Stream<Arguments> parametrizedFuzz() {
100 return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT }, new Object[] { 3, 32, 32 * 32 },
101 new Object[] { 2, 3 }, new Object[] { 1, 10, 100 }, new Object[] { 1, 2, 3 },
102 new Object[] { false, true });
103 }
104 @ParameterizedTest(name = "Mutable-Immutable Compare {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
105 @MethodSource
106 @Tag("fuzz")
107 @Tag("slow")
108 void parametrizedSlowFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
109 boolean evilHash) {
110 runFuzzTest("MutableImmutableCompareS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
111 commitFrequency, evilHash);
112 }
113
114 static Stream<Arguments> parametrizedSlowFuzz() {
115 return FuzzTestUtils.changeStepCount(parametrizedFuzz(), 1);
116 }
117}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/MultiThreadFuzzTest.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/MultiThreadFuzzTest.java
new file mode 100644
index 00000000..e6af13bf
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/MultiThreadFuzzTest.java
@@ -0,0 +1,97 @@
1package tools.refinery.data.map.tests.fuzz;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.fail;
5
6import java.util.Collections;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.stream.Stream;
10
11import org.junit.jupiter.api.Tag;
12import org.junit.jupiter.api.Timeout;
13import org.junit.jupiter.params.ParameterizedTest;
14import org.junit.jupiter.params.provider.Arguments;
15import org.junit.jupiter.params.provider.MethodSource;
16
17import tools.refinery.data.map.ContinousHashProvider;
18import tools.refinery.data.map.VersionedMapStore;
19import tools.refinery.data.map.VersionedMapStoreImpl;
20import tools.refinery.data.map.tests.fuzz.utils.FuzzTestUtils;
21import tools.refinery.data.map.tests.utils.MapTestEnvironment;
22
23class MultiThreadFuzzTest {
24 public static final int noThreads = 32;
25
26 private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency,
27 boolean evilHash) {
28 String[] values = MapTestEnvironment.prepareValues(maxValue);
29 ContinousHashProvider<Integer> chp = MapTestEnvironment.prepareHashProvider(evilHash);
30
31 VersionedMapStore<Integer, String> store = new VersionedMapStoreImpl<Integer, String>(chp, values[0]);
32
33 // initialize runnables
34 MultiThreadTestRunnable[] runnables = new MultiThreadTestRunnable[noThreads];
35 for(int i = 0; i<noThreads; i++) {
36 runnables[i] = new MultiThreadTestRunnable(scenario+"-T"+(i+1), store, steps, maxKey, values, seed, commitFrequency);
37 }
38
39 // initialize threads
40 Thread[] threads = new Thread[noThreads];
41 for(int i = 0; i<noThreads; i++) {
42 threads[i] = new Thread(runnables[i]);
43 }
44
45 // start threads;
46 for(int i = 0; i<noThreads; i++) {
47 threads[i].start();
48 }
49
50 // wait all the threads;
51 for(int i = 0; i<noThreads; i++) {
52 try {
53 threads[i].join();
54 } catch (InterruptedException e) {
55 fail("Thread "+i+" interrupted.");
56 }
57 }
58
59 // collect errors
60 List<Throwable> errors = new LinkedList<>();
61 for(int i = 0; i<noThreads; i++) {
62 errors.addAll(runnables[i].getErrors());
63 }
64
65 assertEquals(Collections.EMPTY_LIST, errors);
66 }
67
68 @ParameterizedTest(name = "Multithread {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
69 @MethodSource
70 @Timeout(value = 10)
71 @Tag("fuzz")
72 void parametrizedFastFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
73 boolean evilHash) {
74 runFuzzTest("MultithreadS" + steps + "K" + noKeys + "V" + noValues + "CF" + commitFrequency + "s" + seed, seed, steps, noKeys, noValues,
75 commitFrequency, evilHash);
76 }
77
78 static Stream<Arguments> parametrizedFastFuzz() {
79 return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT }, new Object[] { 3, 32, 32 * 32 },
80 new Object[] { 2, 3 }, new Object[] { 10, 100 }, new Object[] { 1, 2, 3 },
81 new Object[] { false, true });
82 }
83
84 @ParameterizedTest(name = "Multithread {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
85 @MethodSource
86 @Tag("fuzz")
87 @Tag("slow")
88 void parametrizedSlowFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
89 boolean evilHash) {
90 runFuzzTest("RestoreS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
91 commitFrequency, evilHash);
92 }
93
94 static Stream<Arguments> parametrizedSlowFuzz() {
95 return FuzzTestUtils.changeStepCount(RestoreFuzzTest.parametrizedFastFuzz(), 1);
96 }
97}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/MultiThreadTestRunnable.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/MultiThreadTestRunnable.java
new file mode 100644
index 00000000..922178c6
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/MultiThreadTestRunnable.java
@@ -0,0 +1,101 @@
1package tools.refinery.data.map.tests.fuzz;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Map;
9import java.util.Random;
10
11import tools.refinery.data.map.VersionedMapStore;
12import tools.refinery.data.map.internal.VersionedMapImpl;
13import tools.refinery.data.map.tests.utils.MapTestEnvironment;
14
15public class MultiThreadTestRunnable implements Runnable {
16 String scenario;
17 VersionedMapStore<Integer, String> store;
18 int steps;
19 int maxKey;
20 String[] values;
21 int seed;
22 int commitFrequency;
23 List<Throwable> errors = new LinkedList<>();
24
25 public MultiThreadTestRunnable(String scenario, VersionedMapStore<Integer, String> store, int steps,
26 int maxKey, String[] values, int seed, int commitFrequency) {
27 super();
28 this.scenario = scenario;
29 this.store = store;
30 this.steps = steps;
31 this.maxKey = maxKey;
32 this.values = values;
33 this.seed = seed;
34 this.commitFrequency = commitFrequency;
35 }
36
37 private void logAndThrowError(String message) {
38 AssertionError error = new AssertionError(message);
39 errors.add(error);
40 }
41
42 public List<Throwable> getErrors() {
43 return errors;
44 }
45
46 @Override
47 public void run() {
48 // 1. build a map with versions
49 Random r = new Random(seed);
50 VersionedMapImpl<Integer, String> versioned = (VersionedMapImpl<Integer, String>) store.createMap();
51 Map<Integer, Long> index2Version = new HashMap<>();
52
53 for (int i = 0; i < steps; i++) {
54 int index = i + 1;
55 int nextKey = r.nextInt(maxKey);
56 String nextValue = values[r.nextInt(values.length)];
57 try {
58 versioned.put(nextKey, nextValue);
59 } catch (Exception exception) {
60 exception.printStackTrace();
61 logAndThrowError(scenario + ":" + index + ": exception happened: " + exception);
62 }
63 if (index % commitFrequency == 0) {
64 long version = versioned.commit();
65 index2Version.put(i, version);
66 }
67 MapTestEnvironment.printStatus(scenario, index, steps, "building");
68 }
69 // 2. create a non-versioned
70 VersionedMapImpl<Integer, String> reference = (VersionedMapImpl<Integer, String>) store.createMap();
71 r = new Random(seed);
72 Random r2 = new Random(seed+1);
73
74 for (int i = 0; i < steps; i++) {
75 int index = i + 1;
76 int nextKey = r.nextInt(maxKey);
77 String nextValue = values[r.nextInt(values.length)];
78 try {
79 reference.put(nextKey, nextValue);
80 } catch (Exception exception) {
81 exception.printStackTrace();
82 logAndThrowError(scenario + ":" + index + ": exception happened: " + exception);
83 }
84 // go back to an existing state and compare to the reference
85 if (index % (commitFrequency) == 0) {
86 versioned.restore(index2Version.get(i));
87 MapTestEnvironment.compareTwoMaps(scenario + ":" + index, reference, versioned,errors);
88
89 // go back to a random state (probably created by another thread)
90 List<Long> states = new ArrayList<>(store.getStates());
91 Collections.shuffle(states, r2);
92 for(Long state : states.subList(0, Math.min(states.size(), 100))) {
93 versioned.restore(state);
94 }
95 versioned.restore(index2Version.get(i));
96 }
97
98 MapTestEnvironment.printStatus(scenario, index, steps, "comparison");
99 }
100 }
101}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/MutableFuzzTest.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/MutableFuzzTest.java
new file mode 100644
index 00000000..a16cb8f5
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/MutableFuzzTest.java
@@ -0,0 +1,92 @@
1package tools.refinery.data.map.tests.fuzz;
2
3import static org.junit.jupiter.api.Assertions.fail;
4
5import java.util.Random;
6import java.util.stream.Stream;
7
8import org.junit.jupiter.api.Tag;
9import org.junit.jupiter.api.Timeout;
10import org.junit.jupiter.params.ParameterizedTest;
11import org.junit.jupiter.params.provider.Arguments;
12import org.junit.jupiter.params.provider.MethodSource;
13
14import tools.refinery.data.map.ContinousHashProvider;
15import tools.refinery.data.map.VersionedMapStore;
16import tools.refinery.data.map.VersionedMapStoreImpl;
17import tools.refinery.data.map.internal.VersionedMapImpl;
18import tools.refinery.data.map.tests.fuzz.utils.FuzzTestUtils;
19import tools.refinery.data.map.tests.utils.MapTestEnvironment;
20
21class MutableFuzzTest {
22 private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, boolean evilHash) {
23 String[] values = MapTestEnvironment.prepareValues(maxValue);
24 ContinousHashProvider<Integer> chp = MapTestEnvironment.prepareHashProvider(evilHash);
25
26 VersionedMapStore<Integer, String> store = new VersionedMapStoreImpl<Integer, String>(chp, values[0]);
27 VersionedMapImpl<Integer, String> sut = (VersionedMapImpl<Integer, String>) store.createMap();
28 MapTestEnvironment<Integer, String> e = new MapTestEnvironment<Integer, String>(sut);
29
30 Random r = new Random(seed);
31
32 iterativeRandomPuts(scenario, steps, maxKey, values, e, r);
33 }
34
35 private void iterativeRandomPuts(String scenario, int steps, int maxKey, String[] values,
36 MapTestEnvironment<Integer, String> e, Random r) {
37 int stopAt = -1;
38 for (int i = 0; i < steps; i++) {
39 int index = i + 1;
40 int nextKey = r.nextInt(maxKey);
41 String nextValue = values[r.nextInt(values.length)];
42 if (index == stopAt) {
43 System.out.println("issue!");
44 System.out.println("State before:");
45 e.printComparison();
46 e.sut.prettyPrint();
47 System.out.println("Next: put(" + nextKey + "," + nextValue + ")");
48 }
49 try {
50 e.put(nextKey, nextValue);
51 if (index == stopAt) {
52 e.sut.prettyPrint();
53 }
54 e.checkEquivalence(scenario + ":" + index);
55 } catch (Exception exception) {
56 exception.printStackTrace();
57 fail(scenario + ":" + index + ": exception happened: " + exception);
58 }
59 MapTestEnvironment.printStatus(scenario, index, steps, null);
60 }
61 }
62
63 @ParameterizedTest(name = "Mutable {index}/{0} Steps={1} Keys={2} Values={3} seed={4} evil-hash={5}")
64 @MethodSource
65 @Timeout(value = 10)
66 @Tag("fuzz")
67 void parametrizedFuzz(int test, int steps, int noKeys, int noValues, int seed, boolean evilHash) {
68 runFuzzTest(
69 "MutableS" + steps + "K" + noKeys + "V" + noValues + "s" + seed + "H" + (evilHash ? "Evil" : "Normal"),
70 seed, steps, noKeys, noValues, evilHash);
71 }
72
73 static Stream<Arguments> parametrizedFuzz() {
74 return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT },
75 new Object[] { 3, 32, 32 * 32, 32 * 32 * 32 * 32 }, new Object[] { 2, 3 }, new Object[] { 1, 2, 3 },
76 new Object[] { false, true });
77 }
78
79 @ParameterizedTest(name = "Mutable {index}/{0} Steps={1} Keys={2} Values={3} seed={4} evil-hash={5}")
80 @MethodSource
81 @Tag("fuzz")
82 @Tag("slow")
83 void parametrizedSlowFuzz(int test, int steps, int noKeys, int noValues, int seed, boolean evilHash) {
84 runFuzzTest(
85 "MutableS" + steps + "K" + noKeys + "V" + noValues + "s" + seed + "H" + (evilHash ? "Evil" : "Normal"),
86 seed, steps, noKeys, noValues, evilHash);
87 }
88
89 static Stream<Arguments> parametrizedSlowFuzz() {
90 return FuzzTestUtils.changeStepCount(parametrizedFuzz(), 1);
91 }
92}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/MutableImmutableCompareFuzzTest.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/MutableImmutableCompareFuzzTest.java
new file mode 100644
index 00000000..45308892
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/MutableImmutableCompareFuzzTest.java
@@ -0,0 +1,89 @@
1package tools.refinery.data.map.tests.fuzz;
2
3import static org.junit.jupiter.api.Assertions.fail;
4
5import java.util.Random;
6import java.util.stream.Stream;
7
8import org.junit.jupiter.api.Tag;
9import org.junit.jupiter.api.Timeout;
10import org.junit.jupiter.params.ParameterizedTest;
11import org.junit.jupiter.params.provider.Arguments;
12import org.junit.jupiter.params.provider.MethodSource;
13
14import tools.refinery.data.map.ContinousHashProvider;
15import tools.refinery.data.map.VersionedMapStore;
16import tools.refinery.data.map.VersionedMapStoreImpl;
17import tools.refinery.data.map.internal.VersionedMapImpl;
18import tools.refinery.data.map.tests.fuzz.utils.FuzzTestUtils;
19import tools.refinery.data.map.tests.utils.MapTestEnvironment;
20
21class MutableImmutableCompareFuzzTest {
22 private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency,
23 boolean evilHash) {
24 String[] values = MapTestEnvironment.prepareValues(maxValue);
25 ContinousHashProvider<Integer> chp = MapTestEnvironment.prepareHashProvider(evilHash);
26
27 VersionedMapStore<Integer, String> store = new VersionedMapStoreImpl<Integer, String>(chp, values[0]);
28 VersionedMapImpl<Integer, String> immutable = (VersionedMapImpl<Integer, String>) store.createMap();
29 VersionedMapImpl<Integer, String> mutable = (VersionedMapImpl<Integer, String>) store.createMap();
30
31 Random r = new Random(seed);
32
33 iterativeRandomPutsAndCommitsAndCompare(scenario, immutable, mutable, steps, maxKey, values, r,
34 commitFrequency);
35 }
36
37 private void iterativeRandomPutsAndCommitsAndCompare(String scenario, VersionedMapImpl<Integer, String> immutable,
38 VersionedMapImpl<Integer, String> mutable, int steps, int maxKey, String[] values, Random r,
39 int commitFrequency) {
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 immutable.put(nextKey, nextValue);
46 mutable.put(nextKey, nextValue);
47 } catch (Exception exception) {
48 exception.printStackTrace();
49 fail(scenario + ":" + index + ": exception happened: " + exception);
50 }
51 if (index % commitFrequency == 0) {
52 immutable.commit();
53 }
54 MapTestEnvironment.compareTwoMaps(scenario + ":" + index, immutable, mutable);
55
56 MapTestEnvironment.printStatus(scenario, index, steps, null);
57 }
58 }
59
60 @ParameterizedTest(name = "Mutable-Immutable Compare {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
61 @MethodSource
62 @Timeout(value = 10)
63 @Tag("fuzz")
64 void parametrizedFastFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
65 boolean evilHash) {
66 runFuzzTest("MutableImmutableCompareS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps,
67 noKeys, noValues, commitFrequency, evilHash);
68 }
69
70 static Stream<Arguments> parametrizedFastFuzz() {
71 return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT }, new Object[] { 3, 32, 32 * 32 },
72 new Object[] { 2, 3 }, new Object[] { 1, 10, 100 }, new Object[] { 1, 2, 3 },
73 new Object[] { false, true });
74 }
75
76 @ParameterizedTest(name = "Mutable-Immutable Compare {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
77 @MethodSource
78 @Tag("fuzz")
79 @Tag("slow")
80 void parametrizedSlowFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
81 boolean evilHash) {
82 runFuzzTest("MutableImmutableCompareS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps,
83 noKeys, noValues, commitFrequency, evilHash);
84 }
85
86 static Stream<Arguments> parametrizedSlowFuzz() {
87 return FuzzTestUtils.changeStepCount(MutableImmutableCompareFuzzTest.parametrizedFastFuzz(), 1);
88 }
89}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/RestoreFuzzTest.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/RestoreFuzzTest.java
new file mode 100644
index 00000000..1b8b38c4
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/RestoreFuzzTest.java
@@ -0,0 +1,109 @@
1package tools.refinery.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.junit.jupiter.api.Tag;
11import org.junit.jupiter.api.Timeout;
12import org.junit.jupiter.params.ParameterizedTest;
13import org.junit.jupiter.params.provider.Arguments;
14import org.junit.jupiter.params.provider.MethodSource;
15
16import tools.refinery.data.map.ContinousHashProvider;
17import tools.refinery.data.map.VersionedMapStore;
18import tools.refinery.data.map.VersionedMapStoreImpl;
19import tools.refinery.data.map.internal.VersionedMapImpl;
20import tools.refinery.data.map.tests.fuzz.utils.FuzzTestUtils;
21import tools.refinery.data.map.tests.utils.MapTestEnvironment;
22
23class RestoreFuzzTest {
24 private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency,
25 boolean evilHash) {
26 String[] values = MapTestEnvironment.prepareValues(maxValue);
27 ContinousHashProvider<Integer> chp = MapTestEnvironment.prepareHashProvider(evilHash);
28
29 VersionedMapStore<Integer, String> store = new VersionedMapStoreImpl<Integer, String>(chp, values[0]);
30
31 iterativeRandomPutsAndCommitsThenRestore(scenario, store, steps, maxKey, values, seed, commitFrequency);
32 }
33
34 private void iterativeRandomPutsAndCommitsThenRestore(String scenario, VersionedMapStore<Integer, String> store,
35 int steps, int maxKey, String[] values, int seed, int commitFrequency) {
36 // 1. build a map with versions
37 Random r = new Random(seed);
38 VersionedMapImpl<Integer, String> versioned = (VersionedMapImpl<Integer, String>) store.createMap();
39 Map<Integer, Long> index2Version = new HashMap<>();
40
41 for (int i = 0; i < steps; i++) {
42 int index = i + 1;
43 int nextKey = r.nextInt(maxKey);
44 String nextValue = values[r.nextInt(values.length)];
45 try {
46 versioned.put(nextKey, nextValue);
47 } catch (Exception exception) {
48 exception.printStackTrace();
49 fail(scenario + ":" + index + ": exception happened: " + exception);
50 }
51 if (index % commitFrequency == 0) {
52 long version = versioned.commit();
53 index2Version.put(i, version);
54 }
55 MapTestEnvironment.printStatus(scenario, index, steps, "building");
56 }
57 // 2. create a non-versioned and
58 VersionedMapImpl<Integer, String> reference = (VersionedMapImpl<Integer, String>) store.createMap();
59 r = new Random(seed);
60
61 for (int i = 0; i < steps; i++) {
62 int index = i + 1;
63 int nextKey = r.nextInt(maxKey);
64 String nextValue = values[r.nextInt(values.length)];
65 try {
66 reference.put(nextKey, nextValue);
67 } catch (Exception exception) {
68 exception.printStackTrace();
69 fail(scenario + ":" + index + ": exception happened: " + exception);
70 }
71 if (index % commitFrequency == 0) {
72 versioned.restore(index2Version.get(i));
73 MapTestEnvironment.compareTwoMaps(scenario + ":" + index, reference, versioned);
74 }
75 MapTestEnvironment.printStatus(scenario, index, steps, "comparison");
76 }
77
78 }
79
80 @ParameterizedTest(name = "Restore {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
81 @MethodSource
82 @Timeout(value = 10)
83 @Tag("smoke")
84 void parametrizedFastFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
85 boolean evilHash) {
86 runFuzzTest("RestoreS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
87 commitFrequency, evilHash);
88 }
89
90 static Stream<Arguments> parametrizedFastFuzz() {
91 return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT }, new Object[] { 3, 32, 32 * 32 },
92 new Object[] { 2, 3 }, new Object[] { 1, 10, 100 }, new Object[] { 1, 2, 3 },
93 new Object[] { false, true });
94 }
95
96 @ParameterizedTest(name = "Restore {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
97 @MethodSource
98 @Tag("smoke")
99 @Tag("slow")
100 void parametrizedSlowFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
101 boolean evilHash) {
102 runFuzzTest("RestoreS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
103 commitFrequency, evilHash);
104 }
105
106 static Stream<Arguments> parametrizedSlowFuzz() {
107 return FuzzTestUtils.changeStepCount(RestoreFuzzTest.parametrizedFastFuzz(), 1);
108 }
109}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/SharedStoreFuzzTest.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/SharedStoreFuzzTest.java
new file mode 100644
index 00000000..1703a732
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/SharedStoreFuzzTest.java
@@ -0,0 +1,113 @@
1package tools.refinery.data.map.tests.fuzz;
2
3import java.util.HashMap;
4import java.util.LinkedList;
5import java.util.List;
6import java.util.Map;
7import java.util.Random;
8import java.util.stream.Stream;
9
10import org.junit.jupiter.api.Tag;
11import org.junit.jupiter.api.Timeout;
12import org.junit.jupiter.params.ParameterizedTest;
13import org.junit.jupiter.params.provider.Arguments;
14import org.junit.jupiter.params.provider.MethodSource;
15
16import tools.refinery.data.map.ContinousHashProvider;
17import tools.refinery.data.map.VersionedMapStore;
18import tools.refinery.data.map.VersionedMapStoreImpl;
19import tools.refinery.data.map.internal.VersionedMapImpl;
20import tools.refinery.data.map.tests.fuzz.utils.FuzzTestUtils;
21import tools.refinery.data.map.tests.utils.MapTestEnvironment;
22
23class SharedStoreFuzzTest {
24 private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency,
25 boolean evilHash) {
26 String[] values = MapTestEnvironment.prepareValues(maxValue);
27 ContinousHashProvider<Integer> chp = MapTestEnvironment.prepareHashProvider(evilHash);
28
29 List<VersionedMapStore<Integer, String>> stores = VersionedMapStoreImpl.createSharedVersionedMapStores(5, chp, values[0]);
30
31 iterativeRandomPutsAndCommitsThenRestore(scenario, stores, steps, maxKey, values, seed, commitFrequency);
32 }
33
34 private void iterativeRandomPutsAndCommitsThenRestore(String scenario, List<VersionedMapStore<Integer, String>> stores,
35 int steps, int maxKey, String[] values, int seed, int commitFrequency) {
36 // 1. maps with versions
37 Random r = new Random(seed);
38 List<VersionedMapImpl<Integer, String>> versioneds = new LinkedList<>();
39 for(VersionedMapStore<Integer, String> store : stores) {
40 versioneds.add((VersionedMapImpl<Integer, String>) store.createMap());
41 }
42
43 List<Map<Integer, Long>> index2Version = new LinkedList<>();
44 for(int i = 0; i<stores.size(); i++) {
45 index2Version.add(new HashMap<>());
46 }
47
48 for (int i = 0; i < steps; i++) {
49 int stepIndex = i + 1;
50 for (int storeIndex = 0; storeIndex<versioneds.size(); storeIndex++) {
51 int nextKey = r.nextInt(maxKey);
52 String nextValue = values[r.nextInt(values.length)];
53 versioneds.get(storeIndex).put(nextKey, nextValue);
54 if (stepIndex % commitFrequency == 0) {
55 long version = versioneds.get(storeIndex).commit();
56 index2Version.get(storeIndex).put(i, version);
57 }
58 MapTestEnvironment.printStatus(scenario, stepIndex, steps, "building");
59 }
60 }
61 // 2. create a non-versioned and
62 List<VersionedMapImpl<Integer, String>> reference = new LinkedList<>();
63 for(VersionedMapStore<Integer, String> store : stores) {
64 reference.add((VersionedMapImpl<Integer, String>) store.createMap());
65 }
66 r = new Random(seed);
67
68 for (int i = 0; i < steps; i++) {
69 int index = i + 1;
70 for (int storeIndex = 0; storeIndex<versioneds.size(); storeIndex++) {
71 int nextKey = r.nextInt(maxKey);
72 String nextValue = values[r.nextInt(values.length)];
73 reference.get(storeIndex).put(nextKey, nextValue);
74 if (index % commitFrequency == 0) {
75 versioneds.get(storeIndex).restore(index2Version.get(storeIndex).get(i));
76 MapTestEnvironment.compareTwoMaps(scenario + ":" + index, reference.get(storeIndex), versioneds.get(storeIndex));
77 }
78 }
79 MapTestEnvironment.printStatus(scenario, index, steps, "comparison");
80 }
81
82 }
83
84 @ParameterizedTest(name = "Shared Store {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
85 @MethodSource
86 @Timeout(value = 10)
87 @Tag("smoke")
88 void parametrizedFastFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
89 boolean evilHash) {
90 runFuzzTest("SharedS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
91 commitFrequency, evilHash);
92 }
93
94 static Stream<Arguments> parametrizedFastFuzz() {
95 return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT }, new Object[] { 3, 32, 32 * 32 },
96 new Object[] { 2, 3 }, new Object[] { 1, 10, 100 }, new Object[] { 1, 2, 3 },
97 new Object[] { false, true });
98 }
99
100 @ParameterizedTest(name = "Shared Store {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}")
101 @MethodSource
102 @Tag("smoke")
103 @Tag("slow")
104 void parametrizedSlowFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed,
105 boolean evilHash) {
106 runFuzzTest("SharedS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues,
107 commitFrequency, evilHash);
108 }
109
110 static Stream<Arguments> parametrizedSlowFuzz() {
111 return FuzzTestUtils.changeStepCount(RestoreFuzzTest.parametrizedFastFuzz(), 1);
112 }
113}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/utils/FuzzTestUtils.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/utils/FuzzTestUtils.java
new file mode 100644
index 00000000..23df4aef
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/utils/FuzzTestUtils.java
@@ -0,0 +1,64 @@
1package tools.refinery.data.map.tests.fuzz.utils;
2
3import java.util.Arrays;
4import java.util.LinkedList;
5import java.util.List;
6import java.util.stream.Stream;
7
8import org.junit.jupiter.params.provider.Arguments;
9
10public final class FuzzTestUtils {
11 public static final int FAST_STEP_COUNT = 500;
12 public static final int SLOW_STEP_COUNT = 32 * 32 * 32 * 32;
13
14 private FuzzTestUtils() {
15 throw new IllegalStateException("This is a static utility class and should not be instantiated directly");
16 }
17
18 public static Stream<Arguments> changeStepCount(Stream<Arguments> arguments, int parameterIndex) {
19 return arguments.map(x -> Arguments.of(updatedStepCount(x.get(), parameterIndex)));
20 }
21
22 public static Object[] updatedStepCount(Object[] arguments, int parameterIndex) {
23 Object[] copy = Arrays.copyOf(arguments, arguments.length);
24 copy[parameterIndex] = SLOW_STEP_COUNT;
25 return copy;
26 }
27
28 static List<List<Object>> permutationInternal(int from, Object[]... valueOption) {
29 if (valueOption.length == from) {
30 return List.of(List.of());
31 } else {
32 Object[] permuteThis = valueOption[from];
33 List<List<Object>> otherCombination = permutationInternal(from + 1, valueOption);
34 List<List<Object>> result = new LinkedList<>();
35 for (Object permuteThisElement : permuteThis) {
36 for (List<Object> otherCombinationList : otherCombination) {
37 List<Object> newResult = new LinkedList<>();
38 newResult.add(permuteThisElement);
39 newResult.addAll(otherCombinationList);
40 result.add(newResult);
41 }
42 }
43 return result;
44 }
45 }
46
47 public static Stream<Arguments> permutation(Object[]... valueOption) {
48 List<List<Object>> permutations = permutationInternal(0, valueOption);
49 return permutations.stream().map(x -> Arguments.of(x.toArray()));
50 }
51
52 public static Stream<Arguments> permutationWithSize(Object[]... valueOption) {
53 int size = 1;
54 for (int i = 0; i < valueOption.length; i++) {
55 size *= valueOption[i].length;
56 }
57 Object[][] newValueOption = new Object[valueOption.length + 1][];
58 newValueOption[0] = new Object[] { size };
59 for (int i = 1; i < newValueOption.length; i++) {
60 newValueOption[i] = valueOption[i - 1];
61 }
62 return permutation(newValueOption);
63 }
64}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/fuzz/utils/FuzzTestUtilsTest.java b/store/src/test/java/tools/refinery/data/map/tests/fuzz/utils/FuzzTestUtilsTest.java
new file mode 100644
index 00000000..abf8be3c
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/fuzz/utils/FuzzTestUtilsTest.java
@@ -0,0 +1,33 @@
1package tools.refinery.data.map.tests.fuzz.utils;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4
5import java.util.List;
6
7import org.junit.jupiter.api.Test;
8
9class FuzzTestUtilsTest {
10 @Test
11 void permutationInternalTest() {
12 List<List<Object>> res = FuzzTestUtils.permutationInternal(0, new Object[] { 1, 2, 3 },
13 new Object[] { 'a', 'b', 'c' }, new Object[] { "alpha", "beta", "gamma", "delta" });
14 assertEquals(3 * 3 * 4, res.size());
15 }
16
17 @Test
18 void permutationTest1() {
19 var res = FuzzTestUtils.permutation(new Object[] { 1, 2, 3 }, new Object[] { 'a', 'b', 'c' },
20 new Object[] { "alpha", "beta", "gamma", "delta" });
21 assertEquals(3 * 3 * 4, res.count());
22 }
23
24 @Test
25 void permutationTest2() {
26 var res = FuzzTestUtils.permutation(new Object[] { 1, 2, 3 }, new Object[] { 'a', 'b', 'c' },
27 new Object[] { "alpha", "beta", "gamma", "delta" });
28 var arguments = res.findFirst().get().get();
29 assertEquals(1, arguments[0]);
30 assertEquals('a', arguments[1]);
31 assertEquals("alpha", arguments[2]);
32 }
33}
diff --git a/store/src/test/java/tools/refinery/data/map/tests/utils/MapTestEnvironment.java b/store/src/test/java/tools/refinery/data/map/tests/utils/MapTestEnvironment.java
new file mode 100644
index 00000000..e1cfc2e2
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/map/tests/utils/MapTestEnvironment.java
@@ -0,0 +1,213 @@
1package tools.refinery.data.map.tests.utils;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertTrue;
5import static org.junit.jupiter.api.Assertions.fail;
6
7import java.util.HashMap;
8import java.util.Iterator;
9import java.util.List;
10import java.util.Map;
11import java.util.Map.Entry;
12import java.util.TreeMap;
13
14import tools.refinery.data.map.ContinousHashProvider;
15import tools.refinery.data.map.Cursor;
16import tools.refinery.data.map.VersionedMap;
17import tools.refinery.data.map.internal.VersionedMapImpl;
18
19public class MapTestEnvironment<K, V> {
20 public static String[] prepareValues(int maxValue) {
21 String[] values = new String[maxValue];
22 values[0] = "DEFAULT";
23 for (int i = 1; i < values.length; i++) {
24 values[i] = "VAL" + i;
25 }
26 return values;
27 }
28
29 public static ContinousHashProvider<Integer> prepareHashProvider(final boolean evil) {
30 // Use maxPrime = 2147483629
31
32 ContinousHashProvider<Integer> chp = new ContinousHashProvider<Integer>() {
33
34 @Override
35 public int getHash(Integer key, int index) {
36 if (evil && index < 15 && index < key / 3) {
37 return 7;
38 }
39 int result = 1;
40 final int prime = 31;
41
42 result = prime * result + key;
43 result = prime * result + index;
44
45 return result;
46 }
47 };
48 return chp;
49 }
50
51 public static void printStatus(String scenario, int actual, int max, String stepName) {
52 if (actual % 10000 == 0) {
53 String printStepName = stepName == null ? "" : stepName;
54 System.out.format(scenario + ":%d/%d (%d%%) " + printStepName + "%n", actual, max, actual * 100 / max);
55 }
56
57 }
58
59 public static <K, V> void compareTwoMaps(String title, VersionedMapImpl<K, V> map1,
60 VersionedMapImpl<K, V> map2) {
61 compareTwoMaps(title, map1, map2, null);
62 }
63 public static <K, V> void compareTwoMaps(String title, VersionedMapImpl<K, V> map1,
64 VersionedMapImpl<K, V> map2, List<Throwable> errors) {
65 // 1. Comparing cursors.
66 Cursor<K, V> cursor1 = map1.getAll();
67 Cursor<K, V> cursor2 = map2.getAll();
68 while (!cursor1.isTerminated()) {
69 if (cursor2.isTerminated()) {
70 fail("cursor 2 terminated before cursor1");
71 }
72 assertEqualsList(cursor1.getKey(), cursor2.getKey(),"Keys not equal", errors);
73 assertEqualsList(cursor2.getValue(), cursor2.getValue(), "Values not equal", errors);
74 cursor1.move();
75 cursor2.move();
76 }
77 if (!cursor2.isTerminated())
78 fail("cursor 1 terminated before cursor 2");
79
80 // 2.1. comparing hash codes
81 assertEqualsList(map1.hashCode(), map2.hashCode(), title + ": hash code check",errors);
82 assertEqualsList(map1, map2, title + ": 1.equals(2)",errors);
83 assertEqualsList(map2, map1, title + ": 2.equals(1)",errors);
84 }
85 private static void assertEqualsList(Object o1, Object o2, String message, List<Throwable> errors) {
86 if(errors == null) {
87 assertEquals(o1, o2, message);
88 } else {
89 if(o1 != null) {
90 if(!(o1.equals(o2))) {
91 AssertionError error = new AssertionError((message != null ? message+" " : "") + "expected: " + o1 + " but was : " + o2);
92 errors.add(error);
93 }
94 }
95 }
96 }
97
98 public VersionedMapImpl<K, V> sut;
99 Map<K, V> oracle = new HashMap<K, V>();
100
101 public MapTestEnvironment(VersionedMapImpl<K, V> sut) {
102 this.sut = sut;
103 }
104
105 public void put(K key, V value) {
106 V oldSutValue = sut.put(key, value);
107 V oldOracleValue;
108 if (value != sut.getDefaultValue()) {
109 oldOracleValue = oracle.put(key, value);
110 } else {
111 oldOracleValue = oracle.remove(key);
112 }
113 if(oldSutValue == sut.getDefaultValue() && oldOracleValue != null) {
114 fail("After put, SUT old value was default, but oracle old walue was " + oldOracleValue);
115 }
116 if(oldSutValue != sut.getDefaultValue()) {
117 assertEquals(oldOracleValue, oldSutValue);
118 }
119 }
120
121 public void checkEquivalence(String title) {
122 // 0. Checking integrity
123 try {
124 sut.checkIntegrity();
125 } catch (IllegalStateException e) {
126 fail(title + ": " + e.getMessage());
127 }
128
129 // 1. Checking: if Reference contains <key,value> pair, then SUT contains
130 // <key,value> pair.
131 // Tests get functions
132 for (Entry<K, V> entry : oracle.entrySet()) {
133 V sutValue = sut.get(entry.getKey());
134 V oracleValue = entry.getValue();
135 if (sutValue != oracleValue) {
136 printComparison();
137 fail(title + ": Non-equivalent get(" + entry.getKey() + ") results: SUT=" + sutValue + ", Oracle="
138 + oracleValue + "!");
139 }
140 }
141
142 // 2. Checking: if SUT contains <key,value> pair, then Reference contains
143 // <key,value> pair.
144 // Tests iterators
145 int elementsInSutEntrySet = 0;
146 Cursor<K, V> cursor = sut.getAll();
147 while (cursor.move()) {
148 elementsInSutEntrySet++;
149 K key = cursor.getKey();
150 V sutValue = cursor.getValue();
151 // System.out.println(key + " -> " + sutValue);
152 V oracleValue = oracle.get(key);
153 if (sutValue != oracleValue) {
154 printComparison();
155 fail(title + ": Non-equivalent entry in iterator: SUT=<" + key + "," + sutValue + ">, Oracle=<" + key
156 + "," + oracleValue + ">!");
157 }
158
159 }
160
161 // 3. Checking sizes
162 // Counting of non-default value pairs.
163 int oracleSize = oracle.entrySet().size();
164 long sutSize = sut.getSize();
165 if (oracleSize != sutSize || oracleSize != elementsInSutEntrySet) {
166 printComparison();
167 fail(title + ": Non-eqivalent size() result: SUT.getSize()=" + sutSize + ", SUT.entryset.size="
168 + elementsInSutEntrySet + ", Oracle=" + oracleSize + "!");
169 }
170 }
171
172 public static <K,V> void checkOrder(String scenario, VersionedMap<K,V> versionedMap) {
173 K previous = null;
174 Cursor<K, V> cursor = versionedMap.getAll();
175 while(cursor.move()) {
176 System.out.println(cursor.getKey() + " " + ((VersionedMapImpl<K, V>) versionedMap).getHashProvider().getHash(cursor.getKey(), 0));
177 if(previous != null) {
178 int comparisonResult = ((VersionedMapImpl<K, V>) versionedMap).getHashProvider().compare(previous, cursor.getKey());
179 assertTrue(comparisonResult<0,scenario+" Cursor order is not incremental!");
180 }
181 previous = cursor.getKey();
182 }
183 System.out.println();
184 }
185
186 public void printComparison() {
187 System.out.println("SUT:");
188 printEntrySet(sut.getAll());
189 System.out.println("Oracle:");
190 printEntrySet(oracle.entrySet().iterator());
191 }
192
193 private void printEntrySet(Iterator<Entry<K, V>> iterator) {
194 TreeMap<K, V> treemap = new TreeMap<>();
195 while (iterator.hasNext()) {
196 Entry<K, V> entry = iterator.next();
197 treemap.put(entry.getKey(), entry.getValue());
198 }
199 for (Entry<K, V> e : treemap.entrySet()) {
200 System.out.println("\t" + e.getKey() + " -> " + e.getValue());
201 }
202 }
203
204 private void printEntrySet(Cursor<K, V> cursor) {
205 TreeMap<K, V> treemap = new TreeMap<>();
206 while (cursor.move()) {
207 treemap.put(cursor.getKey(), cursor.getValue());
208 }
209 for (Entry<K, V> e : treemap.entrySet()) {
210 System.out.println("\t" + e.getKey() + " -> " + e.getValue());
211 }
212 }
213}