diff options
author | Kristóf Marussy <kristof@marussy.com> | 2023-02-20 20:23:27 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2023-02-20 20:23:27 +0100 |
commit | b3f7c4d7707435803921c4fec2c4d95b3dd45c53 (patch) | |
tree | 18a90112efe3ece8678709db322dddcefafaace1 /subprojects/store-partial/src/test | |
parent | feat: type inference for class hierarchies (diff) | |
download | refinery-b3f7c4d7707435803921c4fec2c4d95b3dd45c53.tar.gz refinery-b3f7c4d7707435803921c4fec2c4d95b3dd45c53.tar.zst refinery-b3f7c4d7707435803921c4fec2c4d95b3dd45c53.zip |
refactor: split query and partial from store
Allows more complicated dependency hiearchies (e.g., use
store-query-viatra for testing store-partial) and better separation of
test fixtures.
Diffstat (limited to 'subprojects/store-partial/src/test')
4 files changed, 488 insertions, 0 deletions
diff --git a/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/InferredTypeTest.java b/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/InferredTypeTest.java new file mode 100644 index 00000000..9efa76ef --- /dev/null +++ b/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/InferredTypeTest.java | |||
@@ -0,0 +1,32 @@ | |||
1 | package tools.refinery.store.partial.translator.typehierarchy; | ||
2 | |||
3 | import org.junit.jupiter.api.Test; | ||
4 | import tools.refinery.store.partial.representation.PartialRelation; | ||
5 | |||
6 | import java.util.Set; | ||
7 | |||
8 | import static org.hamcrest.MatcherAssert.assertThat; | ||
9 | import static org.hamcrest.Matchers.is; | ||
10 | |||
11 | class InferredTypeTest { | ||
12 | private final PartialRelation c1 = new PartialRelation("C1", 1); | ||
13 | private final PartialRelation c2 = new PartialRelation("C2", 1); | ||
14 | |||
15 | @Test | ||
16 | void untypedIsConsistentTest() { | ||
17 | var sut = new InferredType(Set.of(), Set.of(c1, c2), null); | ||
18 | assertThat(sut.isConsistent(), is(true)); | ||
19 | } | ||
20 | |||
21 | @Test | ||
22 | void typedIsConsistentTest() { | ||
23 | var sut = new InferredType(Set.of(c1), Set.of(c1, c2), c1); | ||
24 | assertThat(sut.isConsistent(), is(true)); | ||
25 | } | ||
26 | |||
27 | @Test | ||
28 | void typedIsInconsistentTest() { | ||
29 | var sut = new InferredType(Set.of(c1), Set.of(), null); | ||
30 | assertThat(sut.isConsistent(), is(false)); | ||
31 | } | ||
32 | } | ||
diff --git a/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/TypeAnalyzerExampleHierarchyTest.java b/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/TypeAnalyzerExampleHierarchyTest.java new file mode 100644 index 00000000..ad81e28f --- /dev/null +++ b/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/TypeAnalyzerExampleHierarchyTest.java | |||
@@ -0,0 +1,204 @@ | |||
1 | package tools.refinery.store.partial.translator.typehierarchy; | ||
2 | |||
3 | import org.hamcrest.Matchers; | ||
4 | import org.junit.jupiter.api.BeforeEach; | ||
5 | import org.junit.jupiter.api.Test; | ||
6 | import tools.refinery.store.partial.representation.PartialRelation; | ||
7 | import tools.refinery.store.representation.TruthValue; | ||
8 | |||
9 | import java.util.LinkedHashMap; | ||
10 | import java.util.Set; | ||
11 | |||
12 | import static org.hamcrest.MatcherAssert.assertThat; | ||
13 | import static org.hamcrest.Matchers.is; | ||
14 | import static org.junit.jupiter.api.Assertions.assertAll; | ||
15 | |||
16 | class TypeAnalyzerExampleHierarchyTest { | ||
17 | private final PartialRelation a1 = new PartialRelation("A1", 1); | ||
18 | private final PartialRelation a2 = new PartialRelation("A2", 1); | ||
19 | private final PartialRelation a3 = new PartialRelation("A3", 1); | ||
20 | private final PartialRelation a4 = new PartialRelation("A4", 1); | ||
21 | private final PartialRelation a5 = new PartialRelation("A5", 1); | ||
22 | private final PartialRelation c1 = new PartialRelation("C1", 1); | ||
23 | private final PartialRelation c2 = new PartialRelation("C2", 1); | ||
24 | private final PartialRelation c3 = new PartialRelation("C3", 1); | ||
25 | private final PartialRelation c4 = new PartialRelation("C4", 1); | ||
26 | |||
27 | private TypeAnalyzer sut; | ||
28 | private TypeAnalyzerTester tester; | ||
29 | |||
30 | @BeforeEach | ||
31 | void beforeEach() { | ||
32 | var typeInfoMap = new LinkedHashMap<PartialRelation, TypeInfo>(); | ||
33 | typeInfoMap.put(a1, TypeInfo.builder().abstractType().build()); | ||
34 | typeInfoMap.put(a2, TypeInfo.builder().abstractType().build()); | ||
35 | typeInfoMap.put(a3, TypeInfo.builder().abstractType().build()); | ||
36 | typeInfoMap.put(a4, TypeInfo.builder().abstractType().build()); | ||
37 | typeInfoMap.put(a5, TypeInfo.builder().abstractType().build()); | ||
38 | typeInfoMap.put(c1, TypeInfo.builder().supertypes(a1, a4).build()); | ||
39 | typeInfoMap.put(c2, TypeInfo.builder().supertypes(a1, a2, a3, a4).build()); | ||
40 | typeInfoMap.put(c3, TypeInfo.builder().supertype(a3).build()); | ||
41 | typeInfoMap.put(c4, TypeInfo.builder().supertype(a4).build()); | ||
42 | sut = new TypeAnalyzer(typeInfoMap); | ||
43 | tester = new TypeAnalyzerTester(sut); | ||
44 | } | ||
45 | |||
46 | @Test | ||
47 | void analysisResultsTest() { | ||
48 | assertAll( | ||
49 | () -> tester.assertAbstractType(a1, c1, c2), | ||
50 | () -> tester.assertEliminatedType(a2, c2), | ||
51 | () -> tester.assertAbstractType(a3, c2, c3), | ||
52 | () -> tester.assertAbstractType(a4, c1, c2, c4), | ||
53 | () -> tester.assertVacuousType(a5), | ||
54 | () -> tester.assertConcreteType(c1), | ||
55 | () -> tester.assertConcreteType(c2), | ||
56 | () -> tester.assertConcreteType(c3), | ||
57 | () -> tester.assertConcreteType(c4) | ||
58 | ); | ||
59 | } | ||
60 | |||
61 | @Test | ||
62 | void inferredTypesTest() { | ||
63 | assertAll( | ||
64 | () -> assertThat(sut.getUnknownType(), Matchers.is(new InferredType(Set.of(), Set.of(c1, c2, c3, c4), null))), | ||
65 | () -> assertThat(tester.getInferredType(a1), Matchers.is(new InferredType(Set.of(a1, a4), Set.of(c1, c2), c1))), | ||
66 | () -> assertThat(tester.getInferredType(a3), Matchers.is(new InferredType(Set.of(a3), Set.of(c2, c3), c2))), | ||
67 | () -> assertThat(tester.getInferredType(a4), Matchers.is(new InferredType(Set.of(a4), Set.of(c1, c2, c4), c1))), | ||
68 | () -> assertThat(tester.getInferredType(a5), Matchers.is(new InferredType(Set.of(a5), Set.of(), null))), | ||
69 | () -> assertThat(tester.getInferredType(c1), Matchers.is(new InferredType(Set.of(a1, a4, c1), Set.of(c1), c1))), | ||
70 | () -> assertThat(tester.getInferredType(c2), | ||
71 | Matchers.is(new InferredType(Set.of(a1, a3, a4, c2), Set.of(c2), c2))), | ||
72 | () -> assertThat(tester.getInferredType(c3), Matchers.is(new InferredType(Set.of(a3, c3), Set.of(c3), c3))), | ||
73 | () -> assertThat(tester.getInferredType(c4), Matchers.is(new InferredType(Set.of(a4, c4), Set.of(c4), c4))) | ||
74 | ); | ||
75 | } | ||
76 | |||
77 | @Test | ||
78 | void consistentMustTest() { | ||
79 | var a1Result = tester.getPreservedType(a1); | ||
80 | var a3Result = tester.getPreservedType(a3); | ||
81 | var expected = new InferredType(Set.of(a1, a3, a4, c2), Set.of(c2), c2); | ||
82 | assertAll( | ||
83 | () -> assertThat(a1Result.merge(a3Result.asInferredType(), TruthValue.TRUE), Matchers.is(expected)), | ||
84 | () -> assertThat(a3Result.merge(a1Result.asInferredType(), TruthValue.TRUE), Matchers.is(expected)), | ||
85 | () -> assertThat(a1Result.merge(sut.getUnknownType(), TruthValue.TRUE), is(a1Result.asInferredType())), | ||
86 | () -> assertThat(a3Result.merge(sut.getUnknownType(), TruthValue.TRUE), is(a3Result.asInferredType())), | ||
87 | () -> assertThat(a1Result.merge(a1Result.asInferredType(), TruthValue.TRUE), | ||
88 | is(a1Result.asInferredType())) | ||
89 | ); | ||
90 | } | ||
91 | |||
92 | @Test | ||
93 | void consistentMayNotTest() { | ||
94 | var a1Result = tester.getPreservedType(a1); | ||
95 | var a3Result = tester.getPreservedType(a3); | ||
96 | var a4Result = tester.getPreservedType(a4); | ||
97 | assertAll( | ||
98 | () -> assertThat(a1Result.merge(a3Result.asInferredType(), TruthValue.FALSE), | ||
99 | Matchers.is(new InferredType(Set.of(a3, c3), Set.of(c3), c3))), | ||
100 | () -> assertThat(a3Result.merge(a1Result.asInferredType(), TruthValue.FALSE), | ||
101 | Matchers.is(new InferredType(Set.of(a1, a4, c1), Set.of(c1), c1))), | ||
102 | () -> assertThat(a4Result.merge(a3Result.asInferredType(), TruthValue.FALSE), | ||
103 | Matchers.is(new InferredType(Set.of(a3, c3), Set.of(c3), c3))), | ||
104 | () -> assertThat(a3Result.merge(a4Result.asInferredType(), TruthValue.FALSE), | ||
105 | Matchers.is(new InferredType(Set.of(a4), Set.of(c1, c4), c1))), | ||
106 | () -> assertThat(a1Result.merge(sut.getUnknownType(), TruthValue.FALSE), | ||
107 | Matchers.is(new InferredType(Set.of(), Set.of(c3, c4), null))), | ||
108 | () -> assertThat(a3Result.merge(sut.getUnknownType(), TruthValue.FALSE), | ||
109 | Matchers.is(new InferredType(Set.of(), Set.of(c1, c4), null))), | ||
110 | () -> assertThat(a4Result.merge(sut.getUnknownType(), TruthValue.FALSE), | ||
111 | Matchers.is(new InferredType(Set.of(), Set.of(c3), null))) | ||
112 | ); | ||
113 | } | ||
114 | |||
115 | @Test | ||
116 | void consistentErrorTest() { | ||
117 | var c1Result = tester.getPreservedType(c1); | ||
118 | var a4Result = tester.getPreservedType(a4); | ||
119 | var expected = new InferredType(Set.of(c1, a1, a4), Set.of(), null); | ||
120 | assertAll( | ||
121 | () -> assertThat(c1Result.merge(a4Result.asInferredType(), TruthValue.ERROR), Matchers.is(expected)), | ||
122 | () -> assertThat(a4Result.merge(c1Result.asInferredType(), TruthValue.ERROR), Matchers.is(expected)) | ||
123 | ); | ||
124 | } | ||
125 | |||
126 | @Test | ||
127 | void consistentUnknownTest() { | ||
128 | var c1Result = tester.getPreservedType(c1); | ||
129 | var a4Result = tester.getPreservedType(a4); | ||
130 | assertAll( | ||
131 | () -> assertThat(c1Result.merge(a4Result.asInferredType(), TruthValue.UNKNOWN), | ||
132 | is(a4Result.asInferredType())), | ||
133 | () -> assertThat(a4Result.merge(c1Result.asInferredType(), TruthValue.UNKNOWN), | ||
134 | is(c1Result.asInferredType())) | ||
135 | ); | ||
136 | } | ||
137 | |||
138 | @Test | ||
139 | void inconsistentMustTest() { | ||
140 | var a1Result = tester.getPreservedType(a1); | ||
141 | var c3Result = tester.getPreservedType(c3); | ||
142 | assertAll( | ||
143 | () -> assertThat(a1Result.merge(c3Result.asInferredType(), TruthValue.TRUE), | ||
144 | Matchers.is(new InferredType(Set.of(a1, a3, c3), Set.of(), null))), | ||
145 | () -> assertThat(c3Result.merge(a1Result.asInferredType(), TruthValue.TRUE), | ||
146 | Matchers.is(new InferredType(Set.of(a1, a3, a4, c3), Set.of(), null))) | ||
147 | ); | ||
148 | } | ||
149 | |||
150 | @Test | ||
151 | void inconsistentMayNotTest() { | ||
152 | var a1Result = tester.getPreservedType(a1); | ||
153 | var a4Result = tester.getPreservedType(a4); | ||
154 | var c1Result = tester.getPreservedType(c1); | ||
155 | assertAll( | ||
156 | () -> assertThat(a4Result.merge(a1Result.asInferredType(), TruthValue.FALSE), | ||
157 | Matchers.is(new InferredType(Set.of(a1, a4), Set.of(), null))), | ||
158 | () -> assertThat(a1Result.merge(c1Result.asInferredType(), TruthValue.FALSE), | ||
159 | Matchers.is(new InferredType(Set.of(a1, a4, c1), Set.of(), null))), | ||
160 | () -> assertThat(a4Result.merge(c1Result.asInferredType(), TruthValue.FALSE), | ||
161 | Matchers.is(new InferredType(Set.of(a1, a4, c1), Set.of(), null))), | ||
162 | () -> assertThat(a1Result.merge(a1Result.asInferredType(), TruthValue.FALSE), | ||
163 | Matchers.is(new InferredType(Set.of(a1, a4), Set.of(), null))) | ||
164 | ); | ||
165 | } | ||
166 | |||
167 | @Test | ||
168 | void vacuousMustTest() { | ||
169 | var c1Result = tester.getPreservedType(c1); | ||
170 | var a5Result = tester.getPreservedType(a5); | ||
171 | assertAll( | ||
172 | () -> assertThat(c1Result.merge(a5Result.asInferredType(), TruthValue.TRUE), | ||
173 | Matchers.is(new InferredType(Set.of(a1, a4, a5, c1), Set.of(), null))), | ||
174 | () -> assertThat(a5Result.merge(c1Result.asInferredType(), TruthValue.TRUE), | ||
175 | Matchers.is(new InferredType(Set.of(a1, a4, a5, c1), Set.of(), null))) | ||
176 | ); | ||
177 | } | ||
178 | |||
179 | @Test | ||
180 | void vacuousMayNotTest() { | ||
181 | var c1Result = tester.getPreservedType(c1); | ||
182 | var a5Result = tester.getPreservedType(a5); | ||
183 | assertAll( | ||
184 | () -> assertThat(c1Result.merge(a5Result.asInferredType(), TruthValue.FALSE), | ||
185 | is(a5Result.asInferredType())), | ||
186 | () -> assertThat(a5Result.merge(c1Result.asInferredType(), TruthValue.FALSE), | ||
187 | is(c1Result.asInferredType())) | ||
188 | ); | ||
189 | } | ||
190 | |||
191 | @Test | ||
192 | void vacuousErrorTest() { | ||
193 | var c1Result = tester.getPreservedType(c1); | ||
194 | var a5Result = tester.getPreservedType(a5); | ||
195 | assertAll( | ||
196 | () -> assertThat(c1Result.merge(a5Result.asInferredType(), TruthValue.ERROR), | ||
197 | Matchers.is(new InferredType(Set.of(a1, a4, a5, c1), Set.of(), null))), | ||
198 | () -> assertThat(a5Result.merge(c1Result.asInferredType(), TruthValue.ERROR), | ||
199 | Matchers.is(new InferredType(Set.of(a1, a4, a5, c1), Set.of(), null))), | ||
200 | () -> assertThat(a5Result.merge(a5Result.asInferredType(), TruthValue.ERROR), | ||
201 | is(a5Result.asInferredType())) | ||
202 | ); | ||
203 | } | ||
204 | } | ||
diff --git a/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/TypeAnalyzerTest.java b/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/TypeAnalyzerTest.java new file mode 100644 index 00000000..1aab75bb --- /dev/null +++ b/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/TypeAnalyzerTest.java | |||
@@ -0,0 +1,201 @@ | |||
1 | package tools.refinery.store.partial.translator.typehierarchy; | ||
2 | |||
3 | import org.hamcrest.Matchers; | ||
4 | import org.junit.jupiter.api.Test; | ||
5 | import tools.refinery.store.partial.representation.PartialRelation; | ||
6 | import tools.refinery.store.representation.TruthValue; | ||
7 | |||
8 | import java.util.LinkedHashMap; | ||
9 | import java.util.Set; | ||
10 | |||
11 | import static org.hamcrest.MatcherAssert.assertThat; | ||
12 | import static org.hamcrest.Matchers.is; | ||
13 | import static org.junit.jupiter.api.Assertions.assertAll; | ||
14 | import static org.junit.jupiter.api.Assertions.assertThrows; | ||
15 | |||
16 | class TypeAnalyzerTest { | ||
17 | @Test | ||
18 | void directSupertypesTest() { | ||
19 | var c1 = new PartialRelation("C1", 1); | ||
20 | var c2 = new PartialRelation("C2", 1); | ||
21 | var c3 = new PartialRelation("C3", 1); | ||
22 | var typeInfoMap = new LinkedHashMap<PartialRelation, TypeInfo>(); | ||
23 | typeInfoMap.put(c1, TypeInfo.builder().supertypes(c2, c3).build()); | ||
24 | typeInfoMap.put(c2, TypeInfo.builder().supertype(c3).build()); | ||
25 | typeInfoMap.put(c3, TypeInfo.builder().build()); | ||
26 | |||
27 | var sut = new TypeAnalyzer(typeInfoMap); | ||
28 | var tester = new TypeAnalyzerTester(sut); | ||
29 | |||
30 | assertAll( | ||
31 | () -> tester.assertConcreteType(c1), | ||
32 | () -> tester.assertConcreteType(c2, c1), | ||
33 | () -> tester.assertConcreteType(c3, c2) | ||
34 | ); | ||
35 | } | ||
36 | |||
37 | @Test | ||
38 | void typeEliminationAbstractToConcreteTest() { | ||
39 | var c1 = new PartialRelation("C1", 1); | ||
40 | var c2 = new PartialRelation("C2", 1); | ||
41 | var a11 = new PartialRelation("A11", 1); | ||
42 | var a12 = new PartialRelation("A12", 1); | ||
43 | var a21 = new PartialRelation("A21", 1); | ||
44 | var a22 = new PartialRelation("A22", 1); | ||
45 | var a3 = new PartialRelation("A3", 1); | ||
46 | var typeInfoMap = new LinkedHashMap<PartialRelation, TypeInfo>(); | ||
47 | typeInfoMap.put(a3, TypeInfo.builder().abstractType().build()); | ||
48 | typeInfoMap.put(a21, TypeInfo.builder().abstractType().supertype(a3).build()); | ||
49 | typeInfoMap.put(a22, TypeInfo.builder().abstractType().supertype(a3).build()); | ||
50 | typeInfoMap.put(a11, TypeInfo.builder().abstractType().supertypes(a21, a22).build()); | ||
51 | typeInfoMap.put(a12, TypeInfo.builder().abstractType().supertypes(a21, a22).build()); | ||
52 | typeInfoMap.put(c1, TypeInfo.builder().supertypes(a11, a12).build()); | ||
53 | typeInfoMap.put(c2, TypeInfo.builder().supertype(a3).build()); | ||
54 | |||
55 | var sut = new TypeAnalyzer(typeInfoMap); | ||
56 | var tester = new TypeAnalyzerTester(sut); | ||
57 | |||
58 | assertAll( | ||
59 | () -> tester.assertConcreteType(c1), | ||
60 | () -> tester.assertConcreteType(c2), | ||
61 | () -> tester.assertEliminatedType(a11, c1), | ||
62 | () -> tester.assertEliminatedType(a12, c1), | ||
63 | () -> tester.assertEliminatedType(a21, c1), | ||
64 | () -> tester.assertEliminatedType(a22, c1), | ||
65 | () -> tester.assertAbstractType(a3, c1, c2) | ||
66 | ); | ||
67 | } | ||
68 | |||
69 | @Test | ||
70 | void typeEliminationConcreteToAbstractTest() { | ||
71 | var c1 = new PartialRelation("C1", 1); | ||
72 | var c2 = new PartialRelation("C2", 1); | ||
73 | var a11 = new PartialRelation("A11", 1); | ||
74 | var a12 = new PartialRelation("A12", 1); | ||
75 | var a21 = new PartialRelation("A21", 1); | ||
76 | var a22 = new PartialRelation("A22", 1); | ||
77 | var a3 = new PartialRelation("A3", 1); | ||
78 | var typeInfoMap = new LinkedHashMap<PartialRelation, TypeInfo>(); | ||
79 | typeInfoMap.put(c1, TypeInfo.builder().supertypes(a11, a12).build()); | ||
80 | typeInfoMap.put(c2, TypeInfo.builder().supertype(a3).build()); | ||
81 | typeInfoMap.put(a11, TypeInfo.builder().abstractType().supertypes(a21, a22).build()); | ||
82 | typeInfoMap.put(a12, TypeInfo.builder().abstractType().supertypes(a21, a22).build()); | ||
83 | typeInfoMap.put(a21, TypeInfo.builder().abstractType().supertype(a3).build()); | ||
84 | typeInfoMap.put(a22, TypeInfo.builder().abstractType().supertype(a3).build()); | ||
85 | typeInfoMap.put(a3, TypeInfo.builder().abstractType().build()); | ||
86 | |||
87 | var sut = new TypeAnalyzer(typeInfoMap); | ||
88 | var tester = new TypeAnalyzerTester(sut); | ||
89 | |||
90 | assertAll( | ||
91 | () -> tester.assertConcreteType(c1), | ||
92 | () -> tester.assertConcreteType(c2), | ||
93 | () -> tester.assertEliminatedType(a11, c1), | ||
94 | () -> tester.assertEliminatedType(a12, c1), | ||
95 | () -> tester.assertEliminatedType(a21, c1), | ||
96 | () -> tester.assertEliminatedType(a22, c1), | ||
97 | () -> tester.assertAbstractType(a3, c1, c2) | ||
98 | ); | ||
99 | } | ||
100 | |||
101 | @Test | ||
102 | void preserveConcreteTypeTest() { | ||
103 | var c1 = new PartialRelation("C1", 1); | ||
104 | var a1 = new PartialRelation("A1", 1); | ||
105 | var c2 = new PartialRelation("C2", 1); | ||
106 | var a2 = new PartialRelation("A2", 1); | ||
107 | var typeInfoMap = new LinkedHashMap<PartialRelation, TypeInfo>(); | ||
108 | typeInfoMap.put(c1, TypeInfo.builder().supertype(a1).build()); | ||
109 | typeInfoMap.put(a1, TypeInfo.builder().abstractType().supertype(c2).build()); | ||
110 | typeInfoMap.put(c2, TypeInfo.builder().supertype(a2).build()); | ||
111 | typeInfoMap.put(a2, TypeInfo.builder().abstractType().build()); | ||
112 | |||
113 | var sut = new TypeAnalyzer(typeInfoMap); | ||
114 | var tester = new TypeAnalyzerTester(sut); | ||
115 | |||
116 | assertAll( | ||
117 | () -> tester.assertConcreteType(c1), | ||
118 | () -> tester.assertEliminatedType(a1, c1), | ||
119 | () -> tester.assertConcreteType(c2, c1), | ||
120 | () -> tester.assertEliminatedType(a2, c2) | ||
121 | ); | ||
122 | } | ||
123 | |||
124 | @Test | ||
125 | void mostGeneralCurrentTypeTest() { | ||
126 | var c1 = new PartialRelation("C1", 1); | ||
127 | var c2 = new PartialRelation("C2", 1); | ||
128 | var c3 = new PartialRelation("C3", 1); | ||
129 | var typeInfoMap = new LinkedHashMap<PartialRelation, TypeInfo>(); | ||
130 | typeInfoMap.put(c1, TypeInfo.builder().supertype(c3).build()); | ||
131 | typeInfoMap.put(c2, TypeInfo.builder().supertype(c3).build()); | ||
132 | typeInfoMap.put(c3, TypeInfo.builder().build()); | ||
133 | |||
134 | var sut = new TypeAnalyzer(typeInfoMap); | ||
135 | var tester = new TypeAnalyzerTester(sut); | ||
136 | var c3Result = tester.getPreservedType(c3); | ||
137 | |||
138 | var expected = new InferredType(Set.of(c3), Set.of(c1, c2, c3), c3); | ||
139 | assertAll( | ||
140 | () -> assertThat(tester.getInferredType(c3), Matchers.is(expected)), | ||
141 | () -> assertThat(c3Result.merge(sut.getUnknownType(), TruthValue.TRUE), Matchers.is(expected)) | ||
142 | ); | ||
143 | } | ||
144 | |||
145 | @Test | ||
146 | void preferFirstConcreteTypeTest() { | ||
147 | var a1 = new PartialRelation("A1", 1); | ||
148 | var c1 = new PartialRelation("C1", 1); | ||
149 | var c2 = new PartialRelation("C2", 1); | ||
150 | var c3 = new PartialRelation("C3", 1); | ||
151 | var c4 = new PartialRelation("C4", 1); | ||
152 | var typeInfoMap = new LinkedHashMap<PartialRelation, TypeInfo>(); | ||
153 | typeInfoMap.put(c1, TypeInfo.builder().supertype(a1).build()); | ||
154 | typeInfoMap.put(c2, TypeInfo.builder().supertype(a1).build()); | ||
155 | typeInfoMap.put(c3, TypeInfo.builder().supertype(a1).build()); | ||
156 | typeInfoMap.put(c4, TypeInfo.builder().supertype(c3).build()); | ||
157 | typeInfoMap.put(a1, TypeInfo.builder().abstractType().build()); | ||
158 | |||
159 | var sut = new TypeAnalyzer(typeInfoMap); | ||
160 | var tester = new TypeAnalyzerTester(sut); | ||
161 | var c1Result = tester.getPreservedType(c1); | ||
162 | var a1Result = tester.getPreservedType(a1); | ||
163 | |||
164 | assertThat(c1Result.merge(a1Result.asInferredType(), TruthValue.FALSE), | ||
165 | Matchers.is(new InferredType(Set.of(a1), Set.of(c2, c3, c4), c2))); | ||
166 | } | ||
167 | |||
168 | @Test | ||
169 | void preferFirstMostGeneralConcreteTypeTest() { | ||
170 | var a1 = new PartialRelation("A1", 1); | ||
171 | var c1 = new PartialRelation("C1", 1); | ||
172 | var c2 = new PartialRelation("C2", 1); | ||
173 | var c3 = new PartialRelation("C3", 1); | ||
174 | var c4 = new PartialRelation("C4", 1); | ||
175 | var typeInfoMap = new LinkedHashMap<PartialRelation, TypeInfo>(); | ||
176 | typeInfoMap.put(c4, TypeInfo.builder().supertype(c3).build()); | ||
177 | typeInfoMap.put(c3, TypeInfo.builder().supertype(a1).build()); | ||
178 | typeInfoMap.put(c2, TypeInfo.builder().supertype(a1).build()); | ||
179 | typeInfoMap.put(c1, TypeInfo.builder().supertype(a1).build()); | ||
180 | typeInfoMap.put(a1, TypeInfo.builder().abstractType().build()); | ||
181 | |||
182 | var sut = new TypeAnalyzer(typeInfoMap); | ||
183 | var tester = new TypeAnalyzerTester(sut); | ||
184 | var c1Result = tester.getPreservedType(c1); | ||
185 | var a1Result = tester.getPreservedType(a1); | ||
186 | |||
187 | assertThat(c1Result.merge(a1Result.asInferredType(), TruthValue.FALSE), | ||
188 | Matchers.is(new InferredType(Set.of(a1), Set.of(c2, c3, c4), c3))); | ||
189 | } | ||
190 | |||
191 | @Test | ||
192 | void circularTypeHierarchyTest() { | ||
193 | var c1 = new PartialRelation("C1", 1); | ||
194 | var c2 = new PartialRelation("C2", 1); | ||
195 | var typeInfoMap = new LinkedHashMap<PartialRelation, TypeInfo>(); | ||
196 | typeInfoMap.put(c1, TypeInfo.builder().supertype(c2).build()); | ||
197 | typeInfoMap.put(c2, TypeInfo.builder().supertype(c1).build()); | ||
198 | |||
199 | assertThrows(IllegalArgumentException.class, () -> new TypeAnalyzer(typeInfoMap)); | ||
200 | } | ||
201 | } | ||
diff --git a/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/TypeAnalyzerTester.java b/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/TypeAnalyzerTester.java new file mode 100644 index 00000000..ce600ea6 --- /dev/null +++ b/subprojects/store-partial/src/test/java/tools/refinery/store/partial/translator/typehierarchy/TypeAnalyzerTester.java | |||
@@ -0,0 +1,51 @@ | |||
1 | package tools.refinery.store.partial.translator.typehierarchy; | ||
2 | |||
3 | import tools.refinery.store.partial.representation.PartialRelation; | ||
4 | |||
5 | import static org.hamcrest.MatcherAssert.assertThat; | ||
6 | import static org.hamcrest.Matchers.*; | ||
7 | import static org.hamcrest.Matchers.is; | ||
8 | |||
9 | class TypeAnalyzerTester { | ||
10 | private final TypeAnalyzer sut; | ||
11 | |||
12 | public TypeAnalyzerTester(TypeAnalyzer sut) { | ||
13 | this.sut = sut; | ||
14 | } | ||
15 | |||
16 | public void assertAbstractType(PartialRelation partialRelation, PartialRelation... directSubtypes) { | ||
17 | assertPreservedType(partialRelation, true, false, directSubtypes); | ||
18 | } | ||
19 | |||
20 | public void assertVacuousType(PartialRelation partialRelation) { | ||
21 | assertPreservedType(partialRelation, true, true); | ||
22 | } | ||
23 | |||
24 | public void assertConcreteType(PartialRelation partialRelation, PartialRelation... directSubtypes) { | ||
25 | assertPreservedType(partialRelation, false, false, directSubtypes); | ||
26 | } | ||
27 | |||
28 | private void assertPreservedType(PartialRelation partialRelation, boolean isAbstract, boolean isVacuous, | ||
29 | PartialRelation... directSubtypes) { | ||
30 | var result = sut.getAnalysisResults().get(partialRelation); | ||
31 | assertThat(result, is(instanceOf(PreservedType.class))); | ||
32 | var preservedResult = (PreservedType) result; | ||
33 | assertThat(preservedResult.isAbstractType(), is(isAbstract)); | ||
34 | assertThat(preservedResult.isVacuous(), is(isVacuous)); | ||
35 | assertThat(preservedResult.getDirectSubtypes(), hasItems(directSubtypes)); | ||
36 | } | ||
37 | |||
38 | public void assertEliminatedType(PartialRelation partialRelation, PartialRelation replacement) { | ||
39 | var result = sut.getAnalysisResults().get(partialRelation); | ||
40 | assertThat(result, is(instanceOf(EliminatedType.class))); | ||
41 | assertThat(((EliminatedType) result).replacement(), is(replacement)); | ||
42 | } | ||
43 | |||
44 | public PreservedType getPreservedType(PartialRelation partialRelation) { | ||
45 | return (PreservedType) sut.getAnalysisResults().get(partialRelation); | ||
46 | } | ||
47 | |||
48 | public InferredType getInferredType(PartialRelation partialRelation) { | ||
49 | return getPreservedType(partialRelation).asInferredType(); | ||
50 | } | ||
51 | } | ||