A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
spectrum-value-test.cc
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 // NS_LOG_COMPONENT_DEFINE ("SpectrumValueTest");
32 
33 namespace ns3 {
34 
35 
36 #define TOLERANCE 1e-6
37 
39 {
40 public:
41  SpectrumValueTestCase (SpectrumValue a, SpectrumValue b, std::string name);
42  virtual ~SpectrumValueTestCase ();
43  virtual void DoRun (void);
44 
45 private:
46  bool MoreOrLessEqual (SpectrumValue x, SpectrumValue y);
47  SpectrumValue m_a;
48  SpectrumValue m_b;
49 };
50 
51 
52 
53 SpectrumValueTestCase::SpectrumValueTestCase (SpectrumValue a, SpectrumValue b, std::string name)
54  : TestCase (name),
55  m_a (a),
56  m_b (b)
57 {
58 }
59 
60 SpectrumValueTestCase::~SpectrumValueTestCase ()
61 {
62 }
63 
64 
65 bool
66 SpectrumValueTestCase::MoreOrLessEqual (SpectrumValue x, SpectrumValue y)
67 {
68  SpectrumValue z = x - y;
69  return (Norm (z) < TOLERANCE);
70 }
71 
72 void
74 {
75  NS_TEST_ASSERT_MSG_SPECTRUM_MODEL_EQ_TOL (*m_a.GetSpectrumModel (), *m_b.GetSpectrumModel (), TOLERANCE, "");
76  NS_TEST_ASSERT_MSG_SPECTRUM_VALUE_EQ_TOL (m_a, m_b, TOLERANCE, "");
77 }
78 
79 
80 
81 
82 
83 
84 
85 
87 {
88 public:
90 };
91 
92 SpectrumValueTestSuite::SpectrumValueTestSuite ()
93  : TestSuite ("spectrum-value", UNIT)
94 {
95 
96  // NS_LOG_INFO("creating SpectrumValueTestSuite");
97 
98 
99  std::vector<double> freqs;
100 
101  for (int i = 1; i <= 5; i++)
102  {
103  freqs.push_back (i);
104  }
105 
106  Ptr<SpectrumModel> f = Create<SpectrumModel> (freqs);
107 
108  SpectrumValue v1 (f), v2 (f), v3 (f), v4 (f), v5 (f), v6 (f), v7 (f), v8 (f), v9 (f), v10 (f);
109 
110  double doubleValue;
111 
112 
113  doubleValue = 1.12345600000000;
114 
115  v1[0] = 0.700539792840;
116  v1[1] = -0.554277600423;
117  v1[2] = 0.750309319469;
118  v1[3] = -0.892299213192;
119  v1[4] = 0.987045234885;
120 
121  v2[0] = 0.870441628737;
122  v2[1] = 0.271419263880;
123  v2[2] = 0.451557288312;
124  v2[3] = 0.968992859395;
125  v2[4] = -0.929186654705;
126 
127  v3[0] = 1.570981421577;
128  v3[1] = -0.282858336543;
129  v3[2] = 1.201866607781;
130  v3[3] = 0.076693646203;
131  v3[4] = 0.057858580180;
132 
133  v4[0] = -0.169901835897;
134  v4[1] = -0.825696864302;
135  v4[2] = 0.298752031158;
136  v4[3] = -1.861292072588;
137  v4[4] = 1.916231889590;
138 
139  v5[0] = 0.609778998275;
140  v5[1] = -0.150441618292;
141  v5[2] = 0.338807641695;
142  v5[3] = -0.864631566028;
143  v5[4] = -0.917149259846;
144 
145  v6[0] = 0.804809615846;
146  v6[1] = -2.042145397125;
147  v6[2] = 1.661603829438;
148  v6[3] = -0.920852207053;
149  v6[4] = -1.062267984465;
150 
151  v7[0] = 1.823995792840;
152  v7[1] = 0.569178399577;
153  v7[2] = 1.873765319469;
154  v7[3] = 0.231156786808;
155  v7[4] = 2.110501234885;
156 
157  v8[0] = -0.422916207160;
158  v8[1] = -1.677733600423;
159  v8[2] = -0.373146680531;
160  v8[3] = -2.015755213192;
161  v8[4] = -0.136410765115;
162 
163  v9[0] = 0.787025633505;
164  v9[1] = -0.622706495860;
165  v9[2] = 0.842939506814;
166  v9[3] = -1.002458904856;
167  v9[4] = 1.108901891403;
168 
169  v10[0] = 0.623557836569;
170  v10[1] = -0.493368320987;
171  v10[2] = 0.667858215604;
172  v10[3] = -0.794244913190;
173  v10[4] = 0.878579343459;
174 
175  SpectrumValue tv3 (f), tv4 (f), tv5 (f), tv6 (f);
176 
177  tv3 = v1 + v2;
178  tv4 = v1 - v2;
179  tv5 = v1 * v2;
180  tv6 = v1 / v2;
181 
182 
183  AddTestCase (new SpectrumValueTestCase (tv3, v3, "tv3 = v1 + v2"), TestCase::QUICK);
184  AddTestCase (new SpectrumValueTestCase (tv4, v4, "tv4 = v1 - v2"), TestCase::QUICK);
185  AddTestCase (new SpectrumValueTestCase (tv5, v5, "tv5 = v1 * v2"), TestCase::QUICK);
186  AddTestCase (new SpectrumValueTestCase (tv6, v6, "tv6 = v1 div v2"), TestCase::QUICK);
187 
188  // std::cerr << v6 << std::endl;
189  // std::cerr << tv6 << std::endl;
190 
191 
192  tv3 = v1;
193  tv4 = v1;
194  tv5 = v1;
195  tv6 = v1;
196 
197  tv3 += v2;
198  tv4 -= v2;
199  tv5 *= v2;
200  tv6 /= v2;
201 
202  AddTestCase (new SpectrumValueTestCase (tv3, v3, "tv3 += v2"), TestCase::QUICK);
203  AddTestCase (new SpectrumValueTestCase (tv4, v4, "tv4 -= v2"), TestCase::QUICK);
204  AddTestCase (new SpectrumValueTestCase (tv5, v5, "tv5 *= v2"), TestCase::QUICK);
205  AddTestCase (new SpectrumValueTestCase (tv6, v6, "tv6 div= v2"), TestCase::QUICK);
206 
207  SpectrumValue tv7a (f), tv8a (f), tv9a (f), tv10a (f);
208  tv7a = v1 + doubleValue;
209  tv8a = v1 - doubleValue;
210  tv9a = v1 * doubleValue;
211  tv10a = v1 / doubleValue;
212  AddTestCase (new SpectrumValueTestCase (tv7a, v7, "tv7a = v1 + doubleValue"), TestCase::QUICK);
213  AddTestCase (new SpectrumValueTestCase (tv8a, v8, "tv8a = v1 - doubleValue"), TestCase::QUICK);
214  AddTestCase (new SpectrumValueTestCase (tv9a, v9, "tv9a = v1 * doubleValue"), TestCase::QUICK);
215  AddTestCase (new SpectrumValueTestCase (tv10a, v10, "tv10a = v1 div doubleValue"), TestCase::QUICK);
216 
217  SpectrumValue tv7b (f), tv8b (f), tv9b (f), tv10b (f);
218  tv7b = doubleValue + v1;
219  tv8b = doubleValue - v1;
220  tv9b = doubleValue * v1;
221  tv10b = doubleValue / v1;
222  AddTestCase (new SpectrumValueTestCase (tv7b, v7, "tv7b = doubleValue + v1"), TestCase::QUICK);
223  AddTestCase (new SpectrumValueTestCase (tv8b, v8, "tv8b = doubleValue - v1"), TestCase::QUICK);
224  AddTestCase (new SpectrumValueTestCase (tv9b, v9, "tv9b = doubleValue * v1"), TestCase::QUICK);
225  AddTestCase (new SpectrumValueTestCase (tv10b, v10, "tv10b = doubleValue div v1"), TestCase::QUICK);
226 
227 
228 
229 
230 
231  SpectrumValue v1ls3 (f), v1rs3 (f);
232  SpectrumValue tv1ls3 (f), tv1rs3 (f);
233 
234  v1ls3[0] = v1[3];
235  v1ls3[1] = v1[4];
236  tv1ls3 = v1 << 3;
237  AddTestCase (new SpectrumValueTestCase (tv1ls3, v1ls3, "tv1ls3 = v1 << 3"), TestCase::QUICK);
238 
239 
240  v1rs3[3] = v1[0];
241  v1rs3[4] = v1[1];
242  tv1rs3 = v1 >> 3;
243  AddTestCase (new SpectrumValueTestCase (tv1rs3, v1rs3, "tv1rs3 = v1 >> 3"), TestCase::QUICK);
244 
245 
246 }
247 
248 
249 
250 
251 
252 
253 
255 {
256 public:
258 };
259 
260 SpectrumConverterTestSuite::SpectrumConverterTestSuite ()
261  : TestSuite ("spectrum-converter", UNIT)
262 {
263  double f;
264 
265 
266  std::vector<double> f1;
267  for (f = 3; f <= 7; f += 2)
268  {
269  f1.push_back (f);
270  }
271  Ptr<SpectrumModel> sof1 = Create<SpectrumModel> (f1);
272 
273 
274 
275  std::vector<double> f2;
276  for (f = 2; f <= 8; f += 1)
277  {
278  f2.push_back (f);
279  }
280  Ptr<SpectrumModel> sof2 = Create<SpectrumModel> (f2);
281 
282 
283 
284  Ptr<SpectrumValue> res;
285 
286 
287  Ptr<SpectrumValue> v1 = Create<SpectrumValue> (sof1);
288  *v1 = 4;
289  SpectrumConverter c12 (sof1, sof2);
290  res = c12.Convert (v1);
291  SpectrumValue t12 (sof2);
292  t12 = 4;
293  t12[0] = 2;
294  t12[6] = 2;
295 // NS_LOG_LOGIC(*v1);
296 // NS_LOG_LOGIC(t12);
297 // NS_LOG_LOGIC(*res);
298 
299  AddTestCase (new SpectrumValueTestCase (t12, *res, ""), TestCase::QUICK);
300  // TEST_ASSERT(MoreOrLessEqual(t12, *res));
301 
302  Ptr<SpectrumValue> v2a = Create<SpectrumValue> (sof2);
303  *v2a = -2;
304  SpectrumConverter c21 (sof2, sof1);
305  res = c21.Convert (v2a);
306  SpectrumValue t21a (sof1);
307  t21a = -2;
308 // NS_LOG_LOGIC(*v2a);
309 // NS_LOG_LOGIC(t21a);
310 // NS_LOG_LOGIC(*res);
311  AddTestCase (new SpectrumValueTestCase (t21a, *res, ""), TestCase::QUICK);
312  // TEST_ASSERT(MoreOrLessEqual(t21a, *res));
313 
314  Ptr<SpectrumValue> v2b = Create<SpectrumValue> (sof2);
315  (*v2b)[0] = 3;
316  (*v2b)[1] = 5;
317  (*v2b)[2] = 1;
318  (*v2b)[3] = 2;
319  (*v2b)[4] = 4;
320  (*v2b)[5] = 6;
321  (*v2b)[6] = 3;
322  res = c21.Convert (v2b);
323  SpectrumValue t21b (sof1);
324  t21b[0] = 3 * 0.25 + 5 * 0.5 + 1 * 0.25;
325  t21b[1] = 1 * 0.25 + 2 * 0.5 + 4 * 0.25;
326  t21b[2] = 4 * 0.25 + 6 * 0.5 + 3 * 0.25;
327 // NS_LOG_LOGIC(*v2b);
328 // NS_LOG_LOGIC(t21b);
329 // NS_LOG_LOGIC(*res);
330  AddTestCase (new SpectrumValueTestCase (t21b, *res, ""), TestCase::QUICK);
331 
332 
333 }
334 
335 
336 
337 
338 
339 
340 // static instance of test suites
341 static SpectrumValueTestSuite g_SpectrumValueTestSuite;
342 static SpectrumConverterTestSuite g_SpectrumConverterTestSuite;
343 
344 
345 
346 } // namespace ns3
A suite of tests to run.
Definition: test.h:962
encapsulates test code
Definition: test.h:834
Ptr< const SpectrumModel > GetSpectrumModel() const
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
virtual void DoRun(void)
Implementation to actually run this test case.
double Norm(const SpectrumValue &x)
Set of values corresponding to a given SpectrumModel.