A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spectrum-value-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 CTTC
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
9#include "ns3/log.h"
10#include "ns3/object.h"
11#include "ns3/spectrum-converter.h"
12#include "ns3/spectrum-test.h"
13#include "ns3/spectrum-value.h"
14#include "ns3/test.h"
15
16#include <cmath>
17#include <iostream>
18
19using namespace ns3;
20
21// NS_LOG_COMPONENT_DEFINE ("SpectrumValueTest");
22
23#define TOLERANCE 1e-6
24
25/**
26 * @ingroup spectrum-tests
27 *
28 * @brief Spectrum Value Test
29 */
31{
32 public:
33 /**
34 * Constructor
35 * @param a first SpectrumModel
36 * @param b second SpectrumModel
37 * @param name test name
38 * @param expectOrthogonal whether the two models are expected to be orthogonal
39 * @param expectAligned whether the two models are expected to be aligned
40 */
43 const std::string& name,
44 bool expectOrthogonal,
45 bool expectAligned);
46 void DoRun() override;
47
48 private:
49 Ptr<SpectrumModel> m_a; //!< first SpectrumModel
50 Ptr<SpectrumModel> m_b; //!< second SpectrumModel
51 bool m_expectOrthogonal; //!< whether the two models are expected to be orthogonal
52 bool m_expectAligned; //!< whether the two models are expected to be aligned
53};
54
57 const std::string& name,
58 bool expectOrthogonal,
59 bool expectAligned)
60 : TestCase(name),
61 m_a(a),
62 m_b(b),
63 m_expectOrthogonal(expectOrthogonal),
64 m_expectAligned(expectAligned)
65{
66}
67
68void
70{
72 m_a->IsOrthogonal(*m_b),
74 (m_expectOrthogonal ? "Expected orthogonal models" : "Expected non-orthogonal models"));
76 m_a->IsAligned(*m_b),
78 (m_expectAligned ? "Expected aligned models" : "Expected non-aligned models"));
79}
80
81/**
82 * @ingroup spectrum-tests
83 *
84 * @brief Spectrum Value Test
85 */
87{
88 public:
89 /**
90 * Constructor
91 * @param a first SpectrumValue
92 * @param b second SpectrumValue
93 * @param name test name
94 */
96 ~SpectrumValueTestCase() override;
97 void DoRun() override;
98
99 private:
100 /**
101 * Check that two SpectrumValue are equal within a tolerance
102 * @param x first SpectrumValue
103 * @param y second SpectrumValue
104 * @return true if the two values are within the tolerance
105 */
107
108 SpectrumValue m_a; //!< first SpectrumValue
109 SpectrumValue m_b; //!< second SpectrumValue
110};
111
113 : TestCase(name),
114 m_a(a),
115 m_b(b)
116{
117}
118
122
123bool
129
130void
132{
134 *m_b.GetSpectrumModel(),
135 TOLERANCE,
136 "");
138}
139
140/**
141 * @ingroup spectrum-tests
142 *
143 * @brief Spectrum Value TestSuite
144 */
146{
147 public:
149};
150
152 : TestSuite("spectrum-value", Type::UNIT)
153{
154 // NS_LOG_INFO("creating SpectrumValueTestSuite");
155
156 std::vector<double> freqs;
157
158 for (int i = 1; i <= 5; i++)
159 {
160 freqs.push_back(i);
161 }
162
164
165 SpectrumValue v1(f);
166 SpectrumValue v2(f);
167 SpectrumValue v3(f);
168 SpectrumValue v4(f);
169 SpectrumValue v5(f);
170 SpectrumValue v6(f);
171 SpectrumValue v7(f);
172 SpectrumValue v8(f);
173 SpectrumValue v9(f);
174 SpectrumValue v10(f);
175
176 double doubleValue;
177
178 doubleValue = 1.12345600000000;
179
180 v1[0] = 0.700539792840;
181 v1[1] = -0.554277600423;
182 v1[2] = 0.750309319469;
183 v1[3] = -0.892299213192;
184 v1[4] = 0.987045234885;
185
186 v2[0] = 0.870441628737;
187 v2[1] = 0.271419263880;
188 v2[2] = 0.451557288312;
189 v2[3] = 0.968992859395;
190 v2[4] = -0.929186654705;
191
192 v3[0] = 1.570981421577;
193 v3[1] = -0.282858336543;
194 v3[2] = 1.201866607781;
195 v3[3] = 0.076693646203;
196 v3[4] = 0.057858580180;
197
198 v4[0] = -0.169901835897;
199 v4[1] = -0.825696864302;
200 v4[2] = 0.298752031158;
201 v4[3] = -1.861292072588;
202 v4[4] = 1.916231889590;
203
204 v5[0] = 0.609778998275;
205 v5[1] = -0.150441618292;
206 v5[2] = 0.338807641695;
207 v5[3] = -0.864631566028;
208 v5[4] = -0.917149259846;
209
210 v6[0] = 0.804809615846;
211 v6[1] = -2.042145397125;
212 v6[2] = 1.661603829438;
213 v6[3] = -0.920852207053;
214 v6[4] = -1.062267984465;
215
216 v7[0] = 1.823995792840;
217 v7[1] = 0.569178399577;
218 v7[2] = 1.873765319469;
219 v7[3] = 0.231156786808;
220 v7[4] = 2.110501234885;
221
222 v8[0] = -0.422916207160;
223 v8[1] = -1.677733600423;
224 v8[2] = -0.373146680531;
225 v8[3] = -2.015755213192;
226 v8[4] = -0.136410765115;
227
228 v9[0] = 0.787025633505;
229 v9[1] = -0.622706495860;
230 v9[2] = 0.842939506814;
231 v9[3] = -1.002458904856;
232 v9[4] = 1.108901891403;
233
234 v10[0] = 0.623557836569;
235 v10[1] = -0.493368320987;
236 v10[2] = 0.667858215604;
237 v10[3] = -0.794244913190;
238 v10[4] = 0.878579343459;
239
240 SpectrumValue tv3(f);
241 SpectrumValue tv4(f);
242 SpectrumValue tv5(f);
243 SpectrumValue tv6(f);
244
245 tv3 = v1 + v2;
246 tv4 = v1 - v2;
247 tv5 = v1 * v2;
248 tv6 = v1 / v2;
249
250 AddTestCase(new SpectrumValueTestCase(tv3, v3, "tv3 = v1 + v2"), TestCase::Duration::QUICK);
251 AddTestCase(new SpectrumValueTestCase(tv4, v4, "tv4 = v1 - v2"), TestCase::Duration::QUICK);
252 AddTestCase(new SpectrumValueTestCase(tv5, v5, "tv5 = v1 * v2"), TestCase::Duration::QUICK);
253 AddTestCase(new SpectrumValueTestCase(tv6, v6, "tv6 = v1 div v2"), TestCase::Duration::QUICK);
254
255 // std::cerr << v6 << std::endl;
256 // std::cerr << tv6 << std::endl;
257
258 tv3 = v1;
259 tv4 = v1;
260 tv5 = v1;
261 tv6 = v1;
262
263 tv3 += v2;
264 tv4 -= v2;
265 tv5 *= v2;
266 tv6 /= v2;
267
271 AddTestCase(new SpectrumValueTestCase(tv6, v6, "tv6 div= v2"), TestCase::Duration::QUICK);
272
273 SpectrumValue tv7a(f);
274 SpectrumValue tv8a(f);
275 SpectrumValue tv9a(f);
276 SpectrumValue tv10a(f);
277 tv7a = v1 + doubleValue;
278 tv8a = v1 - doubleValue;
279 tv9a = v1 * doubleValue;
280 tv10a = v1 / doubleValue;
281 AddTestCase(new SpectrumValueTestCase(tv7a, v7, "tv7a = v1 + doubleValue"),
283 AddTestCase(new SpectrumValueTestCase(tv8a, v8, "tv8a = v1 - doubleValue"),
285 AddTestCase(new SpectrumValueTestCase(tv9a, v9, "tv9a = v1 * doubleValue"),
287 AddTestCase(new SpectrumValueTestCase(tv10a, v10, "tv10a = v1 div doubleValue"),
289
290 SpectrumValue tv7b(f);
291 SpectrumValue tv8b(f);
292 SpectrumValue tv9b(f);
293 SpectrumValue tv10b(f);
294 tv7b = doubleValue + v1;
295 tv8b = doubleValue - v1;
296 tv9b = doubleValue * v1;
297 tv10b = doubleValue / v1;
298 AddTestCase(new SpectrumValueTestCase(tv7b, v7, "tv7b = doubleValue + v1"),
300 AddTestCase(new SpectrumValueTestCase(tv8b, v8, "tv8b = doubleValue - v1"),
302 AddTestCase(new SpectrumValueTestCase(tv9b, v9, "tv9b = doubleValue * v1"),
304 AddTestCase(new SpectrumValueTestCase(tv10b, v10, "tv10b = doubleValue div v1"),
306
307 SpectrumValue v1ls3(f);
308 SpectrumValue v1rs3(f);
309 SpectrumValue tv1ls3(f);
310 SpectrumValue tv1rs3(f);
311
312 v1ls3[0] = v1[3];
313 v1ls3[1] = v1[4];
314 tv1ls3 = v1 << 3;
315 AddTestCase(new SpectrumValueTestCase(tv1ls3, v1ls3, "tv1ls3 = v1 << 3"),
317
318 v1rs3[3] = v1[0];
319 v1rs3[4] = v1[1];
320 tv1rs3 = v1 >> 3;
321 AddTestCase(new SpectrumValueTestCase(tv1rs3, v1rs3, "tv1rs3 = v1 >> 3"),
323}
324
325/**
326 * @ingroup spectrum-tests
327 *
328 * @brief Spectrum Converter TestSuite
329 */
331{
332 public:
334};
335
337 : TestSuite("spectrum-converter", Type::UNIT)
338{
339 double f;
340
341 std::vector<double> f1;
342 for (f = 3; f <= 7; f += 2)
343 {
344 f1.push_back(f);
345 }
346 auto sof1 = Create<SpectrumModel>(f1);
347
348 std::vector<double> f2;
349 for (f = 2; f <= 8; f += 1)
350 {
351 f2.push_back(f);
352 }
353 auto sof2 = Create<SpectrumModel>(f2);
354
355 auto v1 = Create<SpectrumValue>(sof1);
356 *v1 = 4;
357 SpectrumConverter c12(sof1, sof2);
358 auto res = c12.Convert(v1);
359 SpectrumValue t12(sof2);
360 t12 = 4;
361 t12[0] = 2;
362 t12[6] = 2;
363 // NS_LOG_LOGIC(*v1);
364 // NS_LOG_LOGIC(t12);
365 // NS_LOG_LOGIC(*res);
366
368 // TEST_ASSERT(MoreOrLessEqual(t12, *res));
369
370 auto v2a = Create<SpectrumValue>(sof2);
371 *v2a = -2;
372 SpectrumConverter c21(sof2, sof1);
373 res = c21.Convert(v2a);
374 SpectrumValue t21a(sof1);
375 t21a = -2;
376 // NS_LOG_LOGIC(*v2a);
377 // NS_LOG_LOGIC(t21a);
378 // NS_LOG_LOGIC(*res);
380 // TEST_ASSERT(MoreOrLessEqual(t21a, *res));
381
382 auto v2b = Create<SpectrumValue>(sof2);
383 (*v2b)[0] = 3;
384 (*v2b)[1] = 5;
385 (*v2b)[2] = 1;
386 (*v2b)[3] = 2;
387 (*v2b)[4] = 4;
388 (*v2b)[5] = 6;
389 (*v2b)[6] = 3;
390 res = c21.Convert(v2b);
391 SpectrumValue t21b(sof1);
392 t21b[0] = 3 * 0.25 + 5 * 0.5 + 1 * 0.25;
393 t21b[1] = 1 * 0.25 + 2 * 0.5 + 4 * 0.25;
394 t21b[2] = 4 * 0.25 + 6 * 0.5 + 3 * 0.25;
395 // NS_LOG_LOGIC(*v2b);
396 // NS_LOG_LOGIC(t21b);
397 // NS_LOG_LOGIC(*res);
399
400 // orthogonal models
401 std::vector<double> f3{10, 11, 12, 13, 14};
402 auto sof3 = Create<SpectrumModel>(f3);
404 new SpectrumModelTestCase(sof1, sof3, "Check sof1 and sof3 are orthogonal", true, false),
406
407 SpectrumConverter c13(sof1, sof3);
408 res = c13.Convert(v1);
409 SpectrumValue t13(sof3);
410 t13 = 0;
412 new SpectrumValueTestCase(t13, *res, "Check spectrum conversion with orthogonal models"),
414
415 // non-orthogonal and non-aligned models
416 std::vector<double> f4{3, 4, 5, 6, 7};
417 auto sof4 = Create<SpectrumModel>(f4);
419 sof4,
420 "Check sof1 and sof4 are not orthogonal nor aligned",
421 false,
422 false),
424
425 SpectrumConverter c14(sof1, sof4);
426 res = c14.Convert(v1);
427 SpectrumValue t14(sof4);
428 t14 = 4;
429 t14[0] = 4;
430 t14[1] = 4;
431 t14[2] = 4;
432 t14[3] = 4;
433 t14[4] = 4;
435 t14,
436 *res,
437 "Check spectrum conversion with non-orthogonal and non-aligned models"),
439
440 // aligned models
441 std::vector<double> f5{5, 7, 9};
442 auto sof5 = Create<SpectrumModel>(f5);
444 new SpectrumModelTestCase(sof1, sof5, "Check sof1 and sof5 are aligned", false, true),
446
447 SpectrumConverter c15(sof1, sof5);
448 res = c15.Convert(v1);
449 SpectrumValue t15(sof5);
450 t15 = 4;
451 t15[0] = 4;
452 t15[1] = 4;
453 t15[2] = 0;
455 new SpectrumValueTestCase(t15, *res, "Check spectrum conversion with aligned models"),
457
458 // aligned models with different band sizes
459 auto sof6 = Create<SpectrumModel>(std::vector<double>{1, 2.5, 4});
460 auto sof7 = Create<SpectrumModel>(std::vector<double>{2.5, 4, 5.5});
461
463 new SpectrumModelTestCase(sof6, sof7, "Check sof6 and sof7 are aligned", false, true),
465
466 // models with different band sizes where only some bands are aligned
467 auto sof8 = Create<SpectrumModel>(std::vector<double>{2.5, 3.5, 4.5, 5.5});
469 new SpectrumModelTestCase(sof6, sof8, "Check sof6 and sof8 are not aligned", false, false),
471}
472
473/// Static variable for test initialization
475/// Static variable for test initialization
cairo_uint64_t x
_cairo_uint_96by64_32x64_divrem:
Spectrum Converter TestSuite.
Spectrum Value Test.
Ptr< SpectrumModel > m_a
first SpectrumModel
bool m_expectAligned
whether the two models are expected to be aligned
void DoRun() override
Implementation to actually run this TestCase.
bool m_expectOrthogonal
whether the two models are expected to be orthogonal
Ptr< SpectrumModel > m_b
second SpectrumModel
SpectrumModelTestCase(Ptr< SpectrumModel > a, Ptr< SpectrumModel > b, const std::string &name, bool expectOrthogonal, bool expectAligned)
Constructor.
Spectrum Value Test.
bool MoreOrLessEqual(SpectrumValue x, SpectrumValue y)
Check that two SpectrumValue are equal within a tolerance.
SpectrumValue m_b
second SpectrumValue
SpectrumValue m_a
first SpectrumValue
SpectrumValueTestCase(SpectrumValue a, SpectrumValue b, std::string name)
Constructor.
void DoRun() override
Implementation to actually run this TestCase.
Spectrum Value TestSuite.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Class which implements a converter between SpectrumValue which are defined over different SpectrumMod...
Ptr< SpectrumValue > Convert(Ptr< const SpectrumValue > vvf) const
Convert a particular ValueVsFreq instance to.
Set of values corresponding to a given SpectrumModel.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
#define NS_TEST_ASSERT_MSG_SPECTRUM_MODEL_EQ_TOL(actual, expected, tol, msg)
Test if two SpectrumModel instances are equal within a given tolerance.
#define NS_TEST_ASSERT_MSG_SPECTRUM_VALUE_EQ_TOL(actual, expected, tol, msg)
Test if two SpectrumValue instances are equal within a given tolerance.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:240
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double Norm(const SpectrumValue &x)
#define TOLERANCE
static SpectrumConverterTestSuite g_SpectrumConverterTestSuite
Static variable for test initialization.
static SpectrumValueTestSuite g_SpectrumValueTestSuite
Static variable for test initialization.