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