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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Nicola Baldo <nbaldo@cttc.es>
18 */
19
20#include "spectrum-test.h"
21
22#include <ns3/log.h>
23#include <ns3/object.h>
24#include <ns3/spectrum-converter.h>
25#include <ns3/spectrum-value.h>
26#include <ns3/test.h>
27
28#include <cmath>
29#include <iostream>
30
31using namespace ns3;
32
33// NS_LOG_COMPONENT_DEFINE ("SpectrumValueTest");
34
35#define TOLERANCE 1e-6
36
43{
44 public:
52 ~SpectrumValueTestCase() override;
53 void DoRun() override;
54
55 private:
63
66};
67
69 : TestCase(name),
70 m_a(a),
71 m_b(b)
72{
73}
74
76{
77}
78
79bool
81{
82 SpectrumValue z = x - y;
83 return (Norm(z) < TOLERANCE);
84}
85
86void
88{
92 "");
94}
95
102{
103 public:
105};
106
108 : TestSuite("spectrum-value", Type::UNIT)
109{
110 // NS_LOG_INFO("creating SpectrumValueTestSuite");
111
112 std::vector<double> freqs;
113
114 for (int i = 1; i <= 5; i++)
115 {
116 freqs.push_back(i);
117 }
118
119 Ptr<SpectrumModel> f = Create<SpectrumModel>(freqs);
120
121 SpectrumValue v1(f);
122 SpectrumValue v2(f);
123 SpectrumValue v3(f);
124 SpectrumValue v4(f);
125 SpectrumValue v5(f);
126 SpectrumValue v6(f);
127 SpectrumValue v7(f);
128 SpectrumValue v8(f);
129 SpectrumValue v9(f);
130 SpectrumValue v10(f);
131
132 double doubleValue;
133
134 doubleValue = 1.12345600000000;
135
136 v1[0] = 0.700539792840;
137 v1[1] = -0.554277600423;
138 v1[2] = 0.750309319469;
139 v1[3] = -0.892299213192;
140 v1[4] = 0.987045234885;
141
142 v2[0] = 0.870441628737;
143 v2[1] = 0.271419263880;
144 v2[2] = 0.451557288312;
145 v2[3] = 0.968992859395;
146 v2[4] = -0.929186654705;
147
148 v3[0] = 1.570981421577;
149 v3[1] = -0.282858336543;
150 v3[2] = 1.201866607781;
151 v3[3] = 0.076693646203;
152 v3[4] = 0.057858580180;
153
154 v4[0] = -0.169901835897;
155 v4[1] = -0.825696864302;
156 v4[2] = 0.298752031158;
157 v4[3] = -1.861292072588;
158 v4[4] = 1.916231889590;
159
160 v5[0] = 0.609778998275;
161 v5[1] = -0.150441618292;
162 v5[2] = 0.338807641695;
163 v5[3] = -0.864631566028;
164 v5[4] = -0.917149259846;
165
166 v6[0] = 0.804809615846;
167 v6[1] = -2.042145397125;
168 v6[2] = 1.661603829438;
169 v6[3] = -0.920852207053;
170 v6[4] = -1.062267984465;
171
172 v7[0] = 1.823995792840;
173 v7[1] = 0.569178399577;
174 v7[2] = 1.873765319469;
175 v7[3] = 0.231156786808;
176 v7[4] = 2.110501234885;
177
178 v8[0] = -0.422916207160;
179 v8[1] = -1.677733600423;
180 v8[2] = -0.373146680531;
181 v8[3] = -2.015755213192;
182 v8[4] = -0.136410765115;
183
184 v9[0] = 0.787025633505;
185 v9[1] = -0.622706495860;
186 v9[2] = 0.842939506814;
187 v9[3] = -1.002458904856;
188 v9[4] = 1.108901891403;
189
190 v10[0] = 0.623557836569;
191 v10[1] = -0.493368320987;
192 v10[2] = 0.667858215604;
193 v10[3] = -0.794244913190;
194 v10[4] = 0.878579343459;
195
196 SpectrumValue tv3(f);
197 SpectrumValue tv4(f);
198 SpectrumValue tv5(f);
199 SpectrumValue tv6(f);
200
201 tv3 = v1 + v2;
202 tv4 = v1 - v2;
203 tv5 = v1 * v2;
204 tv6 = v1 / v2;
205
206 AddTestCase(new SpectrumValueTestCase(tv3, v3, "tv3 = v1 + v2"), TestCase::Duration::QUICK);
207 AddTestCase(new SpectrumValueTestCase(tv4, v4, "tv4 = v1 - v2"), TestCase::Duration::QUICK);
208 AddTestCase(new SpectrumValueTestCase(tv5, v5, "tv5 = v1 * v2"), TestCase::Duration::QUICK);
209 AddTestCase(new SpectrumValueTestCase(tv6, v6, "tv6 = v1 div v2"), TestCase::Duration::QUICK);
210
211 // std::cerr << v6 << std::endl;
212 // std::cerr << tv6 << std::endl;
213
214 tv3 = v1;
215 tv4 = v1;
216 tv5 = v1;
217 tv6 = v1;
218
219 tv3 += v2;
220 tv4 -= v2;
221 tv5 *= v2;
222 tv6 /= v2;
223
224 AddTestCase(new SpectrumValueTestCase(tv3, v3, "tv3 += v2"), TestCase::Duration::QUICK);
225 AddTestCase(new SpectrumValueTestCase(tv4, v4, "tv4 -= v2"), TestCase::Duration::QUICK);
226 AddTestCase(new SpectrumValueTestCase(tv5, v5, "tv5 *= v2"), TestCase::Duration::QUICK);
227 AddTestCase(new SpectrumValueTestCase(tv6, v6, "tv6 div= v2"), TestCase::Duration::QUICK);
228
229 SpectrumValue tv7a(f);
230 SpectrumValue tv8a(f);
231 SpectrumValue tv9a(f);
232 SpectrumValue tv10a(f);
233 tv7a = v1 + doubleValue;
234 tv8a = v1 - doubleValue;
235 tv9a = v1 * doubleValue;
236 tv10a = v1 / doubleValue;
237 AddTestCase(new SpectrumValueTestCase(tv7a, v7, "tv7a = v1 + doubleValue"),
238 TestCase::Duration::QUICK);
239 AddTestCase(new SpectrumValueTestCase(tv8a, v8, "tv8a = v1 - doubleValue"),
240 TestCase::Duration::QUICK);
241 AddTestCase(new SpectrumValueTestCase(tv9a, v9, "tv9a = v1 * doubleValue"),
242 TestCase::Duration::QUICK);
243 AddTestCase(new SpectrumValueTestCase(tv10a, v10, "tv10a = v1 div doubleValue"),
244 TestCase::Duration::QUICK);
245
246 SpectrumValue tv7b(f);
247 SpectrumValue tv8b(f);
248 SpectrumValue tv9b(f);
249 SpectrumValue tv10b(f);
250 tv7b = doubleValue + v1;
251 tv8b = doubleValue - v1;
252 tv9b = doubleValue * v1;
253 tv10b = doubleValue / v1;
254 AddTestCase(new SpectrumValueTestCase(tv7b, v7, "tv7b = doubleValue + v1"),
255 TestCase::Duration::QUICK);
256 AddTestCase(new SpectrumValueTestCase(tv8b, v8, "tv8b = doubleValue - v1"),
257 TestCase::Duration::QUICK);
258 AddTestCase(new SpectrumValueTestCase(tv9b, v9, "tv9b = doubleValue * v1"),
259 TestCase::Duration::QUICK);
260 AddTestCase(new SpectrumValueTestCase(tv10b, v10, "tv10b = doubleValue div v1"),
261 TestCase::Duration::QUICK);
262
263 SpectrumValue v1ls3(f);
264 SpectrumValue v1rs3(f);
265 SpectrumValue tv1ls3(f);
266 SpectrumValue tv1rs3(f);
267
268 v1ls3[0] = v1[3];
269 v1ls3[1] = v1[4];
270 tv1ls3 = v1 << 3;
271 AddTestCase(new SpectrumValueTestCase(tv1ls3, v1ls3, "tv1ls3 = v1 << 3"),
272 TestCase::Duration::QUICK);
273
274 v1rs3[3] = v1[0];
275 v1rs3[4] = v1[1];
276 tv1rs3 = v1 >> 3;
277 AddTestCase(new SpectrumValueTestCase(tv1rs3, v1rs3, "tv1rs3 = v1 >> 3"),
278 TestCase::Duration::QUICK);
279}
280
287{
288 public:
290};
291
293 : TestSuite("spectrum-converter", Type::UNIT)
294{
295 double f;
296
297 std::vector<double> f1;
298 for (f = 3; f <= 7; f += 2)
299 {
300 f1.push_back(f);
301 }
302 Ptr<SpectrumModel> sof1 = Create<SpectrumModel>(f1);
303
304 std::vector<double> f2;
305 for (f = 2; f <= 8; f += 1)
306 {
307 f2.push_back(f);
308 }
309 Ptr<SpectrumModel> sof2 = Create<SpectrumModel>(f2);
310
312
313 Ptr<SpectrumValue> v1 = Create<SpectrumValue>(sof1);
314 *v1 = 4;
315 SpectrumConverter c12(sof1, sof2);
316 res = c12.Convert(v1);
317 SpectrumValue t12(sof2);
318 t12 = 4;
319 t12[0] = 2;
320 t12[6] = 2;
321 // NS_LOG_LOGIC(*v1);
322 // NS_LOG_LOGIC(t12);
323 // NS_LOG_LOGIC(*res);
324
325 AddTestCase(new SpectrumValueTestCase(t12, *res, ""), TestCase::Duration::QUICK);
326 // TEST_ASSERT(MoreOrLessEqual(t12, *res));
327
328 Ptr<SpectrumValue> v2a = Create<SpectrumValue>(sof2);
329 *v2a = -2;
330 SpectrumConverter c21(sof2, sof1);
331 res = c21.Convert(v2a);
332 SpectrumValue t21a(sof1);
333 t21a = -2;
334 // NS_LOG_LOGIC(*v2a);
335 // NS_LOG_LOGIC(t21a);
336 // NS_LOG_LOGIC(*res);
337 AddTestCase(new SpectrumValueTestCase(t21a, *res, ""), TestCase::Duration::QUICK);
338 // TEST_ASSERT(MoreOrLessEqual(t21a, *res));
339
340 Ptr<SpectrumValue> v2b = Create<SpectrumValue>(sof2);
341 (*v2b)[0] = 3;
342 (*v2b)[1] = 5;
343 (*v2b)[2] = 1;
344 (*v2b)[3] = 2;
345 (*v2b)[4] = 4;
346 (*v2b)[5] = 6;
347 (*v2b)[6] = 3;
348 res = c21.Convert(v2b);
349 SpectrumValue t21b(sof1);
350 t21b[0] = 3 * 0.25 + 5 * 0.5 + 1 * 0.25;
351 t21b[1] = 1 * 0.25 + 2 * 0.5 + 4 * 0.25;
352 t21b[2] = 4 * 0.25 + 6 * 0.5 + 3 * 0.25;
353 // NS_LOG_LOGIC(*v2b);
354 // NS_LOG_LOGIC(t21b);
355 // NS_LOG_LOGIC(*res);
356 AddTestCase(new SpectrumValueTestCase(t21b, *res, ""), TestCase::Duration::QUICK);
357}
358
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:77
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:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
#define NS_TEST_ASSERT_MSG_SPECTRUM_MODEL_EQ_TOL(actual, expected, tol, msg)
Test if two SpectrumModel instances are equal within a given tolerance.
Definition: spectrum-test.h:48
#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.