TestVariableController2.cpp
146 lines
| 5.1 KiB
| text/x-c
|
CppLexer
r2 | #include <cmath> | |||
r3 | #include <algorithm> | |||
#include <numeric> | ||||
r0 | #include <QtTest> | |||
#include <QObject> | ||||
r2 | #include <Variable/VariableController2.h> | |||
#include <Data/DateTimeRange.h> | ||||
#include <Data/IDataProvider.h> | ||||
#include <Data/ScalarSeries.h> | ||||
#include <Data/DataProviderParameters.h> | ||||
#include <Common/containers.h> | ||||
r0 | ||||
r5 | #include <TestUtils/TestProviders.h> | |||
r2 | ||||
r5 | #define TEST_VC2_FIXTURE(slope) \ | |||
VariableController2 vc; \ | ||||
auto provider = std::make_shared<SimpleRange<slope>>();\ | ||||
r2 | ||||
r5 | #define TEST_VC2_CREATE_DEFAULT_VAR(name)\ | |||
auto range = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00),\ | ||||
QDate(2018,8,7),QTime(16,00));\ | ||||
auto name = vc.createVariable("name", {}, provider, range);\ | ||||
r30 | while(!vc.isReady(name))QCoreApplication::processEvents();\ | |||
r2 | ||||
r11 | Q_DECLARE_METATYPE(DateTimeRangeTransformation); | |||
r2 | ||||
class TestVariableController2 : public QObject | ||||
r0 | { | |||
Q_OBJECT | ||||
public: | ||||
explicit TestVariableController2(QObject *parent = nullptr) : QObject(parent){} | ||||
signals: | ||||
private slots: | ||||
void initTestCase(){} | ||||
void cleanupTestCase(){} | ||||
r2 | void testCreateVariable() | |||
{ | ||||
r5 | TEST_VC2_FIXTURE(2); | |||
r9 | auto range = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00), | |||
QDate(2018,8,7),QTime(16,00)); | ||||
r2 | bool callbackCalled = false; | |||
connect(&vc,&VariableController2::variableAdded, [&callbackCalled](std::shared_ptr<Variable>){callbackCalled=true;}); | ||||
QVERIFY(!callbackCalled); | ||||
r9 | auto var1 = vc.createVariable("var1", {}, provider, range); | |||
r2 | QVERIFY(SciQLop::containers::contains(vc.variables(), var1)); | |||
QVERIFY(callbackCalled); | ||||
} | ||||
void testDeleteVariable() | ||||
{ | ||||
r5 | TEST_VC2_FIXTURE(1); | |||
r9 | auto range = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00), | |||
QDate(2018,8,7),QTime(16,00)); | ||||
r2 | bool callbackCalled = false; | |||
connect(&vc,&VariableController2::variableDeleted, [&callbackCalled](std::shared_ptr<Variable>){callbackCalled=true;}); | ||||
r9 | auto var1 = vc.createVariable("var1", {}, provider, range); | |||
r30 | while(!vc.isReady(var1))QCoreApplication::processEvents(); | |||
r2 | QVERIFY(SciQLop::containers::contains(vc.variables(), var1)); | |||
QVERIFY(!callbackCalled); | ||||
vc.deleteVariable(var1); | ||||
QVERIFY(!SciQLop::containers::contains(vc.variables(), var1)); | ||||
QVERIFY(callbackCalled); | ||||
} | ||||
void testGetData() | ||||
r0 | { | |||
r5 | TEST_VC2_FIXTURE(10); | |||
TEST_VC2_CREATE_DEFAULT_VAR(var1); | ||||
r4 | check_variable_state<RangeType<10>>(var1, range); | |||
} | ||||
r5 | void testZoom_data() | |||
r4 | { | |||
r5 | QTest::addColumn<double>("zoom"); | |||
QTest::newRow("Zoom IN 10x") << .1; | ||||
QTest::newRow("Zoom OUT 10x") << 10.; | ||||
QTest::newRow("Zoom IN 1x") << 1.; | ||||
} | ||||
void testZoom() | ||||
{ | ||||
TEST_VC2_FIXTURE(100); | ||||
TEST_VC2_CREATE_DEFAULT_VAR(var1); | ||||
check_variable_state<RangeType<100>>(var1, range); | ||||
r4 | ||||
r5 | QFETCH(double, zoom); | |||
range *=zoom; | ||||
r4 | vc.changeRange(var1, range); | |||
r5 | check_variable_state<RangeType<100>>(var1, range); | |||
r4 | } | |||
r5 | void testPan_data() | |||
r4 | { | |||
r5 | QTest::addColumn<double>("pan"); | |||
QTest::newRow("Right 1000 seconds") << 1000.; | ||||
QTest::newRow("Left 1000 seconds") << -1000.; | ||||
QTest::newRow("Right 0.1 seconds") << .1; | ||||
QTest::newRow("Left 0.1 seconds") << -.1; | ||||
} | ||||
void testPan() | ||||
{ | ||||
TEST_VC2_FIXTURE(10); | ||||
TEST_VC2_CREATE_DEFAULT_VAR(var1); | ||||
r4 | check_variable_state<RangeType<10>>(var1, range); | |||
r5 | QFETCH(double, pan); | |||
range += Seconds<double>{pan}; | ||||
r4 | vc.changeRange(var1, range); | |||
check_variable_state<RangeType<10>>(var1, range); | ||||
r0 | } | |||
r11 | void testCache_data() | |||
{ | ||||
QTest::addColumn<DateTimeRangeTransformation>("transformation"); | ||||
QTest::addColumn<int>("expectedIncrement"); | ||||
QTest::newRow("zoom in") << DateTimeRangeTransformation{0.8,Seconds<double>(0.)} << 0; | ||||
QTest::newRow("tiny zoom out") << DateTimeRangeTransformation{1.01,Seconds<double>(0.)} << 0; | ||||
r13 | QTest::newRow("just under cache zoom out") << DateTimeRangeTransformation{2.0/1.1,Seconds<double>(0.)} << 0; | |||
QTest::newRow("just over cache zoom out") << DateTimeRangeTransformation{2.001/1.1, Seconds<double>(0.)} << 2; | ||||
r11 | QTest::newRow("tiny pan left") << DateTimeRangeTransformation{1.,Seconds<double>(-100.)} << 0; | |||
QTest::newRow("tiny pan right") << DateTimeRangeTransformation{1.,Seconds<double>(100.)} << 0; | ||||
} | ||||
void testCache() | ||||
{ | ||||
TEST_VC2_FIXTURE(10); | ||||
TEST_VC2_CREATE_DEFAULT_VAR(var1); | ||||
check_variable_state<RangeType<10>>(var1, range); | ||||
QFETCH(DateTimeRangeTransformation, transformation); | ||||
QFETCH(int, expectedIncrement); | ||||
auto initialCount = provider->callCounter; | ||||
range = range.transform(transformation); | ||||
vc.changeRange(var1, range); | ||||
check_variable_state<RangeType<10>>(var1, range); | ||||
QCOMPARE(provider->callCounter-initialCount, expectedIncrement); | ||||
} | ||||
r0 | }; | |||
QTEST_MAIN(TestVariableController2) | ||||
#include "TestVariableController2.moc" | ||||