diff options
author | 2021-08-16 02:16:51 +0200 | |
---|---|---|
committer | 2021-08-16 02:16:51 +0200 | |
commit | 4df8b762954da153c25201bd32640de00384a167 (patch) | |
tree | 2f19b958cefdd0f886a031ab22dd6d12a60d0e40 /model-data | |
parent | Added validation for null tuples (diff) | |
download | refinery-4df8b762954da153c25201bd32640de00384a167.tar.gz refinery-4df8b762954da153c25201bd32640de00384a167.tar.zst refinery-4df8b762954da153c25201bd32640de00384a167.zip |
Basic Model building tests
Diffstat (limited to 'model-data')
-rw-r--r-- | model-data/src/test/java/org/eclipse/viatra/solver/data/model/tests/ModelTest.java | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/model-data/src/test/java/org/eclipse/viatra/solver/data/model/tests/ModelTest.java b/model-data/src/test/java/org/eclipse/viatra/solver/data/model/tests/ModelTest.java new file mode 100644 index 00000000..dacea35d --- /dev/null +++ b/model-data/src/test/java/org/eclipse/viatra/solver/data/model/tests/ModelTest.java | |||
@@ -0,0 +1,149 @@ | |||
1 | package org.eclipse.viatra.solver.data.model.tests; | ||
2 | |||
3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
4 | import static org.junit.jupiter.api.Assertions.assertFalse; | ||
5 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
6 | |||
7 | import java.util.Set; | ||
8 | |||
9 | import org.eclipse.viatra.solver.data.model.Model; | ||
10 | import org.eclipse.viatra.solver.data.model.ModelStore; | ||
11 | import org.eclipse.viatra.solver.data.model.ModelStoreImpl; | ||
12 | import org.eclipse.viatra.solver.data.model.Tuple; | ||
13 | import org.eclipse.viatra.solver.data.model.representation.Relation; | ||
14 | import org.junit.jupiter.api.Assertions; | ||
15 | import org.junit.jupiter.api.Test; | ||
16 | |||
17 | class ModelTest { | ||
18 | |||
19 | @Test | ||
20 | void modelConstructionTest() { | ||
21 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
22 | Relation<Boolean> friend = new Relation<>("friend", 2, false); | ||
23 | |||
24 | ModelStore store = new ModelStoreImpl(Set.of(person,friend)); | ||
25 | Model model = store.createModel(); | ||
26 | |||
27 | assertTrue(store.getDataRepresentations().contains(person)); | ||
28 | assertTrue(store.getDataRepresentations().contains(friend)); | ||
29 | assertTrue(model.getDataRepresentations().contains(person)); | ||
30 | assertTrue(model.getDataRepresentations().contains(friend)); | ||
31 | |||
32 | Relation<Integer> other = new Relation<Integer>("other", 2, null); | ||
33 | assertFalse(model.getDataRepresentations().contains(other)); | ||
34 | } | ||
35 | |||
36 | @Test | ||
37 | void modelBuildingTest() { | ||
38 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
39 | Relation<Integer> age = new Relation<Integer>("age", 1, null); | ||
40 | Relation<Boolean> friend = new Relation<>("friend", 2, false); | ||
41 | |||
42 | ModelStore store = new ModelStoreImpl(Set.of(person,age,friend)); | ||
43 | Model model = store.createModel(); | ||
44 | |||
45 | model.put(person, Tuple.of(0), true); | ||
46 | model.put(person, Tuple.of(1), true); | ||
47 | model.put(age, Tuple.of(0), 3); | ||
48 | model.put(age, Tuple.of(1), 1); | ||
49 | model.put(friend, Tuple.of(0,1), true); | ||
50 | model.put(friend, Tuple.of(1,0), true); | ||
51 | |||
52 | assertTrue(model.get(person, Tuple.of(0))); | ||
53 | assertTrue(model.get(person, Tuple.of(1))); | ||
54 | assertFalse(model.get(person, Tuple.of(2))); | ||
55 | |||
56 | assertEquals(3, model.get(age, Tuple.of(0))); | ||
57 | assertEquals(1, model.get(age, Tuple.of(1))); | ||
58 | assertEquals(null, model.get(age, Tuple.of(2))); | ||
59 | |||
60 | assertTrue(model.get(friend, Tuple.of(0,1))); | ||
61 | assertFalse(model.get(friend, Tuple.of(0,5))); | ||
62 | } | ||
63 | |||
64 | @Test | ||
65 | void modelBuildingArityFailTest() { | ||
66 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
67 | ModelStore store = new ModelStoreImpl(Set.of(person)); | ||
68 | Model model = store.createModel(); | ||
69 | |||
70 | final Tuple tuple3 = Tuple.of(1,1,1); | ||
71 | Assertions.assertThrows(IllegalArgumentException.class, ()-> model.put(person, tuple3, true)); | ||
72 | Assertions.assertThrows(IllegalArgumentException.class, ()-> model.get(person, tuple3)); | ||
73 | } | ||
74 | |||
75 | @Test | ||
76 | void modelBuildingNullFailTest() { | ||
77 | Relation<Integer> age = new Relation<Integer>("age", 1, null); | ||
78 | ModelStore store = new ModelStoreImpl(Set.of(age)); | ||
79 | Model model = store.createModel(); | ||
80 | |||
81 | //final Tuple tuple3 = Tuple.of(1,1,1); | ||
82 | |||
83 | model.put(age, Tuple.of(1), null); // valid | ||
84 | Assertions.assertThrows(IllegalArgumentException.class, ()-> model.put(age, null, 1)); | ||
85 | Assertions.assertThrows(IllegalArgumentException.class, ()-> model.get(age, null)); | ||
86 | |||
87 | } | ||
88 | |||
89 | @Test | ||
90 | void modelUpdateTest() { | ||
91 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
92 | Relation<Integer> age = new Relation<Integer>("age", 1, null); | ||
93 | Relation<Boolean> friend = new Relation<>("friend", 2, false); | ||
94 | |||
95 | ModelStore store = new ModelStoreImpl(Set.of(person,age,friend)); | ||
96 | Model model = store.createModel(); | ||
97 | |||
98 | model.put(person, Tuple.of(0), true); | ||
99 | model.put(person, Tuple.of(1), true); | ||
100 | model.put(age, Tuple.of(0), 3); | ||
101 | model.put(age, Tuple.of(1), 1); | ||
102 | model.put(friend, Tuple.of(0,1), true); | ||
103 | model.put(friend, Tuple.of(1,0), true); | ||
104 | |||
105 | assertEquals(3, model.get(age, Tuple.of(0))); | ||
106 | assertTrue(model.get(friend, Tuple.of(0,1))); | ||
107 | |||
108 | model.put(age, Tuple.of(0), 4); | ||
109 | model.put(friend, Tuple.of(0,1), false); | ||
110 | |||
111 | assertEquals(4, model.get(age, Tuple.of(0))); | ||
112 | assertFalse(model.get(friend, Tuple.of(0,1))); | ||
113 | } | ||
114 | |||
115 | @Test | ||
116 | void restoreTest() { | ||
117 | Relation<Boolean> person = new Relation<Boolean>("Person", 1, false); | ||
118 | Relation<Boolean> friend = new Relation<Boolean>("friend", 2, false); | ||
119 | |||
120 | ModelStore store = new ModelStoreImpl(Set.of(person,friend)); | ||
121 | Model model = store.createModel(); | ||
122 | |||
123 | model.put(person, Tuple.of(0), true); | ||
124 | model.put(person, Tuple.of(1), true); | ||
125 | model.put(friend, Tuple.of(0,1), true); | ||
126 | model.put(friend, Tuple.of(1,0), true); | ||
127 | long state1 = model.commit(); | ||
128 | |||
129 | assertFalse(model.get(person, Tuple.of(2))); | ||
130 | assertFalse(model.get(friend, Tuple.of(0,2))); | ||
131 | |||
132 | model.put(person, Tuple.of(2), true); | ||
133 | model.put(friend, Tuple.of(0,2), true); | ||
134 | long state2 = model.commit(); | ||
135 | |||
136 | assertTrue(model.get(person, Tuple.of(2))); | ||
137 | assertTrue(model.get(friend, Tuple.of(0,2))); | ||
138 | |||
139 | model.restore(state1); | ||
140 | |||
141 | assertFalse(model.get(person, Tuple.of(2))); | ||
142 | assertFalse(model.get(friend, Tuple.of(0,2))); | ||
143 | |||
144 | model.restore(state2); | ||
145 | |||
146 | assertTrue(model.get(person, Tuple.of(2))); | ||
147 | assertTrue(model.get(friend, Tuple.of(0,2))); | ||
148 | } | ||
149 | } | ||