From 849b9c5840f8b5e8a676323ac23be0ac288007de 2017-10-30 16:21:12 From: Alexandre Leroux Date: 2017-10-30 16:21:12 Subject: [PATCH] Unit tests for optional axis --- diff --git a/core/tests/Data/TestOptionalAxis.cpp b/core/tests/Data/TestOptionalAxis.cpp new file mode 100644 index 0000000..ad4959b --- /dev/null +++ b/core/tests/Data/TestOptionalAxis.cpp @@ -0,0 +1,151 @@ +#include +#include + +#include +#include + +Q_DECLARE_METATYPE(OptionalAxis) +Q_DECLARE_METATYPE(Unit) + +class TestOptionalAxis : public QObject { + Q_OBJECT + +private slots: + /// Tests the creation of a undefined axis + void testNotDefinedAxisCtor(); + + /// Tests the creation of a undefined axis + void testDefinedAxisCtor_data(); + void testDefinedAxisCtor(); + + /// Tests @sa OptionalAxis::at() method + void testAt_data(); + void testAt(); + + /// Tests @sa OptionalAxis::size() method + void testSize_data(); + void testSize(); + + /// Tests @sa OptionalAxis::unit() method + void testUnit_data(); + void testUnit(); +}; + +void TestOptionalAxis::testNotDefinedAxisCtor() +{ + OptionalAxis notDefinedAxis{}; + QVERIFY(!notDefinedAxis.isDefined()); +} + +void TestOptionalAxis::testDefinedAxisCtor_data() +{ + QTest::addColumn("noData"); // If set to true, nullptr is passed as data of the axis + QTest::addColumn >( + "data"); // Values assigned to the axis when 'noData' flag is set to false + QTest::addColumn("unit"); // Unit assigned to the axis + + QTest::newRow("validData") << false << std::vector{1, 2, 3} << Unit{"Hz"}; + QTest::newRow("invalidData") << true << std::vector{} << Unit{"Hz"}; +} + +void TestOptionalAxis::testDefinedAxisCtor() +{ + QFETCH(bool, noData); + QFETCH(Unit, unit); + + // When there is no data, we expect that the constructor returns exception + if (noData) { + QVERIFY_EXCEPTION_THROWN(OptionalAxis(nullptr, unit), std::invalid_argument); + } + else { + QFETCH(std::vector, data); + + OptionalAxis axis{std::make_shared >(data), unit}; + QVERIFY(axis.isDefined()); + } +} + +void TestOptionalAxis::testAt_data() +{ + QTest::addColumn("axis"); // Axis used for test case (defined or not) + QTest::addColumn("index"); // Index to test in the axis + QTest::addColumn("expectedValue"); // Expected axis value for the index + + OptionalAxis definedAxis{std::make_shared >(std::vector{1, 2, 3}), + Unit{"Hz"}}; + + QTest::newRow("data1") << definedAxis << 0 << 1.; + QTest::newRow("data2") << definedAxis << 1 << 2.; + QTest::newRow("data3") << definedAxis << 2 << 3.; + QTest::newRow("data4 (index out of bounds)") + << definedAxis << 3 + << std::numeric_limits::quiet_NaN(); // Expects NaN for out of bounds index + QTest::newRow("data5 (index out of bounds)") + << definedAxis << -1 + << std::numeric_limits::quiet_NaN(); // Expects NaN for out of bounds index + QTest::newRow("data6 (axis not defined)") + << OptionalAxis{} << 0 + << std::numeric_limits::quiet_NaN(); // Expects NaN for undefined axis +} + +void TestOptionalAxis::testAt() +{ + QFETCH(OptionalAxis, axis); + QFETCH(int, index); + QFETCH(double, expectedValue); + + auto value = axis.at(index); + QVERIFY((std::isnan(value) && std::isnan(expectedValue)) || value == expectedValue); +} + +void TestOptionalAxis::testSize_data() +{ + QTest::addColumn("axis"); // Axis used for test case (defined or not) + QTest::addColumn("expectedSize"); // Expected number of data in the axis + + // Lambda that creates default defined axis (with the values passed in parameter) + auto axis = [](std::vector values) { + return OptionalAxis{std::make_shared >(std::move(values)), Unit{"Hz"}}; + }; + + QTest::newRow("data1") << axis({}) << 0; + QTest::newRow("data2") << axis({1, 2, 3}) << 3; + QTest::newRow("data3") << axis({1, 2, 3, 4}) << 4; + QTest::newRow("data4 (axis not defined)") + << OptionalAxis{} << 0; // Expects 0 for undefined axis +} + +void TestOptionalAxis::testSize() +{ + QFETCH(OptionalAxis, axis); + QFETCH(int, expectedSize); + + QCOMPARE(axis.size(), expectedSize); +} + +void TestOptionalAxis::testUnit_data() +{ + QTest::addColumn("axis"); // Axis used for test case (defined or not) + QTest::addColumn("expectedUnit"); // Expected unit for the axis + + // Lambda that creates default defined axis (with the unit passed in parameter) + auto axis = [](Unit unit) { + return OptionalAxis{std::make_shared >(std::vector{1, 2, 3}), unit}; + }; + + QTest::newRow("data1") << axis(Unit{"Hz"}) << Unit{"Hz"}; + QTest::newRow("data2") << axis(Unit{"t", true}) << Unit{"t", true}; + QTest::newRow("data3 (axis not defined)") + << OptionalAxis{} << Unit{}; // Expects default unit for undefined axis +} + +void TestOptionalAxis::testUnit() +{ + QFETCH(OptionalAxis, axis); + QFETCH(Unit, expectedUnit); + + QCOMPARE(axis.unit(), expectedUnit); +} + +QTEST_MAIN(TestOptionalAxis) +#include "TestOptionalAxis.moc" diff --git a/core/tests/meson.build b/core/tests/meson.build index 5476049..136abe7 100644 --- a/core/tests/meson.build +++ b/core/tests/meson.build @@ -4,6 +4,7 @@ tests = [ [['Common/TestStringUtils.cpp'],'test_string_utils','StringUtils test'], [['Data/TestDataSeries.cpp'],'test_data','DataSeries test'], [['Data/TestOneDimArrayData.cpp'],'test_1d','One Dim Array test'], + [['Data/TestOptionalAxis.cpp'],'test_optional_axis','OptionalAxis test'], [['Data/TestTwoDimArrayData.cpp'],'test_2d','Two Dim Array test'], [['DataSource/TestDataSourceController.cpp'],'test_data_source','DataSourceController test'], [['Variable/TestVariableCacheController.cpp'],'test_variable_cache','VariableCacheController test'],