A Discrete-Event Network Simulator
API
test-cosine-antenna.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 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/log.h>
22 #include <ns3/test.h>
23 #include <ns3/double.h>
24 #include <ns3/cosine-antenna-model.h>
25 #include <ns3/simulator.h>
26 #include <cmath>
27 #include <string>
28 #include <iostream>
29 #include <sstream>
30 
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE ("TestCosineAntennaModel");
35 
42  EQUAL = 0,
43  LESSTHAN = 1
44 };
45 
52 {
53 public:
62  static std::string BuildNameString (Angles a, double b, double o, double g);
72  CosineAntennaModelTestCase (Angles a, double b, double o, double g, double expectedGainDb, CosineAntennaModelGainTestCondition cond);
73 
74 
75 private:
76  virtual void DoRun (void);
78  double m_b;
79  double m_o;
80  double m_g;
81  double m_expectedGain;
83 };
84 
85 std::string CosineAntennaModelTestCase::BuildNameString (Angles a, double b, double o, double g)
86 {
87  std::ostringstream oss;
88  oss << "theta=" << a.GetInclination () << " , phi=" << a.GetAzimuth ()
89  << ", beamdwidth=" << b << "deg"
90  << ", orientation=" << o
91  << ", maxGain=" << g << " dB";
92  return oss.str ();
93 }
94 
95 
96 CosineAntennaModelTestCase::CosineAntennaModelTestCase (Angles a, double b, double o, double g, double expectedGainDb, CosineAntennaModelGainTestCondition cond)
97  : TestCase (BuildNameString (a, b, o, g)),
98  m_a (a),
99  m_b (b),
100  m_o (o),
101  m_g (g),
102  m_expectedGain (expectedGainDb),
103  m_cond (cond)
104 {
105 }
106 
107 void
109 {
110  NS_LOG_FUNCTION (this << BuildNameString (m_a, m_b, m_o, m_g));
111 
112  Ptr<CosineAntennaModel> a = CreateObject<CosineAntennaModel> ();
113  a->SetAttribute ("HorizontalBeamwidth", DoubleValue (m_b));
114  a->SetAttribute ("VerticalBeamwidth", DoubleValue (m_b));
115  a->SetAttribute ("Orientation", DoubleValue (m_o));
116  a->SetAttribute ("MaxGain", DoubleValue (m_g));
117  double actualGain = a->GetGainDb (m_a);
118  switch (m_cond)
119  {
120  case EQUAL:
121  NS_TEST_EXPECT_MSG_EQ_TOL (actualGain, m_expectedGain, 0.001, "wrong value of the radiation pattern");
122  break;
123  case LESSTHAN:
124  NS_TEST_EXPECT_MSG_LT (actualGain, m_expectedGain, "gain higher than expected");
125  break;
126  default:
127  break;
128  }
129 }
130 
131 
132 
133 
140 {
141 public:
143 };
144 
146  : TestSuite ("cosine-antenna-model", UNIT)
147 {
148  // to calculate the azimut angle offset for a given gain in db:
149  // phideg = (2*acos(10^(targetgaindb/(20*n))))*180/pi
150  // e.g., with a 60 deg beamwidth, gain is -20dB at +- 74.945 degrees from boresight
151 
152  // phi, theta, beamwidth, orientation, maxGain, expectedGain, condition
153  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (0), DegreesToRadians (90)), 60, 0, 0, 0, EQUAL), TestCase::QUICK);
154  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (30), DegreesToRadians (90)), 60, 0, 0, -3, EQUAL), TestCase::QUICK);
155  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-30), DegreesToRadians (90)), 60, 0, 0, -3, EQUAL), TestCase::QUICK);
156  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-90), DegreesToRadians (90)), 60, 0, 0, -20, LESSTHAN), TestCase::QUICK);
157  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (90), DegreesToRadians (90)), 60, 0, 0, -20, LESSTHAN), TestCase::QUICK);
158  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (100), DegreesToRadians (90)), 60, 0, 0, -20, LESSTHAN), TestCase::QUICK);
159  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (150), DegreesToRadians (90)), 60, 0, 0, -20, LESSTHAN), TestCase::QUICK);
160  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (180), DegreesToRadians (90)), 60, 0, 0, -20, LESSTHAN), TestCase::QUICK);
161  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-100), DegreesToRadians (90)), 60, 0, 0, -20, LESSTHAN), TestCase::QUICK);
162  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-150), DegreesToRadians (90)), 60, 0, 0, -20, LESSTHAN), TestCase::QUICK);
163  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-180), DegreesToRadians (90)), 60, 0, 0, -20, LESSTHAN), TestCase::QUICK);
164 
165  // test positive orientation
166  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (60), DegreesToRadians (90)), 60, 60, 0, 0, EQUAL), TestCase::QUICK);
167  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (90), DegreesToRadians (90)), 60, 60, 0, -3, EQUAL), TestCase::QUICK);
168  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (30), DegreesToRadians (90)), 60, 60, 0, -3, EQUAL), TestCase::QUICK);
169  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-30), DegreesToRadians (90)), 60, 60, 0, -20, LESSTHAN), TestCase::QUICK);
170  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (150), DegreesToRadians (90)), 60, 60, 0, -20, LESSTHAN), TestCase::QUICK);
171  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (160), DegreesToRadians (90)), 60, 60, 0, -20, LESSTHAN), TestCase::QUICK);
172  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (210), DegreesToRadians (90)), 60, 60, 0, -20, LESSTHAN), TestCase::QUICK);
173  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (240), DegreesToRadians (90)), 60, 60, 0, -20, LESSTHAN), TestCase::QUICK);
174  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-40), DegreesToRadians (90)), 60, 60, 0, -20, LESSTHAN), TestCase::QUICK);
175  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-90), DegreesToRadians (90)), 60, 60, 0, -20, LESSTHAN), TestCase::QUICK);
176  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-120), DegreesToRadians (90)), 60, 60, 0, -20, LESSTHAN), TestCase::QUICK);
177 
178  // test negative orientation and different beamwidths
179  // with a 100 deg beamwidth, gain is -20dB at +- 117.47 degrees from boresight
180  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-150), DegreesToRadians (90)), 100, -150, 0, 0, EQUAL), TestCase::QUICK);
181  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-100), DegreesToRadians (90)), 100, -150, 0, -3, EQUAL), TestCase::QUICK);
182  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-200), DegreesToRadians (90)), 100, -150, 0, -3, EQUAL), TestCase::QUICK);
183  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-32.531),DegreesToRadians (90)), 100, -150, 0, -20, EQUAL), TestCase::QUICK);
184  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (92.531), DegreesToRadians (90)), 100, -150, 0, -20, EQUAL), TestCase::QUICK);
185  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-30), DegreesToRadians (90)), 100, -150, 0, -20, LESSTHAN), TestCase::QUICK);
186  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (0), DegreesToRadians (90)), 100, -150, 0, -20, LESSTHAN), TestCase::QUICK);
187  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (60), DegreesToRadians (90)), 100, -150, 0, -20, LESSTHAN), TestCase::QUICK);
188  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (90), DegreesToRadians (90)), 100, -150, 0, -20, LESSTHAN), TestCase::QUICK);
189  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (30), DegreesToRadians (90)), 100, -150, 0, -20, LESSTHAN), TestCase::QUICK);
190  // with a 150 deg beamwidth, gain is -10dB at +- 124.93 degrees from boresight, and -20dB at +- 155.32 degrees from boresight
191  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-150), DegreesToRadians (90)), 150, -150, 0, 0, EQUAL), TestCase::QUICK);
192  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (135), DegreesToRadians (90)), 150, -150, 0, -3, EQUAL), TestCase::QUICK);
193  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-75), DegreesToRadians (90)), 150, -150, 0, -3, EQUAL), TestCase::QUICK);
194  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (85.070), DegreesToRadians (90)), 150, -150, 0, -10, EQUAL), TestCase::QUICK);
195  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-25.070),DegreesToRadians (90)), 150, -150, 0, -10, EQUAL), TestCase::QUICK);
196  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (5.3230), DegreesToRadians (90)), 150, -150, 0, -20, EQUAL), TestCase::QUICK);
197  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (54.677), DegreesToRadians (90)), 150, -150, 0, -20, EQUAL), TestCase::QUICK);
198  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (30), DegreesToRadians (90)), 150, -150, 0, -20, LESSTHAN), TestCase::QUICK);
199  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (20), DegreesToRadians (90)), 150, -150, 0, -20, LESSTHAN), TestCase::QUICK);
200  // test flat beam, with beamwidth=360 deg
201  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (0), DegreesToRadians (90)), 360, 0, 0, 0, EQUAL), TestCase::QUICK);
202  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (180), DegreesToRadians (90)), 360, 0, 0, 0, EQUAL), TestCase::QUICK);
203  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-180), DegreesToRadians (90)), 360, 0, 0, 0, EQUAL), TestCase::QUICK);
204  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (0), DegreesToRadians (0)), 360, 0, 0, 0, EQUAL), TestCase::QUICK);
205  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (0), DegreesToRadians(180)), 360, 0, 0, 0, EQUAL), TestCase::QUICK);
206 
207  // test maxGain
208  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (0), DegreesToRadians (90)), 60, 0, 10, 10, EQUAL), TestCase::QUICK);
209  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (30), DegreesToRadians (90)), 60, 0, 22, 19, EQUAL), TestCase::QUICK);
210  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-30), DegreesToRadians (90)), 60, 0, -4, -7, EQUAL), TestCase::QUICK);
211  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-90), DegreesToRadians (90)), 60, 0, 10, -10, LESSTHAN), TestCase::QUICK);
212  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (90), DegreesToRadians (90)), 60, 0, -20, -40, LESSTHAN), TestCase::QUICK);
213  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (100), DegreesToRadians (90)), 60, 0, 40, 20, LESSTHAN), TestCase::QUICK);
214  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-150), DegreesToRadians (90)), 100, -150, 2, 2, EQUAL), TestCase::QUICK);
215  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-100), DegreesToRadians (90)), 100, -150, 4, 1, EQUAL), TestCase::QUICK);
216  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-200), DegreesToRadians (90)), 100, -150, -1, -4, EQUAL), TestCase::QUICK);
217 
218 
219  // test elevation angle
220  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (0), DegreesToRadians (60)), 60, 0, 0, -3, EQUAL), TestCase::QUICK);
221  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (30), DegreesToRadians (60)), 60, 0, 0, -6, EQUAL), TestCase::QUICK);
222  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-30), DegreesToRadians (60)), 60, 0, 0, -6, EQUAL), TestCase::QUICK);
223  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-90), DegreesToRadians (60)), 60, 0, 0, -20, LESSTHAN), TestCase::QUICK);
224  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-180), DegreesToRadians (60)), 60, 0, 0, -20, LESSTHAN), TestCase::QUICK);
225  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (60), DegreesToRadians (120)), 60, 60, 0, -3, EQUAL), TestCase::QUICK);
226  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (90), DegreesToRadians (120)), 60, 60, 0, -6, EQUAL), TestCase::QUICK);
227  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (30), DegreesToRadians (120)), 60, 60, 0, -6, EQUAL), TestCase::QUICK);
228  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-120), DegreesToRadians (120)), 60, 60, 0, -20, LESSTHAN), TestCase::QUICK);
229  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-150), DegreesToRadians (140)), 100, -150, 0, -3, EQUAL), TestCase::QUICK);
230  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-100), DegreesToRadians (140)), 100, -150, 0, -6, EQUAL), TestCase::QUICK);
231  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-200), DegreesToRadians (140)), 100, -150, 0, -6, EQUAL), TestCase::QUICK);
232  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-30), DegreesToRadians (140)), 100, -150, 0, -20, LESSTHAN), TestCase::QUICK);
233  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (0), DegreesToRadians (60)), 60, 0, 10, 7, EQUAL), TestCase::QUICK);
234  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (30), DegreesToRadians (60)), 60, 0, 22, 16, EQUAL), TestCase::QUICK);
235  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-30), DegreesToRadians (60)), 60, 0, -4, -10, EQUAL), TestCase::QUICK);
236  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-90), DegreesToRadians (60)), 60, 0, 10, -13, LESSTHAN), TestCase::QUICK);
237  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (90), DegreesToRadians (60)), 60, 0, -20, -43, LESSTHAN), TestCase::QUICK);
238  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (100), DegreesToRadians (60)), 60, 0, 40, 17, LESSTHAN), TestCase::QUICK);
239  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-150), DegreesToRadians (40)), 100, -150, 2, -1, EQUAL), TestCase::QUICK);
240  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-100), DegreesToRadians (40)), 100, -150, 4, -2, EQUAL), TestCase::QUICK);
241  AddTestCase (new CosineAntennaModelTestCase (Angles (DegreesToRadians (-200), DegreesToRadians (40)), 100, -150, -1, -7, EQUAL), TestCase::QUICK);
242 
243 };
244 
CosineAntennaModel Test.
double m_expectedGain
Expected gain.
double m_b
Horizontal and Vertical Beamwidth.
CosineAntennaModelGainTestCondition m_cond
Test condition.
static std::string BuildNameString(Angles a, double b, double o, double g)
Build the test name.
virtual void DoRun(void)
Implementation to actually run this TestCase.
CosineAntennaModelTestCase(Angles a, double b, double o, double g, double expectedGainDb, CosineAntennaModelGainTestCondition cond)
Constructor.
CosineAntennaModel TestSuite.
Class holding the azimuth and inclination angles of spherical coordinates.
Definition: angles.h:119
double GetInclination(void) const
Getter for inclination angle.
Definition: angles.cc:231
double GetAzimuth(void) const
Getter for azimuth angle.
Definition: angles.cc:224
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
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
CosineAntennaModelGainTestCondition
Test condition (equal to or less than)
@ LESSTHAN
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_TEST_EXPECT_MSG_LT(actual, limit, msg)
Test that an actual value is less than a limit and report if not.
Definition: test.h:748
#define NS_TEST_EXPECT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report if ...
Definition: test.h:491
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double DegreesToRadians(double degrees)
converts degrees to radians
Definition: angles.cc:40
static CosineAntennaModelTestSuite g_staticCosineAntennaModelTestSuiteInstance
Static variable for test initialization.