RunSetTest.cpp 8.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "RunSetTest.hpp"
#include "CppUnitAdditionalMacros.hpp"

CPPUNIT_TEST_SUITE_REGISTRATION( RunSetTest );

using namespace hepnos;

void RunSetTest::setUp() {}

void RunSetTest::tearDown() {}

void RunSetTest::testFillDataStore() {
13 14
    auto root = datastore->root();
    auto mds = root.createDataSet("matthieu");
15 16 17 18 19 20 21 22
    CPPUNIT_ASSERT(mds.valid());
    // erroneous run creation
    CPPUNIT_ASSERT_THROW(
            mds.createRun(InvalidRunNumber),
            hepnos::Exception);
    // default-constructed run has InvalidRunNumber number
    Run r0;
    CPPUNIT_ASSERT(!r0.valid());
23
    CPPUNIT_ASSERT_THROW(r0.number(), hepnos::Exception);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    // correct run creation
    Run r1 = mds.createRun(42);
    // assert the characteristics of the created dataset
    CPPUNIT_ASSERT(r1.valid());
    CPPUNIT_ASSERT(42 == r1.number());
    // assert comparison with a default-constructed run
    CPPUNIT_ASSERT(r0 != r1);
    CPPUNIT_ASSERT(!(r0 == r1));
    // assert that r1.next() is not valid
    Run r2 = r1.next();
    CPPUNIT_ASSERT(!r2.valid());
    // create more runs
    Run r3 = mds.createRun(47);
    r2 = mds.createRun(45);
    // assert that these are valid
    CPPUNIT_ASSERT(r2.valid());
    CPPUNIT_ASSERT(r3.valid());
    // assert that r1.next() == r2 and r2.next() == r3
    CPPUNIT_ASSERT(r2 == r1.next());
    CPPUNIT_ASSERT(r3 == r2.next());
    // create more datasets for future tests
    Run r4 = mds.createRun(53);
    Run r5 = mds.createRun(59);
    CPPUNIT_ASSERT(r4.valid());
    CPPUNIT_ASSERT(r5.valid());
}

51
void RunSetTest::testBraketOperator() {
52 53
    auto root = datastore->root();
    DataSet mds = root["matthieu"];
54 55 56
    CPPUNIT_ASSERT(mds.valid());
    // check that accessing a Run that does not exist
    // yields a non-valid Run instance
57
    CPPUNIT_ASSERT_THROW(mds[43], hepnos::Exception);
58 59 60

    // check that accessing a run that exists yields
    // a valid Run instance with correct information
61
    Run r2 = mds[45];
62 63 64 65 66
    CPPUNIT_ASSERT(r2.valid());
    CPPUNIT_ASSERT(45 == r2.number());

    // check that we access the same Run using the runs() function
    // to go through the RunSet
67
    Run r22 = mds.runs()[45];
68 69 70 71
    CPPUNIT_ASSERT(r2 == r22);
}

void RunSetTest::testFind() {
72 73
    auto root = datastore->root();
    DataSet mds = root["matthieu"];
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    CPPUNIT_ASSERT(mds.valid());
    // test calling find for a Run that does not exist
    {
        auto it = mds.runs().find(43);
        CPPUNIT_ASSERT(it == mds.runs().end());
        CPPUNIT_ASSERT(!(it->valid()));
    }
    // test calling find for a Run that exists
    {
        auto it = mds.runs().find(45);
        CPPUNIT_ASSERT(it != mds.runs().end());
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(45 == it->number());
        // test iteration
        ++it;
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(47 == it->number());
    }
}

void RunSetTest::testBeginEnd() {
95 96
    auto root = datastore->root();
    DataSet mds = root["matthieu"];
97 98 99 100 101 102 103 104 105 106 107
    CPPUNIT_ASSERT(mds.valid());

    std::vector<RunNumber> numbers = {42, 45, 47, 53, 59};
    auto it = mds.runs().begin();
    for(int i=0; i < numbers.size(); i++, it++) {
        CPPUNIT_ASSERT_EQUAL(numbers[i], it->number());
    }
    CPPUNIT_ASSERT(it == mds.runs().end());
}

void RunSetTest::testLowerUpperBounds() {
108 109
    auto root = datastore->root();
    DataSet mds = root["matthieu"];
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    CPPUNIT_ASSERT(mds.valid());

    {
        auto it = mds.runs().lower_bound(47);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 47);
    }
    {
        auto it = mds.runs().lower_bound(48);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 53);
    }
    {
        auto it = mds.runs().lower_bound(46);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 47);
    }
    {
        auto it = mds.runs().lower_bound(60);
        CPPUNIT_ASSERT(!(it->valid()));
        CPPUNIT_ASSERT(it == mds.runs().end());
    }
    {
        auto it = mds.runs().upper_bound(47);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 53);
    }
    {
        auto it = mds.runs().upper_bound(48);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 53);
    }
    {
        auto it = mds.runs().upper_bound(46);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 47);
    }
    {
        auto it = mds.runs().upper_bound(59);
        CPPUNIT_ASSERT(!(it->valid()));
        CPPUNIT_ASSERT(it == mds.runs().end());
    }
}

void RunSetTest::testCreateSubRuns() {
155 156
    auto root = datastore->root();
    DataSet mds = root["matthieu"];
157
    CPPUNIT_ASSERT(mds.valid());
158
    Run r2 = mds[45];
159 160

    {
161
        CPPUNIT_ASSERT_THROW(r2[73], hepnos::Exception);
162 163 164 165 166 167 168 169
    }

    {
        SubRun sr = r2.createSubRun(73);
        CPPUNIT_ASSERT(sr.valid());
        CPPUNIT_ASSERT(73 == sr.number());
    }
}
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

void RunSetTest::testAsync() {
    auto root = datastore->root();
    DataSet mds = root.createDataSet("matthieu_async");
    CPPUNIT_ASSERT(mds.valid());
    hepnos::AsyncEngine async(*datastore, 1);
    
    for(unsigned i=0; i < 10; i++) {
        Run r = mds.createRun(async, i);
        CPPUNIT_ASSERT(r.valid());
    }

    async.wait();

    for(unsigned i=0; i < 10; i++) {
        CPPUNIT_ASSERT(mds[i].valid());
    }
}
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

void RunSetTest::testPrefetcher() {
    auto root = datastore->root();
    DataSet mds = root.createDataSet("matthieu_prefetch");
    CPPUNIT_ASSERT(mds.valid());
    
    for(unsigned i=0; i < 20; i++) {
        Run r = mds.createRun(i);
        CPPUNIT_ASSERT(r.valid());
    }

    // test begin/end
    {
        Prefetcher prefetcher(*datastore);
        unsigned i=0;
        for(auto it = mds.runs().begin(prefetcher); it != mds.runs().end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
209 210 211 212 213 214 215 216 217 218
    // test begin/end using a Prefetchable object
    {
        Prefetcher prefetch(*datastore);
        unsigned i=0;
        for(auto& r : prefetch(mds.runs())) {
            CPPUNIT_ASSERT(r.valid());
            CPPUNIT_ASSERT(r.number() == i);
            i += 1;
        }
    }
219 220 221 222
    // test lower_bound
    {
        Prefetcher prefetcher(*datastore);
        unsigned i=5;
223
        auto it = mds.runs().lower_bound(5, prefetcher);
224 225 226 227 228 229
        for(; it != mds.runs().end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
230
    // test upper_bound
231 232 233
    {
        Prefetcher prefetcher(*datastore);
        unsigned i=6;
234
        auto it = mds.runs().upper_bound(5, prefetcher);
235 236 237 238 239 240 241
        for(; it != mds.runs().end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
}
Matthieu Dorier's avatar
Matthieu Dorier committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

void RunSetTest::testAsyncPrefetcher() {
    auto root = datastore->root();
    DataSet mds = root.createDataSet("matthieu_prefetch");
    CPPUNIT_ASSERT(mds.valid());
    
    for(unsigned i=0; i < 20; i++) {
        Run r = mds.createRun(i);
        CPPUNIT_ASSERT(r.valid());
    }

    // test begin/end
    {
        AsyncEngine async(*datastore, 1);
        Prefetcher prefetcher(*datastore, async);
        unsigned i=0;
        for(auto it = mds.runs().begin(prefetcher); it != mds.runs().end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
    // test begin/end using a Prefetchable object
    {
        AsyncEngine async(*datastore, 1);
        Prefetcher prefetch(*datastore, async);
        unsigned i=0;
        for(auto& r : prefetch(mds.runs())) {
            CPPUNIT_ASSERT(r.valid());
            CPPUNIT_ASSERT(r.number() == i);
            i += 1;
        }
    }
    // test lower_bound
    {
        AsyncEngine async(*datastore, 1);
        Prefetcher prefetcher(*datastore, async);
        unsigned i=5;
        auto it = mds.runs().lower_bound(5, prefetcher);
        for(; it != mds.runs().end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
    // test upper_bound
    {
        AsyncEngine async(*datastore, 1);
        Prefetcher prefetcher(*datastore, async);
        unsigned i=6;
        auto it = mds.runs().upper_bound(5, prefetcher);
        for(; it != mds.runs().end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
}