DataStoreTest.cpp 4.45 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
#include "DataStoreTest.hpp"
#include "CppUnitAdditionalMacros.hpp"

CPPUNIT_TEST_SUITE_REGISTRATION( DataStoreTest );

using namespace hepnos;

void DataStoreTest::setUp() {}

void DataStoreTest::tearDown() {}

void DataStoreTest::testFillDataStore() {
    // erroneous dataset creations
    // "/" is forbidden in the name, will throw an exception
    CPPUNIT_ASSERT_THROW(
            datastore->createDataSet("ds0/AAA"),
            hepnos::Exception);
    // "%" is forbidden in the name, will throw an exception
    CPPUNIT_ASSERT_THROW(
            datastore->createDataSet("ds0%RRS"),
            hepnos::Exception);
    // correct dataset creation
    DataSet ds1 = datastore->createDataSet("ds1");
    // assert the characteristics of the created dataset
    CPPUNIT_ASSERT(ds1.valid());
    CPPUNIT_ASSERT_EQUAL_STR("ds1", ds1.name());
    CPPUNIT_ASSERT_EQUAL_STR("", ds1.container());
    CPPUNIT_ASSERT_EQUAL_STR("ds1", ds1.fullname());
29
    // assert invalid dataset when it does not exist
30
    CPPUNIT_ASSERT_THROW((*datastore)["invalid"], hepnos::Exception);
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    // 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 = datastore->createDataSet("ds3");
    ds2 = datastore->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 = datastore->createDataSet("dsB");
    DataSet ds5 = datastore->createDataSet("dsD");
    CPPUNIT_ASSERT(ds4.valid());
    CPPUNIT_ASSERT(ds5.valid());
}

void DataStoreTest::testBraketOperator() {
    // check that accessing a dataset that does not exist
56 57
    // throws an exception 
    CPPUNIT_ASSERT_THROW((*datastore)["ds6"], hepnos::Exception);
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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

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

void DataStoreTest::testFind() {
    // test calling find for a DataSet that does not exist
    {
        auto it = datastore->find("ds6");
        CPPUNIT_ASSERT(it == datastore->end());
        CPPUNIT_ASSERT(!(it->valid()));
    }
    // test calling find for a DataSet that exists
    {
        auto it = datastore->find("ds2");
        CPPUNIT_ASSERT(it != datastore->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 DataStoreTest::testBeginEnd() {
    std::vector<std::string> names = {
        "ds1", "ds2", "ds3", "dsB", "dsD"};
    auto it = datastore->begin();
    for(int i=0; i < names.size(); i++, it++) {
        CPPUNIT_ASSERT_EQUAL(names[i], it->name());
    }
    CPPUNIT_ASSERT(it == datastore->end());
}

void DataStoreTest::testLowerUpperBounds() {
    {
        auto it = datastore->lower_bound("dsB");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsB");
    }
    {
        auto it = datastore->lower_bound("dsC");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsD");
    }
    {
        auto it = datastore->lower_bound("dsA");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsB");
    }
    {
        auto it = datastore->lower_bound("dsE");
        CPPUNIT_ASSERT(!(it->valid()));
        CPPUNIT_ASSERT(it == datastore->end());
    }
    {
        auto it = datastore->upper_bound("dsB");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsD");
    }
    {
        auto it = datastore->upper_bound("dsC");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsD");
    }
    {
        auto it = datastore->upper_bound("dsA");
        CPPUNIT_ASSERT(it->valid());
        CPPUNIT_ASSERT(it->name() == "dsB");
    }
    {
        auto it = datastore->upper_bound("dsD");
        CPPUNIT_ASSERT(!(it->valid()));
        CPPUNIT_ASSERT(it == datastore->end());
    }
}