diff options
Diffstat (limited to 'store/src/test/java/org/eclipse/viatra/solver/data/map/tests/utils')
-rw-r--r-- | store/src/test/java/org/eclipse/viatra/solver/data/map/tests/utils/MapTestEnvironment.java | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/store/src/test/java/org/eclipse/viatra/solver/data/map/tests/utils/MapTestEnvironment.java b/store/src/test/java/org/eclipse/viatra/solver/data/map/tests/utils/MapTestEnvironment.java new file mode 100644 index 00000000..4c043350 --- /dev/null +++ b/store/src/test/java/org/eclipse/viatra/solver/data/map/tests/utils/MapTestEnvironment.java | |||
@@ -0,0 +1,213 @@ | |||
1 | package org.eclipse.viatra.solver.data.map.tests.utils; | ||
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 | import java.util.TreeMap; | ||
13 | |||
14 | import org.eclipse.viatra.solver.data.map.ContinousHashProvider; | ||
15 | import org.eclipse.viatra.solver.data.map.Cursor; | ||
16 | import org.eclipse.viatra.solver.data.map.VersionedMap; | ||
17 | import org.eclipse.viatra.solver.data.map.internal.VersionedMapImpl; | ||
18 | |||
19 | public 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 | } | ||