From e4699d579435cef7f5765138f97cb1ba050bf83c 2017-09-20 08:19:50 From: Alexandre Leroux Date: 2017-09-20 08:19:50 Subject: [PATCH] Implements unit test (3) Implements validation method and generates last value for CosinusProvider --- diff --git a/plugins/mockplugin/src/CosinusProvider.cpp b/plugins/mockplugin/src/CosinusProvider.cpp index 6ba687f..815e550 100644 --- a/plugins/mockplugin/src/CosinusProvider.cpp +++ b/plugins/mockplugin/src/CosinusProvider.cpp @@ -33,8 +33,9 @@ std::shared_ptr CosinusProvider::retrieveData(QUuid acqIdentifier, std::swap(start, end); } - // Generates scalar series containing cosinus values (one value per second) - auto dataCount = end - start; + // Generates scalar series containing cosinus values (one value per second, end value is + // included) + auto dataCount = end - start + 1; auto xAxisData = std::vector{}; xAxisData.resize(dataCount); @@ -44,7 +45,7 @@ std::shared_ptr CosinusProvider::retrieveData(QUuid acqIdentifier, int progress = 0; auto progressEnd = dataCount; - for (auto time = start; time < end; ++time, ++dataIndex) { + for (auto time = start; time <= end; ++time, ++dataIndex) { auto it = m_VariableToEnableProvider.find(acqIdentifier); if (it != m_VariableToEnableProvider.end() && it.value()) { const auto timeOnFreq = time / freq; diff --git a/plugins/mockplugin/tests/TestCosinusAcquisition.cpp b/plugins/mockplugin/tests/TestCosinusAcquisition.cpp index 5dd490f..4aaefe1 100644 --- a/plugins/mockplugin/tests/TestCosinusAcquisition.cpp +++ b/plugins/mockplugin/tests/TestCosinusAcquisition.cpp @@ -26,6 +26,33 @@ const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss:zzz"); /// Delay after each operation on the variable before validating it (in ms) const auto OPERATION_DELAY = 250; +/** + * Verifies that the data in the candidate series are identical to the data in the reference series + * in a specific range + * @param candidate the candidate data series + * @param range the range to check + * @param reference the reference data series + * @return true if the data of the candidate series and the reference series are identical in the + * range, false otherwise + */ +bool checkDataSeries(std::shared_ptr candidate, const SqpRange &range, + std::shared_ptr reference) +{ + if (candidate == nullptr || reference == nullptr) { + return candidate == reference; + } + + auto referenceIt = reference->xAxisRange(range.m_TStart, range.m_TEnd); + + return std::equal(candidate->cbegin(), candidate->cend(), referenceIt.first, referenceIt.second, + [](const auto &it1, const auto &it2) { + // - milliseconds precision for time + // - 1e-6 precision for value + return std::abs(it1.x() - it2.x()) < 1e-3 + && std::abs(it1.value() - it2.value()) < 1e-6; + }); +} + /// Generates the data series from the reading of a data stream std::shared_ptr readDataStream(QTextStream &stream) { @@ -89,6 +116,16 @@ void TestCosinusAcquisition::testAcquisition() QTextStream dataStream{&dataFile}; auto dataSeries = readDataStream(dataStream); + /// Lambda used to validate a variable at each step + auto validateVariable = [dataSeries](std::shared_ptr variable, + const SqpRange &range) { + // Checks that the variable's range has changed + QCOMPARE(variable->range(), range); + + // Checks the variable's data series + QVERIFY(checkDataSeries(variable->dataSeries(), variable->cacheRange(), dataSeries)); + }; + // Creates variable QFETCH(SqpRange, initialRange); sqpApp->timeController().onTimeToUpdate(initialRange); @@ -96,6 +133,8 @@ void TestCosinusAcquisition::testAcquisition() auto variable = sqpApp->variableController().createVariable("MMS", {}, provider); QTest::qWait(OPERATION_DELAY); + validateVariable(variable, initialRange); + // Makes operations on the variable QFETCH(std::vector, operations); for (const auto &operation : operations) { @@ -104,6 +143,7 @@ void TestCosinusAcquisition::testAcquisition() variable->range(), true); QTest::qWait(OPERATION_DELAY); + validateVariable(variable, operation); } } else {