##// END OF EJS Templates
Unit tests for optional axis
Alexandre Leroux -
r866:849b9c5840f8
parent child
Show More
@@ -0,0 +1,151
1 #include <Data/ArrayData.h>
2 #include <Data/OptionalAxis.h>
3
4 #include <QObject>
5 #include <QtTest>
6
7 Q_DECLARE_METATYPE(OptionalAxis)
8 Q_DECLARE_METATYPE(Unit)
9
10 class TestOptionalAxis : public QObject {
11 Q_OBJECT
12
13 private slots:
14 /// Tests the creation of a undefined axis
15 void testNotDefinedAxisCtor();
16
17 /// Tests the creation of a undefined axis
18 void testDefinedAxisCtor_data();
19 void testDefinedAxisCtor();
20
21 /// Tests @sa OptionalAxis::at() method
22 void testAt_data();
23 void testAt();
24
25 /// Tests @sa OptionalAxis::size() method
26 void testSize_data();
27 void testSize();
28
29 /// Tests @sa OptionalAxis::unit() method
30 void testUnit_data();
31 void testUnit();
32 };
33
34 void TestOptionalAxis::testNotDefinedAxisCtor()
35 {
36 OptionalAxis notDefinedAxis{};
37 QVERIFY(!notDefinedAxis.isDefined());
38 }
39
40 void TestOptionalAxis::testDefinedAxisCtor_data()
41 {
42 QTest::addColumn<bool>("noData"); // If set to true, nullptr is passed as data of the axis
43 QTest::addColumn<std::vector<double> >(
44 "data"); // Values assigned to the axis when 'noData' flag is set to false
45 QTest::addColumn<Unit>("unit"); // Unit assigned to the axis
46
47 QTest::newRow("validData") << false << std::vector<double>{1, 2, 3} << Unit{"Hz"};
48 QTest::newRow("invalidData") << true << std::vector<double>{} << Unit{"Hz"};
49 }
50
51 void TestOptionalAxis::testDefinedAxisCtor()
52 {
53 QFETCH(bool, noData);
54 QFETCH(Unit, unit);
55
56 // When there is no data, we expect that the constructor returns exception
57 if (noData) {
58 QVERIFY_EXCEPTION_THROWN(OptionalAxis(nullptr, unit), std::invalid_argument);
59 }
60 else {
61 QFETCH(std::vector<double>, data);
62
63 OptionalAxis axis{std::make_shared<ArrayData<1> >(data), unit};
64 QVERIFY(axis.isDefined());
65 }
66 }
67
68 void TestOptionalAxis::testAt_data()
69 {
70 QTest::addColumn<OptionalAxis>("axis"); // Axis used for test case (defined or not)
71 QTest::addColumn<int>("index"); // Index to test in the axis
72 QTest::addColumn<double>("expectedValue"); // Expected axis value for the index
73
74 OptionalAxis definedAxis{std::make_shared<ArrayData<1> >(std::vector<double>{1, 2, 3}),
75 Unit{"Hz"}};
76
77 QTest::newRow("data1") << definedAxis << 0 << 1.;
78 QTest::newRow("data2") << definedAxis << 1 << 2.;
79 QTest::newRow("data3") << definedAxis << 2 << 3.;
80 QTest::newRow("data4 (index out of bounds)")
81 << definedAxis << 3
82 << std::numeric_limits<double>::quiet_NaN(); // Expects NaN for out of bounds index
83 QTest::newRow("data5 (index out of bounds)")
84 << definedAxis << -1
85 << std::numeric_limits<double>::quiet_NaN(); // Expects NaN for out of bounds index
86 QTest::newRow("data6 (axis not defined)")
87 << OptionalAxis{} << 0
88 << std::numeric_limits<double>::quiet_NaN(); // Expects NaN for undefined axis
89 }
90
91 void TestOptionalAxis::testAt()
92 {
93 QFETCH(OptionalAxis, axis);
94 QFETCH(int, index);
95 QFETCH(double, expectedValue);
96
97 auto value = axis.at(index);
98 QVERIFY((std::isnan(value) && std::isnan(expectedValue)) || value == expectedValue);
99 }
100
101 void TestOptionalAxis::testSize_data()
102 {
103 QTest::addColumn<OptionalAxis>("axis"); // Axis used for test case (defined or not)
104 QTest::addColumn<int>("expectedSize"); // Expected number of data in the axis
105
106 // Lambda that creates default defined axis (with the values passed in parameter)
107 auto axis = [](std::vector<double> values) {
108 return OptionalAxis{std::make_shared<ArrayData<1> >(std::move(values)), Unit{"Hz"}};
109 };
110
111 QTest::newRow("data1") << axis({}) << 0;
112 QTest::newRow("data2") << axis({1, 2, 3}) << 3;
113 QTest::newRow("data3") << axis({1, 2, 3, 4}) << 4;
114 QTest::newRow("data4 (axis not defined)")
115 << OptionalAxis{} << 0; // Expects 0 for undefined axis
116 }
117
118 void TestOptionalAxis::testSize()
119 {
120 QFETCH(OptionalAxis, axis);
121 QFETCH(int, expectedSize);
122
123 QCOMPARE(axis.size(), expectedSize);
124 }
125
126 void TestOptionalAxis::testUnit_data()
127 {
128 QTest::addColumn<OptionalAxis>("axis"); // Axis used for test case (defined or not)
129 QTest::addColumn<Unit>("expectedUnit"); // Expected unit for the axis
130
131 // Lambda that creates default defined axis (with the unit passed in parameter)
132 auto axis = [](Unit unit) {
133 return OptionalAxis{std::make_shared<ArrayData<1> >(std::vector<double>{1, 2, 3}), unit};
134 };
135
136 QTest::newRow("data1") << axis(Unit{"Hz"}) << Unit{"Hz"};
137 QTest::newRow("data2") << axis(Unit{"t", true}) << Unit{"t", true};
138 QTest::newRow("data3 (axis not defined)")
139 << OptionalAxis{} << Unit{}; // Expects default unit for undefined axis
140 }
141
142 void TestOptionalAxis::testUnit()
143 {
144 QFETCH(OptionalAxis, axis);
145 QFETCH(Unit, expectedUnit);
146
147 QCOMPARE(axis.unit(), expectedUnit);
148 }
149
150 QTEST_MAIN(TestOptionalAxis)
151 #include "TestOptionalAxis.moc"
@@ -4,6 +4,7 tests = [
4 4 [['Common/TestStringUtils.cpp'],'test_string_utils','StringUtils test'],
5 5 [['Data/TestDataSeries.cpp'],'test_data','DataSeries test'],
6 6 [['Data/TestOneDimArrayData.cpp'],'test_1d','One Dim Array test'],
7 [['Data/TestOptionalAxis.cpp'],'test_optional_axis','OptionalAxis test'],
7 8 [['Data/TestTwoDimArrayData.cpp'],'test_2d','Two Dim Array test'],
8 9 [['DataSource/TestDataSourceController.cpp'],'test_data_source','DataSourceController test'],
9 10 [['Variable/TestVariableCacheController.cpp'],'test_variable_cache','VariableCacheController test'],
General Comments 0
You need to be logged in to leave comments. Login now