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 "spectrum-test.h"
10
11#include <ns3/log.h>
12#include <ns3/object.h>
13#include <ns3/spectrum-converter.h>
14#include <ns3/spectrum-value.h>
15#include <ns3/test.h>
16
17#include <cmath>
18#include <iostream>
19
20using namespace ns3;
21
22// NS_LOG_COMPONENT_DEFINE ("SpectrumValueTest");
23
24#define TOLERANCE 1e-6
25
26/**
27 * \ingroup spectrum-tests
28 *
29 * \brief Spectrum Value Test
30 */
32{
33 public:
34 /**
35 * Constructor
36 * \param a first SpectrumValue
37 * \param b second SpectrumValue
38 * \param name test name
39 */
41 ~SpectrumValueTestCase() override;
42 void DoRun() override;
43
44 private:
45 /**
46 * Check that two SpectrumValue are equal within a tolerance
47 * \param x first SpectrumValue
48 * \param y second SpectrumValue
49 * \return true if the two values are within the tolerance
50 */
52
53 SpectrumValue m_a; //!< first SpectrumValue
54 SpectrumValue m_b; //!< second SpectrumValue
55};
56
58 : TestCase(name),
59 m_a(a),
60 m_b(b)
61{
62}
63
67
68bool
74
75void
84
85/**
86 * \ingroup spectrum-tests
87 *
88 * \brief Spectrum Value TestSuite
89 */
91{
92 public:
94};
95
97 : TestSuite("spectrum-value", Type::UNIT)
98{
99 // NS_LOG_INFO("creating SpectrumValueTestSuite");
100
101 std::vector<double> freqs;
102
103 for (int i = 1; i <= 5; i++)
104 {
105 freqs.push_back(i);
106 }
107
109
110 SpectrumValue v1(f);
111 SpectrumValue v2(f);
112 SpectrumValue v3(f);
113 SpectrumValue v4(f);
114 SpectrumValue v5(f);
115 SpectrumValue v6(f);
116 SpectrumValue v7(f);
117 SpectrumValue v8(f);
118 SpectrumValue v9(f);
119 SpectrumValue v10(f);
120
121 double doubleValue;
122
123 doubleValue = 1.12345600000000;
124
125 v1[0] = 0.700539792840;
126 v1[1] = -0.554277600423;
127 v1[2] = 0.750309319469;
128 v1[3] = -0.892299213192;
129 v1[4] = 0.987045234885;
130
131 v2[0] = 0.870441628737;
132 v2[1] = 0.271419263880;
133 v2[2] = 0.451557288312;
134 v2[3] = 0.968992859395;
135 v2[4] = -0.929186654705;
136
137 v3[0] = 1.570981421577;
138 v3[1] = -0.282858336543;
139 v3[2] = 1.201866607781;
140 v3[3] = 0.076693646203;
141 v3[4] = 0.057858580180;
142
143 v4[0] = -0.169901835897;
144 v4[1] = -0.825696864302;
145 v4[2] = 0.298752031158;
146 v4[3] = -1.861292072588;
147 v4[4] = 1.916231889590;
148
149 v5[0] = 0.609778998275;
150 v5[1] = -0.150441618292;
151 v5[2] = 0.338807641695;
152 v5[3] = -0.864631566028;
153 v5[4] = -0.917149259846;
154
155 v6[0] = 0.804809615846;
156 v6[1] = -2.042145397125;
157 v6[2] = 1.661603829438;
158 v6[3] = -0.920852207053;
159 v6[4] = -1.062267984465;
160
161 v7[0] = 1.823995792840;
162 v7[1] = 0.569178399577;
163 v7[2] = 1.873765319469;
164 v7[3] = 0.231156786808;
165 v7[4] = 2.110501234885;
166
167 v8[0] = -0.422916207160;
168 v8[1] = -1.677733600423;
169 v8[2] = -0.373146680531;
170 v8[3] = -2.015755213192;
171 v8[4] = -0.136410765115;
172
173 v9[0] = 0.787025633505;
174 v9[1] = -0.622706495860;
175 v9[2] = 0.842939506814;
176 v9[3] = -1.002458904856;
177 v9[4] = 1.108901891403;
178
179 v10[0] = 0.623557836569;
180 v10[1] = -0.493368320987;
181 v10[2] = 0.667858215604;
182 v10[3] = -0.794244913190;
183 v10[4] = 0.878579343459;
184
185 SpectrumValue tv3(f);
186 SpectrumValue tv4(f);
187 SpectrumValue tv5(f);
188 SpectrumValue tv6(f);
189
190 tv3 = v1 + v2;
191 tv4 = v1 - v2;
192 tv5 = v1 * v2;
193 tv6 = v1 / v2;
194
195 AddTestCase(new SpectrumValueTestCase(tv3, v3, "tv3 = v1 + v2"), TestCase::Duration::QUICK);
196 AddTestCase(new SpectrumValueTestCase(tv4, v4, "tv4 = v1 - v2"), TestCase::Duration::QUICK);
197 AddTestCase(new SpectrumValueTestCase(tv5, v5, "tv5 = v1 * v2"), TestCase::Duration::QUICK);
198 AddTestCase(new SpectrumValueTestCase(tv6, v6, "tv6 = v1 div v2"), TestCase::Duration::QUICK);
199
200 // std::cerr << v6 << std::endl;
201 // std::cerr << tv6 << std::endl;
202
203 tv3 = v1;
204 tv4 = v1;
205 tv5 = v1;
206 tv6 = v1;
207
208 tv3 += v2;
209 tv4 -= v2;
210 tv5 *= v2;
211 tv6 /= v2;
212
213 AddTestCase(new SpectrumValueTestCase(tv3, v3, "tv3 += v2"), TestCase::Duration::QUICK);
214 AddTestCase(new SpectrumValueTestCase(tv4, v4, "tv4 -= v2"), TestCase::Duration::QUICK);
215 AddTestCase(new SpectrumValueTestCase(tv5, v5, "tv5 *= v2"), TestCase::Duration::QUICK);
216 AddTestCase(new SpectrumValueTestCase(tv6, v6, "tv6 div= v2"), TestCase::Duration::QUICK);
217
218 SpectrumValue tv7a(f);
219 SpectrumValue tv8a(f);
220 SpectrumValue tv9a(f);
221 SpectrumValue tv10a(f);
222 tv7a = v1 + doubleValue;
223 tv8a = v1 - doubleValue;
224 tv9a = v1 * doubleValue;
225 tv10a = v1 / doubleValue;
226 AddTestCase(new SpectrumValueTestCase(tv7a, v7, "tv7a = v1 + doubleValue"),
227 TestCase::Duration::QUICK);
228 AddTestCase(new SpectrumValueTestCase(tv8a, v8, "tv8a = v1 - doubleValue"),
229 TestCase::Duration::QUICK);
230 AddTestCase(new SpectrumValueTestCase(tv9a, v9, "tv9a = v1 * doubleValue"),
231 TestCase::Duration::QUICK);
232 AddTestCase(new SpectrumValueTestCase(tv10a, v10, "tv10a = v1 div doubleValue"),
233 TestCase::Duration::QUICK);
234
235 SpectrumValue tv7b(f);
236 SpectrumValue tv8b(f);
237 SpectrumValue tv9b(f);
238 SpectrumValue tv10b(f);
239 tv7b = doubleValue + v1;
240 tv8b = doubleValue - v1;
241 tv9b = doubleValue * v1;
242 tv10b = doubleValue / v1;
243 AddTestCase(new SpectrumValueTestCase(tv7b, v7, "tv7b = doubleValue + v1"),
244 TestCase::Duration::QUICK);
245 AddTestCase(new SpectrumValueTestCase(tv8b, v8, "tv8b = doubleValue - v1"),
246 TestCase::Duration::QUICK);
247 AddTestCase(new SpectrumValueTestCase(tv9b, v9, "tv9b = doubleValue * v1"),
248 TestCase::Duration::QUICK);
249 AddTestCase(new SpectrumValueTestCase(tv10b, v10, "tv10b = doubleValue div v1"),
250 TestCase::Duration::QUICK);
251
252 SpectrumValue v1ls3(f);
253 SpectrumValue v1rs3(f);
254 SpectrumValue tv1ls3(f);
255 SpectrumValue tv1rs3(f);
256
257 v1ls3[0] = v1[3];
258 v1ls3[1] = v1[4];
259 tv1ls3 = v1 << 3;
260 AddTestCase(new SpectrumValueTestCase(tv1ls3, v1ls3, "tv1ls3 = v1 << 3"),
261 TestCase::Duration::QUICK);
262
263 v1rs3[3] = v1[0];
264 v1rs3[4] = v1[1];
265 tv1rs3 = v1 >> 3;
266 AddTestCase(new SpectrumValueTestCase(tv1rs3, v1rs3, "tv1rs3 = v1 >> 3"),
267 TestCase::Duration::QUICK);
268}
269
270/**
271 * \ingroup spectrum-tests
272 *
273 * \brief Spectrum Converter TestSuite
274 */
276{
277 public:
279};
280
282 : TestSuite("spectrum-converter", Type::UNIT)
283{
284 double f;
285
286 std::vector<double> f1;
287 for (f = 3; f <= 7; f += 2)
288 {
289 f1.push_back(f);
290 }
292
293 std::vector<double> f2;
294 for (f = 2; f <= 8; f += 1)
295 {
296 f2.push_back(f);
297 }
299
301
303 *v1 = 4;
304 SpectrumConverter c12(sof1, sof2);
305 res = c12.Convert(v1);
306 SpectrumValue t12(sof2);
307 t12 = 4;
308 t12[0] = 2;
309 t12[6] = 2;
310 // NS_LOG_LOGIC(*v1);
311 // NS_LOG_LOGIC(t12);
312 // NS_LOG_LOGIC(*res);
313
314 AddTestCase(new SpectrumValueTestCase(t12, *res, ""), TestCase::Duration::QUICK);
315 // TEST_ASSERT(MoreOrLessEqual(t12, *res));
316
318 *v2a = -2;
319 SpectrumConverter c21(sof2, sof1);
320 res = c21.Convert(v2a);
321 SpectrumValue t21a(sof1);
322 t21a = -2;
323 // NS_LOG_LOGIC(*v2a);
324 // NS_LOG_LOGIC(t21a);
325 // NS_LOG_LOGIC(*res);
326 AddTestCase(new SpectrumValueTestCase(t21a, *res, ""), TestCase::Duration::QUICK);
327 // TEST_ASSERT(MoreOrLessEqual(t21a, *res));
328
330 (*v2b)[0] = 3;
331 (*v2b)[1] = 5;
332 (*v2b)[2] = 1;
333 (*v2b)[3] = 2;
334 (*v2b)[4] = 4;
335 (*v2b)[5] = 6;
336 (*v2b)[6] = 3;
337 res = c21.Convert(v2b);
338 SpectrumValue t21b(sof1);
339 t21b[0] = 3 * 0.25 + 5 * 0.5 + 1 * 0.25;
340 t21b[1] = 1 * 0.25 + 2 * 0.5 + 4 * 0.25;
341 t21b[2] = 4 * 0.25 + 6 * 0.5 + 3 * 0.25;
342 // NS_LOG_LOGIC(*v2b);
343 // NS_LOG_LOGIC(t21b);
344 // NS_LOG_LOGIC(*res);
345 AddTestCase(new SpectrumValueTestCase(t21b, *res, ""), TestCase::Duration::QUICK);
346}
347
348/// Static variable for test initialization
350/// Static variable for test initialization
Spectrum Converter TestSuite.
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.
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.
Ptr< const SpectrumModel > GetSpectrumModel() const
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
Type
Type of test.
Definition test.h:1274
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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.
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.