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 SpectrumValue
36 * @param b second SpectrumValue
37 * @param name test name
38 */
40 ~SpectrumValueTestCase() override;
41 void DoRun() override;
42
43 private:
44 /**
45 * Check that two SpectrumValue are equal within a tolerance
46 * @param x first SpectrumValue
47 * @param y second SpectrumValue
48 * @return true if the two values are within the tolerance
49 */
51
52 SpectrumValue m_a; //!< first SpectrumValue
53 SpectrumValue m_b; //!< second SpectrumValue
54};
55
57 : TestCase(name),
58 m_a(a),
59 m_b(b)
60{
61}
62
66
67bool
73
74void
83
84/**
85 * @ingroup spectrum-tests
86 *
87 * @brief Spectrum Value TestSuite
88 */
90{
91 public:
93};
94
96 : TestSuite("spectrum-value", Type::UNIT)
97{
98 // NS_LOG_INFO("creating SpectrumValueTestSuite");
99
100 std::vector<double> freqs;
101
102 for (int i = 1; i <= 5; i++)
103 {
104 freqs.push_back(i);
105 }
106
108
109 SpectrumValue v1(f);
110 SpectrumValue v2(f);
111 SpectrumValue v3(f);
112 SpectrumValue v4(f);
113 SpectrumValue v5(f);
114 SpectrumValue v6(f);
115 SpectrumValue v7(f);
116 SpectrumValue v8(f);
117 SpectrumValue v9(f);
118 SpectrumValue v10(f);
119
120 double doubleValue;
121
122 doubleValue = 1.12345600000000;
123
124 v1[0] = 0.700539792840;
125 v1[1] = -0.554277600423;
126 v1[2] = 0.750309319469;
127 v1[3] = -0.892299213192;
128 v1[4] = 0.987045234885;
129
130 v2[0] = 0.870441628737;
131 v2[1] = 0.271419263880;
132 v2[2] = 0.451557288312;
133 v2[3] = 0.968992859395;
134 v2[4] = -0.929186654705;
135
136 v3[0] = 1.570981421577;
137 v3[1] = -0.282858336543;
138 v3[2] = 1.201866607781;
139 v3[3] = 0.076693646203;
140 v3[4] = 0.057858580180;
141
142 v4[0] = -0.169901835897;
143 v4[1] = -0.825696864302;
144 v4[2] = 0.298752031158;
145 v4[3] = -1.861292072588;
146 v4[4] = 1.916231889590;
147
148 v5[0] = 0.609778998275;
149 v5[1] = -0.150441618292;
150 v5[2] = 0.338807641695;
151 v5[3] = -0.864631566028;
152 v5[4] = -0.917149259846;
153
154 v6[0] = 0.804809615846;
155 v6[1] = -2.042145397125;
156 v6[2] = 1.661603829438;
157 v6[3] = -0.920852207053;
158 v6[4] = -1.062267984465;
159
160 v7[0] = 1.823995792840;
161 v7[1] = 0.569178399577;
162 v7[2] = 1.873765319469;
163 v7[3] = 0.231156786808;
164 v7[4] = 2.110501234885;
165
166 v8[0] = -0.422916207160;
167 v8[1] = -1.677733600423;
168 v8[2] = -0.373146680531;
169 v8[3] = -2.015755213192;
170 v8[4] = -0.136410765115;
171
172 v9[0] = 0.787025633505;
173 v9[1] = -0.622706495860;
174 v9[2] = 0.842939506814;
175 v9[3] = -1.002458904856;
176 v9[4] = 1.108901891403;
177
178 v10[0] = 0.623557836569;
179 v10[1] = -0.493368320987;
180 v10[2] = 0.667858215604;
181 v10[3] = -0.794244913190;
182 v10[4] = 0.878579343459;
183
184 SpectrumValue tv3(f);
185 SpectrumValue tv4(f);
186 SpectrumValue tv5(f);
187 SpectrumValue tv6(f);
188
189 tv3 = v1 + v2;
190 tv4 = v1 - v2;
191 tv5 = v1 * v2;
192 tv6 = v1 / v2;
193
194 AddTestCase(new SpectrumValueTestCase(tv3, v3, "tv3 = v1 + v2"), TestCase::Duration::QUICK);
195 AddTestCase(new SpectrumValueTestCase(tv4, v4, "tv4 = v1 - v2"), TestCase::Duration::QUICK);
196 AddTestCase(new SpectrumValueTestCase(tv5, v5, "tv5 = v1 * v2"), TestCase::Duration::QUICK);
197 AddTestCase(new SpectrumValueTestCase(tv6, v6, "tv6 = v1 div v2"), TestCase::Duration::QUICK);
198
199 // std::cerr << v6 << std::endl;
200 // std::cerr << tv6 << std::endl;
201
202 tv3 = v1;
203 tv4 = v1;
204 tv5 = v1;
205 tv6 = v1;
206
207 tv3 += v2;
208 tv4 -= v2;
209 tv5 *= v2;
210 tv6 /= v2;
211
215 AddTestCase(new SpectrumValueTestCase(tv6, v6, "tv6 div= v2"), TestCase::Duration::QUICK);
216
217 SpectrumValue tv7a(f);
218 SpectrumValue tv8a(f);
219 SpectrumValue tv9a(f);
220 SpectrumValue tv10a(f);
221 tv7a = v1 + doubleValue;
222 tv8a = v1 - doubleValue;
223 tv9a = v1 * doubleValue;
224 tv10a = v1 / doubleValue;
225 AddTestCase(new SpectrumValueTestCase(tv7a, v7, "tv7a = v1 + doubleValue"),
227 AddTestCase(new SpectrumValueTestCase(tv8a, v8, "tv8a = v1 - doubleValue"),
229 AddTestCase(new SpectrumValueTestCase(tv9a, v9, "tv9a = v1 * doubleValue"),
231 AddTestCase(new SpectrumValueTestCase(tv10a, v10, "tv10a = v1 div doubleValue"),
233
234 SpectrumValue tv7b(f);
235 SpectrumValue tv8b(f);
236 SpectrumValue tv9b(f);
237 SpectrumValue tv10b(f);
238 tv7b = doubleValue + v1;
239 tv8b = doubleValue - v1;
240 tv9b = doubleValue * v1;
241 tv10b = doubleValue / v1;
242 AddTestCase(new SpectrumValueTestCase(tv7b, v7, "tv7b = doubleValue + v1"),
244 AddTestCase(new SpectrumValueTestCase(tv8b, v8, "tv8b = doubleValue - v1"),
246 AddTestCase(new SpectrumValueTestCase(tv9b, v9, "tv9b = doubleValue * v1"),
248 AddTestCase(new SpectrumValueTestCase(tv10b, v10, "tv10b = doubleValue div v1"),
250
251 SpectrumValue v1ls3(f);
252 SpectrumValue v1rs3(f);
253 SpectrumValue tv1ls3(f);
254 SpectrumValue tv1rs3(f);
255
256 v1ls3[0] = v1[3];
257 v1ls3[1] = v1[4];
258 tv1ls3 = v1 << 3;
259 AddTestCase(new SpectrumValueTestCase(tv1ls3, v1ls3, "tv1ls3 = v1 << 3"),
261
262 v1rs3[3] = v1[0];
263 v1rs3[4] = v1[1];
264 tv1rs3 = v1 >> 3;
265 AddTestCase(new SpectrumValueTestCase(tv1rs3, v1rs3, "tv1rs3 = v1 >> 3"),
267}
268
269/**
270 * @ingroup spectrum-tests
271 *
272 * @brief Spectrum Converter TestSuite
273 */
275{
276 public:
278};
279
281 : TestSuite("spectrum-converter", Type::UNIT)
282{
283 double f;
284
285 std::vector<double> f1;
286 for (f = 3; f <= 7; f += 2)
287 {
288 f1.push_back(f);
289 }
291
292 std::vector<double> f2;
293 for (f = 2; f <= 8; f += 1)
294 {
295 f2.push_back(f);
296 }
298
300
302 *v1 = 4;
303 SpectrumConverter c12(sof1, sof2);
304 res = c12.Convert(v1);
305 SpectrumValue t12(sof2);
306 t12 = 4;
307 t12[0] = 2;
308 t12[6] = 2;
309 // NS_LOG_LOGIC(*v1);
310 // NS_LOG_LOGIC(t12);
311 // NS_LOG_LOGIC(*res);
312
314 // TEST_ASSERT(MoreOrLessEqual(t12, *res));
315
317 *v2a = -2;
318 SpectrumConverter c21(sof2, sof1);
319 res = c21.Convert(v2a);
320 SpectrumValue t21a(sof1);
321 t21a = -2;
322 // NS_LOG_LOGIC(*v2a);
323 // NS_LOG_LOGIC(t21a);
324 // NS_LOG_LOGIC(*res);
326 // TEST_ASSERT(MoreOrLessEqual(t21a, *res));
327
329 (*v2b)[0] = 3;
330 (*v2b)[1] = 5;
331 (*v2b)[2] = 1;
332 (*v2b)[3] = 2;
333 (*v2b)[4] = 4;
334 (*v2b)[5] = 6;
335 (*v2b)[6] = 3;
336 res = c21.Convert(v2b);
337 SpectrumValue t21b(sof1);
338 t21b[0] = 3 * 0.25 + 5 * 0.5 + 1 * 0.25;
339 t21b[1] = 1 * 0.25 + 2 * 0.5 + 4 * 0.25;
340 t21b[2] = 4 * 0.25 + 6 * 0.5 + 3 * 0.25;
341 // NS_LOG_LOGIC(*v2b);
342 // NS_LOG_LOGIC(t21b);
343 // NS_LOG_LOGIC(*res);
345}
346
347/// Static variable for test initialization
349/// 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.
Definition ptr.h:67
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:292
@ QUICK
Fast test.
Definition test.h:1054
TestCase(const TestCase &)=delete
Type
Type of test.
Definition test.h:1257
@ UNIT
This test suite implements a Unit Test.
Definition test.h:1259
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:490
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:439
#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.