author | Mitch Watrous <watrous@u.washington.edu> |
Mon, 10 Jan 2011 15:55:29 -0800 | |
changeset 6774 | 034f362af24d |
parent 6349 | 4bab6b10a034 |
child 6775 | 0783f42a364b |
permissions | -rw-r--r-- |
6349 | 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 <math.h> |
|
28 |
||
29 |
// NS_LOG_COMPONENT_DEFINE ("SpectrumValueTest"); |
|
30 |
||
31 |
namespace ns3 { |
|
32 |
||
33 |
||
34 |
#define TOLERANCE 1e-6 |
|
35 |
||
36 |
class SpectrumValueTestCase : public TestCase |
|
37 |
{ |
|
38 |
public: |
|
39 |
SpectrumValueTestCase (SpectrumValue a, SpectrumValue b, std::string name); |
|
40 |
virtual ~SpectrumValueTestCase (); |
|
41 |
virtual bool DoRun (void); |
|
42 |
||
43 |
private: |
|
44 |
bool MoreOrLessEqual (SpectrumValue x, SpectrumValue y); |
|
45 |
SpectrumValue m_a; |
|
46 |
SpectrumValue m_b; |
|
47 |
}; |
|
48 |
||
49 |
||
50 |
||
51 |
SpectrumValueTestCase::SpectrumValueTestCase (SpectrumValue a, SpectrumValue b, std::string name) |
|
52 |
: TestCase (name), |
|
53 |
m_a (a), |
|
54 |
m_b (b) |
|
55 |
{ |
|
56 |
} |
|
57 |
||
58 |
SpectrumValueTestCase::~SpectrumValueTestCase () |
|
59 |
{ |
|
60 |
} |
|
61 |
||
62 |
||
63 |
bool |
|
64 |
SpectrumValueTestCase::MoreOrLessEqual (SpectrumValue x, SpectrumValue y) |
|
65 |
{ |
|
66 |
SpectrumValue z = x - y; |
|
67 |
return (Norm (z) < TOLERANCE); |
|
68 |
} |
|
69 |
||
70 |
bool |
|
71 |
SpectrumValueTestCase::DoRun (void) |
|
72 |
{ |
|
73 |
bool error = !MoreOrLessEqual (m_a, m_b); |
|
74 |
std::cerr << m_a << std::endl |
|
75 |
<< m_b << std::endl; |
|
76 |
if (error) |
|
77 |
{ |
|
78 |
std::ostringstream actualStream; |
|
79 |
actualStream << m_a; |
|
80 |
std::ostringstream limitStream; |
|
81 |
limitStream << m_b; |
|
82 |
UpdateErrorStatus (error); |
|
83 |
ReportTestFailure (std::string ("vector elements equal"), |
|
84 |
actualStream.str (), |
|
85 |
limitStream.str (), |
|
86 |
std::string ("no message"), |
|
87 |
__FILE__, __LINE__); |
|
88 |
} |
|
89 |
return error; |
|
90 |
} |
|
91 |
||
92 |
||
93 |
||
94 |
||
95 |
||
96 |
||
97 |
||
98 |
||
99 |
class SpectrumValueTestSuite : public TestSuite |
|
100 |
{ |
|
101 |
public: |
|
102 |
SpectrumValueTestSuite (); |
|
103 |
}; |
|
104 |
||
105 |
SpectrumValueTestSuite::SpectrumValueTestSuite () |
|
106 |
: TestSuite ("spectrum-value", UNIT) |
|
107 |
{ |
|
108 |
||
109 |
// NS_LOG_INFO("creating SpectrumValueTestSuite"); |
|
110 |
||
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), v2 (f), v3 (f), v4 (f), v5 (f), v6 (f), v7 (f), v8 (f), v9 (f), v10 (f); |
|
122 |
double nv1, nv2; |
|
123 |
||
124 |
double doubleValue; |
|
125 |
||
126 |
||
127 |
doubleValue = 1.12345600000000; |
|
128 |
||
129 |
v1[0] = 0.700539792840; |
|
130 |
v1[1] = -0.554277600423; |
|
131 |
v1[2] = 0.750309319469; |
|
132 |
v1[3] = -0.892299213192; |
|
133 |
v1[4] = 0.987045234885; |
|
134 |
||
135 |
v2[0] = 0.870441628737; |
|
136 |
v2[1] = 0.271419263880; |
|
137 |
v2[2] = 0.451557288312; |
|
138 |
v2[3] = 0.968992859395; |
|
139 |
v2[4] = -0.929186654705; |
|
140 |
||
141 |
v3[0] = 1.570981421577; |
|
142 |
v3[1] = -0.282858336543; |
|
143 |
v3[2] = 1.201866607781; |
|
144 |
v3[3] = 0.076693646203; |
|
145 |
v3[4] = 0.057858580180; |
|
146 |
||
147 |
v4[0] = -0.169901835897; |
|
148 |
v4[1] = -0.825696864302; |
|
149 |
v4[2] = 0.298752031158; |
|
150 |
v4[3] = -1.861292072588; |
|
151 |
v4[4] = 1.916231889590; |
|
152 |
||
153 |
v5[0] = 0.609778998275; |
|
154 |
v5[1] = -0.150441618292; |
|
155 |
v5[2] = 0.338807641695; |
|
156 |
v5[3] = -0.864631566028; |
|
157 |
v5[4] = -0.917149259846; |
|
158 |
||
159 |
v6[0] = 0.804809615846; |
|
160 |
v6[1] = -2.042145397125; |
|
161 |
v6[2] = 1.661603829438; |
|
162 |
v6[3] = -0.920852207053; |
|
163 |
v6[4] = -1.062267984465; |
|
164 |
||
165 |
v7[0] = 1.823995792840; |
|
166 |
v7[1] = 0.569178399577; |
|
167 |
v7[2] = 1.873765319469; |
|
168 |
v7[3] = 0.231156786808; |
|
169 |
v7[4] = 2.110501234885; |
|
170 |
||
171 |
v8[0] = -0.422916207160; |
|
172 |
v8[1] = -1.677733600423; |
|
173 |
v8[2] = -0.373146680531; |
|
174 |
v8[3] = -2.015755213192; |
|
175 |
v8[4] = -0.136410765115; |
|
176 |
||
177 |
v9[0] = 0.787025633505; |
|
178 |
v9[1] = -0.622706495860; |
|
179 |
v9[2] = 0.842939506814; |
|
180 |
v9[3] = -1.002458904856; |
|
181 |
v9[4] = 1.108901891403; |
|
182 |
||
183 |
v10[0] = 0.623557836569; |
|
184 |
v10[1] = -0.493368320987; |
|
185 |
v10[2] = 0.667858215604; |
|
186 |
v10[3] = -0.794244913190; |
|
187 |
v10[4] = 0.878579343459; |
|
188 |
||
189 |
nv1 = 1.76957619675970; |
|
190 |
nv2 = 1.68451062071783; |
|
191 |
||
192 |
||
193 |
||
194 |
||
195 |
// std::cerr.precision(15); |
|
196 |
||
197 |
// std::cerr<< nv1 << " "<< v1.Norm() << std::endl; |
|
198 |
||
199 |
SpectrumValue tv3 (f), tv4 (f), tv5 (f), tv6 (f); |
|
200 |
||
201 |
tv3 = v1 + v2; |
|
202 |
tv4 = v1 - v2; |
|
203 |
tv5 = v1 * v2; |
|
204 |
tv6 = v1 / v2; |
|
205 |
||
206 |
||
207 |
AddTestCase (new SpectrumValueTestCase (tv3, v3, " tv3 = v1 + v2")); |
|
208 |
AddTestCase (new SpectrumValueTestCase (tv4, v4, " tv4 = v1 - v2")); |
|
209 |
AddTestCase (new SpectrumValueTestCase (tv5, v5, " tv5 = v1 * v2")); |
|
210 |
AddTestCase (new SpectrumValueTestCase (tv6, v6, " tv6 = v1 / v2")); |
|
211 |
||
212 |
// std::cerr << v6 << std::endl; |
|
213 |
// std::cerr << tv6 << std::endl; |
|
214 |
||
215 |
||
216 |
tv3 = v1; |
|
217 |
tv4 = v1; |
|
218 |
tv5 = v1; |
|
219 |
tv6 = v1; |
|
220 |
||
221 |
tv3 += v2; |
|
222 |
tv4 -= v2; |
|
223 |
tv5 *= v2; |
|
224 |
tv6 /= v2; |
|
225 |
||
226 |
AddTestCase (new SpectrumValueTestCase (tv3, v3, "tv3 += v2")); |
|
227 |
AddTestCase (new SpectrumValueTestCase (tv4, v4, "tv4 -= v2")); |
|
228 |
AddTestCase (new SpectrumValueTestCase (tv5, v5, "tv5 *= v2")); |
|
229 |
AddTestCase (new SpectrumValueTestCase (tv6, v6, "tv6 /= v2")); |
|
230 |
||
231 |
SpectrumValue tv7a (f), tv8a (f), tv9a (f), tv10a (f); |
|
232 |
tv7a = v1 + doubleValue; |
|
233 |
tv8a = v1 - doubleValue; |
|
234 |
tv9a = v1 * doubleValue; |
|
235 |
tv10a = v1 / doubleValue; |
|
236 |
AddTestCase (new SpectrumValueTestCase (tv7a, v7, "tv7a = v1 + doubleValue")); |
|
237 |
AddTestCase (new SpectrumValueTestCase (tv8a, v8, "tv8a = v1 - doubleValue")); |
|
238 |
AddTestCase (new SpectrumValueTestCase (tv9a, v9, "tv9a = v1 * doubleValue")); |
|
239 |
AddTestCase (new SpectrumValueTestCase (tv10a, v10, "tv10a = v1 / doubleValue")); |
|
240 |
||
241 |
SpectrumValue tv7b (f), tv8b (f), tv9b (f), tv10b (f); |
|
242 |
tv7b = doubleValue + v1; |
|
243 |
tv8b = doubleValue - v1; |
|
244 |
tv9b = doubleValue * v1; |
|
245 |
tv10b = doubleValue / v1; |
|
246 |
AddTestCase (new SpectrumValueTestCase (tv7b, v7, "tv7b = doubleValue + v1")); |
|
247 |
AddTestCase (new SpectrumValueTestCase (tv8b, v8, "tv8b = doubleValue - v1")); |
|
248 |
AddTestCase (new SpectrumValueTestCase (tv9b, v9, "tv9b = doubleValue * v1")); |
|
249 |
AddTestCase (new SpectrumValueTestCase (tv10b, v10, "tv10b = doubleValue / v1")); |
|
250 |
||
251 |
||
252 |
||
253 |
||
254 |
||
255 |
SpectrumValue v1ls3 (f), v1rs3 (f); |
|
256 |
SpectrumValue tv1ls3 (f), tv1rs3 (f); |
|
257 |
||
258 |
v1ls3[0] = v1[3]; |
|
259 |
v1ls3[1] = v1[4]; |
|
260 |
tv1ls3 = v1 << 3; |
|
261 |
AddTestCase (new SpectrumValueTestCase (tv1ls3, v1ls3, "tv1ls3 = v1 << 3")); |
|
262 |
||
263 |
||
264 |
v1rs3[3] = v1[0]; |
|
265 |
v1rs3[4] = v1[1]; |
|
266 |
tv1rs3 = v1 >> 3; |
|
267 |
AddTestCase (new SpectrumValueTestCase (tv1rs3, v1rs3, "tv1rs3 = v1 >> 3")); |
|
268 |
||
269 |
||
270 |
} |
|
271 |
||
272 |
||
273 |
||
274 |
||
275 |
||
276 |
||
277 |
||
278 |
class SpectrumConverterTestSuite : public TestSuite |
|
279 |
{ |
|
280 |
public: |
|
281 |
SpectrumConverterTestSuite (); |
|
282 |
}; |
|
283 |
||
284 |
SpectrumConverterTestSuite::SpectrumConverterTestSuite () |
|
285 |
: TestSuite ("spectrum-converter", UNIT) |
|
286 |
{ |
|
287 |
double f; |
|
288 |
||
289 |
||
290 |
std::vector<double> f1; |
|
291 |
for (f = 3; f <= 7; f += 2) |
|
292 |
{ |
|
293 |
f1.push_back (f); |
|
294 |
} |
|
295 |
Ptr<SpectrumModel> sof1 = Create<SpectrumModel> (f1); |
|
296 |
||
297 |
||
298 |
||
299 |
std::vector<double> f2; |
|
300 |
for (f = 2; f <= 8; f += 1) |
|
301 |
{ |
|
302 |
f2.push_back (f); |
|
303 |
} |
|
304 |
Ptr<SpectrumModel> sof2 = Create<SpectrumModel> (f2); |
|
305 |
||
306 |
||
307 |
||
308 |
Ptr<SpectrumValue> res; |
|
309 |
||
310 |
||
311 |
Ptr<SpectrumValue> v1 = Create<SpectrumValue> (sof1); |
|
312 |
*v1 = 4; |
|
313 |
SpectrumConverter c12 (sof1, sof2); |
|
314 |
res = c12.Convert (v1); |
|
315 |
SpectrumValue t12 (sof2); |
|
316 |
t12 = 4; |
|
317 |
t12[0] = 2; |
|
318 |
t12[6] = 2; |
|
319 |
// NS_LOG_LOGIC(*v1); |
|
320 |
// NS_LOG_LOGIC(t12); |
|
321 |
// NS_LOG_LOGIC(*res); |
|
322 |
||
323 |
AddTestCase (new SpectrumValueTestCase (t12, *res, "")); |
|
324 |
// TEST_ASSERT(MoreOrLessEqual(t12, *res)); |
|
325 |
||
326 |
Ptr<SpectrumValue> v2a = Create<SpectrumValue> (sof2); |
|
327 |
*v2a = -2; |
|
328 |
SpectrumConverter c21 (sof2, sof1); |
|
329 |
res = c21.Convert (v2a); |
|
330 |
SpectrumValue t21a (sof1); |
|
331 |
t21a = -2; |
|
332 |
// NS_LOG_LOGIC(*v2a); |
|
333 |
// NS_LOG_LOGIC(t21a); |
|
334 |
// NS_LOG_LOGIC(*res); |
|
335 |
AddTestCase (new SpectrumValueTestCase (t21a, *res, "")); |
|
336 |
// TEST_ASSERT(MoreOrLessEqual(t21a, *res)); |
|
337 |
||
338 |
Ptr<SpectrumValue> v2b = Create<SpectrumValue> (sof2); |
|
339 |
(*v2b)[0] = 3; |
|
340 |
(*v2b)[1] = 5; |
|
341 |
(*v2b)[2] = 1; |
|
342 |
(*v2b)[3] = 2; |
|
343 |
(*v2b)[4] = 4; |
|
344 |
(*v2b)[5] = 6; |
|
345 |
(*v2b)[6] = 3; |
|
346 |
res = c21.Convert (v2b); |
|
347 |
SpectrumValue t21b (sof1); |
|
348 |
t21b[0] = 3 * 0.25 + 5 * 0.5 + 1 * 0.25; |
|
349 |
t21b[1] = 1 * 0.25 + 2 * 0.5 + 4 * 0.25; |
|
350 |
t21b[2] = 4 * 0.25 + 6 * 0.5 + 3 * 0.25; |
|
351 |
// NS_LOG_LOGIC(*v2b); |
|
352 |
// NS_LOG_LOGIC(t21b); |
|
353 |
// NS_LOG_LOGIC(*res); |
|
354 |
AddTestCase (new SpectrumValueTestCase (t21b, *res, "")); |
|
355 |
||
356 |
||
357 |
} |
|
358 |
||
359 |
||
360 |
||
361 |
||
362 |
||
363 |
||
364 |
// static instance of test suites |
|
6774
034f362af24d
Make all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6349
diff
changeset
|
365 |
static SpectrumValueTestSuite g_SpectrumValueTestSuite; |
034f362af24d
Make all TestSuite instances be static
Mitch Watrous <watrous@u.washington.edu>
parents:
6349
diff
changeset
|
366 |
static SpectrumConverterTestSuite g_SpectrumConverterTestSuite; |
6349 | 367 |
|
368 |
||
369 |
||
370 |
} // namespace ns3 |