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