SubRunTest.cpp 7.9 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6 7 8 9 10 11 12
#include "SubRunTest.hpp"
#include "CppUnitAdditionalMacros.hpp"

CPPUNIT_TEST_SUITE_REGISTRATION( SubRunTest );

using namespace hepnos;

void SubRunTest::setUp() {}

void SubRunTest::tearDown() {}

void SubRunTest::testFillDataStore() {
13 14
    auto root = datastore->root();
    auto mds = root.createDataSet("matthieu");
Matthieu Dorier's avatar
Matthieu Dorier committed
15 16 17 18 19 20
    CPPUNIT_ASSERT(mds.valid());
    Run r1 = mds.createRun(42);
    CPPUNIT_ASSERT(r1.valid());
    SubRun sr1 = r1.createSubRun(3);
}

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
void SubRunTest::testDescriptor() {
    auto root = datastore->root();
    auto mds = root["matthieu"];
    auto r1 = mds[42];
    auto sr1 = r1[3];
    CPPUNIT_ASSERT(sr1.valid());
    SubRunDescriptor sr1_desc;
    sr1.toDescriptor(sr1_desc);

    SubRun sr2 = SubRun::fromDescriptor(*datastore, sr1_desc);
    CPPUNIT_ASSERT(sr2.valid());

    SubRunDescriptor invalid_desc;
    SubRun sr3 = SubRun::fromDescriptor(*datastore, invalid_desc);
    CPPUNIT_ASSERT(!sr3.valid());

    SubRun sr4 = SubRun::fromDescriptor(*datastore, invalid_desc, false);
    CPPUNIT_ASSERT(sr4.valid());
}

Matthieu Dorier's avatar
Matthieu Dorier committed
41
void SubRunTest::testCreateEvents() {
42 43
    auto root = datastore->root();
    auto mds = root["matthieu"];
Matthieu Dorier's avatar
Matthieu Dorier committed
44
    CPPUNIT_ASSERT(mds.valid());
45
    Run r1 = mds[42];
Matthieu Dorier's avatar
Matthieu Dorier committed
46
    CPPUNIT_ASSERT(r1.valid());
47
    SubRun sr1 = r1[3];
Matthieu Dorier's avatar
Matthieu Dorier committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    CPPUNIT_ASSERT(sr1.valid());

    Event e10 = sr1.createEvent(10);
    CPPUNIT_ASSERT(e10.valid());
    CPPUNIT_ASSERT(10 == e10.number());

    Event e23 = sr1.createEvent(23);
    CPPUNIT_ASSERT(e23.valid());
    CPPUNIT_ASSERT(23 == e23.number());

    Event e13 = sr1.createEvent(13);
    CPPUNIT_ASSERT(e13.valid());
    CPPUNIT_ASSERT(13 == e13.number());

    Event e38 = sr1.createEvent(38);
    CPPUNIT_ASSERT(e38.valid());
    CPPUNIT_ASSERT(38 == e38.number());
}

67
void SubRunTest::testBraketOperator() {
68 69
    auto root = datastore->root();
    auto mds = root["matthieu"];
Matthieu Dorier's avatar
Matthieu Dorier committed
70
    CPPUNIT_ASSERT(mds.valid());
71
    Run r1 = mds[42];
Matthieu Dorier's avatar
Matthieu Dorier committed
72
    CPPUNIT_ASSERT(r1.valid());
73
    SubRun sr1 = r1[3];
Matthieu Dorier's avatar
Matthieu Dorier committed
74 75 76
    CPPUNIT_ASSERT(sr1.valid());

    // check access to non-existing SubRun
77
    CPPUNIT_ASSERT_THROW(sr1[12], hepnos::Exception);
Matthieu Dorier's avatar
Matthieu Dorier committed
78 79

    // check access to existing SubRun
80
    Event e13 = sr1[13];
Matthieu Dorier's avatar
Matthieu Dorier committed
81 82 83 84 85
    CPPUNIT_ASSERT(e13.valid());
    CPPUNIT_ASSERT(13 == e13.number());
}

void SubRunTest::testFind() {
86 87
    auto root = datastore->root();
    auto mds = root["matthieu"];
Matthieu Dorier's avatar
Matthieu Dorier committed
88
    CPPUNIT_ASSERT(mds.valid());
89
    Run r1 = mds[42];
Matthieu Dorier's avatar
Matthieu Dorier committed
90
    CPPUNIT_ASSERT(r1.valid());
91
    SubRun sr1 = r1[3];
Matthieu Dorier's avatar
Matthieu Dorier committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    CPPUNIT_ASSERT(sr1.valid());
    // test calling find for a SubRun that does not exist
    {
        auto it = sr1.find(12);
        CPPUNIT_ASSERT(it == sr1.end());
        CPPUNIT_ASSERT(!(it->valid()));
    }
    // test calling find for a SubRun that exists
    {
        auto it = sr1.find(13);
        CPPUNIT_ASSERT(it != sr1.end());
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(13 == it->number());
        // test iteration
        ++it;
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(23 == it->number());
    }
}

void SubRunTest::testBeginEnd() {
113 114
    auto root = datastore->root();
    auto mds = root["matthieu"];
Matthieu Dorier's avatar
Matthieu Dorier committed
115
    CPPUNIT_ASSERT(mds.valid());
116
    Run r1 = mds[42];
Matthieu Dorier's avatar
Matthieu Dorier committed
117
    CPPUNIT_ASSERT(r1.valid());
118
    SubRun sr1 = r1[3];
Matthieu Dorier's avatar
Matthieu Dorier committed
119 120 121 122 123 124 125 126 127 128 129
    CPPUNIT_ASSERT(sr1.valid());

    std::vector<EventNumber> numbers = {10, 13, 23, 38};
    auto it = sr1.begin();
    for(int i=0; i < numbers.size(); i++, it++) {
        CPPUNIT_ASSERT_EQUAL(numbers[i], it->number());
    }
    CPPUNIT_ASSERT(it == sr1.end());
}

void SubRunTest::testLowerUpperBounds() {
130 131
    auto root = datastore->root();
    auto mds = root["matthieu"];
Matthieu Dorier's avatar
Matthieu Dorier committed
132
    CPPUNIT_ASSERT(mds.valid());
133
    Run r1 = mds[42];
Matthieu Dorier's avatar
Matthieu Dorier committed
134
    CPPUNIT_ASSERT(r1.valid());
135
    SubRun sr1 = r1[3];
Matthieu Dorier's avatar
Matthieu Dorier committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    CPPUNIT_ASSERT(sr1.valid());

    {
        auto it = sr1.lower_bound(13);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 13);
    }
    {
        auto it = sr1.lower_bound(14);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 23);
    }
    {
        auto it = sr1.lower_bound(12);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 13);
    }
    {
        auto it = sr1.lower_bound(40);
        CPPUNIT_ASSERT(!(it->valid()));
        CPPUNIT_ASSERT(it == sr1.end());
    }
    {
        auto it = sr1.upper_bound(13);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 23);
    }
    {
        auto it = sr1.upper_bound(14);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 23);
    }
    {
        auto it = sr1.upper_bound(12);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 13);
    }
    {
        auto it = sr1.upper_bound(38);
        CPPUNIT_ASSERT(!(it->valid()));
        CPPUNIT_ASSERT(it == sr1.end());
    }
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
void SubRunTest::testAsync() {
    auto root = datastore->root();
    DataSet mds = root.createDataSet("matthieu_async");
    Run r = mds.createRun(1);
    SubRun sr = r.createSubRun(3);
    hepnos::AsyncEngine async(*datastore, 1);

    for(unsigned i=0; i < 10; i++) {
        Event e = sr.createEvent(async, i);
        CPPUNIT_ASSERT(e.valid());
    }

    async.wait();

    for(unsigned i=0; i < 10; i++) {
        Event e = sr[i];
        CPPUNIT_ASSERT(e.valid());
    }
}
199 200

void SubRunTest::testPrefetcher() {
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    auto root = datastore->root();
    DataSet mds = root.createDataSet("matthieu_prefetch");
    CPPUNIT_ASSERT(mds.valid());
    Run r = mds.createRun(42);
    CPPUNIT_ASSERT(r.valid());
    SubRun sr = r.createSubRun(3);
    CPPUNIT_ASSERT(sr.valid());

    for(unsigned i=0; i < 20; i++) {
        Event e = sr.createEvent(i);
        CPPUNIT_ASSERT(e.valid());
    }

    // test begin/end
    {
        Prefetcher prefetcher(*datastore);
        unsigned i=0;
        for(auto it = sr.begin(prefetcher); it != sr.end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
    // test lower_bound
    {
        Prefetcher prefetcher(*datastore);
        unsigned i=5;
228
        auto it = sr.lower_bound(5, prefetcher);
229 230 231 232 233 234 235 236 237 238
        for(; it != sr.end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
    // test upper_bound
    {
        Prefetcher prefetcher(*datastore);
        unsigned i=6;
239
        auto it = sr.upper_bound(5, prefetcher);
240 241 242 243 244 245
        for(; it != sr.end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
246
}
Matthieu Dorier's avatar
Matthieu Dorier committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

void SubRunTest::testAsyncPrefetcher() {
    auto root = datastore->root();
    DataSet mds = root.createDataSet("matthieu_prefetch");
    CPPUNIT_ASSERT(mds.valid());
    Run r = mds.createRun(42);
    CPPUNIT_ASSERT(r.valid());
    SubRun sr = r.createSubRun(3);

    for(unsigned i=0; i < 20; i++) {
        Event e = sr.createEvent(i);
        CPPUNIT_ASSERT(e.valid());
    }
    // test begin/end
    {
        AsyncEngine async(*datastore, 1);
Matthieu Dorier's avatar
Matthieu Dorier committed
263
        Prefetcher prefetcher(async);
Matthieu Dorier's avatar
Matthieu Dorier committed
264 265 266 267 268 269 270 271 272 273 274
        unsigned i=0;
        for(auto it = sr.begin(prefetcher); it != sr.end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
        CPPUNIT_ASSERT_EQUAL(20, (int)i);
    }
    // test lower_bound
    {
        AsyncEngine async(*datastore, 1);
Matthieu Dorier's avatar
Matthieu Dorier committed
275
        Prefetcher prefetcher(async);
Matthieu Dorier's avatar
Matthieu Dorier committed
276 277 278 279 280 281 282 283 284 285 286
        unsigned i=5;
        auto it = sr.lower_bound(5, prefetcher);
        for(; it != sr.end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
        CPPUNIT_ASSERT(i == 20);
    }
    // test upper_bound
    {
287
        AsyncEngine async(*datastore, 2);
Matthieu Dorier's avatar
Matthieu Dorier committed
288
        Prefetcher prefetcher(async);
Matthieu Dorier's avatar
Matthieu Dorier committed
289 290 291 292 293 294 295 296 297 298
        unsigned i=6;
        auto it = sr.upper_bound(5, prefetcher);
        for(; it != sr.end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
        CPPUNIT_ASSERT(i == 20);
    }
}