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().

{
MassAbundance ma(1, 2);
MassAbundance ma2(1, 4);
unit_assert(ma != ma2);
ma2.abundance = 2;
unit_assert(ma == ma2);
}
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().

{
for (int i=0; i < testFormulaDataSize; ++i)
{
Formula formula(testFormula.formula);
const double EPSILON = 0.001;
unit_assert_equal(formula.monoisotopicMass(), testFormula.monoMass, EPSILON);
unit_assert_equal(formula.molecularWeight(), testFormula.avgMass, EPSILON);
if (os_) *os_ << formula << " " << formula.monoisotopicMass() << " " << formula.molecularWeight() << endl;
formula[Element::C] += 2;
unit_assert_equal(formula.monoisotopicMass(), testFormula.monoMass+24, EPSILON);
unit_assert_equal(formula.molecularWeight(), testFormula.avgMass+12.0107*2, EPSILON);
if (os_) *os_ << formula << " " << formula.monoisotopicMass() << " " << formula.molecularWeight() << endl;
//const Formula& constFormula = formula;
//constFormula[Element::C] = 1; // this won't compile, by design
// test copy constructor
Formula formula2 = formula;
formula2[Element::C] -= 2;
unit_assert_equal(formula.monoisotopicMass(), testFormula.monoMass+24, EPSILON);
unit_assert_equal(formula2.monoisotopicMass(), testFormula.monoMass, EPSILON);
if (os_) *os_ << "formula: " << formula << endl;
if (os_) *os_ << "formula2: " << formula2 << endl;
// test operator=
formula = formula2;
unit_assert_equal(formula.monoisotopicMass(), formula2.monoisotopicMass(), EPSILON);
if (os_) *os_ << "formula: " << formula << endl;
if (os_) *os_ << "formula2: " << formula2 << endl;
// test operator==
unit_assert(formula == testFormula.formula); // implicit construction from string
unit_assert(formula == formula2);
formula2[Element::C] += 4; // test difference in CHONSP
unit_assert(formula != formula2);
formula2[Element::C] -= 4;
unit_assert(formula == formula2);
formula2[Element::U] += 2; // test difference outside CHONSP
unit_assert(formula != formula2);
formula2[Element::U] -= 2;
unit_assert(formula == formula2);
// test data()
Formula::Map data = formula.data();
if (os_)
{
*os_ << "map: ";
for (Formula::Map::iterator it=data.begin(), end=data.end(); it!=end; ++it)
*os_ << it->first << it->second << " ";
*os_ << "\n";
}
}
}
void testFormulaOperations ( )

Definition at line 131 of file ChemistryTest.cpp.

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

Referenced by testThreadSafetyWorker().

{
using namespace Element;
Formula water("H2O1");
Formula a("C1 H2 N3 O4 S5");
a *= 2;
unit_assert(a[C]==2 && a[H]==4 && a[N]==6 && a[O]==8 && a[S]==10);
a += water;
unit_assert(a[H]==6 && a[O]==9);
a -= water;
unit_assert(a[C]==2 && a[H]==4 && a[N]==6 && a[O]==8 && a[S]==10);
a += 2*water;
unit_assert(a[H]==8 && a[O]==10);
a = (a - water*2);
unit_assert(a[C]==2 && a[H]==4 && a[N]==6 && a[O]==8 && a[S]==10);
a = water + water;
unit_assert(a[H]==4 && a[O]==2);
if (os_) *os_ << "water: " << a-water << endl;
}
void testInfo ( )

Definition at line 154 of file ChemistryTest.cpp.

References C, Ca, e(), 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().

{
if (os_)
{
*os_ << Element::Info::record(e).symbol << " " << Element::Info::record(e).atomicNumber << endl;
*os_ << endl;
}
unit_assert(Element::Info::record("C").atomicNumber == 6);
unit_assert(Element::Info::record("U").atomicNumber == 92);
unit_assert(Element::Info::record(Element::Uuh).atomicNumber == 116);
unit_assert(Element::Info::record("Uuh").atomicNumber == 116);
runtime_error,
"[chemistry::text2enum()] Error translating symbol foo");
}
void infoExample ( )

Definition at line 176 of file ChemistryTest.cpp.

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

Referenced by testThreadSafetyWorker().

{
if (os_)
{
*os_ << "Sulfur isotopes: " << Element::Info::record(Element::S).isotopes.size() << endl
}
}
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().

{
Formula formula("Si6C12H36O6");
if (os_)
{
*os_ << "polysiloxane:\n"
<< formula << " "
<< formula.monoisotopicMass() << " "
<< formula.molecularWeight() << endl
<< "ion: " << Ion::mz(formula.monoisotopicMass(), 1) << endl;
}
}
void testThreadSafetyWorker ( boost::barrier *  testBarrier)

Definition at line 201 of file ChemistryTest.cpp.

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

{
testBarrier->wait(); // wait until all threads have started
try
{
}
catch (exception& e)
{
cerr << "Exception in worker thread: " << e.what() << endl;
}
catch (...)
{
cerr << "Unhandled exception in worker thread." << endl;
}
}
void testThreadSafety ( const int &  testThreadCount)

Definition at line 224 of file ChemistryTest.cpp.

References testThreadSafetyWorker().

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

Definition at line 234 of file ChemistryTest.cpp.

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

{
TEST_PROLOG(argc, argv)
if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
if (os_) *os_ << "ChemistryTest\n" << setprecision(12);
try
{
//testThreadSafety(1); // does not test thread-safety of singleton initialization
}
catch (exception& e)
{
TEST_FAILED(e.what())
}
catch (...)
{
TEST_FAILED("Caught unknown exception.")
}
}

Variable Documentation

ostream* os_ = 0

Definition at line 38 of file ChemistryTest.cpp.

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().