diff options
author | Kristóf Marussy <marussy@mit.bme.hu> | 2021-07-29 19:01:25 +0200 |
---|---|---|
committer | Kristóf Marussy <marussy@mit.bme.hu> | 2021-07-29 19:01:25 +0200 |
commit | 7f6a2528bf83f93e3d35eff228f7180e0181f4eb (patch) | |
tree | 068dc3454cc9f968162e5ae03cc8733f9011de72 /model-data/src/test/java/org/eclipse/viatra/solver/data/map/tests/support | |
parent | Refactoring based on Sonar reports (diff) | |
download | refinery-7f6a2528bf83f93e3d35eff228f7180e0181f4eb.tar.gz refinery-7f6a2528bf83f93e3d35eff228f7180e0181f4eb.tar.zst refinery-7f6a2528bf83f93e3d35eff228f7180e0181f4eb.zip |
Add new data structure for backend
Co-authored-by: Oszkár Semeráth <semerath@mit.bme.hu>
Diffstat (limited to 'model-data/src/test/java/org/eclipse/viatra/solver/data/map/tests/support')
-rw-r--r-- | model-data/src/test/java/org/eclipse/viatra/solver/data/map/tests/support/MapTestEnvironment.java | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/model-data/src/test/java/org/eclipse/viatra/solver/data/map/tests/support/MapTestEnvironment.java b/model-data/src/test/java/org/eclipse/viatra/solver/data/map/tests/support/MapTestEnvironment.java new file mode 100644 index 00000000..6c55be62 --- /dev/null +++ b/model-data/src/test/java/org/eclipse/viatra/solver/data/map/tests/support/MapTestEnvironment.java | |||
@@ -0,0 +1,174 @@ | |||
1 | package org.eclipse.viatra.solver.data.map.tests.support; | ||
2 | |||
3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
4 | import static org.junit.jupiter.api.Assertions.fail; | ||
5 | |||
6 | import java.util.HashMap; | ||
7 | import java.util.Iterator; | ||
8 | import java.util.Map; | ||
9 | import java.util.Map.Entry; | ||
10 | import java.util.TreeMap; | ||
11 | |||
12 | import org.eclipse.viatra.solver.data.map.ContinousHashProvider; | ||
13 | import org.eclipse.viatra.solver.data.map.Cursor; | ||
14 | import org.eclipse.viatra.solver.data.map.internal.VersionedMapImpl; | ||
15 | |||
16 | public class MapTestEnvironment<KEY, VALUE> { | ||
17 | public static String[] prepareValues(int maxValue) { | ||
18 | String[] values = new String[maxValue]; | ||
19 | values[0] = "DEFAULT"; | ||
20 | for (int i = 1; i < values.length; i++) { | ||
21 | values[i] = "VAL" + i; | ||
22 | } | ||
23 | return values; | ||
24 | } | ||
25 | |||
26 | public static ContinousHashProvider<Integer> prepareHashProvider(final boolean evil) { | ||
27 | // Use maxPrime = 2147483629 | ||
28 | |||
29 | ContinousHashProvider<Integer> chp = new ContinousHashProvider<Integer>() { | ||
30 | |||
31 | @Override | ||
32 | public int getHash(Integer key, int index) { | ||
33 | if (evil && index < 15 && index < key / 3) { | ||
34 | return 7; | ||
35 | } | ||
36 | int result = 1; | ||
37 | final int prime = 31; | ||
38 | |||
39 | result = prime * result + key; | ||
40 | result = prime * result + index; | ||
41 | |||
42 | return result; | ||
43 | } | ||
44 | }; | ||
45 | return chp; | ||
46 | } | ||
47 | |||
48 | public static void printStatus(String scenario, int actual, int max, String stepName) { | ||
49 | if (actual % 10000 == 0) { | ||
50 | String printStepName = stepName == null ? "" : stepName; | ||
51 | System.out.format(scenario + ":%d/%d (%d%%) " + printStepName + "%n", actual, max, actual * 100 / max); | ||
52 | } | ||
53 | |||
54 | } | ||
55 | |||
56 | public static <KEY, VALUE> void compareTwoMaps(String title, VersionedMapImpl<KEY, VALUE> map1, | ||
57 | VersionedMapImpl<KEY, VALUE> map2) { | ||
58 | // 1. Comparing cursors. | ||
59 | Cursor<KEY, VALUE> cursor1 = map1.getCursor(); | ||
60 | Cursor<KEY, VALUE> cursor2 = map2.getCursor(); | ||
61 | while (!cursor1.isTerminated()) { | ||
62 | if (cursor2.isTerminated()) { | ||
63 | fail("cursor 2 terminated before cursor1"); | ||
64 | } | ||
65 | assertEquals(cursor1.getKey(), cursor2.getKey()); | ||
66 | assertEquals(cursor2.getValue(), cursor2.getValue()); | ||
67 | cursor1.move(); | ||
68 | cursor2.move(); | ||
69 | } | ||
70 | if (!cursor2.isTerminated()) | ||
71 | fail("cursor 1 terminated before cursor 2"); | ||
72 | |||
73 | // 2.1. comparing hash codes | ||
74 | assertEquals(map1.hashCode(), map2.hashCode(), title + ": hash code check"); | ||
75 | assertEquals(map1, map2, title + ": 1.equals(2)"); | ||
76 | assertEquals(map2, map1, title + ": 2.equals(1)"); | ||
77 | } | ||
78 | |||
79 | public VersionedMapImpl<KEY, VALUE> sut; | ||
80 | Map<KEY, VALUE> oracle = new HashMap<KEY, VALUE>(); | ||
81 | |||
82 | public MapTestEnvironment(VersionedMapImpl<KEY, VALUE> sut) { | ||
83 | this.sut = sut; | ||
84 | } | ||
85 | |||
86 | public void put(KEY key, VALUE value) { | ||
87 | sut.put(key, value); | ||
88 | if (value != sut.getDefaultValue()) { | ||
89 | oracle.put(key, value); | ||
90 | } else { | ||
91 | oracle.remove(key); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | public void checkEquivalence(String title) { | ||
96 | // 0. Checking integrity | ||
97 | try { | ||
98 | sut.checkIntegrity(); | ||
99 | } catch (IllegalStateException e) { | ||
100 | fail(title + ": " + e.getMessage()); | ||
101 | } | ||
102 | |||
103 | // 1. Checking: if Reference contains <key,value> pair, then SUT contains | ||
104 | // <key,value> pair. | ||
105 | // Tests get functions | ||
106 | for (Entry<KEY, VALUE> entry : oracle.entrySet()) { | ||
107 | VALUE sutValue = sut.get(entry.getKey()); | ||
108 | VALUE oracleValue = entry.getValue(); | ||
109 | if (sutValue != oracleValue) { | ||
110 | printComparison(); | ||
111 | fail(title + ": Non-equivalent get(" + entry.getKey() + ") results: SUT=" + sutValue + ", Oracle=" | ||
112 | + oracleValue + "!"); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | // 2. Checking: if SUT contains <key,value> pair, then Reference contains | ||
117 | // <key,value> pair. | ||
118 | // Tests iterators | ||
119 | // TODO: Counts the number of elements in the entryset | ||
120 | int elementsInSutEntrySet = 0; | ||
121 | Cursor<KEY, VALUE> cursor = sut.getCursor(); | ||
122 | while (cursor.move()) { | ||
123 | elementsInSutEntrySet++; | ||
124 | KEY key = cursor.getKey(); | ||
125 | VALUE sutValue = cursor.getValue(); | ||
126 | // System.out.println(key + " -> " + sutValue); | ||
127 | VALUE oracleValue = oracle.get(key); | ||
128 | if (sutValue != oracleValue) { | ||
129 | printComparison(); | ||
130 | fail(title + ": Non-equivalent entry in iterator: SUT=<" + key + "," + sutValue + ">, Oracle=<" + key | ||
131 | + "," + oracleValue + ">!"); | ||
132 | } | ||
133 | |||
134 | } | ||
135 | |||
136 | // 3. Checking sizes | ||
137 | // Counting of non-default value pairs. | ||
138 | int oracleSize = oracle.entrySet().size(); | ||
139 | long sutSize = sut.getSize(); | ||
140 | if (oracleSize != sutSize || oracleSize != elementsInSutEntrySet) { | ||
141 | printComparison(); | ||
142 | fail(title + ": Non-eqivalent size() result: SUT.getSize()=" + sutSize + ", SUT.entryset.size=" | ||
143 | + elementsInSutEntrySet + ", Oracle=" + oracleSize + "!"); | ||
144 | } | ||
145 | } | ||
146 | |||
147 | public void printComparison() { | ||
148 | System.out.println("SUT:"); | ||
149 | printEntrySet(sut.getCursor()); | ||
150 | System.out.println("Oracle:"); | ||
151 | printEntrySet(oracle.entrySet().iterator()); | ||
152 | } | ||
153 | |||
154 | private void printEntrySet(Iterator<Entry<KEY, VALUE>> iterator) { | ||
155 | TreeMap<KEY, VALUE> treemap = new TreeMap<>(); | ||
156 | while (iterator.hasNext()) { | ||
157 | Entry<KEY, VALUE> entry = iterator.next(); | ||
158 | treemap.put(entry.getKey(), entry.getValue()); | ||
159 | } | ||
160 | for (Entry<KEY, VALUE> e : treemap.entrySet()) { | ||
161 | System.out.println("\t" + e.getKey() + " -> " + e.getValue()); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | private void printEntrySet(Cursor<KEY, VALUE> cursor) { | ||
166 | TreeMap<KEY, VALUE> treemap = new TreeMap<>(); | ||
167 | while (cursor.move()) { | ||
168 | treemap.put(cursor.getKey(), cursor.getValue()); | ||
169 | } | ||
170 | for (Entry<KEY, VALUE> e : treemap.entrySet()) { | ||
171 | System.out.println("\t" + e.getKey() + " -> " + e.getValue()); | ||
172 | } | ||
173 | } | ||
174 | } | ||