| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * FormulaUnitTest.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Jul 21, 2010 | 
|---|
| 27 | *      Author: crueger | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 36 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 37 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 38 |  | 
|---|
| 39 | #include <iostream> | 
|---|
| 40 |  | 
|---|
| 41 | #include "Formula.hpp" | 
|---|
| 42 | #include "World.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include "FormulaUnitTest.hpp" | 
|---|
| 45 |  | 
|---|
| 46 | #ifdef HAVE_TESTRUNNER | 
|---|
| 47 | #include "UnitTestMain.hpp" | 
|---|
| 48 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 49 |  | 
|---|
| 50 | CPPUNIT_TEST_SUITE_REGISTRATION( FormulaUnittest ); | 
|---|
| 51 |  | 
|---|
| 52 | void FormulaUnittest::setUp() | 
|---|
| 53 | { | 
|---|
| 54 | // failing asserts should be thrown | 
|---|
| 55 | ASSERT_DO(Assert::Throw); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | void FormulaUnittest::tearDown() | 
|---|
| 59 | { | 
|---|
| 60 | World::purgeInstance(); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | void FormulaUnittest::baseTest(){ | 
|---|
| 64 | Formula formula; | 
|---|
| 65 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)0); | 
|---|
| 66 | CPPUNIT_ASSERT(formula.begin()==formula.end()); | 
|---|
| 67 |  | 
|---|
| 68 | // test some one letter element names | 
|---|
| 69 | formula += "H"; | 
|---|
| 70 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1); | 
|---|
| 71 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0); | 
|---|
| 72 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 73 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 74 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)1); | 
|---|
| 75 | CPPUNIT_ASSERT(formula.begin()!=formula.end()); | 
|---|
| 76 | formula += "H"; | 
|---|
| 77 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2); | 
|---|
| 78 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0); | 
|---|
| 79 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 80 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 81 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)1); | 
|---|
| 82 | CPPUNIT_ASSERT(formula.begin()!=formula.end()); | 
|---|
| 83 | formula += "O"; | 
|---|
| 84 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2); | 
|---|
| 85 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)1); | 
|---|
| 86 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 87 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 88 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)2); | 
|---|
| 89 | CPPUNIT_ASSERT(formula.begin()!=formula.end()); | 
|---|
| 90 |  | 
|---|
| 91 | // test copy constructor | 
|---|
| 92 | Formula formula2=formula; | 
|---|
| 93 | CPPUNIT_ASSERT_EQUAL(formula2["H"],(unsigned int)2); | 
|---|
| 94 | CPPUNIT_ASSERT_EQUAL(formula2["O"],(unsigned int)1); | 
|---|
| 95 | CPPUNIT_ASSERT_EQUAL(formula2["C"],(unsigned int)0); | 
|---|
| 96 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 97 | CPPUNIT_ASSERT_EQUAL(formula2.getElementCount(),(unsigned int)2); | 
|---|
| 98 | CPPUNIT_ASSERT(formula2.begin()!=formula2.end()); | 
|---|
| 99 |  | 
|---|
| 100 | // test Assignment operator | 
|---|
| 101 | formula2=Formula(); | 
|---|
| 102 | CPPUNIT_ASSERT_EQUAL(formula2["H"],(unsigned int)0); | 
|---|
| 103 | CPPUNIT_ASSERT_EQUAL(formula2["O"],(unsigned int)0); | 
|---|
| 104 | CPPUNIT_ASSERT_EQUAL(formula2["C"],(unsigned int)0); | 
|---|
| 105 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 106 | CPPUNIT_ASSERT_EQUAL(formula2.getElementCount(),(unsigned int)0); | 
|---|
| 107 | CPPUNIT_ASSERT(formula2.begin()==formula2.end()); | 
|---|
| 108 |  | 
|---|
| 109 | // test comparison | 
|---|
| 110 | CPPUNIT_ASSERT(formula!=formula2); | 
|---|
| 111 | Formula formula3; | 
|---|
| 112 | formula3 += "H"; | 
|---|
| 113 | formula3 += "H"; | 
|---|
| 114 | formula3 += "O"; | 
|---|
| 115 | CPPUNIT_ASSERT(formula==formula3); | 
|---|
| 116 |  | 
|---|
| 117 | // inequality | 
|---|
| 118 | formula3 += "O"; | 
|---|
| 119 | CPPUNIT_ASSERT(formula!=formula3); | 
|---|
| 120 |  | 
|---|
| 121 | // after removing it should be fine again | 
|---|
| 122 | formula3 -= "O"; | 
|---|
| 123 | CPPUNIT_ASSERT(formula==formula3); | 
|---|
| 124 |  | 
|---|
| 125 | // add something from the far end, to test resizing | 
|---|
| 126 | formula3 += "Rh"; | 
|---|
| 127 | CPPUNIT_ASSERT(formula!=formula3); | 
|---|
| 128 | formula3 -= "Rh"; | 
|---|
| 129 | CPPUNIT_ASSERT(formula==formula3); | 
|---|
| 130 |  | 
|---|
| 131 | // removal of elements | 
|---|
| 132 | formula -= "H"; | 
|---|
| 133 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1); | 
|---|
| 134 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)1); | 
|---|
| 135 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 136 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 137 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)2); | 
|---|
| 138 | CPPUNIT_ASSERT(formula.begin()!=formula.end()); | 
|---|
| 139 |  | 
|---|
| 140 | formula -= "O"; | 
|---|
| 141 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1); | 
|---|
| 142 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0); | 
|---|
| 143 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 144 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 145 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)1); | 
|---|
| 146 | CPPUNIT_ASSERT(formula.begin()!=formula.end()); | 
|---|
| 147 |  | 
|---|
| 148 | formula -= "H"; | 
|---|
| 149 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0); | 
|---|
| 150 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0); | 
|---|
| 151 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 152 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 153 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)0); | 
|---|
| 154 | CPPUNIT_ASSERT(formula.begin()==formula.end()); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | void FormulaUnittest::parseTest(){ | 
|---|
| 158 | { | 
|---|
| 159 | Formula formula(""); | 
|---|
| 160 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0); | 
|---|
| 161 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0); | 
|---|
| 162 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 163 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 164 | } | 
|---|
| 165 | { | 
|---|
| 166 | Formula formula("H"); | 
|---|
| 167 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1); | 
|---|
| 168 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0); | 
|---|
| 169 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 170 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 171 | } | 
|---|
| 172 | { | 
|---|
| 173 | Formula formula("H2"); | 
|---|
| 174 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2); | 
|---|
| 175 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0); | 
|---|
| 176 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 177 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 178 | } | 
|---|
| 179 | { | 
|---|
| 180 | Formula formula("H2O"); | 
|---|
| 181 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2); | 
|---|
| 182 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)1); | 
|---|
| 183 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 184 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 185 | } | 
|---|
| 186 | { | 
|---|
| 187 | Formula formula("CH3COOH"); | 
|---|
| 188 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)4); | 
|---|
| 189 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)2); | 
|---|
| 190 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)2); | 
|---|
| 191 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 192 | } | 
|---|
| 193 | { | 
|---|
| 194 | Formula formula("CH3COONa"); | 
|---|
| 195 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)3); | 
|---|
| 196 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)2); | 
|---|
| 197 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)2); | 
|---|
| 198 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)1); | 
|---|
| 199 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 200 | } | 
|---|
| 201 | { | 
|---|
| 202 | Formula formula("NaCH3COO"); | 
|---|
| 203 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)3); | 
|---|
| 204 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)2); | 
|---|
| 205 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)2); | 
|---|
| 206 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)1); | 
|---|
| 207 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 208 | } | 
|---|
| 209 | { | 
|---|
| 210 | Formula formula("Na2CO3"); | 
|---|
| 211 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0); | 
|---|
| 212 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)3); | 
|---|
| 213 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)1); | 
|---|
| 214 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)2); | 
|---|
| 215 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 216 | } | 
|---|
| 217 | { | 
|---|
| 218 | Formula formula("CH2(COOH)2"); | 
|---|
| 219 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)4); | 
|---|
| 220 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)4); | 
|---|
| 221 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)3); | 
|---|
| 222 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0); | 
|---|
| 223 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 224 | } | 
|---|
| 225 | { | 
|---|
| 226 | Formula formula("K4[Fe(CN)6]"); | 
|---|
| 227 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0); | 
|---|
| 228 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0); | 
|---|
| 229 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)6); | 
|---|
| 230 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0); | 
|---|
| 231 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 232 | CPPUNIT_ASSERT_EQUAL(formula["K"],(unsigned int)4); | 
|---|
| 233 | CPPUNIT_ASSERT_EQUAL(formula["Fe"],(unsigned int)1); | 
|---|
| 234 | CPPUNIT_ASSERT_EQUAL(formula["N"],(unsigned int)6); | 
|---|
| 235 | } | 
|---|
| 236 | { | 
|---|
| 237 | Formula formula("[CrCl3(H2O)3]"); | 
|---|
| 238 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)6); | 
|---|
| 239 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)3); | 
|---|
| 240 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0); | 
|---|
| 241 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0); | 
|---|
| 242 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 243 | CPPUNIT_ASSERT_EQUAL(formula["Cr"],(unsigned int)1); | 
|---|
| 244 | CPPUNIT_ASSERT_EQUAL(formula["Cl"],(unsigned int)3); | 
|---|
| 245 | } | 
|---|
| 246 | { | 
|---|
| 247 | Formula formula("Mg3[Fe(CN)6]2"); | 
|---|
| 248 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0); | 
|---|
| 249 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0); | 
|---|
| 250 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)12); | 
|---|
| 251 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0); | 
|---|
| 252 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 253 | CPPUNIT_ASSERT_EQUAL(formula["Mg"],(unsigned int)3); | 
|---|
| 254 | CPPUNIT_ASSERT_EQUAL(formula["Fe"],(unsigned int)2); | 
|---|
| 255 | CPPUNIT_ASSERT_EQUAL(formula["N"],(unsigned int)12); | 
|---|
| 256 | } | 
|---|
| 257 | { | 
|---|
| 258 | Formula formula("Na[Fe((HO2CCH2)2NCH2CH2N(CH2CO2H)2)]"); | 
|---|
| 259 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)16); | 
|---|
| 260 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)8); | 
|---|
| 261 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)10); | 
|---|
| 262 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)1); | 
|---|
| 263 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0); | 
|---|
| 264 | CPPUNIT_ASSERT_EQUAL(formula["Mg"],(unsigned int)0); | 
|---|
| 265 | CPPUNIT_ASSERT_EQUAL(formula["Fe"],(unsigned int)1); | 
|---|
| 266 | CPPUNIT_ASSERT_EQUAL(formula["N"],(unsigned int)2); | 
|---|
| 267 | } | 
|---|
| 268 | { | 
|---|
| 269 | CPPUNIT_ASSERT_THROW(Formula formula("74107"),FormulaStringParseException); | 
|---|
| 270 | CPPUNIT_ASSERT_THROW(Formula formula("  "),FormulaStringParseException); | 
|---|
| 271 | CPPUNIT_ASSERT_THROW(Formula formula("nAcL"),FormulaStringParseException); | 
|---|
| 272 | CPPUNIT_ASSERT_THROW(Formula formula("NAcL"),FormulaStringParseException); | 
|---|
| 273 | CPPUNIT_ASSERT_THROW(Formula formula("Na Cl"),FormulaStringParseException); | 
|---|
| 274 | CPPUNIT_ASSERT_THROW(Formula formula("1NaCl"),FormulaStringParseException); | 
|---|
| 275 | CPPUNIT_ASSERT_THROW(Formula formula("Mag"),FormulaStringParseException); | 
|---|
| 276 | CPPUNIT_ASSERT_THROW(Formula formula("AgCl)"),FormulaStringParseException); | 
|---|
| 277 | CPPUNIT_ASSERT_THROW(Formula formula("(Na"),FormulaStringParseException); | 
|---|
| 278 | CPPUNIT_ASSERT_THROW(Formula formula("(Mag)"),FormulaStringParseException); | 
|---|
| 279 | CPPUNIT_ASSERT_THROW(Formula formula("MgCl2)"),FormulaStringParseException); | 
|---|
| 280 | CPPUNIT_ASSERT_THROW(Formula formula("((MgCl2)"),FormulaStringParseException); | 
|---|
| 281 | CPPUNIT_ASSERT_THROW(Formula formula("(MgCl2))"),FormulaStringParseException); | 
|---|
| 282 | CPPUNIT_ASSERT_THROW(Formula formula("(MgCl2]"),FormulaStringParseException); | 
|---|
| 283 | CPPUNIT_ASSERT_THROW(Formula formula("[MgCl2)"),FormulaStringParseException); | 
|---|
| 284 | CPPUNIT_ASSERT_THROW(Formula formula("N(aCl"),FormulaStringParseException); | 
|---|
| 285 | CPPUNIT_ASSERT_THROW(Formula formula("Na()Cl"),FormulaStringParseException); | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 |  | 
|---|
| 289 | } | 
|---|