From 7c9c9e90e0b87d72382da9c5abcf459775dff717 2018-08-10 14:47:20 From: Alexis Jeandet Date: 2018-08-10 14:47:20 Subject: [PATCH] VariableController2 tests refactoring Signed-off-by: Alexis Jeandet --- diff --git a/include/Variable/Variable.h b/include/Variable/Variable.h index ef0cdf5..6ac5f4d 100644 --- a/include/Variable/Variable.h +++ b/include/Variable/Variable.h @@ -44,7 +44,7 @@ public: /// @return the number of points hold by the variable. The number of points is updated each time /// the data series changes - int nbPoints() const noexcept; + unsigned int nbPoints() const noexcept; /// Returns the real range of the variable, i.e. the min and max x-axis values of the data /// series between the range of the variable. The real range is updated each time the variable diff --git a/src/Variable/Variable.cpp b/src/Variable/Variable.cpp index 06b5397..6a54e18 100644 --- a/src/Variable/Variable.cpp +++ b/src/Variable/Variable.cpp @@ -100,7 +100,7 @@ struct Variable::VariablePrivate { QVariantHash m_Metadata; std::shared_ptr m_DataSeries; DateTimeRange m_RealRange; - int m_NbPoints; + unsigned int m_NbPoints; DataSeriesType m_Type; QReadWriteLock m_Lock; @@ -171,7 +171,7 @@ void Variable::setCacheRange(const DateTimeRange &cacheRange) noexcept impl->unlock(); } -int Variable::nbPoints() const noexcept +unsigned int Variable::nbPoints() const noexcept { return impl->m_NbPoints; } diff --git a/tests/Variable/TestVariableController2.cpp b/tests/Variable/TestVariableController2.cpp index 4235d87..99d3d03 100644 --- a/tests/Variable/TestVariableController2.cpp +++ b/tests/Variable/TestVariableController2.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -8,12 +10,13 @@ #include #include -class FakeProvider: public IDataProvider +template +class SimpleRange: public IDataProvider { public: - FakeProvider() = default; + SimpleRange() = default; - std::shared_ptr clone() const override{ return std::make_shared(); } + std::shared_ptr clone() const override{ return std::make_shared(); } IDataSeries* getData(const DataProviderParameters ¶meters) override { @@ -24,7 +27,7 @@ public: for(auto i = tstart;i +auto sumdiff(T begin, T end) +{ + std::vector diff_vect(end-begin-1); + auto diff = [](auto next,auto item) + { + return next.value() - item.value(); + }; + std::transform (begin+1, end, begin, diff_vect.begin(),diff); + return std::accumulate(diff_vect.cbegin(), diff_vect.cend(), 0); +} + +template +struct RangeType +{ + static void check_properties(std::shared_ptr v, DateTimeRange r) + { + auto s = sumdiff(v->dataSeries()->cbegin(), v->dataSeries()->cend()) / slope; + QCOMPARE(v->nbPoints(), int(s)+1); + QCOMPARE(r.m_TStart, v->dataSeries()->begin()->value()/slope); + } +}; + +template +void check_variable_state(std::shared_ptr v, DateTimeRange r) +{ + QCOMPARE(v->nbPoints(), int(r.delta())); + T::check_properties(v,r); +} + class TestVariableController2 : public QObject { Q_OBJECT @@ -61,7 +95,7 @@ private slots: VariableController2 vc; bool callbackCalled = false; connect(&vc,&VariableController2::variableAdded, [&callbackCalled](std::shared_ptr){callbackCalled=true;}); - auto provider = std::make_shared(); + auto provider = std::make_shared>(); QVERIFY(!callbackCalled); auto var1 = vc.createVariable("var1",{},provider,DateTimeRange()); QVERIFY(SciQLop::containers::contains(vc.variables(), var1)); @@ -73,7 +107,7 @@ private slots: VariableController2 vc; bool callbackCalled = false; connect(&vc,&VariableController2::variableDeleted, [&callbackCalled](std::shared_ptr){callbackCalled=true;}); - auto provider = std::make_shared(); + auto provider = std::make_shared>(); auto var1 = vc.createVariable("var1",{},provider,DateTimeRange()); QVERIFY(SciQLop::containers::contains(vc.variables(), var1)); QVERIFY(!callbackCalled); @@ -85,20 +119,17 @@ private slots: void testGetData() { VariableController2 vc; - auto provider = std::make_shared(); + auto provider = std::make_shared>(); auto range1 = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00), QDate(2018,8,7),QTime(16,00)); auto range2 = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(12,00), QDate(2018,8,7),QTime(18,00)); auto var1 = vc.createVariable("var1", {}, provider, range1); - QCOMPARE(var1->nbPoints(), int(range1.delta())); + check_variable_state>(var1, range1); vc.changeRange(var1, range2); - QCOMPARE(var1->nbPoints(), int(range2.delta())); - + check_variable_state>(var1, range2); } -private: - };