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

CPPUNIT_TEST_SUITE_REGISTRATION( RunTest );

using namespace hepnos;

void RunTest::setUp() {}

void RunTest::tearDown() {}

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

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
void RunTest::testDescriptor() {
    auto root = datastore->root();
    auto mds = root["matthieu"];
    auto r1 = mds[42];
    CPPUNIT_ASSERT(r1.valid());
    RunDescriptor r1_desc;
    r1.toDescriptor(r1_desc);

    Run r2 = Run::fromDescriptor(*datastore, r1_desc);
    CPPUNIT_ASSERT(r2.valid());

    RunDescriptor invalid_desc;
    Run r3 = Run::fromDescriptor(*datastore, invalid_desc);
    CPPUNIT_ASSERT(!r3.valid());

    Run r4 = Run::fromDescriptor(*datastore, invalid_desc, false);
    CPPUNIT_ASSERT(r4.valid());
}

Matthieu Dorier's avatar
Matthieu Dorier committed
39
void RunTest::testCreateSubRuns() {
40 41
    auto root = datastore->root();
    DataSet mds = root["matthieu"];
Matthieu Dorier's avatar
Matthieu Dorier committed
42
    CPPUNIT_ASSERT(mds.valid());
43
    Run r1 = mds[42];
Matthieu Dorier's avatar
Matthieu Dorier committed
44

45 46 47 48
    SubRun sr0 = r1.createSubRun(0);
    CPPUNIT_ASSERT(sr0.valid());
    CPPUNIT_ASSERT(0 == sr0.number());

Matthieu Dorier's avatar
Matthieu Dorier committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    SubRun sr10 = r1.createSubRun(10);
    CPPUNIT_ASSERT(sr10.valid());
    CPPUNIT_ASSERT(10 == sr10.number());

    SubRun sr23 = r1.createSubRun(23);
    CPPUNIT_ASSERT(sr23.valid());
    CPPUNIT_ASSERT(23 == sr23.number());

    SubRun sr13 = r1.createSubRun(13);
    CPPUNIT_ASSERT(sr13.valid());
    CPPUNIT_ASSERT(13 == sr13.number());

    SubRun sr38 = r1.createSubRun(38);
    CPPUNIT_ASSERT(sr38.valid());
    CPPUNIT_ASSERT(38 == sr38.number());
}

66
void RunTest::testBraketOperator() {
67 68
    auto root = datastore->root();
    DataSet mds = root["matthieu"];
Matthieu Dorier's avatar
Matthieu Dorier committed
69 70
    CPPUNIT_ASSERT(mds.valid());

71
    Run r1 = mds[42];
Matthieu Dorier's avatar
Matthieu Dorier committed
72 73 74
    CPPUNIT_ASSERT(r1.valid());

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

    // check access to existing SubRun
78
    SubRun sr13 = r1[13];
Matthieu Dorier's avatar
Matthieu Dorier committed
79 80 81 82 83
    CPPUNIT_ASSERT(sr13.valid());
    CPPUNIT_ASSERT(13 == sr13.number());
}

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

void RunTest::testBeginEnd() {
109 110
    auto root = datastore->root();
    DataSet mds = root["matthieu"];
Matthieu Dorier's avatar
Matthieu Dorier committed
111
    CPPUNIT_ASSERT(mds.valid());
112
    Run r1 = mds[42];
Matthieu Dorier's avatar
Matthieu Dorier committed
113 114
    CPPUNIT_ASSERT(r1.valid());

115
    std::vector<SubRunNumber> numbers = {0, 10, 13, 23, 38};
Matthieu Dorier's avatar
Matthieu Dorier committed
116 117 118 119 120 121 122 123
    auto it = r1.begin();
    for(int i=0; i < numbers.size(); i++, it++) {
        CPPUNIT_ASSERT_EQUAL(numbers[i], it->number());
    }
    CPPUNIT_ASSERT(it == r1.end());
}

void RunTest::testLowerUpperBounds() {
124 125
    auto root = datastore->root();
    DataSet mds = root["matthieu"];
Matthieu Dorier's avatar
Matthieu Dorier committed
126
    CPPUNIT_ASSERT(mds.valid());
127
    Run r1 = mds[42];
Matthieu Dorier's avatar
Matthieu Dorier committed
128 129
    CPPUNIT_ASSERT(r1.valid());

130 131 132 133 134
    {
        auto it = r1.lower_bound(0);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 0);
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    {
        auto it = r1.lower_bound(13);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 13);
    }
    {
        auto it = r1.lower_bound(14);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 23);
    }
    {
        auto it = r1.lower_bound(12);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 13);
    }
    {
        auto it = r1.lower_bound(40);
        CPPUNIT_ASSERT(!(it->valid()));
        CPPUNIT_ASSERT(it == r1.end());
    }
155 156 157 158 159
    {
        auto it = r1.upper_bound(0);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 10);
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    {
        auto it = r1.upper_bound(13);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 23);
    }
    {
        auto it = r1.upper_bound(14);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 23);
    }
    {
        auto it = r1.upper_bound(12);
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->number() == 13);
    }
    {
        auto it = r1.upper_bound(38);
        CPPUNIT_ASSERT(!(it->valid()));
        CPPUNIT_ASSERT(it == r1.end());
    }
}

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

    for(unsigned i=0; i < 10; i++) {
        SubRun sr = r.createSubRun(async, i);
        CPPUNIT_ASSERT(sr.valid());
    }

    async.wait();

    for(unsigned i=0; i < 10; i++) {
        SubRun sr = r[i];
        CPPUNIT_ASSERT(sr.valid());
    }
}

201
void RunTest::testPrefetcher() {
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    auto root = datastore->root();
    DataSet mds = root.createDataSet("matthieu_prefetch");
    CPPUNIT_ASSERT(mds.valid());
    Run r = mds.createRun(42);
    CPPUNIT_ASSERT(r.valid());

    for(unsigned i=0; i < 20; i++) {
        SubRun sr = r.createSubRun(i);
        CPPUNIT_ASSERT(sr.valid());
    }

    // test begin/end
    {
        Prefetcher prefetcher(*datastore);
        unsigned i=0;
        for(auto it = r.begin(prefetcher); it != r.end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
    // test lower_bound
    {
        Prefetcher prefetcher(*datastore);
        unsigned i=5;
        auto it = r.lower_bound(5);
        for(; it != r.end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
    // test upper_bound
    {
        Prefetcher prefetcher(*datastore);
        unsigned i=6;
        auto it = r.upper_bound(5);
        for(; it != r.end(); it++) {
            CPPUNIT_ASSERT(it->valid());
            CPPUNIT_ASSERT(it->number() == i);
            i += 1;
        }
    }
245
}