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