diff options
author | 2021-09-29 02:45:57 +0200 | |
---|---|---|
committer | 2021-09-29 03:16:01 +0200 | |
commit | a155f6ba02e08a75ce6e474a86900b8363f506e8 (patch) | |
tree | b78804c1c0f0968a9625f0656e08f5dadc16924c /store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/ContentEqualsFuzzTest.java | |
parent | Simplify branding (diff) | |
download | refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.tar.gz refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.tar.zst refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.zip |
build: migration to Gradle 7
Diffstat (limited to 'store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/ContentEqualsFuzzTest.java')
-rw-r--r-- | store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/ContentEqualsFuzzTest.java | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/ContentEqualsFuzzTest.java b/store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/ContentEqualsFuzzTest.java new file mode 100644 index 00000000..5004c152 --- /dev/null +++ b/store/src/test/java/org/eclipse/viatra/solver/data/map/tests/fuzz/ContentEqualsFuzzTest.java | |||
@@ -0,0 +1,142 @@ | |||
1 | package org.eclipse.viatra.solver.data.map.tests.fuzz; | ||
2 | |||
3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
4 | import static org.junit.jupiter.api.Assertions.fail; | ||
5 | |||
6 | import java.util.AbstractMap.SimpleEntry; | ||
7 | import java.util.Collections; | ||
8 | import java.util.LinkedList; | ||
9 | import java.util.List; | ||
10 | import java.util.Random; | ||
11 | import java.util.stream.Stream; | ||
12 | |||
13 | import org.eclipse.viatra.solver.data.map.ContinousHashProvider; | ||
14 | import org.eclipse.viatra.solver.data.map.Cursor; | ||
15 | import org.eclipse.viatra.solver.data.map.VersionedMap; | ||
16 | import org.eclipse.viatra.solver.data.map.VersionedMapStore; | ||
17 | import org.eclipse.viatra.solver.data.map.VersionedMapStoreImpl; | ||
18 | import org.eclipse.viatra.solver.data.map.internal.VersionedMapImpl; | ||
19 | import org.eclipse.viatra.solver.data.map.tests.fuzz.utils.FuzzTestUtils; | ||
20 | import org.eclipse.viatra.solver.data.map.tests.utils.MapTestEnvironment; | ||
21 | import org.junit.jupiter.api.Tag; | ||
22 | import org.junit.jupiter.api.Timeout; | ||
23 | import org.junit.jupiter.params.ParameterizedTest; | ||
24 | import org.junit.jupiter.params.provider.Arguments; | ||
25 | import org.junit.jupiter.params.provider.MethodSource; | ||
26 | |||
27 | class ContentEqualsFuzzTest { | ||
28 | private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency, | ||
29 | boolean evilHash) { | ||
30 | String[] values = MapTestEnvironment.prepareValues(maxValue); | ||
31 | ContinousHashProvider<Integer> chp = MapTestEnvironment.prepareHashProvider(evilHash); | ||
32 | |||
33 | Random r = new Random(seed); | ||
34 | |||
35 | iterativeRandomPutsAndCommitsThenCompare(scenario, chp, steps, maxKey, values, r, commitFrequency); | ||
36 | } | ||
37 | |||
38 | private void iterativeRandomPutsAndCommitsThenCompare(String scenario, ContinousHashProvider<Integer> chp, int steps, int maxKey, String[] values, Random r, int commitFrequency) { | ||
39 | |||
40 | VersionedMapStore<Integer, String> store1 = new VersionedMapStoreImpl<Integer, String>(chp, values[0]); | ||
41 | VersionedMap<Integer, String> sut1 = store1.createMap(); | ||
42 | |||
43 | // Fill one map | ||
44 | for (int i = 0; i < steps; i++) { | ||
45 | int index1 = i + 1; | ||
46 | int nextKey = r.nextInt(maxKey); | ||
47 | String nextValue = values[r.nextInt(values.length)]; | ||
48 | try { | ||
49 | sut1.put(nextKey, nextValue); | ||
50 | } catch (Exception exception) { | ||
51 | exception.printStackTrace(); | ||
52 | fail(scenario + ":" + index1 + ": exception happened: " + exception); | ||
53 | } | ||
54 | MapTestEnvironment.printStatus(scenario, index1, steps, "Fill"); | ||
55 | if (index1 % commitFrequency == 0) { | ||
56 | sut1.commit(); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | // Get the content of the first map | ||
61 | List<SimpleEntry<Integer, String>> content = new LinkedList<>(); | ||
62 | Cursor<Integer, String> cursor = sut1.getAll(); | ||
63 | while (cursor.move()) { | ||
64 | content.add(new SimpleEntry<>(cursor.getKey(), cursor.getValue())); | ||
65 | } | ||
66 | |||
67 | // Randomize the order of the content | ||
68 | Collections.shuffle(content, r); | ||
69 | |||
70 | VersionedMapStore<Integer, String> store2 = new VersionedMapStoreImpl<Integer, String>(chp, values[0]); | ||
71 | VersionedMap<Integer, String> sut2 = store2.createMap(); | ||
72 | int index2 = 1; | ||
73 | for (SimpleEntry<Integer, String> entry : content) { | ||
74 | sut2.put(entry.getKey(), entry.getValue()); | ||
75 | if(index2++%commitFrequency == 0) | ||
76 | sut2.commit(); | ||
77 | } | ||
78 | |||
79 | // Check the integrity of the maps | ||
80 | ((VersionedMapImpl<Integer,String>) sut1).checkIntegrity(); | ||
81 | ((VersionedMapImpl<Integer,String>) sut2).checkIntegrity(); | ||
82 | |||
83 | // // Compare the two maps | ||
84 | // By size | ||
85 | assertEquals(sut1.getSize(), content.size()); | ||
86 | assertEquals(sut2.getSize(), content.size()); | ||
87 | |||
88 | |||
89 | |||
90 | // By cursors | ||
91 | Cursor<Integer, String> cursor1 = sut1.getAll(); | ||
92 | Cursor<Integer, String> cursor2 = sut2.getAll(); | ||
93 | int index3 = 1; | ||
94 | boolean canMove = true; | ||
95 | do{ | ||
96 | boolean canMove1 = cursor1.move(); | ||
97 | boolean canMove2 = cursor2.move(); | ||
98 | assertEquals(canMove1, canMove2, scenario + ":" + index3 +" Cursors stopped at different times!"); | ||
99 | assertEquals(cursor1.getKey(), cursor2.getKey(), scenario + ":" + index3 +" Cursors have different keys!"); | ||
100 | assertEquals(cursor1.getValue(), cursor2.getValue(), scenario + ":" + index3 +" Cursors have different values!"); | ||
101 | |||
102 | canMove = canMove1; | ||
103 | MapTestEnvironment.printStatus(scenario, index3++, content.size(), "Compare"); | ||
104 | } while (canMove); | ||
105 | |||
106 | // By hashcode | ||
107 | assertEquals(sut1.hashCode(), sut2.hashCode(), "Hash codes are not equal!"); | ||
108 | |||
109 | // By equals | ||
110 | assertEquals(sut1, sut2, "Maps are not equals"); | ||
111 | } | ||
112 | |||
113 | @ParameterizedTest(name = "Compare {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}") | ||
114 | @MethodSource | ||
115 | @Timeout(value = 10) | ||
116 | @Tag("fuzz") | ||
117 | void parametrizedFastFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed, | ||
118 | boolean evilHash) { | ||
119 | runFuzzTest("CompareS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues, | ||
120 | commitFrequency, evilHash); | ||
121 | } | ||
122 | |||
123 | static Stream<Arguments> parametrizedFastFuzz() { | ||
124 | return FuzzTestUtils.permutationWithSize(new Object[] { FuzzTestUtils.FAST_STEP_COUNT }, new Object[] { 3, 32, 32 * 32 }, | ||
125 | new Object[] { 2, 3 }, new Object[] { 1, 10, 100 }, new Object[] { 1, 2, 3 }, | ||
126 | new Object[] { false, true }); | ||
127 | } | ||
128 | |||
129 | @ParameterizedTest(name = "Compare {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}") | ||
130 | @MethodSource | ||
131 | @Tag("fuzz") | ||
132 | @Tag("slow") | ||
133 | void parametrizedSlowFuzz(int tests, int steps, int noKeys, int noValues, int commitFrequency, int seed, | ||
134 | boolean evilHash) { | ||
135 | runFuzzTest("CompareS" + steps + "K" + noKeys + "V" + noValues + "s" + seed, seed, steps, noKeys, noValues, | ||
136 | commitFrequency, evilHash); | ||
137 | } | ||
138 | |||
139 | static Stream<Arguments> parametrizedSlowFuzz() { | ||
140 | return FuzzTestUtils.changeStepCount(parametrizedFastFuzz(), 1); | ||
141 | } | ||
142 | } | ||