diff options
author | 2023-04-07 19:49:23 +0200 | |
---|---|---|
committer | 2023-04-07 19:53:43 +0200 | |
commit | 6ae4346b6248198cb687a9cbbeba3bfb9c37c4b5 (patch) | |
tree | af42f0e78812057f5730ba86838aad58bb5686a7 /subprojects/store-query-viatra/src/test | |
parent | Merge pull request #24 from kris7t/partial-interpretation (diff) | |
download | refinery-6ae4346b6248198cb687a9cbbeba3bfb9c37c4b5.tar.gz refinery-6ae4346b6248198cb687a9cbbeba3bfb9c37c4b5.tar.zst refinery-6ae4346b6248198cb687a9cbbeba3bfb9c37c4b5.zip |
refactor: remove TupleLike
* Directly transform VIATRA tuples into Refinery tuples, since creating
the additional wrapper object doesn't save any memory.
* Adds static arity Tuple3 and Tuple4 implementations to be more
aligned with VIATRA internals and save memory for queries with up to
4 parameters.
* Makes sure no new objects are allocated (for varargs handling) when a
static arity tuple is hashed.
Diffstat (limited to 'subprojects/store-query-viatra/src/test')
2 files changed, 235 insertions, 1 deletions
diff --git a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/internal/matcher/MatcherUtilsTest.java b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/internal/matcher/MatcherUtilsTest.java new file mode 100644 index 00000000..ea0b15ec --- /dev/null +++ b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/internal/matcher/MatcherUtilsTest.java | |||
@@ -0,0 +1,234 @@ | |||
1 | package tools.refinery.store.query.viatra.internal.matcher; | ||
2 | |||
3 | import org.eclipse.viatra.query.runtime.matchers.tuple.*; | ||
4 | import org.junit.jupiter.api.Test; | ||
5 | import tools.refinery.store.tuple.Tuple; | ||
6 | import tools.refinery.store.tuple.*; | ||
7 | |||
8 | import java.util.List; | ||
9 | |||
10 | import static org.hamcrest.MatcherAssert.assertThat; | ||
11 | import static org.hamcrest.Matchers.*; | ||
12 | import static org.junit.jupiter.api.Assertions.assertThrows; | ||
13 | |||
14 | class MatcherUtilsTest { | ||
15 | @Test | ||
16 | void toViatra0Test() { | ||
17 | var viatraTuple = MatcherUtils.toViatraTuple(Tuple.of()); | ||
18 | assertThat(viatraTuple.getSize(), is(0)); | ||
19 | assertThat(viatraTuple, instanceOf(FlatTuple0.class)); | ||
20 | } | ||
21 | |||
22 | @Test | ||
23 | void toViatra1Test() { | ||
24 | var viatraTuple = MatcherUtils.toViatraTuple(Tuple.of(2)); | ||
25 | assertThat(viatraTuple.getSize(), is(1)); | ||
26 | assertThat(viatraTuple.get(0), is(Tuple.of(2))); | ||
27 | assertThat(viatraTuple, instanceOf(FlatTuple1.class)); | ||
28 | } | ||
29 | |||
30 | @Test | ||
31 | void toViatra2Test() { | ||
32 | var viatraTuple = MatcherUtils.toViatraTuple(Tuple.of(2, 3)); | ||
33 | assertThat(viatraTuple.getSize(), is(2)); | ||
34 | assertThat(viatraTuple.get(0), is(Tuple.of(2))); | ||
35 | assertThat(viatraTuple.get(1), is(Tuple.of(3))); | ||
36 | assertThat(viatraTuple, instanceOf(FlatTuple2.class)); | ||
37 | } | ||
38 | |||
39 | @Test | ||
40 | void toViatra3Test() { | ||
41 | var viatraTuple = MatcherUtils.toViatraTuple(Tuple.of(2, 3, 5)); | ||
42 | assertThat(viatraTuple.getSize(), is(3)); | ||
43 | assertThat(viatraTuple.get(0), is(Tuple.of(2))); | ||
44 | assertThat(viatraTuple.get(1), is(Tuple.of(3))); | ||
45 | assertThat(viatraTuple.get(2), is(Tuple.of(5))); | ||
46 | assertThat(viatraTuple, instanceOf(FlatTuple3.class)); | ||
47 | } | ||
48 | |||
49 | @Test | ||
50 | void toViatra4Test() { | ||
51 | var viatraTuple = MatcherUtils.toViatraTuple(Tuple.of(2, 3, 5, 8)); | ||
52 | assertThat(viatraTuple.getSize(), is(4)); | ||
53 | assertThat(viatraTuple.get(0), is(Tuple.of(2))); | ||
54 | assertThat(viatraTuple.get(1), is(Tuple.of(3))); | ||
55 | assertThat(viatraTuple.get(2), is(Tuple.of(5))); | ||
56 | assertThat(viatraTuple.get(3), is(Tuple.of(8))); | ||
57 | assertThat(viatraTuple, instanceOf(FlatTuple4.class)); | ||
58 | } | ||
59 | |||
60 | @Test | ||
61 | void toViatra5Test() { | ||
62 | var viatraTuple = MatcherUtils.toViatraTuple(Tuple.of(2, 3, 5, 8, 13)); | ||
63 | assertThat(viatraTuple.getSize(), is(5)); | ||
64 | assertThat(viatraTuple.get(0), is(Tuple.of(2))); | ||
65 | assertThat(viatraTuple.get(1), is(Tuple.of(3))); | ||
66 | assertThat(viatraTuple.get(2), is(Tuple.of(5))); | ||
67 | assertThat(viatraTuple.get(3), is(Tuple.of(8))); | ||
68 | assertThat(viatraTuple.get(4), is(Tuple.of(13))); | ||
69 | assertThat(viatraTuple, instanceOf(FlatTuple.class)); | ||
70 | } | ||
71 | |||
72 | @Test | ||
73 | void toRefinery0Test() { | ||
74 | var refineryTuple = MatcherUtils.toRefineryTuple(Tuples.flatTupleOf()); | ||
75 | assertThat(refineryTuple.getSize(), is(0)); | ||
76 | assertThat(refineryTuple, instanceOf(Tuple0.class)); | ||
77 | } | ||
78 | |||
79 | @Test | ||
80 | void toRefinery1Test() { | ||
81 | var refineryTuple = MatcherUtils.toRefineryTuple(Tuples.flatTupleOf(Tuple.of(2))); | ||
82 | assertThat(refineryTuple.getSize(), is(1)); | ||
83 | assertThat(refineryTuple.get(0), is(2)); | ||
84 | assertThat(refineryTuple, instanceOf(Tuple1.class)); | ||
85 | } | ||
86 | |||
87 | @Test | ||
88 | void toRefinery2Test() { | ||
89 | var refineryTuple = MatcherUtils.toRefineryTuple(Tuples.flatTupleOf(Tuple.of(2), Tuple.of(3))); | ||
90 | assertThat(refineryTuple.getSize(), is(2)); | ||
91 | assertThat(refineryTuple.get(0), is(2)); | ||
92 | assertThat(refineryTuple.get(1), is(3)); | ||
93 | assertThat(refineryTuple, instanceOf(Tuple2.class)); | ||
94 | } | ||
95 | |||
96 | @Test | ||
97 | void toRefinery3Test() { | ||
98 | var refineryTuple = MatcherUtils.toRefineryTuple(Tuples.flatTupleOf(Tuple.of(2), Tuple.of(3), Tuple.of(5))); | ||
99 | assertThat(refineryTuple.getSize(), is(3)); | ||
100 | assertThat(refineryTuple.get(0), is(2)); | ||
101 | assertThat(refineryTuple.get(1), is(3)); | ||
102 | assertThat(refineryTuple.get(2), is(5)); | ||
103 | assertThat(refineryTuple, instanceOf(Tuple3.class)); | ||
104 | } | ||
105 | |||
106 | @Test | ||
107 | void toRefinery4Test() { | ||
108 | var refineryTuple = MatcherUtils.toRefineryTuple(Tuples.flatTupleOf(Tuple.of(2), Tuple.of(3), Tuple.of(5), | ||
109 | Tuple.of(8))); | ||
110 | assertThat(refineryTuple.getSize(), is(4)); | ||
111 | assertThat(refineryTuple.get(0), is(2)); | ||
112 | assertThat(refineryTuple.get(1), is(3)); | ||
113 | assertThat(refineryTuple.get(2), is(5)); | ||
114 | assertThat(refineryTuple.get(3), is(8)); | ||
115 | assertThat(refineryTuple, instanceOf(Tuple4.class)); | ||
116 | } | ||
117 | |||
118 | @Test | ||
119 | void toRefinery5Test() { | ||
120 | var refineryTuple = MatcherUtils.toRefineryTuple(Tuples.flatTupleOf(Tuple.of(2), Tuple.of(3), Tuple.of(5), | ||
121 | Tuple.of(8), Tuple.of(13))); | ||
122 | assertThat(refineryTuple.getSize(), is(5)); | ||
123 | assertThat(refineryTuple.get(0), is(2)); | ||
124 | assertThat(refineryTuple.get(1), is(3)); | ||
125 | assertThat(refineryTuple.get(2), is(5)); | ||
126 | assertThat(refineryTuple.get(3), is(8)); | ||
127 | assertThat(refineryTuple.get(4), is(13)); | ||
128 | assertThat(refineryTuple, instanceOf(TupleN.class)); | ||
129 | } | ||
130 | |||
131 | @Test | ||
132 | void toRefineryInvalidValueTest() { | ||
133 | var viatraTuple = Tuples.flatTupleOf(Tuple.of(2), -98); | ||
134 | assertThrows(IllegalArgumentException.class, () -> MatcherUtils.toRefineryTuple(viatraTuple)); | ||
135 | } | ||
136 | |||
137 | @Test | ||
138 | void keyToRefinery0Test() { | ||
139 | var refineryTuple = MatcherUtils.keyToRefineryTuple(Tuples.flatTupleOf(-99)); | ||
140 | assertThat(refineryTuple.getSize(), is(0)); | ||
141 | assertThat(refineryTuple, instanceOf(Tuple0.class)); | ||
142 | } | ||
143 | |||
144 | @Test | ||
145 | void keyToRefinery1Test() { | ||
146 | var refineryTuple = MatcherUtils.keyToRefineryTuple(Tuples.flatTupleOf(Tuple.of(2), -99)); | ||
147 | assertThat(refineryTuple.getSize(), is(1)); | ||
148 | assertThat(refineryTuple.get(0), is(2)); | ||
149 | assertThat(refineryTuple, instanceOf(Tuple1.class)); | ||
150 | } | ||
151 | |||
152 | @Test | ||
153 | void keyToRefinery2Test() { | ||
154 | var refineryTuple = MatcherUtils.keyToRefineryTuple(Tuples.flatTupleOf(Tuple.of(2), Tuple.of(3), -99)); | ||
155 | assertThat(refineryTuple.getSize(), is(2)); | ||
156 | assertThat(refineryTuple.get(0), is(2)); | ||
157 | assertThat(refineryTuple.get(1), is(3)); | ||
158 | assertThat(refineryTuple, instanceOf(Tuple2.class)); | ||
159 | } | ||
160 | |||
161 | @Test | ||
162 | void keyToRefinery3Test() { | ||
163 | var refineryTuple = MatcherUtils.keyToRefineryTuple(Tuples.flatTupleOf(Tuple.of(2), Tuple.of(3), Tuple.of(5), | ||
164 | -99)); | ||
165 | assertThat(refineryTuple.getSize(), is(3)); | ||
166 | assertThat(refineryTuple.get(0), is(2)); | ||
167 | assertThat(refineryTuple.get(1), is(3)); | ||
168 | assertThat(refineryTuple.get(2), is(5)); | ||
169 | assertThat(refineryTuple, instanceOf(Tuple3.class)); | ||
170 | } | ||
171 | |||
172 | @Test | ||
173 | void keyToRefinery4Test() { | ||
174 | var refineryTuple = MatcherUtils.keyToRefineryTuple(Tuples.flatTupleOf(Tuple.of(2), Tuple.of(3), Tuple.of(5), | ||
175 | Tuple.of(8), -99)); | ||
176 | assertThat(refineryTuple.getSize(), is(4)); | ||
177 | assertThat(refineryTuple.get(0), is(2)); | ||
178 | assertThat(refineryTuple.get(1), is(3)); | ||
179 | assertThat(refineryTuple.get(2), is(5)); | ||
180 | assertThat(refineryTuple.get(3), is(8)); | ||
181 | assertThat(refineryTuple, instanceOf(Tuple4.class)); | ||
182 | } | ||
183 | |||
184 | @Test | ||
185 | void keyToRefinery5Test() { | ||
186 | var refineryTuple = MatcherUtils.keyToRefineryTuple(Tuples.flatTupleOf(Tuple.of(2), Tuple.of(3), Tuple.of(5), | ||
187 | Tuple.of(8), Tuple.of(13), -99)); | ||
188 | assertThat(refineryTuple.getSize(), is(5)); | ||
189 | assertThat(refineryTuple.get(0), is(2)); | ||
190 | assertThat(refineryTuple.get(1), is(3)); | ||
191 | assertThat(refineryTuple.get(2), is(5)); | ||
192 | assertThat(refineryTuple.get(3), is(8)); | ||
193 | assertThat(refineryTuple.get(4), is(13)); | ||
194 | assertThat(refineryTuple, instanceOf(TupleN.class)); | ||
195 | } | ||
196 | |||
197 | @Test | ||
198 | void keyToRefineryTooShortTest() { | ||
199 | var viatraTuple = Tuples.flatTupleOf(); | ||
200 | assertThrows(IllegalArgumentException.class, () -> MatcherUtils.keyToRefineryTuple(viatraTuple)); | ||
201 | } | ||
202 | |||
203 | @Test | ||
204 | void keyToRefineryInvalidValueTest() { | ||
205 | var viatraTuple = Tuples.flatTupleOf(Tuple.of(2), -98, -99); | ||
206 | assertThrows(IllegalArgumentException.class, () -> MatcherUtils.keyToRefineryTuple(viatraTuple)); | ||
207 | } | ||
208 | |||
209 | @Test | ||
210 | void getSingleValueTest() { | ||
211 | var value = MatcherUtils.getSingleValue(List.of(Tuples.flatTupleOf(Tuple.of(2), -99))); | ||
212 | assertThat(value, is(-99)); | ||
213 | } | ||
214 | |||
215 | // Static analysis accurately determines that the result is always {@code null}, but we check anyways. | ||
216 | @SuppressWarnings("ConstantValue") | ||
217 | @Test | ||
218 | void getSingleValueNullTest() { | ||
219 | var value = MatcherUtils.getSingleValue((Iterable<? extends ITuple>) null); | ||
220 | assertThat(value, nullValue()); | ||
221 | } | ||
222 | |||
223 | @Test | ||
224 | void getSingleValueEmptyTest() { | ||
225 | var value = MatcherUtils.getSingleValue(List.of()); | ||
226 | assertThat(value, nullValue()); | ||
227 | } | ||
228 | |||
229 | @Test | ||
230 | void getSingleValueMultipleTest() { | ||
231 | var viatraTuples = List.of(Tuples.flatTupleOf(Tuple.of(2), -98), Tuples.flatTupleOf(Tuple.of(2), -99)); | ||
232 | assertThrows(IllegalStateException.class, () -> MatcherUtils.getSingleValue(viatraTuples)); | ||
233 | } | ||
234 | } | ||
diff --git a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/tests/QueryAssertions.java b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/tests/QueryAssertions.java index 6f50ec73..2769621d 100644 --- a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/tests/QueryAssertions.java +++ b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/tests/QueryAssertions.java | |||
@@ -42,7 +42,7 @@ public final class QueryAssertions { | |||
42 | var cursor = resultSet.getAll(); | 42 | var cursor = resultSet.getAll(); |
43 | while (cursor.move()) { | 43 | while (cursor.move()) { |
44 | var key = cursor.getKey(); | 44 | var key = cursor.getKey(); |
45 | var previous = actual.put(key.toTuple(), cursor.getValue()); | 45 | var previous = actual.put(key, cursor.getValue()); |
46 | assertThat("duplicate value for key " + key, previous, nullValue()); | 46 | assertThat("duplicate value for key " + key, previous, nullValue()); |
47 | } | 47 | } |
48 | executables.add(() -> assertThat("results cursor", actual, is(filteredExpected))); | 48 | executables.add(() -> assertThat("results cursor", actual, is(filteredExpected))); |