diff options
author | 2022-12-13 03:21:58 +0100 | |
---|---|---|
committer | 2022-12-13 12:25:02 +0100 | |
commit | 6ccc179ea305fc27ae121253b1d1f172bad676fd (patch) | |
tree | 8c5bb8dd7bf45157cabb57b19d17ce9af11aca5f /subprojects/store/src/test/java | |
parent | chore(deps): upgrade to yarn canary (diff) | |
download | refinery-6ccc179ea305fc27ae121253b1d1f172bad676fd.tar.gz refinery-6ccc179ea305fc27ae121253b1d1f172bad676fd.tar.zst refinery-6ccc179ea305fc27ae121253b1d1f172bad676fd.zip |
refactor(store): simplify return types
Prefers sealed non-generic interfaces over wildcard types to avoid
confusion about method return types, especially in collections
(see SonarQube rule java:S1452).
Diffstat (limited to 'subprojects/store/src/test/java')
2 files changed, 32 insertions, 58 deletions
diff --git a/subprojects/store/src/test/java/tools/refinery/store/map/tests/fuzz/ContentEqualsFuzzTest.java b/subprojects/store/src/test/java/tools/refinery/store/map/tests/fuzz/ContentEqualsFuzzTest.java index 263cb2cd..d7f77d1a 100644 --- a/subprojects/store/src/test/java/tools/refinery/store/map/tests/fuzz/ContentEqualsFuzzTest.java +++ b/subprojects/store/src/test/java/tools/refinery/store/map/tests/fuzz/ContentEqualsFuzzTest.java | |||
@@ -1,30 +1,25 @@ | |||
1 | package tools.refinery.store.map.tests.fuzz; | 1 | package tools.refinery.store.map.tests.fuzz; |
2 | 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.junit.jupiter.api.Tag; | 3 | import org.junit.jupiter.api.Tag; |
14 | import org.junit.jupiter.api.Timeout; | 4 | import org.junit.jupiter.api.Timeout; |
15 | import org.junit.jupiter.params.ParameterizedTest; | 5 | import org.junit.jupiter.params.ParameterizedTest; |
16 | import org.junit.jupiter.params.provider.Arguments; | 6 | import org.junit.jupiter.params.provider.Arguments; |
17 | import org.junit.jupiter.params.provider.MethodSource; | 7 | import org.junit.jupiter.params.provider.MethodSource; |
18 | 8 | import tools.refinery.store.map.*; | |
19 | import tools.refinery.store.map.ContinousHashProvider; | ||
20 | import tools.refinery.store.map.Cursor; | ||
21 | import tools.refinery.store.map.VersionedMap; | ||
22 | import tools.refinery.store.map.VersionedMapStore; | ||
23 | import tools.refinery.store.map.VersionedMapStoreImpl; | ||
24 | import tools.refinery.store.map.internal.VersionedMapImpl; | 9 | import tools.refinery.store.map.internal.VersionedMapImpl; |
25 | import tools.refinery.store.map.tests.fuzz.utils.FuzzTestUtils; | 10 | import tools.refinery.store.map.tests.fuzz.utils.FuzzTestUtils; |
26 | import tools.refinery.store.map.tests.utils.MapTestEnvironment; | 11 | import tools.refinery.store.map.tests.utils.MapTestEnvironment; |
27 | 12 | ||
13 | import java.util.AbstractMap.SimpleEntry; | ||
14 | import java.util.Collections; | ||
15 | import java.util.LinkedList; | ||
16 | import java.util.List; | ||
17 | import java.util.Random; | ||
18 | import java.util.stream.Stream; | ||
19 | |||
20 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
21 | import static org.junit.jupiter.api.Assertions.fail; | ||
22 | |||
28 | class ContentEqualsFuzzTest { | 23 | class ContentEqualsFuzzTest { |
29 | private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency, | 24 | private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency, |
30 | boolean evilHash) { | 25 | boolean evilHash) { |
@@ -37,10 +32,9 @@ class ContentEqualsFuzzTest { | |||
37 | } | 32 | } |
38 | 33 | ||
39 | private void iterativeRandomPutsAndCommitsThenCompare(String scenario, ContinousHashProvider<Integer> chp, int steps, int maxKey, String[] values, Random r, int commitFrequency) { | 34 | 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]); | 35 | VersionedMapStore<Integer, String> store1 = new VersionedMapStoreImpl<Integer, String>(chp, values[0]); |
42 | VersionedMap<Integer, String> sut1 = store1.createMap(); | 36 | VersionedMap<Integer, String> sut1 = store1.createMap(); |
43 | 37 | ||
44 | // Fill one map | 38 | // Fill one map |
45 | for (int i = 0; i < steps; i++) { | 39 | for (int i = 0; i < steps; i++) { |
46 | int index1 = i + 1; | 40 | int index1 = i + 1; |
@@ -57,17 +51,17 @@ class ContentEqualsFuzzTest { | |||
57 | sut1.commit(); | 51 | sut1.commit(); |
58 | } | 52 | } |
59 | } | 53 | } |
60 | 54 | ||
61 | // Get the content of the first map | 55 | // Get the content of the first map |
62 | List<SimpleEntry<Integer, String>> content = new LinkedList<>(); | 56 | List<SimpleEntry<Integer, String>> content = new LinkedList<>(); |
63 | Cursor<Integer, String> cursor = sut1.getAll(); | 57 | Cursor<Integer, String> cursor = sut1.getAll(); |
64 | while (cursor.move()) { | 58 | while (cursor.move()) { |
65 | content.add(new SimpleEntry<>(cursor.getKey(), cursor.getValue())); | 59 | content.add(new SimpleEntry<>(cursor.getKey(), cursor.getValue())); |
66 | } | 60 | } |
67 | 61 | ||
68 | // Randomize the order of the content | 62 | // Randomize the order of the content |
69 | Collections.shuffle(content, r); | 63 | Collections.shuffle(content, r); |
70 | 64 | ||
71 | VersionedMapStore<Integer, String> store2 = new VersionedMapStoreImpl<Integer, String>(chp, values[0]); | 65 | VersionedMapStore<Integer, String> store2 = new VersionedMapStoreImpl<Integer, String>(chp, values[0]); |
72 | VersionedMap<Integer, String> sut2 = store2.createMap(); | 66 | VersionedMap<Integer, String> sut2 = store2.createMap(); |
73 | int index2 = 1; | 67 | int index2 = 1; |
@@ -76,18 +70,16 @@ class ContentEqualsFuzzTest { | |||
76 | if(index2++%commitFrequency == 0) | 70 | if(index2++%commitFrequency == 0) |
77 | sut2.commit(); | 71 | sut2.commit(); |
78 | } | 72 | } |
79 | 73 | ||
80 | // Check the integrity of the maps | 74 | // Check the integrity of the maps |
81 | ((VersionedMapImpl<Integer,String>) sut1).checkIntegrity(); | 75 | ((VersionedMapImpl<Integer,String>) sut1).checkIntegrity(); |
82 | ((VersionedMapImpl<Integer,String>) sut2).checkIntegrity(); | 76 | ((VersionedMapImpl<Integer,String>) sut2).checkIntegrity(); |
83 | 77 | ||
84 | // // Compare the two maps | 78 | // // Compare the two maps |
85 | // By size | 79 | // By size |
86 | assertEquals(sut1.getSize(), content.size()); | 80 | assertEquals(sut1.getSize(), content.size()); |
87 | assertEquals(sut2.getSize(), content.size()); | 81 | assertEquals(sut2.getSize(), content.size()); |
88 | 82 | ||
89 | |||
90 | |||
91 | // By cursors | 83 | // By cursors |
92 | Cursor<Integer, String> cursor1 = sut1.getAll(); | 84 | Cursor<Integer, String> cursor1 = sut1.getAll(); |
93 | Cursor<Integer, String> cursor2 = sut2.getAll(); | 85 | Cursor<Integer, String> cursor2 = sut2.getAll(); |
@@ -99,16 +91,10 @@ class ContentEqualsFuzzTest { | |||
99 | assertEquals(canMove1, canMove2, scenario + ":" + index3 +" Cursors stopped at different times!"); | 91 | assertEquals(canMove1, canMove2, scenario + ":" + index3 +" Cursors stopped at different times!"); |
100 | assertEquals(cursor1.getKey(), cursor2.getKey(), scenario + ":" + index3 +" Cursors have different keys!"); | 92 | assertEquals(cursor1.getKey(), cursor2.getKey(), scenario + ":" + index3 +" Cursors have different keys!"); |
101 | assertEquals(cursor1.getValue(), cursor2.getValue(), scenario + ":" + index3 +" Cursors have different values!"); | 93 | assertEquals(cursor1.getValue(), cursor2.getValue(), scenario + ":" + index3 +" Cursors have different values!"); |
102 | 94 | ||
103 | canMove = canMove1; | 95 | canMove = canMove1; |
104 | MapTestEnvironment.printStatus(scenario, index3++, content.size(), "Compare"); | 96 | MapTestEnvironment.printStatus(scenario, index3++, content.size(), "Compare"); |
105 | } while (canMove); | 97 | } 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 | } | 98 | } |
113 | 99 | ||
114 | @ParameterizedTest(name = "Compare {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}") | 100 | @ParameterizedTest(name = "Compare {index}/{0} Steps={1} Keys={2} Values={3} commit frequency={4} seed={5} evil-hash={6}") |
diff --git a/subprojects/store/src/test/java/tools/refinery/store/map/tests/utils/MapTestEnvironment.java b/subprojects/store/src/test/java/tools/refinery/store/map/tests/utils/MapTestEnvironment.java index a4ba7441..b73f8b32 100644 --- a/subprojects/store/src/test/java/tools/refinery/store/map/tests/utils/MapTestEnvironment.java +++ b/subprojects/store/src/test/java/tools/refinery/store/map/tests/utils/MapTestEnvironment.java | |||
@@ -1,21 +1,14 @@ | |||
1 | package tools.refinery.store.map.tests.utils; | 1 | package tools.refinery.store.map.tests.utils; |
2 | 2 | ||
3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
4 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
5 | import static org.junit.jupiter.api.Assertions.fail; | ||
6 | |||
7 | import java.util.HashMap; | ||
8 | import java.util.Iterator; | ||
9 | import java.util.List; | ||
10 | import java.util.Map; | ||
11 | import java.util.Map.Entry; | ||
12 | |||
13 | import tools.refinery.store.map.ContinousHashProvider; | 3 | import tools.refinery.store.map.ContinousHashProvider; |
14 | import tools.refinery.store.map.Cursor; | 4 | import tools.refinery.store.map.Cursor; |
15 | import tools.refinery.store.map.VersionedMap; | 5 | import tools.refinery.store.map.VersionedMap; |
16 | import tools.refinery.store.map.internal.VersionedMapImpl; | 6 | import tools.refinery.store.map.internal.VersionedMapImpl; |
17 | 7 | ||
18 | import java.util.TreeMap; | 8 | import java.util.*; |
9 | import java.util.Map.Entry; | ||
10 | |||
11 | import static org.junit.jupiter.api.Assertions.*; | ||
19 | 12 | ||
20 | public class MapTestEnvironment<K, V> { | 13 | public class MapTestEnvironment<K, V> { |
21 | public static String[] prepareValues(int maxValue) { | 14 | public static String[] prepareValues(int maxValue) { |
@@ -63,7 +56,6 @@ public class MapTestEnvironment<K, V> { | |||
63 | } | 56 | } |
64 | public static <K, V> void compareTwoMaps(String title, VersionedMapImpl<K, V> map1, | 57 | public static <K, V> void compareTwoMaps(String title, VersionedMapImpl<K, V> map1, |
65 | VersionedMapImpl<K, V> map2, List<Throwable> errors) { | 58 | VersionedMapImpl<K, V> map2, List<Throwable> errors) { |
66 | // 1. Comparing cursors. | ||
67 | Cursor<K, V> cursor1 = map1.getAll(); | 59 | Cursor<K, V> cursor1 = map1.getAll(); |
68 | Cursor<K, V> cursor2 = map2.getAll(); | 60 | Cursor<K, V> cursor2 = map2.getAll(); |
69 | while (!cursor1.isTerminated()) { | 61 | while (!cursor1.isTerminated()) { |
@@ -77,12 +69,8 @@ public class MapTestEnvironment<K, V> { | |||
77 | } | 69 | } |
78 | if (!cursor2.isTerminated()) | 70 | if (!cursor2.isTerminated()) |
79 | fail("cursor 1 terminated before cursor 2"); | 71 | fail("cursor 1 terminated before cursor 2"); |
80 | |||
81 | // 2.1. comparing hash codes | ||
82 | assertEqualsList(map1.hashCode(), map2.hashCode(), title + ": hash code check",errors); | ||
83 | assertEqualsList(map1, map2, title + ": 1.equals(2)",errors); | ||
84 | assertEqualsList(map2, map1, title + ": 2.equals(1)",errors); | ||
85 | } | 72 | } |
73 | |||
86 | private static void assertEqualsList(Object o1, Object o2, String message, List<Throwable> errors) { | 74 | private static void assertEqualsList(Object o1, Object o2, String message, List<Throwable> errors) { |
87 | if(errors == null) { | 75 | if(errors == null) { |
88 | assertEquals(o1, o2, message); | 76 | assertEquals(o1, o2, message); |
@@ -112,7 +100,7 @@ public class MapTestEnvironment<K, V> { | |||
112 | oldOracleValue = oracle.remove(key); | 100 | oldOracleValue = oracle.remove(key); |
113 | } | 101 | } |
114 | if(oldSutValue == sut.getDefaultValue() && oldOracleValue != null) { | 102 | if(oldSutValue == sut.getDefaultValue() && oldOracleValue != null) { |
115 | fail("After put, SUT old nodeId was default, but oracle old walue was " + oldOracleValue); | 103 | fail("After put, SUT old nodeId was default, but oracle old value was " + oldOracleValue); |
116 | } | 104 | } |
117 | if(oldSutValue != sut.getDefaultValue()) { | 105 | if(oldSutValue != sut.getDefaultValue()) { |
118 | assertEquals(oldOracleValue, oldSutValue); | 106 | assertEquals(oldOracleValue, oldSutValue); |
@@ -165,7 +153,7 @@ public class MapTestEnvironment<K, V> { | |||
165 | long sutSize = sut.getSize(); | 153 | long sutSize = sut.getSize(); |
166 | if (oracleSize != sutSize || oracleSize != elementsInSutEntrySet) { | 154 | if (oracleSize != sutSize || oracleSize != elementsInSutEntrySet) { |
167 | printComparison(); | 155 | printComparison(); |
168 | fail(title + ": Non-eqivalent size() result: SUT.getSize()=" + sutSize + ", SUT.entryset.size=" | 156 | fail(title + ": Non-equivalent size() result: SUT.getSize()=" + sutSize + ", SUT.entryset.size=" |
169 | + elementsInSutEntrySet + ", Oracle=" + oracleSize + "!"); | 157 | + elementsInSutEntrySet + ", Oracle=" + oracleSize + "!"); |
170 | } | 158 | } |
171 | } | 159 | } |
@@ -192,22 +180,22 @@ public class MapTestEnvironment<K, V> { | |||
192 | } | 180 | } |
193 | 181 | ||
194 | private void printEntrySet(Iterator<Entry<K, V>> iterator) { | 182 | private void printEntrySet(Iterator<Entry<K, V>> iterator) { |
195 | TreeMap<K, V> treemap = new TreeMap<>(); | 183 | Map<K, V> map = new LinkedHashMap<>(); |
196 | while (iterator.hasNext()) { | 184 | while (iterator.hasNext()) { |
197 | Entry<K, V> entry = iterator.next(); | 185 | Entry<K, V> entry = iterator.next(); |
198 | treemap.put(entry.getKey(), entry.getValue()); | 186 | map.put(entry.getKey(), entry.getValue()); |
199 | } | 187 | } |
200 | for (Entry<K, V> e : treemap.entrySet()) { | 188 | for (Entry<K, V> e : map.entrySet()) { |
201 | System.out.println("\t" + e.getKey() + " -> " + e.getValue()); | 189 | System.out.println("\t" + e.getKey() + " -> " + e.getValue()); |
202 | } | 190 | } |
203 | } | 191 | } |
204 | 192 | ||
205 | private void printEntrySet(Cursor<K, V> cursor) { | 193 | private void printEntrySet(Cursor<K, V> cursor) { |
206 | TreeMap<K, V> treemap = new TreeMap<>(); | 194 | Map<K, V> map = new LinkedHashMap<>(); |
207 | while (cursor.move()) { | 195 | while (cursor.move()) { |
208 | treemap.put(cursor.getKey(), cursor.getValue()); | 196 | map.put(cursor.getKey(), cursor.getValue()); |
209 | } | 197 | } |
210 | for (Entry<K, V> e : treemap.entrySet()) { | 198 | for (Entry<K, V> e : map.entrySet()) { |
211 | System.out.println("\t" + e.getKey() + " -> " + e.getValue()); | 199 | System.out.println("\t" + e.getKey() + " -> " + e.getValue()); |
212 | } | 200 | } |
213 | } | 201 | } |