A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
val-array-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Biljana Bojovic <bbojovic@cttc.es>
7 */
8
9#include "ns3/log.h"
10#include "ns3/test.h"
11#include "ns3/val-array.h"
12
13/**
14 * @defgroup valArray-tests ValArray tests
15 * @ingroup core-tests
16 * @ingroup Matrices
17 */
18
19/**
20 * @file
21 * @ingroup valArray-tests
22 * ValArray test suite
23 */
24
25/**
26 * @file
27 * @ingroup core-tests
28 */
29
30namespace ns3
31{
32namespace tests
33{
34
35NS_LOG_COMPONENT_DEFINE("ValArrayTest");
36
37/**
38 * @ingroup valArray-tests
39 *
40 * @brief ValArray test case for testing ValArray class
41 *
42 * @tparam T the template parameter that can be a complex number, double or int
43 */
44template <class T>
46{
47 public:
48 /** Default constructor*/
49 ValArrayTestCase() = default;
50 /**
51 * Constructor
52 *
53 * @param [in] name reference name
54 */
55 ValArrayTestCase(const std::string& name);
56
57 /** Destructor. */
58 ~ValArrayTestCase() override;
59 /**
60 * @brief Copy constructor.
61 * Instruct the compiler to generate the implicitly declared copy constructor
62 */
64 /**
65 * @brief Copy assignment operator.
66 * Instruct the compiler to generate the implicitly declared copy assignment operator.
67 * @return A reference to this ValArrayTestCase
68 */
70 /**
71 * @brief Move constructor.
72 * Instruct the compiler to generate the implicitly declared move constructor
73 */
75 /**
76 * @brief Move assignment operator.
77 * Instruct the compiler to generate the implicitly declared copy constructor
78 * @return A reference to this ValArrayTestCase
79 */
81
82 private:
83 void DoRun() override;
84};
85
86template <class T>
88 : TestCase(name)
89{
90}
91
92template <class T>
96
97template <class T>
98void
100{
101 ValArray<T> v1 = ValArray<T>(2, 3);
102 for (size_t i = 0; i < v1.GetNumRows(); ++i)
103 {
104 for (size_t j = 0; j < v1.GetNumCols(); ++j)
105 {
106 v1(i, j) = 1;
107 }
108 }
109
110 ValArray<T> v2 = ValArray<T>(v1);
111 NS_TEST_ASSERT_MSG_EQ(v1.GetNumRows(), v2.GetNumRows(), "The number of rows are not equal.");
112 NS_TEST_ASSERT_MSG_EQ(v1.GetNumCols(), v2.GetNumCols(), "The number of cols are not equal.");
113
114 // test copy constructor
115 for (size_t i = 0; i < v1.GetNumRows(); ++i)
116 {
117 for (size_t j = 0; j < v1.GetNumCols(); ++j)
118 {
119 NS_TEST_ASSERT_MSG_EQ(v1(i, j), v2(i, j), "The elements are not equal.");
120 }
121 }
122
123 // test assign constructor
124 ValArray<T> v3 = v1;
125 NS_TEST_ASSERT_MSG_EQ(v1.GetNumRows(), v3.GetNumRows(), "The number of rows are not equal.");
126 NS_TEST_ASSERT_MSG_EQ(v1.GetNumCols(), v3.GetNumCols(), "The number of cols are not equal.");
127 for (size_t i = 0; i < v1.GetNumRows(); ++i)
128 {
129 for (size_t j = 0; j < v1.GetNumCols(); ++j)
130 {
131 NS_TEST_ASSERT_MSG_EQ(v1(i, j), v2(i, j), "The elements are not equal.");
132 }
133 }
134
135 // test move assignment operator
136 ValArray<T> v4;
137 NS_LOG_INFO("v1 size before move: " << v1.GetSize());
138 NS_LOG_INFO("v4 size before move: " << v4.GetSize());
139 size_t v1size = v1.GetSize();
140 v4 = std::move(v1);
141 NS_LOG_INFO("v4 size after move: " << v4.GetSize());
142 NS_TEST_ASSERT_MSG_EQ(v1size, v4.GetSize(), "The number of elements are not equal.");
143 for (size_t i = 0; i < v4.GetNumRows(); ++i)
144 {
145 for (size_t j = 0; j < v4.GetNumCols(); ++j)
146 {
147 // Use v3 for comparison since it hasn't moved
148 NS_TEST_ASSERT_MSG_EQ(v3(i, j), v4(i, j), "The elements are not equal.");
149 }
150 }
151
152 // test move constructor
153 NS_LOG_INFO("v3 size before move: " << v3.GetSize());
154 size_t v3size = v3.GetSize();
155 ValArray<T> v5(std::move(v3));
156 NS_TEST_ASSERT_MSG_EQ(v3size, v5.GetSize(), "The number of elements are not equal.");
157 for (size_t i = 0; i < v5.GetNumRows(); ++i)
158 {
159 for (size_t j = 0; j < v5.GetNumCols(); ++j)
160 {
161 // Use v4 for comparison since it hasn't moved
162 NS_TEST_ASSERT_MSG_EQ(v4(i, j), v5(i, j), "The elements are not equal.");
163 }
164 }
165
166 // test constructor with initialization valArray
167 std::valarray<int> initArray1{0, 1, 2, 3, 4, 5, 6, 7};
168 std::valarray<T> valArray1(initArray1.size()); // length is 8 elements
169 for (size_t i = 0; i < initArray1.size(); i++)
170 {
171 valArray1[i] = static_cast<T>(initArray1[i]);
172 }
173 ValArray<T> v6 = ValArray<T>(2, 4, valArray1);
174
175 // test constructor that moves valArray
176 NS_LOG_INFO("valarray1 size before move: " << valArray1.size());
177 ValArray<T> v11 = ValArray<T>(2, 4, std::move(valArray1));
178 NS_LOG_INFO("valarray1 size after move: " << valArray1.size());
179 NS_LOG_INFO("v11 size after move: " << v11.GetSize());
180
181 // test whether column-major order was respected during the initialization and
182 // also in the access operator if we iterate over rows first we should find 0, 2, 4, 6, ...
183 std::valarray<int> initArray2{0, 2, 4, 6, 1, 3, 5, 7};
184 size_t testIndex = 0;
185 for (size_t i = 0; i < v6.GetNumRows(); ++i)
186 {
187 for (size_t j = 0; j < v6.GetNumCols(); ++j)
188 {
189 NS_TEST_ASSERT_MSG_EQ(v6(i, j),
190 static_cast<T>(initArray2[testIndex]),
191 "The values are not equal.");
192 testIndex++;
193 }
194 }
195
196 // test constructor with initialization valArray for 3D array
197 std::valarray<int> initArray3{0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7};
198 std::valarray<T> valArray2(initArray3.size()); // length is 8 elements
199 for (size_t i = 0; i < initArray3.size(); i++)
200 {
201 valArray2[i] = static_cast<T>(initArray3[i]);
202 }
203
204 ValArray<T> v7 = ValArray<T>(2, 4, 2, valArray2);
205 // test whether column-major order was respected during the initialization and
206 // also in the access operator
207 // if we iterate over rows first we should find 0, 2, 4, 6, ...
208 std::valarray<int> initArray4{0, 2, 4, 6, 1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};
209 testIndex = 0;
210 for (size_t p = 0; p < v7.GetNumPages(); ++p)
211 {
212 for (size_t i = 0; i < v7.GetNumRows(); ++i)
213 {
214 for (size_t j = 0; j < v7.GetNumCols(); ++j)
215 {
216 NS_TEST_ASSERT_MSG_EQ(v7(i, j, p),
217 static_cast<T>(initArray4[testIndex]),
218 "The values are not equal.");
219 testIndex++;
220 }
221 }
222 }
223
224 // multiplication with a scalar value with 3D array
225 ValArray<T> v8 = v7 * (static_cast<T>(5.0));
226 for (size_t p = 0; p < v8.GetNumPages(); ++p)
227 {
228 for (size_t i = 0; i < v8.GetNumRows(); ++i)
229 {
230 for (size_t j = 0; j < v8.GetNumCols(); ++j)
231 {
232 NS_TEST_ASSERT_MSG_EQ(v7(i, j, p) * (static_cast<T>(5.0)),
233 v8(i, j, p),
234 "The values are not equal");
235 }
236 }
237 }
238
239 NS_LOG_INFO("v8 = v7 * 5:" << v8);
240 // test +, - (binary, unary) operators
241 NS_LOG_INFO("v8 + v8" << v8 + v8);
242 NS_LOG_INFO("v8 - v8" << v8 - v8);
243 NS_LOG_INFO("-v8" << -v8);
244
245 // test += and -= assignment operators
246 ValArray<T> v9(v8.GetNumRows(), v8.GetNumCols(), v8.GetNumPages());
247 v9 += v8;
248 NS_LOG_INFO("v9 += v8" << v9);
249 ValArray<T> v10(v8.GetNumRows(), v8.GetNumCols(), v8.GetNumPages());
250 v10 -= v8;
251 NS_LOG_INFO("v10 -= v8" << v10);
252
253 // test == and != operators
254 NS_TEST_ASSERT_MSG_EQ(bool(v9 == v8), true, "Matrices v8 and v9 should be equal");
255 NS_TEST_ASSERT_MSG_EQ(bool(v10 == v8), false, "Matrices v8 and v10 should not be equal");
256 NS_TEST_ASSERT_MSG_EQ(bool(v10 != v8), true, "Matrices v8 and v10 should not be equal");
257 // test whether arrays are equal when they have different lengths
258 NS_TEST_ASSERT_MSG_NE(ValArray<int>(std::valarray({1, 2, 3})),
259 ValArray<int>(std::valarray({1, 2, 3, 4})),
260 "Arrays should not be equal, they have different dimensions.");
261
262 // test the function IsAlmostEqual
263 v9(0, 0, 0) = v9(0, 0, 0) + static_cast<T>(1);
264 NS_TEST_ASSERT_MSG_EQ(v9.IsAlmostEqual(v8, 2) && (v9 != v8),
265 true,
266 "Matrices should be almost equal, but not equal.");
267
268 // test the initialization with std::vector
269 ValArray<T> v12 = ValArray(std::vector<T>({1, 2, 3}));
270 NS_LOG_INFO("v12:" << v12);
271}
272
273/**
274 * @ingroup valArray-tests
275 * ValArray test suite
276 *
277 * @brief The test checks the correct behaviour of ValArray class
278 */
280{
281 public:
282 /** Constructor. */
284};
285
287 : TestSuite("val-array-test")
288{
289 AddTestCase(new ValArrayTestCase<double>("Test ValArray<double>"));
290 AddTestCase(new ValArrayTestCase<std::complex<double>>("Test ValArray<std::complex<double>>"));
291 AddTestCase(new ValArrayTestCase<int>("Test ValArray<int>"));
292}
293
294/**
295 * @ingroup valArray-tests
296 * ValArrayTestSuite instance variable.
297 */
299
300} // namespace tests
301} // namespace ns3
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
ValArray is a class to efficiently store 3D array.
Definition val-array.h:74
bool IsAlmostEqual(const ValArray< T > &rhs, T tol) const
Compare Valarray up to a given absolute tolerance.
Definition val-array.h:678
size_t GetNumPages() const
Definition val-array.h:387
size_t GetSize() const
Definition val-array.h:394
size_t GetNumRows() const
Definition val-array.h:373
size_t GetNumCols() const
Definition val-array.h:380
ValArray test case for testing ValArray class.
ValArrayTestCase()=default
Default constructor.
ValArrayTestCase< T > & operator=(ValArrayTestCase< T > &&)=default
Move assignment operator.
ValArrayTestCase(ValArrayTestCase< T > &&)=default
Move constructor.
ValArrayTestCase(const ValArrayTestCase< T > &)=default
Copy constructor.
ValArrayTestCase< T > & operator=(const ValArrayTestCase< T > &)=default
Copy assignment operator.
~ValArrayTestCase() override
Destructor.
void DoRun() override
Implementation to actually run this TestCase.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:134
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Definition test.h:554
static ValArrayTestSuite g_valArrayTestSuite
ValArrayTestSuite instance variable.
Every class exported by the ns3 library is enclosed in the ns3 namespace.