A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
30
namespace
ns3
31
{
32
namespace
tests
33
{
34
35
NS_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
*/
44
template
<
class
T>
45
class
ValArrayTestCase
:
public
TestCase
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
*/
63
ValArrayTestCase
(
const
ValArrayTestCase<T>
&) =
default
;
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
*/
69
ValArrayTestCase<T>
&
operator=
(
const
ValArrayTestCase<T>
&) =
default
;
70
/**
71
* @brief Move constructor.
72
* Instruct the compiler to generate the implicitly declared move constructor
73
*/
74
ValArrayTestCase
(
ValArrayTestCase<T>
&&) =
default
;
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
*/
80
ValArrayTestCase<T>
&
operator=
(
ValArrayTestCase<T>
&&) =
default
;
81
82
private
:
83
void
DoRun
()
override
;
84
};
85
86
template
<
class
T>
87
ValArrayTestCase<T>::ValArrayTestCase
(
const
std::string& name)
88
:
TestCase
(name)
89
{
90
}
91
92
template
<
class
T>
93
ValArrayTestCase<T>::~ValArrayTestCase
()
94
{
95
}
96
97
template
<
class
T>
98
void
99
ValArrayTestCase<T>::DoRun
()
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
*/
279
class
ValArrayTestSuite
:
public
TestSuite
280
{
281
public
:
282
/** Constructor. */
283
ValArrayTestSuite
();
284
};
285
286
ValArrayTestSuite::ValArrayTestSuite
()
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
*/
298
static
ValArrayTestSuite
g_valArrayTestSuite
;
299
300
}
// namespace tests
301
}
// namespace ns3
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::ValArray
ValArray is a class to efficiently store 3D array.
Definition
val-array.h:74
ns3::tests::ValArrayTestCase
ValArray test case for testing ValArray class.
Definition
val-array-test-suite.cc:46
ns3::tests::ValArrayTestCase::ValArrayTestCase
ValArrayTestCase()=default
Default constructor.
ns3::tests::ValArrayTestCase::operator=
ValArrayTestCase< T > & operator=(ValArrayTestCase< T > &&)=default
Move assignment operator.
ns3::tests::ValArrayTestCase::ValArrayTestCase
ValArrayTestCase(ValArrayTestCase< T > &&)=default
Move constructor.
ns3::tests::ValArrayTestCase::ValArrayTestCase
ValArrayTestCase(const ValArrayTestCase< T > &)=default
Copy constructor.
ns3::tests::ValArrayTestCase::operator=
ValArrayTestCase< T > & operator=(const ValArrayTestCase< T > &)=default
Copy assignment operator.
ns3::tests::ValArrayTestCase::~ValArrayTestCase
~ValArrayTestCase() override
Destructor.
Definition
val-array-test-suite.cc:93
ns3::tests::ValArrayTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
val-array-test-suite.cc:99
ns3::tests::ValArrayTestSuite
ValArray test suite.
Definition
val-array-test-suite.cc:280
ns3::tests::ValArrayTestSuite::ValArrayTestSuite
ValArrayTestSuite()
Constructor.
Definition
val-array-test-suite.cc:286
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition
log.h:264
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
NS_TEST_ASSERT_MSG_EQ
#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
NS_TEST_ASSERT_MSG_NE
#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
ns3::tests::g_valArrayTestSuite
static ValArrayTestSuite g_valArrayTestSuite
ValArrayTestSuite instance variable.
Definition
val-array-test-suite.cc:298
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
core
test
val-array-test-suite.cc
Generated on Mon Dec 15 2025 15:21:51 for ns-3 by
1.9.8