A Discrete-Event Network Simulator
API
rand-cart-around-geo-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 University of Washington
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: Benjamin Cizdziel <ben.cizdziel@gmail.com>
19  */
20 
21 #include <ns3/test.h>
22 #include <ns3/log.h>
23 #include <cmath>
24 #include <ns3/geographic-positions.h>
25 
45 NS_LOG_COMPONENT_DEFINE ("RandCartAroundGeoTest");
46 
47 using namespace ns3;
48 
49 // 0.1 meter tolerance for testing, which is very small compared to the maximum
50 // distances from origin being tested
51 const double TOLERANCE = 0.1;
52 
53 // earth's radius in meters if modeled as a perfect sphere
54 static const double EARTH_RADIUS = 6371e3;
55 
57 {
58 public:
59  RandCartAroundGeoTestCase (double originLatitude,
60  double originLongitude,
61  double maxAltitude,
62  int numPoints,
63  double maxDistFromOrigin,
65  virtual ~RandCartAroundGeoTestCase ();
66 
67 private:
68  virtual void DoRun (void);
69  static std::string Name (double originLatitude,
70  double originLongitude,
71  double maxDistFromOrigin);
74  double m_maxAltitude;
78 };
79 
80 std::string
81 RandCartAroundGeoTestCase::Name (double originLatitude,
82  double originLongitude,
83  double maxDistFromOrigin)
84 {
85  std::ostringstream oss;
86  oss << "origin latitude = " << originLatitude << " degrees, "
87  << "origin longitude = " << originLongitude << " degrees, "
88  << "max distance from origin = " << maxDistFromOrigin;
89  return oss.str();
90 }
91 
93  double originLongitude,
94  double maxAltitude,
95  int numPoints,
96  double maxDistFromOrigin,
98  : TestCase (Name (originLatitude, originLongitude, maxDistFromOrigin)),
99  m_originLatitude (originLatitude),
100  m_originLongitude (originLongitude),
101  m_maxAltitude (maxAltitude),
102  m_numPoints (numPoints),
103  m_maxDistFromOrigin (maxDistFromOrigin),
104  m_uniRand (uniRand)
105 {
106 }
107 
109 {
110 }
111 
112 void
114 {
115  std::list<Vector> points = GeographicPositions::RandCartesianPointsAroundGeographicPoint (m_originLatitude,
117  m_maxAltitude,
118  m_numPoints,
120  m_uniRand);
121  Vector origin = GeographicPositions::GeographicToCartesianCoordinates (m_originLatitude,
124  GeographicPositions::SPHERE);
125  Vector randPoint;
126  while (!points.empty ())
127  {
128  randPoint = points.front ();
129  points.pop_front ();
130 
131  // pythagorean distance between random point and origin, not distance on surface of earth
132  double straightDistFromOrigin = sqrt (pow (randPoint.x - origin.x, 2) +
133  pow (randPoint.y - origin.y, 2) +
134  pow (randPoint.z - origin.z, 2));
135 
136  // arc length distance between random point and origin, on surface of earth
137  double arcDistFromOrigin = 2 * EARTH_RADIUS * asin (straightDistFromOrigin / (2 * EARTH_RADIUS));
138 
139  NS_TEST_ASSERT_MSG_LT (arcDistFromOrigin,
141  "random point (" << randPoint.x << ", " << randPoint.y
142  << ", " << randPoint.z << ") is outside of max radius from origin");
143  }
144 }
145 
146 
148 {
149 public:
151 };
152 
154  : TestSuite ("rand-cart-around-geo", UNIT)
155 {
156  NS_LOG_INFO ("creating RandCartAroundGeoTestSuite");
157  Ptr<UniformRandomVariable> uniRand = CreateObject<UniformRandomVariable> ();
158  uniRand->SetStream (5);
159  for (double originLatitude = -89.9; originLatitude <= 89.9; originLatitude += 35.96)
160  {
161  for (double originLongitude = 0; originLongitude <= 360; originLongitude += 72)
162  {
163  for (double maxDistFromOrigin = 1000; maxDistFromOrigin <= 1000000; maxDistFromOrigin *= 10)
164  {
165  AddTestCase (new RandCartAroundGeoTestCase (originLatitude,
166  originLongitude,
167  0, // on earth's surface
168  50, // 50 points generated
169  maxDistFromOrigin,
170  uniRand),
171  TestCase::QUICK);
172  }
173  }
174  }
175 }
176 
virtual void DoRun(void)
Implementation to actually run this TestCase.
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
A suite of tests to run.
Definition: test.h:1333
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
encapsulates test code
Definition: test.h:1147
RandCartAroundGeoTestCase(double originLatitude, double originLongitude, double maxAltitude, int numPoints, double maxDistFromOrigin, Ptr< UniformRandomVariable > uniRand)
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:97
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
static std::string Name(double originLatitude, double originLongitude, double maxDistFromOrigin)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static RandCartAroundGeoTestSuite g_RandCartAroundGeoTestSuite
static const double EARTH_RADIUS
Earth's radius in meters if modeled as a perfect sphere.
const double TOLERANCE
Ptr< UniformRandomVariable > m_uniRand
static const double EARTH_RADIUS
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.
#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:804