From a3c02fe3bfd0b5fa761c70d19ec04561a13349f0 2017-10-30 16:31:16 From: Alexandre Leroux Date: 2017-10-30 16:31:16 Subject: [PATCH] Unit tests (3) Adds unit tests of the main methods of SpectrogramSeries --- diff --git a/core/tests/Data/TestSpectrogramSeries.cpp b/core/tests/Data/TestSpectrogramSeries.cpp new file mode 100644 index 0000000..8954231 --- /dev/null +++ b/core/tests/Data/TestSpectrogramSeries.cpp @@ -0,0 +1,199 @@ +#include "Data/SpectrogramSeries.h" + +#include "DataSeriesBuilders.h" +#include "DataSeriesUtils.h" + +#include +#include + +namespace { + +// Aliases used to facilitate reading of test inputs +using X = DataContainer; +using Y = DataContainer; +using Values = DataContainer; +using Components = std::vector; + +} // namespace + +/** + * @brief The TestSpectrogramSeries class defines unit tests on spectrogram series. + * + * Most of these unit tests use generic tests defined for DataSeries (@sa DataSeriesUtils) + */ +class TestSpectrogramSeries : public QObject { + Q_OBJECT +private slots: + + /// Tests construction of a spectrogram series + void testCtor_data(); + void testCtor(); + + /// Tests merge of two spectrogram series + void testMerge_data(); + void testMerge(); + + /// @todo ALX: test subdataseries + /// Tests get subdata of a spectrogram series + void testSubDataSeries_data(); + void testSubDataSeries(); +}; + +void TestSpectrogramSeries::testCtor_data() +{ + // x-axis data + QTest::addColumn("xAxisData"); + // y-axis data + QTest::addColumn("yAxisData"); + // values data + QTest::addColumn("valuesData"); + + // construction expected to be valid + QTest::addColumn("expectOK"); + // expected x-axis data (when construction is valid) + QTest::addColumn("expectedXAxisData"); + // expected components data (when construction is valid) + QTest::addColumn("expectedComponentsData"); + + QTest::newRow( + "invalidData (number of values by component aren't equal to the number of x-axis data)") + << X{1., 2., 3., 4., 5.} << Y{1., 2., 3.} << Values{1., 2., 3.} << false << X{} + << Components{}; + + QTest::newRow("invalidData (number of components aren't equal to the number of y-axis data)") + << X{1., 2., 3., 4., 5.} << Y{1., 2.} // 2 y-axis data + << Values{1., 2., 3., 4., 5.} // 1 component + << false << X{} << Components{}; + + QTest::newRow("sortedData") << X{1., 2., 3., 4., 5.} << Y{1., 2.} // 2 y-axis data + << Values{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} // 2 components + << true << X{1., 2., 3., 4., 5.} + << Components{{1., 3., 5., 7., 9.}, {2., 4., 6., 8., 10.}}; + + QTest::newRow("unsortedData") << X{5., 4., 3., 2., 1.} << Y{1., 2.} + << Values{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} << true + << X{1., 2., 3., 4., 5.} + << Components{{9., 7., 5., 3., 1.}, {10., 8., 6., 4., 2.}}; +} + +void TestSpectrogramSeries::testCtor() +{ + // Creates series + QFETCH(X, xAxisData); + QFETCH(Y, yAxisData); + QFETCH(Values, valuesData); + QFETCH(bool, expectOK); + + if (expectOK) { + auto series = SpectrogramBuilder{} + .setX(std::move(xAxisData)) + .setY(std::move(yAxisData)) + .setValues(std::move(valuesData)) + .build(); + + // Validates results + QFETCH(X, expectedXAxisData); + QFETCH(Components, expectedComponentsData); + validateRange(series->cbegin(), series->cend(), expectedXAxisData, expectedComponentsData); + } + else { + QVERIFY_EXCEPTION_THROWN(SpectrogramBuilder{} + .setX(std::move(xAxisData)) + .setY(std::move(yAxisData)) + .setValues(std::move(valuesData)) + .build(), + std::invalid_argument); + } +} + +void TestSpectrogramSeries::testMerge_data() +{ + testMerge_struct(); + + QTest::newRow("sortedMerge") << SpectrogramBuilder{} + .setX({1., 2., 3.}) + .setY({1., 2.}) + .setValues({10., 11., 20., 21., 30., 31}) + .build() + << SpectrogramBuilder{} + .setX({4., 5., 6.}) + .setY({1., 2.}) + .setValues({40., 41., 50., 51., 60., 61}) + .build() + << DataContainer{1., 2., 3., 4., 5., 6.} + << Components{{10., 20., 30., 40., 50., 60.}, + {11., 21., 31., 41., 51., 61}}; + + QTest::newRow( + "unsortedMerge (merge not made because the two data series have different y-axes)") + << SpectrogramBuilder{} + .setX({4., 5., 6.}) + .setY({1., 2.}) + .setValues({40., 41., 50., 51., 60., 61}) + .build() + << SpectrogramBuilder{} + .setX({1., 2., 3.}) + .setY({3., 4.}) + .setValues({10., 11., 20., 21., 30., 31}) + .build() + << DataContainer{4., 5., 6.} << Components{{40., 50., 60.}, {41., 51., 61}}; + + QTest::newRow( + "unsortedMerge (unsortedMerge (merge is made because the two data series have the same " + "y-axis)") + << SpectrogramBuilder{} + .setX({4., 5., 6.}) + .setY({1., 2.}) + .setValues({40., 41., 50., 51., 60., 61}) + .build() + << SpectrogramBuilder{} + .setX({1., 2., 3.}) + .setY({1., 2.}) + .setValues({10., 11., 20., 21., 30., 31}) + .build() + << DataContainer{1., 2., 3., 4., 5., 6.} + << Components{{10., 20., 30., 40., 50., 60.}, {11., 21., 31., 41., 51., 61}}; +} + +void TestSpectrogramSeries::testMerge() +{ + testMerge_t(); +} + +void TestSpectrogramSeries::testSubDataSeries_data() +{ + testSubDataSeries_struct(); + + QTest::newRow("subDataSeries (the range includes all data)") + << SpectrogramBuilder{} + .setX({1., 2., 3.}) + .setY({1., 2.}) + .setValues({10., 11., 20., 21., 30., 31}) + .build() + << SqpRange{0., 5.} << DataContainer{1., 2., 3.} + << Components{{10., 20., 30.}, {11., 21., 31.}}; + + QTest::newRow("subDataSeries (the range includes no data)") + << SpectrogramBuilder{} + .setX({1., 2., 3.}) + .setY({1., 2.}) + .setValues({10., 11., 20., 21., 30., 31}) + .build() + << SqpRange{4., 5.} << DataContainer{} << Components{{}, {}}; + + QTest::newRow("subDataSeries (the range includes some data)") + << SpectrogramBuilder{} + .setX({1., 2., 3.}) + .setY({1., 2.}) + .setValues({10., 11., 20., 21., 30., 31}) + .build() + << SqpRange{1.1, 3} << DataContainer{2., 3.} << Components{{20., 30.}, {21., 31.}}; +} + +void TestSpectrogramSeries::testSubDataSeries() +{ + testSubDataSeries_t(); +} + +QTEST_MAIN(TestSpectrogramSeries) +#include "TestSpectrogramSeries.moc" diff --git a/core/tests/meson.build b/core/tests/meson.build index 015b1b7..6847e97 100644 --- a/core/tests/meson.build +++ b/core/tests/meson.build @@ -3,6 +3,7 @@ tests = [ [['Common/TestStringUtils.cpp'],'test_string_utils','StringUtils test'], [['Data/TestScalarSeries.cpp'],'test_scalar','ScalarSeries test'], + [['Data/TestSpectrogramSeries.cpp'],'test_spectrogram','SpectrogramSeries test'], [['Data/TestVectorSeries.cpp'],'test_vector','VectorSeries test'], [['Data/TestOneDimArrayData.cpp'],'test_1d','One Dim Array test'], [['Data/TestOptionalAxis.cpp'],'test_optional_axis','OptionalAxis test'],