A Discrete-Event Network Simulator
API
rand-cart-around-geo-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 University of Washington
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: Benjamin Cizdziel <ben.cizdziel@gmail.com>
18  */
19 
20 #include <ns3/geographic-positions.h>
21 #include <ns3/log.h>
22 #include <ns3/test.h>
23 
24 #include <cmath>
25 
45 NS_LOG_COMPONENT_DEFINE("RandCartAroundGeoTest");
46 
47 using namespace ns3;
48 
53 const double TOLERANCE = 0.1;
54 
56 static const double EARTH_RADIUS = 6371e3;
57 
64 {
65  public:
76  RandCartAroundGeoTestCase(double originLatitude,
77  double originLongitude,
78  double maxAltitude,
79  int numPoints,
80  double maxDistFromOrigin,
82  ~RandCartAroundGeoTestCase() override;
83 
84  private:
85  void DoRun() override;
93  static std::string Name(double originLatitude,
94  double originLongitude,
95  double maxDistFromOrigin);
98  double m_maxAltitude;
102 };
103 
104 std::string
105 RandCartAroundGeoTestCase::Name(double originLatitude,
106  double originLongitude,
107  double maxDistFromOrigin)
108 {
109  std::ostringstream oss;
110  oss << "origin latitude = " << originLatitude << " degrees, "
111  << "origin longitude = " << originLongitude << " degrees, "
112  << "max distance from origin = " << maxDistFromOrigin;
113  return oss.str();
114 }
115 
117  double originLongitude,
118  double maxAltitude,
119  int numPoints,
120  double maxDistFromOrigin,
122  : TestCase(Name(originLatitude, originLongitude, maxDistFromOrigin)),
123  m_originLatitude(originLatitude),
124  m_originLongitude(originLongitude),
125  m_maxAltitude(maxAltitude),
126  m_numPoints(numPoints),
127  m_maxDistFromOrigin(maxDistFromOrigin),
128  m_uniRand(uniRand)
129 {
130 }
131 
133 {
134 }
135 
136 void
138 {
139  std::list<Vector> points =
140  GeographicPositions::RandCartesianPointsAroundGeographicPoint(m_originLatitude,
143  m_numPoints,
145  m_uniRand);
146  Vector origin =
147  GeographicPositions::GeographicToCartesianCoordinates(m_originLatitude,
150  GeographicPositions::SPHERE);
151  Vector randPoint;
152  while (!points.empty())
153  {
154  randPoint = points.front();
155  points.pop_front();
156 
157  // pythagorean distance between random point and origin, not distance on surface of earth
158  double straightDistFromOrigin =
159  sqrt(pow(randPoint.x - origin.x, 2) + pow(randPoint.y - origin.y, 2) +
160  pow(randPoint.z - origin.z, 2));
161 
162  // arc length distance between random point and origin, on surface of earth
163  double arcDistFromOrigin =
164  2 * EARTH_RADIUS * asin(straightDistFromOrigin / (2 * EARTH_RADIUS));
165 
166  NS_TEST_ASSERT_MSG_LT(arcDistFromOrigin,
168  "random point (" << randPoint.x << ", " << randPoint.y << ", "
169  << randPoint.z
170  << ") is outside of max radius from origin");
171  }
172 }
173 
180 {
181  public:
183 };
184 
186  : TestSuite("rand-cart-around-geo", UNIT)
187 {
188  NS_LOG_INFO("creating RandCartAroundGeoTestSuite");
189  Ptr<UniformRandomVariable> uniRand = CreateObject<UniformRandomVariable>();
190  uniRand->SetStream(5);
191  for (double originLatitude = -89.9; originLatitude <= 89.9; originLatitude += 35.96)
192  {
193  for (double originLongitude = 0; originLongitude <= 360; originLongitude += 72)
194  {
195  for (double maxDistFromOrigin = 1000; maxDistFromOrigin <= 1000000;
196  maxDistFromOrigin *= 10)
197  {
198  AddTestCase(new RandCartAroundGeoTestCase(originLatitude,
199  originLongitude,
200  0, // on earth's surface
201  50, // 50 points generated
202  maxDistFromOrigin,
203  uniRand),
204  TestCase::QUICK);
205  }
206  }
207  }
208 }
209 
Rand Cart Around Geo Test Case.
double m_maxAltitude
maximum altitude
void DoRun() override
Implementation to actually run this TestCase.
double m_originLongitude
origin longitude
double m_originLatitude
origin latitude
static std::string Name(double originLatitude, double originLongitude, double maxDistFromOrigin)
name function
RandCartAroundGeoTestCase(double originLatitude, double originLongitude, double maxAltitude, int numPoints, double maxDistFromOrigin, Ptr< UniformRandomVariable > uniRand)
Constructor.
double m_maxDistFromOrigin
maximum distance from origin
Ptr< UniformRandomVariable > m_uniRand
random number
Rand Cart Around Geo Test Suite.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
static RandCartAroundGeoTestSuite g_RandCartAroundGeoTestSuite
Static variable for test initialization.
#define NS_TEST_ASSERT_MSG_LT(actual, limit, msg)
Test that an actual value is less than a limit and report and abort if not.
Definition: test.h:709
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static constexpr double EARTH_RADIUS
Earth's radius in meters if modeled as a perfect sphere.
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.
static const double EARTH_RADIUS
earth's radius in meters if modeled as a perfect sphere
const double TOLERANCE
0.1 meter tolerance for testing, which is very small compared to the maximum distances from origin be...
static std::string Name(std::string str, uint32_t totalStreamSize, uint32_t sourceWriteSize, uint32_t serverReadSize, uint32_t serverWriteSize, uint32_t sourceReadSize, bool useIpv6)
Definition: tcp-test.cc:166