DataSetTest.cpp 5.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include "DataSetTest.hpp"
#include "CppUnitAdditionalMacros.hpp"

CPPUNIT_TEST_SUITE_REGISTRATION( DataSetTest );

using namespace hepnos;

void DataSetTest::setUp() {}

void DataSetTest::tearDown() {}

void DataSetTest::testFillDataStore() {

    auto mds = datastore->createDataSet("matthieu");
    datastore->createDataSet("shane");
    datastore->createDataSet("phil");
    datastore->createDataSet("rob");

    // erroneous dataset creations
    // "/" is forbidden in the name, will throw an exception
    CPPUNIT_ASSERT_THROW(
            mds.createDataSet("ds0/AAA"),
            hepnos::Exception);
    // "%" is forbidden in the name, will throw an exception
    CPPUNIT_ASSERT_THROW(
            mds.createDataSet("ds0%RRS"),
            hepnos::Exception);
    // correct dataset creation
    DataSet ds1 = mds.createDataSet("ds1");
    // assert the characteristics of the created dataset
    CPPUNIT_ASSERT(ds1.valid());
    CPPUNIT_ASSERT_EQUAL_STR("ds1", ds1.name());
    CPPUNIT_ASSERT_EQUAL_STR("matthieu", ds1.container());
    CPPUNIT_ASSERT_EQUAL_STR("matthieu/ds1", ds1.fullname());
35 36 37 38 39 40 41 42 43 44 45
    // assert access from DataStore using full path
    DataSet matthieu_ds1 = (*datastore)["matthieu/ds1"];
    CPPUNIT_ASSERT(matthieu_ds1.valid());
    CPPUNIT_ASSERT(matthieu_ds1 == ds1);
    // create a dataset inside ds1
    DataSet ds11 = ds1.createDataSet("ds11");
    CPPUNIT_ASSERT(ds11.valid());
    // access ds11 using path from "matthieu"
    DataSet ds1_ds11 = mds["ds1/ds11"];
    CPPUNIT_ASSERT(ds1_ds11.valid());
    CPPUNIT_ASSERT(ds1_ds11 == ds11);
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 73
    // assert comparison with a default-constructed dataset
    DataSet ds0;
    CPPUNIT_ASSERT(ds0 != ds1);
    CPPUNIT_ASSERT(!(ds0 == ds1));
    // assert that ds1.next() is not valid
    DataSet ds2 = ds1.next();
    CPPUNIT_ASSERT(!ds2.valid());
    // create more datasets
    DataSet ds3 = mds.createDataSet("ds3");
    ds2 = mds.createDataSet("ds2");
    // assert that these are valid
    CPPUNIT_ASSERT(ds2.valid());
    CPPUNIT_ASSERT(ds3.valid());
    // assert that ds1.next() == ds2 and ds2.next() == ds3
    CPPUNIT_ASSERT(ds2 == ds1.next());
    CPPUNIT_ASSERT(ds3 == ds2.next());
    // create more datasets for future tests
    DataSet ds4 = mds.createDataSet("dsB");
    DataSet ds5 = mds.createDataSet("dsD");
    CPPUNIT_ASSERT(ds4.valid());
    CPPUNIT_ASSERT(ds5.valid());
}

void DataSetTest::testBraketOperator() {
    DataSet mds = (*datastore)["matthieu"];
    CPPUNIT_ASSERT(mds.valid());
    // check that accessing a dataset that does not exist
    // yields a non-valid DataSet
74
    CPPUNIT_ASSERT_THROW(mds["ds6"], hepnos::Exception);
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 102 103 104 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 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

    // check that accessing a dataset that exists yields
    // a valid DataSet instance with correct information
    DataSet ds2 = mds["ds2"];
    CPPUNIT_ASSERT(ds2.valid());
    CPPUNIT_ASSERT_EQUAL_STR("ds2", ds2.name());
    CPPUNIT_ASSERT_EQUAL_STR("matthieu", ds2.container());
    CPPUNIT_ASSERT_EQUAL_STR("matthieu/ds2", ds2.fullname());
}

void DataSetTest::testFind() {
    DataSet mds = (*datastore)["matthieu"];
    CPPUNIT_ASSERT(mds.valid());
    // test calling find for a DataSet that does not exist
    {
        auto it = mds.find("ds6");
        CPPUNIT_ASSERT(it == mds.end());
        CPPUNIT_ASSERT(!(it->valid()));
    }
    // test calling find for a DataSet that exists
    {
        auto it = mds.find("ds2");
        CPPUNIT_ASSERT(it != mds.end());
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT_EQUAL_STR("ds2",it->name());
        // test iteration
        ++it;
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT_EQUAL_STR("ds3",it->name());
    }
}

void DataSetTest::testBeginEnd() {
    DataSet mds = (*datastore)["matthieu"];
    CPPUNIT_ASSERT(mds.valid());

    std::vector<std::string> names = {
        "ds1", "ds2", "ds3", "dsB", "dsD"};
    auto it = mds.begin();
    for(int i=0; i < names.size(); i++, it++) {
        CPPUNIT_ASSERT_EQUAL(names[i], it->name());
    }
    CPPUNIT_ASSERT(it == mds.end());
}

void DataSetTest::testLowerUpperBounds() {
    DataSet mds = (*datastore)["matthieu"];
    CPPUNIT_ASSERT(mds.valid());

    {
        auto it = mds.lower_bound("dsB");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsB");
    }
    {
        auto it = mds.lower_bound("dsC");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsD");
    }
    {
        auto it = mds.lower_bound("dsA");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsB");
    }
    {
        auto it = mds.lower_bound("dsE");
        CPPUNIT_ASSERT(!(it->valid()));
        CPPUNIT_ASSERT(it == mds.end());
    }
    {
        auto it = mds.upper_bound("dsB");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsD");
    }
    {
        auto it = mds.upper_bound("dsC");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsD");
    }
    {
        auto it = mds.upper_bound("dsA");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsB");
    }
    {
        auto it = mds.upper_bound("dsD");
        CPPUNIT_ASSERT(!(it->valid()));
        CPPUNIT_ASSERT(it == mds.end());
    }
}

void DataSetTest::testCreateRuns() {
    DataSet mds = (*datastore)["matthieu"];
    CPPUNIT_ASSERT(mds.valid());

    {
171
        CPPUNIT_ASSERT_THROW(mds[45], hepnos::Exception);
172 173 174 175 176 177 178 179 180 181
    }

    {
        Run r = mds.createRun(45);
        CPPUNIT_ASSERT(r.valid());
        CPPUNIT_ASSERT(45 == r.number());
        CPPUNIT_ASSERT_EQUAL_STR("matthieu", r.container());
    }
    
}