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