From 6ccc179ea305fc27ae121253b1d1f172bad676fd Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Tue, 13 Dec 2022 03:21:58 +0100 Subject: 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). --- .../map/tests/fuzz/ContentEqualsFuzzTest.java | 52 ++++++++-------------- .../store/map/tests/utils/MapTestEnvironment.java | 38 ++++++---------- 2 files changed, 32 insertions(+), 58 deletions(-) (limited to 'subprojects/store/src/test/java') 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 @@ package tools.refinery.store.map.tests.fuzz; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -import java.util.AbstractMap.SimpleEntry; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Random; -import java.util.stream.Stream; - import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Timeout; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; - -import tools.refinery.store.map.ContinousHashProvider; -import tools.refinery.store.map.Cursor; -import tools.refinery.store.map.VersionedMap; -import tools.refinery.store.map.VersionedMapStore; -import tools.refinery.store.map.VersionedMapStoreImpl; +import tools.refinery.store.map.*; import tools.refinery.store.map.internal.VersionedMapImpl; import tools.refinery.store.map.tests.fuzz.utils.FuzzTestUtils; import tools.refinery.store.map.tests.utils.MapTestEnvironment; +import java.util.AbstractMap.SimpleEntry; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + class ContentEqualsFuzzTest { private void runFuzzTest(String scenario, int seed, int steps, int maxKey, int maxValue, int commitFrequency, boolean evilHash) { @@ -37,10 +32,9 @@ class ContentEqualsFuzzTest { } private void iterativeRandomPutsAndCommitsThenCompare(String scenario, ContinousHashProvider chp, int steps, int maxKey, String[] values, Random r, int commitFrequency) { - VersionedMapStore store1 = new VersionedMapStoreImpl(chp, values[0]); VersionedMap sut1 = store1.createMap(); - + // Fill one map for (int i = 0; i < steps; i++) { int index1 = i + 1; @@ -57,17 +51,17 @@ class ContentEqualsFuzzTest { sut1.commit(); } } - + // Get the content of the first map List> content = new LinkedList<>(); Cursor cursor = sut1.getAll(); while (cursor.move()) { content.add(new SimpleEntry<>(cursor.getKey(), cursor.getValue())); } - + // Randomize the order of the content Collections.shuffle(content, r); - + VersionedMapStore store2 = new VersionedMapStoreImpl(chp, values[0]); VersionedMap sut2 = store2.createMap(); int index2 = 1; @@ -76,18 +70,16 @@ class ContentEqualsFuzzTest { if(index2++%commitFrequency == 0) sut2.commit(); } - + // Check the integrity of the maps ((VersionedMapImpl) sut1).checkIntegrity(); ((VersionedMapImpl) sut2).checkIntegrity(); - + // // Compare the two maps // By size assertEquals(sut1.getSize(), content.size()); assertEquals(sut2.getSize(), content.size()); - - - + // By cursors Cursor cursor1 = sut1.getAll(); Cursor cursor2 = sut2.getAll(); @@ -99,16 +91,10 @@ class ContentEqualsFuzzTest { assertEquals(canMove1, canMove2, scenario + ":" + index3 +" Cursors stopped at different times!"); assertEquals(cursor1.getKey(), cursor2.getKey(), scenario + ":" + index3 +" Cursors have different keys!"); assertEquals(cursor1.getValue(), cursor2.getValue(), scenario + ":" + index3 +" Cursors have different values!"); - + canMove = canMove1; MapTestEnvironment.printStatus(scenario, index3++, content.size(), "Compare"); } while (canMove); - - // By hashcode - assertEquals(sut1.hashCode(), sut2.hashCode(), "Hash codes are not equal!"); - - // By equals - assertEquals(sut1, sut2, "Maps are not equals"); } @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 @@ package tools.refinery.store.map.tests.utils; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - import tools.refinery.store.map.ContinousHashProvider; import tools.refinery.store.map.Cursor; import tools.refinery.store.map.VersionedMap; import tools.refinery.store.map.internal.VersionedMapImpl; -import java.util.TreeMap; +import java.util.*; +import java.util.Map.Entry; + +import static org.junit.jupiter.api.Assertions.*; public class MapTestEnvironment { public static String[] prepareValues(int maxValue) { @@ -63,7 +56,6 @@ public class MapTestEnvironment { } public static void compareTwoMaps(String title, VersionedMapImpl map1, VersionedMapImpl map2, List errors) { - // 1. Comparing cursors. Cursor cursor1 = map1.getAll(); Cursor cursor2 = map2.getAll(); while (!cursor1.isTerminated()) { @@ -77,12 +69,8 @@ public class MapTestEnvironment { } if (!cursor2.isTerminated()) fail("cursor 1 terminated before cursor 2"); - - // 2.1. comparing hash codes - assertEqualsList(map1.hashCode(), map2.hashCode(), title + ": hash code check",errors); - assertEqualsList(map1, map2, title + ": 1.equals(2)",errors); - assertEqualsList(map2, map1, title + ": 2.equals(1)",errors); } + private static void assertEqualsList(Object o1, Object o2, String message, List errors) { if(errors == null) { assertEquals(o1, o2, message); @@ -112,7 +100,7 @@ public class MapTestEnvironment { oldOracleValue = oracle.remove(key); } if(oldSutValue == sut.getDefaultValue() && oldOracleValue != null) { - fail("After put, SUT old nodeId was default, but oracle old walue was " + oldOracleValue); + fail("After put, SUT old nodeId was default, but oracle old value was " + oldOracleValue); } if(oldSutValue != sut.getDefaultValue()) { assertEquals(oldOracleValue, oldSutValue); @@ -165,7 +153,7 @@ public class MapTestEnvironment { long sutSize = sut.getSize(); if (oracleSize != sutSize || oracleSize != elementsInSutEntrySet) { printComparison(); - fail(title + ": Non-eqivalent size() result: SUT.getSize()=" + sutSize + ", SUT.entryset.size=" + fail(title + ": Non-equivalent size() result: SUT.getSize()=" + sutSize + ", SUT.entryset.size=" + elementsInSutEntrySet + ", Oracle=" + oracleSize + "!"); } } @@ -192,22 +180,22 @@ public class MapTestEnvironment { } private void printEntrySet(Iterator> iterator) { - TreeMap treemap = new TreeMap<>(); + Map map = new LinkedHashMap<>(); while (iterator.hasNext()) { Entry entry = iterator.next(); - treemap.put(entry.getKey(), entry.getValue()); + map.put(entry.getKey(), entry.getValue()); } - for (Entry e : treemap.entrySet()) { + for (Entry e : map.entrySet()) { System.out.println("\t" + e.getKey() + " -> " + e.getValue()); } } private void printEntrySet(Cursor cursor) { - TreeMap treemap = new TreeMap<>(); + Map map = new LinkedHashMap<>(); while (cursor.move()) { - treemap.put(cursor.getKey(), cursor.getValue()); + map.put(cursor.getKey(), cursor.getValue()); } - for (Entry e : treemap.entrySet()) { + for (Entry e : map.entrySet()) { System.out.println("\t" + e.getKey() + " -> " + e.getValue()); } } -- cgit v1.2.3-70-g09d2