diff --git a/test/LoadStoreTest.cpp b/test/LoadStoreTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfaeadac3a08e96dc10035d57d985e91c7027bd5 --- /dev/null +++ b/test/LoadStoreTest.cpp @@ -0,0 +1,195 @@ +#include "LoadStoreTest.hpp" +#include "CppUnitAdditionalMacros.hpp" + +CPPUNIT_TEST_SUITE_REGISTRATION( LoadStoreTest ); + +using namespace hepnos; + +void LoadStoreTest::setUp() {} + +void LoadStoreTest::tearDown() {} + +void LoadStoreTest::testFillDataStore() { + + auto mds = datastore->createDataSet("matthieu"); + CPPUNIT_ASSERT(mds.valid()); + Run r1 = mds.createRun(42); + CPPUNIT_ASSERT(r1.valid()); + SubRun sr1 = r1.createSubRun(3); + CPPUNIT_ASSERT(sr1.valid()); + Event ev1 = sr1.createEvent(22); + CPPUNIT_ASSERT(ev1.valid()); +} + +void LoadStoreTest::testLoadStoreDataSet() { + + auto mds = (*datastore)["matthieu"]; + auto run = mds[42]; + auto subrun = run[3]; + auto event = subrun[22]; + CPPUNIT_ASSERT(mds.valid()); + CPPUNIT_ASSERT(run.valid()); + CPPUNIT_ASSERT(subrun.valid()); + CPPUNIT_ASSERT(event.valid()); + + TestObjectA out_obj_a; + out_obj_a.x() = 44; + out_obj_a.y() = 1.2; + TestObjectB out_obj_b; + out_obj_b.a() = 33; + out_obj_b.b() = "you"; + std::string key1 = "mykey"; + + // we can store obj_a + CPPUNIT_ASSERT(mds.store(key1, out_obj_a)); + // we cannot store at that key again something of the same type + TestObjectA tmpa; + CPPUNIT_ASSERT(!mds.store(key1, tmpa)); + // we can store obj_b at the same key because it's not the same type + CPPUNIT_ASSERT(mds.store(key1, out_obj_b)); + + TestObjectA in_obj_a; + TestObjectB in_obj_b; + + std::string key2 = "otherkey"; + // we can't load something at a key that does not exist + CPPUNIT_ASSERT(!mds.load(key2, in_obj_a)); + // we can reload obj_a from key1 + CPPUNIT_ASSERT(mds.load(key1, in_obj_a)); + // and they are the same + CPPUNIT_ASSERT(in_obj_a == out_obj_a); + // we can reload obj_b from key1 + CPPUNIT_ASSERT(mds.load(key1, in_obj_b)); + // and they are the same + CPPUNIT_ASSERT(in_obj_b == out_obj_b); +} + +void LoadStoreTest::testLoadStoreRun() { + + auto mds = (*datastore)["matthieu"]; + auto run = mds[42]; + auto subrun = run[3]; + auto event = subrun[22]; + CPPUNIT_ASSERT(mds.valid()); + CPPUNIT_ASSERT(run.valid()); + CPPUNIT_ASSERT(subrun.valid()); + CPPUNIT_ASSERT(event.valid()); + + TestObjectA out_obj_a; + out_obj_a.x() = 44; + out_obj_a.y() = 1.2; + TestObjectB out_obj_b; + out_obj_b.a() = 33; + out_obj_b.b() = "you"; + std::string key1 = "mykey"; + + // we can store obj_a + CPPUNIT_ASSERT(run.store(key1, out_obj_a)); + // we cannot store at that key again something of the same type + TestObjectA tmpa; + CPPUNIT_ASSERT(!run.store(key1, tmpa)); + // we can store obj_b at the same key because it's not the same type + CPPUNIT_ASSERT(run.store(key1, out_obj_b)); + + TestObjectA in_obj_a; + TestObjectB in_obj_b; + + std::string key2 = "otherkey"; + // we can't load something at a key that does not exist + CPPUNIT_ASSERT(!run.load(key2, in_obj_a)); + // we can reload obj_a from key1 + CPPUNIT_ASSERT(run.load(key1, in_obj_a)); + // and they are the same + CPPUNIT_ASSERT(in_obj_a == out_obj_a); + // we can reload obj_b from key1 + CPPUNIT_ASSERT(run.load(key1, in_obj_b)); + // and they are the same + CPPUNIT_ASSERT(in_obj_b == out_obj_b); +} + +void LoadStoreTest::testLoadStoreSubRun() { + + auto mds = (*datastore)["matthieu"]; + auto run = mds[42]; + auto subrun = run[3]; + auto event = subrun[22]; + CPPUNIT_ASSERT(mds.valid()); + CPPUNIT_ASSERT(run.valid()); + CPPUNIT_ASSERT(subrun.valid()); + CPPUNIT_ASSERT(event.valid()); + + TestObjectA out_obj_a; + out_obj_a.x() = 44; + out_obj_a.y() = 1.2; + TestObjectB out_obj_b; + out_obj_b.a() = 33; + out_obj_b.b() = "you"; + std::string key1 = "mykey"; + + // we can store obj_a + CPPUNIT_ASSERT(subrun.store(key1, out_obj_a)); + // we cannot store at that key again something of the same type + TestObjectA tmpa; + CPPUNIT_ASSERT(!subrun.store(key1, tmpa)); + // we can store obj_b at the same key because it's not the same type + CPPUNIT_ASSERT(subrun.store(key1, out_obj_b)); + + TestObjectA in_obj_a; + TestObjectB in_obj_b; + + std::string key2 = "otherkey"; + // we can't load something at a key that does not exist + CPPUNIT_ASSERT(!subrun.load(key2, in_obj_a)); + // we can reload obj_a from key1 + CPPUNIT_ASSERT(subrun.load(key1, in_obj_a)); + // and they are the same + CPPUNIT_ASSERT(in_obj_a == out_obj_a); + // we can reload obj_b from key1 + CPPUNIT_ASSERT(subrun.load(key1, in_obj_b)); + // and they are the same + CPPUNIT_ASSERT(in_obj_b == out_obj_b); +} + +void LoadStoreTest::testLoadStoreEvent() { + + auto mds = (*datastore)["matthieu"]; + auto run = mds[42]; + auto subrun = run[3]; + auto event = subrun[22]; + CPPUNIT_ASSERT(mds.valid()); + CPPUNIT_ASSERT(run.valid()); + CPPUNIT_ASSERT(subrun.valid()); + CPPUNIT_ASSERT(event.valid()); + + TestObjectA out_obj_a; + out_obj_a.x() = 44; + out_obj_a.y() = 1.2; + TestObjectB out_obj_b; + out_obj_b.a() = 33; + out_obj_b.b() = "you"; + std::string key1 = "mykey"; + + // we can store obj_a + CPPUNIT_ASSERT(event.store(key1, out_obj_a)); + // we cannot store at that key again something of the same type + TestObjectA tmpa; + CPPUNIT_ASSERT(!event.store(key1, tmpa)); + // we can store obj_b at the same key because it's not the same type + CPPUNIT_ASSERT(event.store(key1, out_obj_b)); + + TestObjectA in_obj_a; + TestObjectB in_obj_b; + + std::string key2 = "otherkey"; + // we can't load something at a key that does not exist + CPPUNIT_ASSERT(!event.load(key2, in_obj_a)); + // we can reload obj_a from key1 + CPPUNIT_ASSERT(event.load(key1, in_obj_a)); + // and they are the same + CPPUNIT_ASSERT(in_obj_a == out_obj_a); + // we can reload obj_b from key1 + CPPUNIT_ASSERT(event.load(key1, in_obj_b)); + // and they are the same + CPPUNIT_ASSERT(in_obj_b == out_obj_b); +} + diff --git a/test/LoadStoreTest.hpp b/test/LoadStoreTest.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a794760d96799a1f75adf9a6b398dffb2fc0066c --- /dev/null +++ b/test/LoadStoreTest.hpp @@ -0,0 +1,83 @@ +#ifndef __HEPNOS_TEST_LOADSTORE_H +#define __HEPNOS_TEST_LOADSTORE_H + +#include +#include +#include +#include + +class TestObjectA { + + friend class boost::serialization::access; + + public: + + int& x() { return _x; } + double& y() { return _y; } + + bool operator==(const TestObjectA& other) const { + return _x == other._x && _y == other._y; + } + + private: + + int _x; + double _y; + + template + void serialize(Archive& ar, const unsigned int version) { + ar & _x; + ar & _y; + } +}; + +class TestObjectB { + + friend class boost::serialization::access; + + public: + + int& a() { return _a; } + std::string& b() { return _b; } + + bool operator==(const TestObjectB& other) const { + return _a == other._a && _b == other._b; + } + + private: + + int _a; + std::string _b; + + template + void serialize(Archive& ar, const unsigned int version) { + ar & _a; + ar & _b; + } +}; + +extern hepnos::DataStore* datastore; + +class LoadStoreTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( LoadStoreTest ); + CPPUNIT_TEST( testFillDataStore ); + CPPUNIT_TEST( testLoadStoreDataSet ); + CPPUNIT_TEST( testLoadStoreRun ); + CPPUNIT_TEST( testLoadStoreSubRun ); + CPPUNIT_TEST( testLoadStoreEvent ); + CPPUNIT_TEST_SUITE_END(); + + public: + + void setUp(); + void tearDown(); + + void testFillDataStore(); + void testLoadStoreDataSet(); + void testLoadStoreRun(); + void testLoadStoreSubRun(); + void testLoadStoreEvent(); +}; + +#endif