Commit 7bc4b87c authored by Matthieu Dorier's avatar Matthieu Dorier

added loadstore test

parent 407030cf
#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);
}
#ifndef __HEPNOS_TEST_LOADSTORE_H
#define __HEPNOS_TEST_LOADSTORE_H
#include <boost/serialization/access.hpp>
#include <boost/serialization/string.hpp>
#include <cppunit/extensions/HelperMacros.h>
#include <hepnos.hpp>
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<typename Archive>
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<typename Archive>
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment