diff options
Diffstat (limited to 'subprojects/store/src/test')
2 files changed, 373 insertions, 0 deletions
diff --git a/subprojects/store/src/test/java/tools/refinery/store/statecoding/EquivalenceTest.java b/subprojects/store/src/test/java/tools/refinery/store/statecoding/EquivalenceTest.java new file mode 100644 index 00000000..8a9c0e9b --- /dev/null +++ b/subprojects/store/src/test/java/tools/refinery/store/statecoding/EquivalenceTest.java | |||
@@ -0,0 +1,178 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.statecoding; | ||
7 | |||
8 | import org.junit.jupiter.api.Test; | ||
9 | import tools.refinery.store.map.Version; | ||
10 | import tools.refinery.store.model.Model; | ||
11 | import tools.refinery.store.model.ModelStore; | ||
12 | import tools.refinery.store.representation.Symbol; | ||
13 | import tools.refinery.store.tuple.Tuple; | ||
14 | |||
15 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
16 | |||
17 | class EquivalenceTest { | ||
18 | Symbol<Boolean> person = new Symbol<>("Person", 1, Boolean.class, false); | ||
19 | Symbol<Integer> age = new Symbol<>("age", 1, Integer.class, null); | ||
20 | Symbol<Boolean> friend = new Symbol<>("friend", 2, Boolean.class, false); | ||
21 | Symbol<Boolean> parents = new Symbol<>("parents", 3, Boolean.class, false); | ||
22 | Symbol<Integer> population = new Symbol<>("population", 0, Integer.class, 0); | ||
23 | |||
24 | private ModelStore createStore() { | ||
25 | return ModelStore.builder() | ||
26 | .symbols(person, age, friend, parents, population) | ||
27 | .with(StateCoderAdapter.builder()) | ||
28 | .build(); | ||
29 | } | ||
30 | |||
31 | @Test | ||
32 | void emptyModelCode0() { | ||
33 | ModelStore store = createStore(); | ||
34 | var stateCoder = store.getAdapter(StateCoderStoreAdapter.class); | ||
35 | Model model = createStore().createEmptyModel(); | ||
36 | Version v1 = model.commit(); | ||
37 | Version v2 = model.commit(); | ||
38 | |||
39 | assertEquals(StateEquivalenceChecker.EquivalenceResult.ISOMORPHIC, stateCoder.checkEquivalence(v1, v2)); | ||
40 | |||
41 | var personI = model.getInterpretation(person); | ||
42 | var friendI = model.getInterpretation(friend); | ||
43 | |||
44 | personI.put(Tuple.of(1), true); | ||
45 | personI.put(Tuple.of(2), true); | ||
46 | friendI.put(Tuple.of(1, 2), true); | ||
47 | |||
48 | Version v3 = model.commit(); | ||
49 | |||
50 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v1, v3)); | ||
51 | } | ||
52 | |||
53 | @Test | ||
54 | void nullRelationTest() { | ||
55 | ModelStore store = createStore(); | ||
56 | var stateCoder = store.getAdapter(StateCoderStoreAdapter.class); | ||
57 | Model model = createStore().createEmptyModel(); | ||
58 | |||
59 | var populationI = model.getInterpretation(population); | ||
60 | |||
61 | Version v1 = model.commit(); | ||
62 | |||
63 | populationI.put(Tuple.of(), 1); | ||
64 | Version v2 = model.commit(); | ||
65 | |||
66 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v1, v2)); | ||
67 | |||
68 | populationI.put(Tuple.of(), 2); | ||
69 | Version v3 = model.commit(); | ||
70 | |||
71 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v2, v3)); | ||
72 | } | ||
73 | |||
74 | @Test | ||
75 | void unaryBooleanTest() { | ||
76 | ModelStore store = createStore(); | ||
77 | var stateCoder = store.getAdapter(StateCoderStoreAdapter.class); | ||
78 | Model model = createStore().createEmptyModel(); | ||
79 | |||
80 | var personI = model.getInterpretation(person); | ||
81 | |||
82 | Version v1 = model.commit(); | ||
83 | |||
84 | personI.put(Tuple.of(1), true); | ||
85 | Version v2 = model.commit(); | ||
86 | |||
87 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v1, v2)); | ||
88 | |||
89 | personI.put(Tuple.of(2), true); | ||
90 | Version v3 = model.commit(); | ||
91 | |||
92 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v2, v3)); | ||
93 | |||
94 | personI.put(Tuple.of(1), false); | ||
95 | Version v4 = model.commit(); | ||
96 | |||
97 | assertEquals(StateEquivalenceChecker.EquivalenceResult.ISOMORPHIC, stateCoder.checkEquivalence(v2, v4)); | ||
98 | } | ||
99 | |||
100 | @Test | ||
101 | void unaryIntTest() { | ||
102 | ModelStore store = createStore(); | ||
103 | var stateCoder = store.getAdapter(StateCoderStoreAdapter.class); | ||
104 | Model model = createStore().createEmptyModel(); | ||
105 | |||
106 | var ageI = model.getInterpretation(age); | ||
107 | |||
108 | ageI.put(Tuple.of(1), 3); | ||
109 | Version v1 = model.commit(); | ||
110 | |||
111 | ageI.put(Tuple.of(1), 4); | ||
112 | Version v2 = model.commit(); | ||
113 | |||
114 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v1, v2)); | ||
115 | |||
116 | ageI.put(Tuple.of(2), 4); | ||
117 | Version v3 = model.commit(); | ||
118 | |||
119 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v2, v3)); | ||
120 | |||
121 | ageI.put(Tuple.of(1), null); | ||
122 | Version v4 = model.commit(); | ||
123 | |||
124 | assertEquals(StateEquivalenceChecker.EquivalenceResult.ISOMORPHIC, stateCoder.checkEquivalence(v2, v4)); | ||
125 | } | ||
126 | |||
127 | @Test | ||
128 | void binaryTest() { | ||
129 | ModelStore store = createStore(); | ||
130 | var stateCoder = store.getAdapter(StateCoderStoreAdapter.class); | ||
131 | Model model = createStore().createEmptyModel(); | ||
132 | |||
133 | var friendI = model.getInterpretation(friend); | ||
134 | |||
135 | Version v1 = model.commit(); | ||
136 | |||
137 | friendI.put(Tuple.of(1, 2), true); | ||
138 | Version v2 = model.commit(); | ||
139 | |||
140 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v1, v2)); | ||
141 | |||
142 | friendI.put(Tuple.of(2, 1), true); | ||
143 | Version v3 = model.commit(); | ||
144 | |||
145 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v2, v3)); | ||
146 | |||
147 | friendI.put(Tuple.of(1, 2), false); | ||
148 | Version v4 = model.commit(); | ||
149 | |||
150 | assertEquals(StateEquivalenceChecker.EquivalenceResult.ISOMORPHIC, stateCoder.checkEquivalence(v2, v4)); | ||
151 | } | ||
152 | |||
153 | @Test | ||
154 | void NaryTest() { | ||
155 | ModelStore store = createStore(); | ||
156 | var stateCoder = store.getAdapter(StateCoderStoreAdapter.class); | ||
157 | Model model = createStore().createEmptyModel(); | ||
158 | |||
159 | var parentsI = model.getInterpretation(parents); | ||
160 | |||
161 | Version v1 = model.commit(); | ||
162 | |||
163 | parentsI.put(Tuple.of(3, 1, 2), true); | ||
164 | Version v2 = model.commit(); | ||
165 | |||
166 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v1, v2)); | ||
167 | |||
168 | parentsI.put(Tuple.of(4, 1, 2), true); | ||
169 | Version v3 = model.commit(); | ||
170 | |||
171 | assertEquals(StateEquivalenceChecker.EquivalenceResult.DIFFERENT, stateCoder.checkEquivalence(v2, v3)); | ||
172 | |||
173 | parentsI.put(Tuple.of(3, 1, 2), false); | ||
174 | Version v4 = model.commit(); | ||
175 | |||
176 | assertEquals(StateEquivalenceChecker.EquivalenceResult.ISOMORPHIC, stateCoder.checkEquivalence(v2, v4)); | ||
177 | } | ||
178 | } | ||
diff --git a/subprojects/store/src/test/java/tools/refinery/store/statecoding/StateCoderUnitTest.java b/subprojects/store/src/test/java/tools/refinery/store/statecoding/StateCoderUnitTest.java new file mode 100644 index 00000000..d94df841 --- /dev/null +++ b/subprojects/store/src/test/java/tools/refinery/store/statecoding/StateCoderUnitTest.java | |||
@@ -0,0 +1,195 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.statecoding; | ||
7 | |||
8 | import org.junit.jupiter.api.Test; | ||
9 | import tools.refinery.store.model.Model; | ||
10 | import tools.refinery.store.model.ModelStore; | ||
11 | import tools.refinery.store.representation.Symbol; | ||
12 | import tools.refinery.store.tuple.Tuple; | ||
13 | |||
14 | import java.util.Objects; | ||
15 | |||
16 | import static org.junit.jupiter.api.Assertions.*; | ||
17 | |||
18 | class StateCoderUnitTest { | ||
19 | Symbol<Boolean> person = new Symbol<>("Person", 1, Boolean.class, false); | ||
20 | Symbol<Integer> age = new Symbol<>("age", 1, Integer.class, null); | ||
21 | Symbol<Boolean> friend = new Symbol<>("friend", 2, Boolean.class, false); | ||
22 | Symbol<Boolean> parents = new Symbol<>("parents", 3, Boolean.class, false); | ||
23 | Symbol<Integer> population = new Symbol<>("population", 0, Integer.class, 0); | ||
24 | |||
25 | private Model createEmptyModel() { | ||
26 | var store = ModelStore.builder() | ||
27 | .symbols(person, age, friend, parents, population) | ||
28 | .with(StateCoderAdapter.builder()) | ||
29 | .build(); | ||
30 | |||
31 | return store.createEmptyModel(); | ||
32 | } | ||
33 | |||
34 | @Test | ||
35 | void emptyModelCode0() { | ||
36 | Model model = createEmptyModel(); | ||
37 | var stateCoder = model.getAdapter(StateCoderAdapter.class); | ||
38 | |||
39 | assertEquals(0, stateCoder.calculateModelCode()); | ||
40 | |||
41 | var personI = model.getInterpretation(person); | ||
42 | var friendI = model.getInterpretation(friend); | ||
43 | |||
44 | personI.put(Tuple.of(1), true); | ||
45 | personI.put(Tuple.of(2), true); | ||
46 | friendI.put(Tuple.of(1, 2), true); | ||
47 | |||
48 | assertNotEquals(0, stateCoder.calculateModelCode()); | ||
49 | } | ||
50 | |||
51 | @Test | ||
52 | void emptyObjectCode0() { | ||
53 | Model model = createEmptyModel(); | ||
54 | var stateCoder = model.getAdapter(StateCoderAdapter.class); | ||
55 | |||
56 | var personI = model.getInterpretation(person); | ||
57 | var friendI = model.getInterpretation(friend); | ||
58 | |||
59 | assertEquals(0, stateCoder.calculateObjectCode().get(1)); | ||
60 | assertEquals(0, stateCoder.calculateObjectCode().get(17)); | ||
61 | |||
62 | personI.put(Tuple.of(1), true); | ||
63 | personI.put(Tuple.of(2), true); | ||
64 | friendI.put(Tuple.of(1, 2), true); | ||
65 | |||
66 | assertNotEquals(0, stateCoder.calculateObjectCode().get(1)); | ||
67 | assertEquals(0, stateCoder.calculateObjectCode().get(17)); | ||
68 | } | ||
69 | |||
70 | @Test | ||
71 | void nullRelationTest() { | ||
72 | Model model = createEmptyModel(); | ||
73 | var stateCoder = model.getAdapter(StateCoderAdapter.class); | ||
74 | |||
75 | var populationI = model.getInterpretation(population); | ||
76 | |||
77 | final int hashOf0 = Objects.hashCode(0); | ||
78 | |||
79 | assertEquals(hashOf0, stateCoder.calculateModelCode()); | ||
80 | |||
81 | populationI.put(Tuple.of(), 1); | ||
82 | int code1 = stateCoder.calculateModelCode(); | ||
83 | |||
84 | assertNotEquals(hashOf0, stateCoder.calculateModelCode()); | ||
85 | |||
86 | populationI.put(Tuple.of(), 2); | ||
87 | int code2 = stateCoder.calculateModelCode(); | ||
88 | |||
89 | assertNotEquals(code1, code2); | ||
90 | |||
91 | populationI.put(Tuple.of(), 1); | ||
92 | assertEquals(code1, stateCoder.calculateModelCode()); | ||
93 | } | ||
94 | |||
95 | @Test | ||
96 | void unaryBooleanTest() { | ||
97 | Model model = createEmptyModel(); | ||
98 | var stateCoder = model.getAdapter(StateCoderAdapter.class); | ||
99 | |||
100 | var personI = model.getInterpretation(person); | ||
101 | |||
102 | assertEquals(0, stateCoder.calculateModelCode()); | ||
103 | |||
104 | personI.put(Tuple.of(1), true); | ||
105 | int code1 = stateCoder.calculateModelCode(); | ||
106 | |||
107 | assertNotEquals(0, stateCoder.calculateModelCode()); | ||
108 | |||
109 | personI.put(Tuple.of(2), true); | ||
110 | int code2 = stateCoder.calculateModelCode(); | ||
111 | |||
112 | assertNotEquals(code1, code2); | ||
113 | |||
114 | personI.put(Tuple.of(1), false); | ||
115 | assertEquals(code1, stateCoder.calculateModelCode()); | ||
116 | } | ||
117 | |||
118 | @Test | ||
119 | void unaryIntTest() { | ||
120 | Model model = createEmptyModel(); | ||
121 | var stateCoder = model.getAdapter(StateCoderAdapter.class); | ||
122 | |||
123 | var ageI = model.getInterpretation(age); | ||
124 | |||
125 | assertEquals(0, stateCoder.calculateModelCode()); | ||
126 | |||
127 | ageI.put(Tuple.of(1), 4); | ||
128 | int code0 = stateCoder.calculateModelCode(); | ||
129 | |||
130 | assertNotEquals(0, code0); | ||
131 | |||
132 | ageI.put(Tuple.of(1), 5); | ||
133 | int code1 = stateCoder.calculateModelCode(); | ||
134 | |||
135 | assertNotEquals(code0, code1); | ||
136 | |||
137 | ageI.put(Tuple.of(2), 5); | ||
138 | int code2 = stateCoder.calculateModelCode(); | ||
139 | |||
140 | assertNotEquals(code1, code2); | ||
141 | |||
142 | ageI.put(Tuple.of(1), null); | ||
143 | assertEquals(code1, stateCoder.calculateModelCode()); | ||
144 | } | ||
145 | |||
146 | @Test | ||
147 | void binaryTest() { | ||
148 | Model model = createEmptyModel(); | ||
149 | var stateCoder = model.getAdapter(StateCoderAdapter.class); | ||
150 | |||
151 | var friendI = model.getInterpretation(friend); | ||
152 | |||
153 | assertEquals(0, stateCoder.calculateModelCode()); | ||
154 | |||
155 | friendI.put(Tuple.of(1, 2), true); | ||
156 | int code1 = stateCoder.calculateModelCode(); | ||
157 | |||
158 | assertNotEquals(0, code1); | ||
159 | |||
160 | friendI.put(Tuple.of(2, 1), true); | ||
161 | int code2 = stateCoder.calculateModelCode(); | ||
162 | |||
163 | assertNotEquals(code1, code2); | ||
164 | |||
165 | friendI.put(Tuple.of(1, 2), false); | ||
166 | int code3 = stateCoder.calculateModelCode(); | ||
167 | |||
168 | assertEquals(code1, code3); | ||
169 | } | ||
170 | |||
171 | @Test | ||
172 | void NaryTest() { | ||
173 | Model model = createEmptyModel(); | ||
174 | var stateCoder = model.getAdapter(StateCoderAdapter.class); | ||
175 | |||
176 | var parentsI = model.getInterpretation(parents); | ||
177 | |||
178 | assertEquals(0, stateCoder.calculateModelCode()); | ||
179 | |||
180 | parentsI.put(Tuple.of(3, 1, 2), true); | ||
181 | int code1 = stateCoder.calculateModelCode(); | ||
182 | |||
183 | assertNotEquals(0, code1); | ||
184 | |||
185 | parentsI.put(Tuple.of(4, 1, 2), true); | ||
186 | int code2 = stateCoder.calculateModelCode(); | ||
187 | |||
188 | assertNotEquals(code1, code2); | ||
189 | |||
190 | parentsI.put(Tuple.of(3, 1, 2), false); | ||
191 | int code3 = stateCoder.calculateModelCode(); | ||
192 | |||
193 | assertEquals(code1, code3); | ||
194 | } | ||
195 | } | ||