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