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 | CPPUNIT_ASSERT_THROW(Formula formula("74107"),ParseError);
|
---|
188 | CPPUNIT_ASSERT_THROW(Formula formula(" "),ParseError);
|
---|
189 | CPPUNIT_ASSERT_THROW(Formula formula("nAcL"),ParseError);
|
---|
190 | CPPUNIT_ASSERT_THROW(Formula formula("NAcL"),ParseError);
|
---|
191 | CPPUNIT_ASSERT_THROW(Formula formula("Na Cl"),ParseError);
|
---|
192 | CPPUNIT_ASSERT_THROW(Formula formula("1NaCl"),ParseError);
|
---|
193 | CPPUNIT_ASSERT_THROW(Formula formula("Mag"),ParseError);
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | }
|
---|