[bcf653] | 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 |
|
---|
[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 |
|
---|
| 37 | void FormulaUnittest::setUp(){}
|
---|
| 38 | void FormulaUnittest::tearDown(){
|
---|
| 39 | World::purgeInstance();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | void FormulaUnittest::baseTest(){
|
---|
| 43 | Formula formula;
|
---|
| 44 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)0);
|
---|
| 45 | CPPUNIT_ASSERT(formula.begin()==formula.end());
|
---|
| 46 |
|
---|
| 47 | // test some one letter element names
|
---|
| 48 | formula += "H";
|
---|
| 49 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1);
|
---|
| 50 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
|
---|
| 51 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 52 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 53 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)1);
|
---|
| 54 | CPPUNIT_ASSERT(formula.begin()!=formula.end());
|
---|
| 55 | formula += "H";
|
---|
| 56 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2);
|
---|
| 57 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
|
---|
| 58 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 59 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 60 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)1);
|
---|
| 61 | CPPUNIT_ASSERT(formula.begin()!=formula.end());
|
---|
| 62 | formula += "O";
|
---|
| 63 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2);
|
---|
| 64 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)1);
|
---|
| 65 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 66 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 67 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)2);
|
---|
| 68 | CPPUNIT_ASSERT(formula.begin()!=formula.end());
|
---|
| 69 |
|
---|
| 70 | // test copy constructor
|
---|
| 71 | Formula formula2=formula;
|
---|
| 72 | CPPUNIT_ASSERT_EQUAL(formula2["H"],(unsigned int)2);
|
---|
| 73 | CPPUNIT_ASSERT_EQUAL(formula2["O"],(unsigned int)1);
|
---|
| 74 | CPPUNIT_ASSERT_EQUAL(formula2["C"],(unsigned int)0);
|
---|
| 75 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 76 | CPPUNIT_ASSERT_EQUAL(formula2.getElementCount(),(unsigned int)2);
|
---|
| 77 | CPPUNIT_ASSERT(formula2.begin()!=formula2.end());
|
---|
| 78 |
|
---|
| 79 | // test Assignment operator
|
---|
| 80 | formula2=Formula();
|
---|
| 81 | CPPUNIT_ASSERT_EQUAL(formula2["H"],(unsigned int)0);
|
---|
| 82 | CPPUNIT_ASSERT_EQUAL(formula2["O"],(unsigned int)0);
|
---|
| 83 | CPPUNIT_ASSERT_EQUAL(formula2["C"],(unsigned int)0);
|
---|
| 84 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 85 | CPPUNIT_ASSERT_EQUAL(formula2.getElementCount(),(unsigned int)0);
|
---|
| 86 | CPPUNIT_ASSERT(formula2.begin()==formula2.end());
|
---|
| 87 |
|
---|
| 88 | // test comparison
|
---|
| 89 | CPPUNIT_ASSERT(formula!=formula2);
|
---|
| 90 | Formula formula3;
|
---|
| 91 | formula3 += "H";
|
---|
| 92 | formula3 += "H";
|
---|
| 93 | formula3 += "O";
|
---|
| 94 | CPPUNIT_ASSERT(formula==formula3);
|
---|
| 95 |
|
---|
[3d27e6] | 96 | // inequality
|
---|
| 97 | formula3 += "O";
|
---|
| 98 | CPPUNIT_ASSERT(formula!=formula3);
|
---|
| 99 |
|
---|
| 100 | // after removing it should be fine again
|
---|
| 101 | formula3 -= "O";
|
---|
| 102 | CPPUNIT_ASSERT(formula==formula3);
|
---|
| 103 |
|
---|
| 104 | // add something from the far end, to test resizing
|
---|
| 105 | formula3 += "Rh";
|
---|
| 106 | CPPUNIT_ASSERT(formula!=formula3);
|
---|
| 107 | formula3 -= "Rh";
|
---|
| 108 | CPPUNIT_ASSERT(formula==formula3);
|
---|
| 109 |
|
---|
[9f632c] | 110 | // removal of elements
|
---|
| 111 | formula -= "H";
|
---|
| 112 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1);
|
---|
| 113 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)1);
|
---|
| 114 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 115 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 116 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)2);
|
---|
| 117 | CPPUNIT_ASSERT(formula.begin()!=formula.end());
|
---|
| 118 |
|
---|
| 119 | formula -= "O";
|
---|
| 120 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1);
|
---|
| 121 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
|
---|
| 122 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 123 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 124 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)1);
|
---|
| 125 | CPPUNIT_ASSERT(formula.begin()!=formula.end());
|
---|
| 126 |
|
---|
| 127 | formula -= "H";
|
---|
| 128 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0);
|
---|
| 129 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
|
---|
| 130 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 131 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 132 | CPPUNIT_ASSERT_EQUAL(formula.getElementCount(),(unsigned int)0);
|
---|
| 133 | CPPUNIT_ASSERT(formula.begin()==formula.end());
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void FormulaUnittest::parseTest(){
|
---|
| 137 | {
|
---|
| 138 | Formula formula("");
|
---|
| 139 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0);
|
---|
| 140 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
|
---|
| 141 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 142 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 143 | }
|
---|
| 144 | {
|
---|
| 145 | Formula formula("H");
|
---|
| 146 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)1);
|
---|
| 147 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
|
---|
| 148 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 149 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 150 | }
|
---|
| 151 | {
|
---|
| 152 | Formula formula("H2");
|
---|
| 153 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2);
|
---|
| 154 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
|
---|
| 155 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 156 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 157 | }
|
---|
| 158 | {
|
---|
| 159 | Formula formula("H2O");
|
---|
| 160 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)2);
|
---|
| 161 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)1);
|
---|
| 162 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 163 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 164 | }
|
---|
| 165 | {
|
---|
| 166 | Formula formula("CH3COOH");
|
---|
| 167 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)4);
|
---|
| 168 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)2);
|
---|
| 169 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)2);
|
---|
| 170 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 171 | }
|
---|
| 172 | {
|
---|
| 173 | Formula formula("CH3COONa");
|
---|
| 174 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)3);
|
---|
| 175 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)2);
|
---|
| 176 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)2);
|
---|
| 177 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)1);
|
---|
| 178 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 179 | }
|
---|
| 180 | {
|
---|
| 181 | Formula formula("NaCH3COO");
|
---|
| 182 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)3);
|
---|
| 183 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)2);
|
---|
| 184 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)2);
|
---|
| 185 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)1);
|
---|
| 186 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 187 | }
|
---|
| 188 | {
|
---|
| 189 | Formula formula("Na2CO3");
|
---|
| 190 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0);
|
---|
| 191 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)3);
|
---|
| 192 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)1);
|
---|
| 193 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)2);
|
---|
| 194 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 195 | }
|
---|
[668e28] | 196 | {
|
---|
| 197 | Formula formula("CH2(COOH)2");
|
---|
| 198 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)4);
|
---|
| 199 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)4);
|
---|
| 200 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)3);
|
---|
| 201 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0);
|
---|
| 202 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 203 | }
|
---|
| 204 | {
|
---|
| 205 | Formula formula("K4[Fe(CN)6]");
|
---|
| 206 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)0);
|
---|
| 207 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)0);
|
---|
| 208 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)6);
|
---|
| 209 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0);
|
---|
| 210 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 211 | CPPUNIT_ASSERT_EQUAL(formula["K"],(unsigned int)4);
|
---|
| 212 | CPPUNIT_ASSERT_EQUAL(formula["Fe"],(unsigned int)1);
|
---|
| 213 | CPPUNIT_ASSERT_EQUAL(formula["N"],(unsigned int)6);
|
---|
| 214 | }
|
---|
| 215 | {
|
---|
| 216 | Formula formula("[CrCl3(H2O)3]");
|
---|
| 217 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)6);
|
---|
| 218 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)3);
|
---|
| 219 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)0);
|
---|
| 220 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0);
|
---|
| 221 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 222 | CPPUNIT_ASSERT_EQUAL(formula["Cr"],(unsigned int)1);
|
---|
| 223 | CPPUNIT_ASSERT_EQUAL(formula["Cl"],(unsigned int)3);
|
---|
| 224 | }
|
---|
| 225 | {
|
---|
| 226 | Formula formula("Mg3[Fe(CN)6]2");
|
---|
| 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)12);
|
---|
| 230 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)0);
|
---|
| 231 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 232 | CPPUNIT_ASSERT_EQUAL(formula["Mg"],(unsigned int)3);
|
---|
| 233 | CPPUNIT_ASSERT_EQUAL(formula["Fe"],(unsigned int)2);
|
---|
| 234 | CPPUNIT_ASSERT_EQUAL(formula["N"],(unsigned int)12);
|
---|
| 235 | }
|
---|
[997b2a] | 236 | {
|
---|
| 237 | Formula formula("Na[Fe((HO2CCH2)2NCH2CH2N(CH2CO2H)2)]");
|
---|
| 238 | CPPUNIT_ASSERT_EQUAL(formula["H"],(unsigned int)16);
|
---|
| 239 | CPPUNIT_ASSERT_EQUAL(formula["O"],(unsigned int)8);
|
---|
| 240 | CPPUNIT_ASSERT_EQUAL(formula["C"],(unsigned int)10);
|
---|
| 241 | CPPUNIT_ASSERT_EQUAL(formula["Na"],(unsigned int)1);
|
---|
| 242 | CPPUNIT_ASSERT_EQUAL(formula["He"],(unsigned int)0);
|
---|
| 243 | CPPUNIT_ASSERT_EQUAL(formula["Mg"],(unsigned int)0);
|
---|
| 244 | CPPUNIT_ASSERT_EQUAL(formula["Fe"],(unsigned int)1);
|
---|
| 245 | CPPUNIT_ASSERT_EQUAL(formula["N"],(unsigned int)2);
|
---|
| 246 | }
|
---|
[9f632c] | 247 | {
|
---|
[266875] | 248 | CPPUNIT_ASSERT_THROW(Formula formula("74107"),FormulaStringParseException);
|
---|
| 249 | CPPUNIT_ASSERT_THROW(Formula formula(" "),FormulaStringParseException);
|
---|
| 250 | CPPUNIT_ASSERT_THROW(Formula formula("nAcL"),FormulaStringParseException);
|
---|
| 251 | CPPUNIT_ASSERT_THROW(Formula formula("NAcL"),FormulaStringParseException);
|
---|
| 252 | CPPUNIT_ASSERT_THROW(Formula formula("Na Cl"),FormulaStringParseException);
|
---|
| 253 | CPPUNIT_ASSERT_THROW(Formula formula("1NaCl"),FormulaStringParseException);
|
---|
| 254 | CPPUNIT_ASSERT_THROW(Formula formula("Mag"),FormulaStringParseException);
|
---|
| 255 | CPPUNIT_ASSERT_THROW(Formula formula("AgCl)"),FormulaStringParseException);
|
---|
| 256 | CPPUNIT_ASSERT_THROW(Formula formula("(Na"),FormulaStringParseException);
|
---|
| 257 | CPPUNIT_ASSERT_THROW(Formula formula("(Mag)"),FormulaStringParseException);
|
---|
| 258 | CPPUNIT_ASSERT_THROW(Formula formula("MgCl2)"),FormulaStringParseException);
|
---|
| 259 | CPPUNIT_ASSERT_THROW(Formula formula("((MgCl2)"),FormulaStringParseException);
|
---|
| 260 | CPPUNIT_ASSERT_THROW(Formula formula("(MgCl2))"),FormulaStringParseException);
|
---|
| 261 | CPPUNIT_ASSERT_THROW(Formula formula("(MgCl2]"),FormulaStringParseException);
|
---|
| 262 | CPPUNIT_ASSERT_THROW(Formula formula("[MgCl2)"),FormulaStringParseException);
|
---|
| 263 | CPPUNIT_ASSERT_THROW(Formula formula("N(aCl"),FormulaStringParseException);
|
---|
| 264 | CPPUNIT_ASSERT_THROW(Formula formula("Na()Cl"),FormulaStringParseException);
|
---|
[9f632c] | 265 | }
|
---|
| 266 |
|
---|
| 267 |
|
---|
| 268 | }
|
---|