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