ProteoWizard
Classes | Functions | Variables
ChemistryTest.cpp File Reference
#include "pwiz/utility/misc/unit.hpp"
#include "Chemistry.hpp"
#include "Ion.hpp"
#include "pwiz/utility/math/round.hpp"
#include <cstring>
#include "pwiz/utility/misc/Std.hpp"
#include "boost/thread/thread.hpp"
#include "boost/thread/barrier.hpp"

Go to the source code of this file.

Classes

struct  TestFormula
 

Functions

void testMassAbundance ()
 
void testFormula ()
 
void testFormulaOperations ()
 
void testInfo ()
 
void infoExample ()
 
void testPolysiloxane ()
 
void testThreadSafetyWorker (boost::barrier *testBarrier)
 
void testThreadSafety (const int &testThreadCount)
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 
const TestFormula testFormulaData []
 
const int testFormulaDataSize = sizeof(testFormulaData)/sizeof(TestFormula)
 

Function Documentation

void testMassAbundance ( )

Definition at line 41 of file ChemistryTest.cpp.

References pwiz::chemistry::MassAbundance::abundance, and unit_assert.

Referenced by testThreadSafetyWorker().

42 {
43  MassAbundance ma(1, 2);
44  MassAbundance ma2(1, 4);
45  unit_assert(ma != ma2);
46  ma2.abundance = 2;
47  unit_assert(ma == ma2);
48 }
struct for holding isotope information
Definition: Chemistry.hpp:51
#define unit_assert(x)
Definition: unit.hpp:85
void testFormula ( )

Definition at line 71 of file ChemistryTest.cpp.

References TestFormula::avgMass, C, pwiz::chemistry::Formula::data(), TestFormula::formula, pwiz::chemistry::Formula::molecularWeight(), pwiz::chemistry::Formula::monoisotopicMass(), TestFormula::monoMass, os_, testFormulaData, testFormulaDataSize, U, unit_assert, and unit_assert_equal.

Referenced by testThreadSafetyWorker().

72 {
73  for (int i=0; i < testFormulaDataSize; ++i)
74  {
76  Formula formula(testFormula.formula);
77 
78  const double EPSILON = 0.001;
79 
80  unit_assert_equal(formula.monoisotopicMass(), testFormula.monoMass, EPSILON);
81  unit_assert_equal(formula.molecularWeight(), testFormula.avgMass, EPSILON);
82  if (os_) *os_ << formula << " " << formula.monoisotopicMass() << " " << formula.molecularWeight() << endl;
83 
84  formula[Element::C] += 2;
85  unit_assert_equal(formula.monoisotopicMass(), testFormula.monoMass+24, EPSILON);
86  unit_assert_equal(formula.molecularWeight(), testFormula.avgMass+12.0107*2, EPSILON);
87  if (os_) *os_ << formula << " " << formula.monoisotopicMass() << " " << formula.molecularWeight() << endl;
88 
89  //const Formula& constFormula = formula;
90  //constFormula[Element::C] = 1; // this won't compile, by design
91 
92  // test copy constructor
93  Formula formula2 = formula;
94  formula2[Element::C] -= 2;
95  unit_assert_equal(formula.monoisotopicMass(), testFormula.monoMass+24, EPSILON);
96  unit_assert_equal(formula2.monoisotopicMass(), testFormula.monoMass, EPSILON);
97  if (os_) *os_ << "formula: " << formula << endl;
98  if (os_) *os_ << "formula2: " << formula2 << endl;
99 
100  // test operator=
101  formula = formula2;
102  unit_assert_equal(formula.monoisotopicMass(), formula2.monoisotopicMass(), EPSILON);
103  if (os_) *os_ << "formula: " << formula << endl;
104  if (os_) *os_ << "formula2: " << formula2 << endl;
105 
106  // test operator==
107  unit_assert(formula == testFormula.formula); // implicit construction from string
108  unit_assert(formula == formula2);
109  formula2[Element::C] += 4; // test difference in CHONSP
110  unit_assert(formula != formula2);
111  formula2[Element::C] -= 4;
112  unit_assert(formula == formula2);
113  formula2[Element::U] += 2; // test difference outside CHONSP
114  unit_assert(formula != formula2);
115  formula2[Element::U] -= 2;
116  unit_assert(formula == formula2);
117 
118  // test data()
119  Formula::Map data = formula.data();
120  if (os_)
121  {
122  *os_ << "map: ";
123  for (Formula::Map::iterator it=data.begin(), end=data.end(); it!=end; ++it)
124  *os_ << it->first << it->second << " ";
125  *os_ << "\n";
126  }
127  }
128 }
const TestFormula testFormulaData[]
double monoisotopicMass() const
class to represent a chemical formula
Definition: Chemistry.hpp:133
void testFormula()
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:93
const int testFormulaDataSize
U
Definition: Chemistry.hpp:80
ostream * os_
const char * formula
C
Definition: Chemistry.hpp:80
#define unit_assert(x)
Definition: unit.hpp:85
std::map< Element::Type, int > Map
Definition: Chemistry.hpp:153
void testFormulaOperations ( )

Definition at line 131 of file ChemistryTest.cpp.

References C, H, N, O, os_, S, and unit_assert.

Referenced by testThreadSafetyWorker().

132 {
133  using namespace Element;
134 
135  Formula water("H2O1");
136  Formula a("C1 H2 N3 O4 S5");
137 
138  a *= 2;
139  unit_assert(a[C]==2 && a[H]==4 && a[N]==6 && a[O]==8 && a[S]==10);
140  a += water;
141  unit_assert(a[H]==6 && a[O]==9);
142  a -= water;
143  unit_assert(a[C]==2 && a[H]==4 && a[N]==6 && a[O]==8 && a[S]==10);
144  a += 2*water;
145  unit_assert(a[H]==8 && a[O]==10);
146  a = (a - water*2);
147  unit_assert(a[C]==2 && a[H]==4 && a[N]==6 && a[O]==8 && a[S]==10);
148  a = water + water;
149  unit_assert(a[H]==4 && a[O]==2);
150  if (os_) *os_ << "water: " << a-water << endl;
151 }
class to represent a chemical formula
Definition: Chemistry.hpp:133
ostream * os_
N
Definition: Chemistry.hpp:80
H
Definition: Chemistry.hpp:80
S
Definition: Chemistry.hpp:80
O
Definition: Chemistry.hpp:80
C
Definition: Chemistry.hpp:80
#define unit_assert(x)
Definition: unit.hpp:85
void testInfo ( )

Definition at line 154 of file ChemistryTest.cpp.

References C, Ca, H, os_, pwiz::proteome::AminoAcid::Info::record(), pwiz::proteome::AminoAcid::Info::Record::symbol, U, unit_assert, and unit_assert_throws_what.

Referenced by runSpecialTest(), and testThreadSafetyWorker().

155 {
156  if (os_)
157  {
158  for (Element::Type e=Element::H; e<=Element::Ca; e=(Element::Type)(e+1))
159  *os_ << Element::Info::record(e).symbol << " " << Element::Info::record(e).atomicNumber << endl;
160  *os_ << endl;
161  }
162 
163  unit_assert(Element::Info::record(Element::C).atomicNumber == 6);
164  unit_assert(Element::Info::record("C").atomicNumber == 6);
165  unit_assert(Element::Info::record(Element::U).atomicNumber == 92);
166  unit_assert(Element::Info::record("U").atomicNumber == 92);
167  unit_assert(Element::Info::record(Element::Uuh).atomicNumber == 116);
168  unit_assert(Element::Info::record("Uuh").atomicNumber == 116);
169 
171  runtime_error,
172  "[chemistry::text2enum()] Error translating symbol foo");
173 }
#define unit_assert_throws_what(x, exception, whatStr)
Definition: unit.hpp:110
PWIZ_API_DECL const Record & record(Type type)
returns the amino acid's Record by type
U
Definition: Chemistry.hpp:80
ostream * os_
Ca
Definition: Chemistry.hpp:80
H
Definition: Chemistry.hpp:80
pwiz::chemistry::Element::Type Type
C
Definition: Chemistry.hpp:80
#define unit_assert(x)
Definition: unit.hpp:85
void infoExample ( )

Definition at line 176 of file ChemistryTest.cpp.

References os_, pwiz::proteome::AminoAcid::Info::record(), and S.

Referenced by testThreadSafetyWorker().

177 {
178  if (os_)
179  {
180  *os_ << "Sulfur isotopes: " << Element::Info::record(Element::S).isotopes.size() << endl
181  << Element::Info::record(Element::S).isotopes;
182  }
183 }
PWIZ_API_DECL const Record & record(Type type)
returns the amino acid's Record by type
ostream * os_
S
Definition: Chemistry.hpp:80
void testPolysiloxane ( )

Definition at line 186 of file ChemistryTest.cpp.

References pwiz::chemistry::Formula::molecularWeight(), pwiz::chemistry::Formula::monoisotopicMass(), pwiz::chemistry::Ion::mz(), and os_.

Referenced by testThreadSafetyWorker().

187 {
188  Formula formula("Si6C12H36O6");
189 
190  if (os_)
191  {
192  *os_ << "polysiloxane:\n"
193  << formula << " "
194  << formula.monoisotopicMass() << " "
195  << formula.molecularWeight() << endl
196  << "ion: " << Ion::mz(formula.monoisotopicMass(), 1) << endl;
197  }
198 }
class to represent a chemical formula
Definition: Chemistry.hpp:133
ostream * os_
double mz(double neutralMass, int protonDelta, int electronDelta=0, int neutronDelta=0)
Definition: Ion.hpp:78
void testThreadSafetyWorker ( boost::barrier *  testBarrier)

Definition at line 201 of file ChemistryTest.cpp.

References infoExample(), testFormula(), testFormulaOperations(), testInfo(), testMassAbundance(), and testPolysiloxane().

Referenced by testThreadSafety().

202 {
203  testBarrier->wait(); // wait until all threads have started
204 
205  try
206  {
208  testFormula();
210  testInfo();
211  infoExample();
213  }
214  catch (exception& e)
215  {
216  cerr << "Exception in worker thread: " << e.what() << endl;
217  }
218  catch (...)
219  {
220  cerr << "Unhandled exception in worker thread." << endl;
221  }
222 }
void testMassAbundance()
void testFormula()
void testFormulaOperations()
void infoExample()
void testPolysiloxane()
void testInfo()
void testThreadSafety ( const int &  testThreadCount)

Definition at line 224 of file ChemistryTest.cpp.

References testThreadSafetyWorker().

Referenced by main().

225 {
226  boost::barrier testBarrier(testThreadCount);
227  boost::thread_group testThreadGroup;
228  for (int i=0; i < testThreadCount; ++i)
229  testThreadGroup.add_thread(new boost::thread(&testThreadSafetyWorker, &testBarrier));
230  testThreadGroup.join_all();
231 }
void testThreadSafetyWorker(boost::barrier *testBarrier)
int main ( int  argc,
char *  argv[] 
)

Definition at line 234 of file ChemistryTest.cpp.

References os_, TEST_EPILOG, TEST_FAILED, TEST_PROLOG, and testThreadSafety().

235 {
236  TEST_PROLOG(argc, argv)
237 
238  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
239  if (os_) *os_ << "ChemistryTest\n" << setprecision(12);
240 
241  try
242  {
243  //testThreadSafety(1); // does not test thread-safety of singleton initialization
244  testThreadSafety(2);
245  testThreadSafety(4);
246  testThreadSafety(8);
247  testThreadSafety(16);
248  }
249  catch (exception& e)
250  {
251  TEST_FAILED(e.what())
252  }
253  catch (...)
254  {
255  TEST_FAILED("Caught unknown exception.")
256  }
257 
259 }
#define TEST_EPILOG
Definition: unit.hpp:173
ostream * os_
void testThreadSafety(const int &testThreadCount)
#define TEST_FAILED(x)
Definition: unit.hpp:167
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:165

Variable Documentation

ostream* os_ = 0
const TestFormula testFormulaData[]
Initial value:
=
{
{ "C1H2N3O4S5", 1, 2, 3, 4, 5, 279.864884, 280.36928 },
{ "C1H 2N3O4 S5", 1, 2, 3, 4, 5, 279.864884, 280.36928 },
{ "H-42", 0, -42, 0, 0, 0, -42.328651, -42.333512 },
{ "N2C-1", -1, 0, 2, 0, 0, 28.006148-12, 28.013486-12.0107 },
{ "C39H67N11O10", 39, 67, 11, 10, 0, 849.507238, 850.01698 },
{ "C3H7N1O2Se1", 3, 7, 1, 2, 0, 168.9642, 168.0532 }
}

Definition at line 59 of file ChemistryTest.cpp.

Referenced by testFormula().

const int testFormulaDataSize = sizeof(testFormulaData)/sizeof(TestFormula)

Definition at line 69 of file ChemistryTest.cpp.

Referenced by testFormula().