TEST_F(HIST_PERC_Fixture,TestPercentilesMany)
{
    bool failure = false;
    try {
        factory->process("create percentiles aaa with time_window=30 collect_period=10 ps=50:90:99:99.9");
    }
    catch(std::runtime_error& ex) {
        cout << "Failed: " << ex.what() << endl;
        failure = true;
    }
    catch(...) {
        failure = true;
    }
    ASSERT_FALSE(failure);

    Dataset* dataset = Dataset::open(path, CWSE_DATA_ACCESS_READ_ONLY);
    try {
        uint16_t ids[4];
        ids[0] = dataset->findColumn("aaa.50");
        ids[1] = dataset->findColumn("aaa.90");
        ids[2] = dataset->findColumn("aaa.99");
        ids[3] = dataset->findColumn("aaa.99.9");

        for (int i=0; i < 4; i++) {
            ASSERT_TRUE(ids[i] != CWSE_INVALID_COLUMN_ID);
            ASSERT_EQ(0, dataset->get(ids[i]));
        }

        Metric* metric = factory->find("aaa");
        ASSERT_FALSE(metric == NULL);

        Percentiles* p = dynamic_cast<Percentiles*>(metric);
        ASSERT_FALSE(p == NULL);

        p->resetNextTimePoint(10);

        for (int i=1; i <= 100; i++) {
            metric->update(i);
        }

        p->onTimeTick(10);

        ASSERT_EQ(50.5, dataset->get(ids[0]));
        ASSERT_EQ(90.5, dataset->get(ids[1]));
        ASSERT_EQ(99.5, dataset->get(ids[2]));
        ASSERT_EQ(100, dataset->get(ids[3]));
    }
    catch(std::runtime_error& ex) {
        cout << "Failed: " << ex.what() << endl;
        failure = true;
    }
    catch(...) {
        delete dataset;
        ASSERT_TRUE(false);
    }
    delete dataset;
}
TEST_F(HIST_PERC_Fixture,TestPercentilesExample)
{
    bool failure = false;
    try {
        factory->process("create percentiles aaa with time_window=30 collect_period=10 ps=20:50:90");
    }
    catch(std::runtime_error& ex) {
        cout << "Failed: " << ex.what() << endl;
        failure = true;
    }
    catch(...) {
        failure = true;
    }
    ASSERT_FALSE(failure);

    Dataset* dataset = Dataset::open(path, CWSE_DATA_ACCESS_READ_ONLY);
    try {
        uint16_t ids[3];
        ids[0] = dataset->findColumn("aaa.20");
        ids[1] = dataset->findColumn("aaa.50");
        ids[2] = dataset->findColumn("aaa.90");

        for (size_t i=0; i < sizeof(ids)/sizeof(*ids); i++) {
            ASSERT_TRUE(ids[i] != CWSE_INVALID_COLUMN_ID);
            ASSERT_EQ(0, dataset->get(ids[i]));
        }

        Metric* metric = factory->find("aaa");
        ASSERT_FALSE(metric == NULL);

        Percentiles* p = dynamic_cast<Percentiles*>(metric);
        ASSERT_FALSE(p == NULL);

        p->resetNextTimePoint(10);

        double source[] = { 43, 54, 56, 61, 62, 66, 68, 69, 69, 70, 71, 72, 77, 78, 79, 85, 87, 88, 89, 93, 95, 96, 98, 99, 99 };
        for (size_t i=0; i < sizeof(source)/sizeof(*source); i++) {
            metric->update(source[i]);
        }

        p->onTimeTick(10);

        ASSERT_EQ(64, dataset->get(ids[0]));
        ASSERT_EQ(77, dataset->get(ids[1]));
        ASSERT_EQ(98, dataset->get(ids[2]));
    }
    catch(std::runtime_error& ex) {
        cout << "Failed: " << ex.what() << endl;
        failure = true;
    }
    catch(...) {
        delete dataset;
        ASSERT_TRUE(false);
    }
    delete dataset;
}
TEST_F(HIST_PERC_Fixture,TestPercentilesAnotherExample)
{
    bool failure = false;
    try {
        factory->process("create percentiles aaa with time_window=30 collect_period=10 ps=25:85");
    }
    catch(std::runtime_error& ex) {
        cout << "Failed: " << ex.what() << endl;
        failure = true;
    }
    catch(...) {
        failure = true;
    }
    ASSERT_FALSE(failure);

    Dataset* dataset = Dataset::open(path, CWSE_DATA_ACCESS_READ_ONLY);
    try {
        uint16_t ids[2];
        ids[0] = dataset->findColumn("aaa.25");
        ids[1] = dataset->findColumn("aaa.85");

        for (size_t i=0; i < sizeof(ids)/sizeof(*ids); i++) {
            ASSERT_TRUE(ids[i] != CWSE_INVALID_COLUMN_ID);
            ASSERT_EQ(0, dataset->get(ids[i]));
        }

        Metric* metric = factory->find("aaa");
        ASSERT_FALSE(metric == NULL);

        Percentiles* p = dynamic_cast<Percentiles*>(metric);
        ASSERT_FALSE(p == NULL);

        p->resetNextTimePoint(10);

        double source[] = { 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10 };
        for (size_t i=0; i < sizeof(source)/sizeof(*source); i++) {
            metric->update(source[i]);
        }

        p->onTimeTick(10);

        ASSERT_EQ(5, dataset->get(ids[0]));
        ASSERT_TRUE(9.5 == dataset->get(ids[1])) << "  val=" << dataset->get(ids[1]) << endl;
    }
    catch(std::runtime_error& ex) {
        cout << "Failed: " << ex.what() << endl;
        failure = true;
    }
    catch(...) {
        delete dataset;
        ASSERT_TRUE(false);
    }
    delete dataset;
}