/* * DescriptorUnittest.cpp * * Created on: Feb 9, 2010 * Author: crueger */ #include "DescriptorUnittest.hpp" #include #include #include #include #include #include #include "World.hpp" #include "atom.hpp" // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( DescriptorUnittest ); // set up and tear down void DescriptorUnittest::setUp(){ World::get(); for(int i=0;icreateAtom(); atomIds[i] = atoms[i]->getId(); } } void DescriptorUnittest::tearDown(){ World::destroy(); } // some helper functions bool hasAll(std::vector atoms,int ids[ATOM_COUNT], std::set excluded = std::set()){ for(int i=0;i::iterator iter; bool res=false; for(iter=atoms.begin();iter!=atoms.end();++iter){ res |= (*iter)->getId() == id; } if(!res) { cout << "Atom " << id << " missing in returned list" << endl; return false; } } } return true; } bool hasNoDuplicates(std::vector atoms){ std::set found; std::vector::iterator iter; for(iter=atoms.begin();iter!=atoms.end();++iter){ int id = (*iter)->getId(); if(found.count(id)) return false; found.insert(id); } return true; } void DescriptorUnittest::AtomBaseSetsTest(){ std::vector allAtoms = World::get()->getAllAtoms(AllAtoms()); CPPUNIT_ASSERT_EQUAL( true , hasAll(allAtoms,atomIds)); CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(allAtoms)); std::vector noAtoms = World::get()->getAllAtoms(NoAtoms()); CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty()); } void DescriptorUnittest::AtomIdTest(){ // test Atoms from boundaries and middle of the set atom* testAtom; testAtom = World::get()->getAtom(AtomById(atomIds[0])); CPPUNIT_ASSERT(testAtom); CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId()); testAtom = World::get()->getAtom(AtomById(atomIds[ATOM_COUNT/2])); CPPUNIT_ASSERT(testAtom); CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId()); testAtom = World::get()->getAtom(AtomById(atomIds[ATOM_COUNT-1])); CPPUNIT_ASSERT(testAtom); CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId()); // find some ID that has not been created int outsideId =-1; bool res = false; while(!res) { ++outsideId; res = true; for(int i = 0; i < ATOM_COUNT; ++i){ res &= atomIds[i]!=outsideId; } } // test from outside of set testAtom = World::get()->getAtom(AtomById(outsideId)); CPPUNIT_ASSERT(!testAtom); } void DescriptorUnittest::AtomCalcTest(){ // test some elementary set operations { std::vector testAtoms = World::get()->getAllAtoms(AllAtoms()||NoAtoms()); CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds)); CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms)); } { std::vector testAtoms = World::get()->getAllAtoms(NoAtoms()||AllAtoms()); CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds)); CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms)); } { std::vector testAtoms = World::get()->getAllAtoms(NoAtoms()&&AllAtoms()); CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); } { std::vector testAtoms = World::get()->getAllAtoms(AllAtoms()&&NoAtoms()); CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); } { std::vector testAtoms = World::get()->getAllAtoms(!AllAtoms()); CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); } { std::vector testAtoms = World::get()->getAllAtoms(!NoAtoms()); CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds)); CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms)); } // exclude and include some atoms { std::vector testAtoms = World::get()->getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2]))); std::set excluded; excluded.insert(atomIds[ATOM_COUNT/2]); CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds,excluded)); CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms)); CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size()); } { std::vector testAtoms = World::get()->getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2]))); CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size()); CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId()); } } /********************************************** Main routine **************************************/ int main(int argc, char **argv) { // Get the top level suite from the registry CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); // Adds the test to the list of test to run CppUnit::TextUi::TestRunner runner; runner.addTest( suite ); // Change the default outputter to a compiler error format outputter runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), std::cerr ) ); // Run the tests. bool wasSucessful = runner.run(); // Return error code 1 if the one of test failed. return wasSucessful ? 0 : 1; };