$darkmode
DENOPTIM
EdgeTest.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.graph;
20
21import static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertFalse;
23import static org.junit.jupiter.api.Assertions.assertNull;
24import static org.junit.jupiter.api.Assertions.assertTrue;
25
26import org.junit.jupiter.api.BeforeEach;
27import org.junit.jupiter.api.Test;
28
29import denoptim.graph.Edge.BondType;
30import denoptim.graph.Vertex.BBType;
31
38public class EdgeTest {
41 private StringBuilder reason = new StringBuilder();
45
46//------------------------------------------------------------------------------
47
48 @BeforeEach
49 public void setUpClass() {
55
59 }
60
61//------------------------------------------------------------------------------
62
63 @Test
64 public void testUndirectedComparison() throws Exception {
65 EmptyVertex vA1 = new EmptyVertex();
66 vA1.setBuildingBlockId(1);
68 vA1.addAP();
69 vA1.addAP();
70 AttachmentPoint apA11 = vA1.getAP(0);
71
72 EmptyVertex vB1 = new EmptyVertex();
73 vB1.setBuildingBlockId(2);
75 vB1.addAP();
76 vB1.addAP();
77 vB1.addAP();
78 AttachmentPoint apB1 = vB1.getAP(2);
79
80 EmptyVertex vA2 = new EmptyVertex();
81 vA2.setBuildingBlockId(1);
83 vA2.addAP();
84 vA2.addAP();
85 AttachmentPoint apA21 = vA2.getAP(0);
86
87 EmptyVertex vB2 = new EmptyVertex();
88 vB2.setBuildingBlockId(2);
90 vB2.addAP();
91 vB2.addAP();
92 vB2.addAP();
93 AttachmentPoint apB2 = vB2.getAP(2);
94
95 Edge e1 = new Edge(apA11, apB1, BondType.SINGLE);
96 assertEquals(0,e1.compareAsUndirected(e1),"Self-comparison");
97
98 Edge e1rev = new Edge(apA21, apB2, BondType.SINGLE);
99 assertEquals(0,e1.compareAsUndirected(e1rev),
100 "Inverse edges should be equal (A)");
101 assertEquals(0,e1rev.compareAsUndirected(e1),
102 "Inverse edges should be equal (B)");
103
104
105 EmptyVertex vA3a = new EmptyVertex();
106 vA3a.setBuildingBlockId(1);
108 vA3a.addAP();
109 vA3a.addAP();
110 AttachmentPoint apA31a = vA3a.getAP(0);
111
112 EmptyVertex vA3b = new EmptyVertex();
113 vA3b.setBuildingBlockId(1);
115 vA3b.addAP();
116 vA3b.addAP();
117 AttachmentPoint apA31b = vA3b.getAP(0);
118
119 EmptyVertex vA3c = new EmptyVertex();
120 vA3c.setBuildingBlockId(1);
122 vA3c.addAP();
123 vA3c.addAP();
124 AttachmentPoint apA31c = vA3c.getAP(0);
125
126 EmptyVertex vB3 = new EmptyVertex();
127 vB3.setBuildingBlockId(2);
129 vB3.addAP();
130 vB3.addAP();
131 vB3.addAP();
132 AttachmentPoint apB3 = vB3.getAP(2);
133
134 EmptyVertex vS = new EmptyVertex();
135 vS.setBuildingBlockId(0);
137 vS.addAP();
138 vS.addAP();
139 AttachmentPoint apS1 = vS.getAP(0);
140
141 EmptyVertex vC = new EmptyVertex();
142 vC.setBuildingBlockId(0);
144 vC.addAP();
145 vC.addAP();
146 AttachmentPoint apC1 = vC.getAP(0);
147
148 Edge e3f = new Edge(apA31a, apB3, BondType.SINGLE);
149 Edge e3s = new Edge(apA31b, apS1, BondType.SINGLE);
150 Edge e3c = new Edge(apA31c, apC1, BondType.SINGLE);
151 assertEquals(1,e3f.compareAsUndirected(e3s),"Ranking (A)");
152 assertEquals(-1,e3s.compareAsUndirected(e3f),"Ranking (Arev)");
153 assertEquals(-1,e3f.compareAsUndirected(e3c),"Ranking (B)");
154 assertEquals(1,e3c.compareAsUndirected(e3f),"Ranking (Brev)");
155 assertEquals(1,e3c.compareAsUndirected(e3s),"Ranking (C)");
156 assertEquals(-1,e3s.compareAsUndirected(e3c),"Ranking (Crev)");
157
158
159 EmptyVertex vA4 = new EmptyVertex();
160 vA4.setBuildingBlockId(1);
162 vA4.addAP();
163 vA4.addAP();
164 AttachmentPoint apA41 = vA4.getAP(0);
165 AttachmentPoint apA42 = vA4.getAP(1);
166
167 EmptyVertex vB4 = new EmptyVertex();
168 vB4.setBuildingBlockId(2);
170 vB4.addAP();
171 vB4.addAP();
172 vB4.addAP();
173 AttachmentPoint apB41 = vB4.getAP(0);
174 AttachmentPoint apB42 = vB4.getAP(2);
175
176 Edge e41 = new Edge(apA41, apB41, BondType.SINGLE);
177 Edge e42 = new Edge(apA42, apB42, BondType.SINGLE);
178 assertEquals(-1,e41.compareAsUndirected(e42),
179 "Different APs lead to different edge (A)");
180 assertEquals(1,e42.compareAsUndirected(e41),
181 "Different APs lead to different edge (B)");
182
183
184 EmptyVertex vA5 = new EmptyVertex();
185 vA5.setBuildingBlockId(3);
187 vA5.addAP();
188 vA5.addAP();
189 AttachmentPoint apA51 = vA5.getAP(0);
190
191 EmptyVertex vB5 = new EmptyVertex();
192 vB5.setBuildingBlockId(24);
194 vB5.addAP();
195 vB5.addAP();
196 vB5.addAP();
197 AttachmentPoint apB51 = vB5.getAP(0);
198
199 EmptyVertex vA6 = new EmptyVertex();
200 vA6.setBuildingBlockId(3);
202 vA6.addAP();
203 vA6.addAP();
204 AttachmentPoint apA61 = vA6.getAP(0);
205
206 EmptyVertex vB6 = new EmptyVertex();
207 vB6.setBuildingBlockId(24);
209 vB6.addAP();
210 vB6.addAP();
211 vB6.addAP();
212 AttachmentPoint apB61 = vB6.getAP(0);
213
214 Edge e5 = new Edge(apA51, apB51, BondType.SINGLE);
215 Edge e6 = new Edge(apA61, apB61, BondType.UNDEFINED);
216 assertTrue(0 < e5.compareAsUndirected(e6),
217 "Different bond types lead to different edge (A)");
218 assertTrue(0 > e6.compareAsUndirected(e5),
219 "Different bond types lead to different edge (B)");
220 }
221
222//------------------------------------------------------------------------------
223
224 @Test
225 public void testConnectionDeconnectionLoop() throws Exception {
226
228 assertEquals(e.getSrcAP().hashCode(), dummyApA1.hashCode(),
229 "SrcAP hashcode");
230 assertEquals(e.getTrgAP().hashCode(), dummyApB.hashCode(),
231 "TrgAP hashcode");
232 assertEquals(e.hashCode(), dummyApA1.getEdgeUser().hashCode(),
233 "Src AP user hashcode");
234 assertEquals(e.hashCode(), dummyApB.getEdgeUser().hashCode(),
235 "Trg AP user hashcode");
236
237 DGraph g = new DGraph();
240 g.addEdge(e);
241
242 g.removeEdge(e);
243
244 assertNull(dummyApA1.getEdgeUser(), "Src AP user removed");
245 assertNull(dummyApB.getEdgeUser(), "Trg AP user removed");
246 }
247
248//------------------------------------------------------------------------------
249
250 @Test
251 public void testSameAs_Equal() throws Exception {
252
253 Edge eA = new Edge(dummyApA1, dummyApA2,
255 Edge eB = new Edge(dummyApA1, dummyApA2,
257
258 assertTrue(eA.sameAs(eB, reason));
259 }
260
261//------------------------------------------------------------------------------
262
263 @Test
264 public void testSameAs_DiffBndTyp() throws Exception {
265 Edge eA = new Edge(dummyApA1, dummyApA2,
267 Edge eB = new Edge(dummyApA1, dummyApA2,
269
270 assertFalse(eA.sameAs(eB, reason));
271 }
272
273//------------------------------------------------------------------------------
274}
An attachment point (AP) is a possibility to attach a Vertex onto the vertex holding the AP (i....
Edge getEdgeUser()
Gets the edge that is using this AP, or null if no edge is using this AP.
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
void addVertex(Vertex vertex)
Appends a vertex to this graph without creating any edge.
Definition: DGraph.java:1097
void removeEdge(Edge edge)
Removes an edge and update the free valences of the attachment points that were originally involved i...
Definition: DGraph.java:2620
void addEdge(Edge edge)
Adds the edge to the list of edges belonging to this graph.
Definition: DGraph.java:1021
This class represents the edge between two vertices.
Definition: Edge.java:38
boolean sameAs(Edge other, StringBuilder reason)
Compares this and another edge ignoring edge and vertex IDs.
Definition: Edge.java:180
int compareAsUndirected(Edge other)
Compares this and another edge ignoring the directionality of both, i.e., as if both edges were undir...
Definition: Edge.java:221
AttachmentPoint getTrgAP()
Definition: Edge.java:115
AttachmentPoint getSrcAP()
Definition: Edge.java:94
Unit test for DENOPTIMEdge.
Definition: EdgeTest.java:38
void testConnectionDeconnectionLoop()
Definition: EdgeTest.java:225
EmptyVertex dummyVertexA
Definition: EdgeTest.java:39
AttachmentPoint dummyApB
Definition: EdgeTest.java:44
AttachmentPoint dummyApA2
Definition: EdgeTest.java:43
AttachmentPoint dummyApA1
Definition: EdgeTest.java:42
StringBuilder reason
Definition: EdgeTest.java:41
EmptyVertex dummyVertexB
Definition: EdgeTest.java:40
void testUndirectedComparison()
Definition: EdgeTest.java:64
An empty vertex has the behaviors of a vertex, but has no molecular structure.
void addAP()
Adds an attachment point with no APClass or other attribute.
void setBuildingBlockId(int buildingBlockId)
Definition: Vertex.java:291
void setBuildingBlockType(Vertex.BBType buildingBlockType)
Definition: Vertex.java:305
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
Definition: Vertex.java:920
Possible chemical bond types an edge can represent.
Definition: Edge.java:303
The type of building block.
Definition: Vertex.java:86