PtrTest.cpp 4 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1
#include <map>
2
#include <type_traits>
Matthieu Dorier's avatar
Matthieu Dorier committed
3
#include <boost/serialization/map.hpp>
4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "PtrTest.hpp"
#include "CppUnitAdditionalMacros.hpp"
#include "TestObjects.hpp"

CPPUNIT_TEST_SUITE_REGISTRATION( PtrTest );

using namespace hepnos;

void PtrTest::setUp() {}

void PtrTest::tearDown() {}

void PtrTest::testFillDataStore() {

18
    auto mds = datastore->root().createDataSet("matthieu");
19 20 21 22 23 24 25 26 27 28
    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 PtrTest::testMakePtr() {
29 30
    auto root = datastore->root();
    auto mds = root["matthieu"];
31 32 33 34 35 36 37 38
    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());

39 40 41
    CPPUNIT_ASSERT(!std::is_pod<hepnos::Ptr<TestObjectA>>::value);
    CPPUNIT_ASSERT(!std::is_pod<hepnos::Ptr<TestObjectB>>::value);

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    TestObjectA objA;
    objA.x() = 44;
    objA.y() = 1.2;
    TestObjectB objB;
    objB.a() = 33;
    objB.b() = "you";
    std::string key1 = "mykey";

    // we store obj_a
    auto objA_product_id = event.store(key1, objA);
    CPPUNIT_ASSERT(objA_product_id.valid());
    // we store obj_b
    auto objB_product_id = subrun.store(key1, objB);
    CPPUNIT_ASSERT(objB_product_id.valid());

    // we make a pointer to object A
    auto ptrA = datastore->makePtr<TestObjectA>(objA_product_id);
    CPPUNIT_ASSERT(ptrA.valid());

    // we make a pointer to object B
    auto ptrB = datastore->makePtr<TestObjectB>(objB_product_id);
    CPPUNIT_ASSERT(ptrB.valid());

    // store pointer
    std::string keyPtrA = "mypointerA";
    std::string keyPtrB = "mypointerB";
    CPPUNIT_ASSERT(run.store(keyPtrA, ptrA));
    CPPUNIT_ASSERT(run.store(keyPtrB, ptrB));
}

void PtrTest::testPtrLoad() {
73 74
    auto root = datastore->root();
    auto mds = root["matthieu"];
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    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 objA;
    objA.x() = 44;
    objA.y() = 1.2;
    TestObjectB objB;
    objB.a() = 33;
    objB.b() = "you";

    Ptr<TestObjectA> ptrA;
    Ptr<TestObjectB> ptrB;
    std::string keyPtrA = "mypointerA";
    std::string keyPtrB = "mypointerB";

    CPPUNIT_ASSERT(run.load(keyPtrA, ptrA));
    CPPUNIT_ASSERT(run.load(keyPtrB, ptrB));

    CPPUNIT_ASSERT(objA == *ptrA);
    CPPUNIT_ASSERT(objB == *ptrB);
}

Matthieu Dorier's avatar
Matthieu Dorier committed
102
void PtrTest::testPtrLoadFromArray() {
103 104
    auto root = datastore->root();
    auto mds = root["matthieu"];
Matthieu Dorier's avatar
Matthieu Dorier committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    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());

    std::vector<TestObjectA> vecA(3);
    vecA[0].x() = 43;
    vecA[0].y() = 4.3;
    vecA[1].x() = 44;
    vecA[1].y() = 4.4;
    vecA[2].x() = 45;
    vecA[2].y() = 4.5;

    auto prodIDvec = event.store("somekey", vecA);

    auto ptrToVecA = datastore->makePtr<TestObjectA>(prodIDvec,1);
    
    CPPUNIT_ASSERT(vecA[1] == *ptrToVecA);
   
    std::map<unsigned,TestObjectA> mapA;
    mapA[0].x() = 43;
    mapA[0].y() = 4.3;
    mapA[1].x() = 44;
    mapA[1].y() = 4.4;
    mapA[2].x() = 45;
    mapA[2].y() = 4.5;

    auto prodIDmap = event.store("somekey", mapA);

    auto ptrToMapA = datastore->makePtr<TestObjectA,std::map<unsigned,TestObjectA>>(prodIDmap,1);

    CPPUNIT_ASSERT(mapA[1] == *ptrToMapA);
140 141 142 143 144 145 146 147 148 149 150 151

    event.store("somekey", ptrToVecA);
    event.store("somekey", ptrToMapA);

    decltype(ptrToVecA) ptrToVecAReloaded;
    decltype(ptrToMapA) ptrToMapAReloaded;

    event.load("somekey", ptrToVecAReloaded);
    event.load("somekey", ptrToMapAReloaded);

    CPPUNIT_ASSERT(vecA[1] == *ptrToVecAReloaded);
    CPPUNIT_ASSERT(mapA[1] == *ptrToMapAReloaded);
Matthieu Dorier's avatar
Matthieu Dorier committed
152
}