$darkmode
DENOPTIM
APClassTest.java
Go to the documentation of this file.
1package denoptim.graph;
2
3/*
4 * DENOPTIM
5 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
6 * and Marco Foscato <marco.foscato@uib.no>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published
10 * by the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22import static org.junit.jupiter.api.Assertions.assertEquals;
23import static org.junit.jupiter.api.Assertions.assertFalse;
24import static org.junit.jupiter.api.Assertions.assertThrows;
25import static org.junit.jupiter.api.Assertions.assertTrue;
26
27import java.util.ArrayList;
28import java.util.Arrays;
29import java.util.Collections;
30import java.util.HashMap;
31import java.util.List;
32import java.util.Map;
33
34import org.junit.jupiter.api.Test;
35
36import denoptim.constants.DENOPTIMConstants;
37import denoptim.exception.DENOPTIMException;
38import denoptim.graph.Edge.BondType;
39
46public class APClassTest
47{
48//-----------------------------------------------------------------------------
49
50 @Test
51 public void testConstructor() throws Exception
52 {
53 assertThrows(DENOPTIMException.class, () -> {APClass.make("r7: :f9", 0,
54 BondType.UNDEFINED);},
55 "Wrong syntax for whole APClass string");
56 assertThrows(DENOPTIMException.class, () -> {APClass.make("r ",0);},
57 "Wrong syntax for 'rule' string");
58 }
59
60//-----------------------------------------------------------------------------
61
62 @Test
63 public void testIsValidAPSubCLassString() throws Exception
64 {
65 assertFalse(APClass.isValidAPSubCLassString("1.1"),"Double");
66 assertFalse(APClass.isValidAPSubCLassString("-1"),"Signed(-)");
67 assertFalse(APClass.isValidAPSubCLassString("+1"),"Signed(+)");
68 assertFalse(APClass.isValidAPSubCLassString("1a"),"Literal");
69 assertFalse(APClass.isValidAPSubCLassString("a1"),"Literal");
70 assertTrue(APClass.isValidAPSubCLassString("1"),"Good one");
71 }
72
73//-----------------------------------------------------------------------------
74
75 @Test
76 public void testIsValidAPRuleString() throws Exception
77 {
78 assertFalse(APClass.isValidAPRuleString("1.1"),"Double");
79 assertFalse(APClass.isValidAPRuleString("asd"
80 + DENOPTIMConstants.SEPARATORAPPROPSCL+"affg"),"With separator");
81 assertFalse(APClass.isValidAPRuleString("+asd"),"Signed");
82 assertFalse(APClass.isValidAPRuleString(""),"empty");
83 assertFalse(APClass.isValidAPRuleString(null),"null");
84 assertFalse(APClass.isValidAPRuleString("qwe et"),"space");
85 assertTrue(APClass.isValidAPRuleString("qwerty"),"Good one");
86 }
87
88//-----------------------------------------------------------------------------
89
90 @Test
91 public void testEquals() throws Exception
92 {
95 assertTrue(a.equals(b));
96 assertTrue(a==b,"a==b operator");
97
99 assertTrue(a.equals(c));
100 assertTrue(c.equals(b));
101 assertTrue(a==c,"a==c operator");
102 assertTrue(c==b,"c==b operator");
103
106 assertFalse(a.equals(d));
107 assertFalse(a.equals(e));
108 }
109
110//-----------------------------------------------------------------------------
111
112 @Test
113 public void testListContains() throws Exception
114 {
116 APClass b = APClass.make("a:1");
117 APClass c = APClass.make("a",1);
118 APClass d = a;
119
120 List<APClass> list = new ArrayList<APClass>();
121 list.add(a);
122 assertTrue(list.contains(a),"List contains A");
123 assertTrue(list.contains(d),"List contains B");
124 assertTrue(list.contains(c),"List contains C");
125 assertTrue(list.contains(b),"List contains D");
126
127 Map<APClass,APClass> map = new HashMap<APClass,APClass>();
128 map.put(a,b);
129 map.put(c,d);
130 assertTrue(map.containsKey(a),"Map contains A");
131 assertTrue(map.containsKey(b),"Map contains B");
132 assertTrue(map.containsKey(c),"Map contains C");
133 assertTrue(map.containsKey(d),"Map contains D");
134 }
135
136//-----------------------------------------------------------------------------
137
138 @Test
139 public void testCompareTo() throws Exception
140 {
145 List<APClass> l = new ArrayList<APClass>(Arrays.asList(d,a,b,c));
146 Collections.sort(l);
147 List<APClass> ref = new ArrayList<APClass>(Arrays.asList(b,a,c,d));
148 for (int i=0; i<l.size(); i++)
149 {
150 APClass el=l.get(i);
151 APClass er=ref.get(i);
152 assertEquals(er,el," Entries "+er+" != "+el);
153 }
154 }
155
156//------------------------------------------------------------------------------
157}
General set of constants used in DENOPTIM.
static final String SEPARATORAPPROPSCL
Separator between APClass and APSubClass and coordinates.
boolean equals(Object o)
Definition: APClass.java:488
static boolean isValidAPRuleString(String s)
Evaluates the given string as a candidate attachment point rule, i.e., as name of a fragmentation rul...
Definition: APClass.java:396
static final BondType DEFAULTBT
Default bond type for all but APClasses of RCVs.
Definition: APClass.java:109
static boolean isValidAPSubCLassString(String s)
Evaluate the given string as a candidate for attachment point subclass, i.e., the attachment point cl...
Definition: APClass.java:381
static APClass make(String ruleAndSubclass)
Creates an APClass if it does not exist already, or returns the reference to the existing instance.
Definition: APClass.java:136
Unit test for APClass.
Possible chemical bond types an edge can represent.
Definition: Edge.java:303