satellite-geo-coordinate-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Magister Solutions Ltd
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: Sami Rantanen <sami.rantanen@magister.fi>
19  */
20 
27 #include "../utils/satellite-env-variables.h"
28 
29 #include "ns3/geo-coordinate.h"
30 #include "ns3/log.h"
31 #include "ns3/simulator.h"
32 #include "ns3/singleton.h"
33 #include "ns3/test.h"
34 
35 #include <stdint.h>
36 
37 using namespace ns3;
38 
39 // #define PRINT_POSITION_INFO // uncomment to see info while executing test
40 
41 #ifdef PRINT_POSITION_INFO
42 static void
43 PrintPositionInfo(GeoCoordinate pos)
44 {
45  Vector pos2 = pos.ToVector();
46 
47  // sets number of decimal places
48  std::cout.setf(std::ios::fixed, std::ios::floatfield);
49  std::cout.precision(15);
50  std::cout << pos.GetRefEllipsoid()
51  << " position="
52  "x="
53  << pos2.x << ", y=" << pos2.y << ", z=" << pos2.z
54  << ", longitude=" << pos.GetLongitude() << ", latitude=" << pos.GetLatitude()
55  << ", altitude=" << pos.GetAltitude() << std::endl;
56 }
57 #endif
58 
62 class GeoCoordinateTestCase : public TestCase
63 {
64  public:
66  virtual ~GeoCoordinateTestCase();
67 
68  private:
69  virtual void DoRun(void);
70  void Validate(GeoCoordinate& coord1, GeoCoordinate& coord2);
71 };
72 
74  : TestCase("Test Geo Coordinate correctness")
75 {
76 }
77 
79 {
80 }
81 
82 void
84 {
85  GeoCoordinate position1;
86  GeoCoordinate position2;
87 
88  // Set simulation output details
89  Singleton<SatEnvVariables>::Get()->DoInitialize();
90  Singleton<SatEnvVariables>::Get()->SetOutputVariables("test-geo-coordinate", "", true);
91 
92  for (int i = -180; i <= 180; i += 30)
93  {
94  position1 = GeoCoordinate(i / 2, i, i * 30);
95  position2 = GeoCoordinate(position1.ToVector());
96 
97  Validate(position1, position2);
98 
99  position1 = GeoCoordinate(i / 2, i, i * 30, GeoCoordinate::SPHERE);
100  position2 = GeoCoordinate(position1.ToVector(), GeoCoordinate::SPHERE);
101 
102  Validate(position1, position2);
103 
104  position1 = GeoCoordinate(i / 2, i, i * 30, GeoCoordinate::WGS84);
105  position2 = GeoCoordinate(position1.ToVector(), GeoCoordinate::WGS84);
106 
107  Validate(position1, position2);
108 
109  position1 = GeoCoordinate(i / 2, i, i * 30, GeoCoordinate::GRS80);
110  position2 = GeoCoordinate(position1.ToVector(), GeoCoordinate::GRS80);
111 
112  Validate(position1, position2);
113  }
114 
115  Singleton<SatEnvVariables>::Get()->DoDispose();
116 }
117 
118 void
120 {
121  double altDiff, lonDiff, latDiff = 0;
122  bool altSignSame, lonSignSame, latSignSame = false;
123 
124 #ifdef PRINT_POSITION_INFO
125  PrintPositionInfo(position1);
126  PrintPositionInfo(position2);
127 #endif
128 
129  altDiff = std::abs(position1.GetAltitude() - position2.GetAltitude());
130  lonDiff = std::abs(position1.GetLongitude() - position2.GetLongitude());
131  latDiff = std::abs(position1.GetLatitude() - position2.GetLatitude());
132 
133  altSignSame = ((position1.GetAltitude() > 0) == (position2.GetAltitude() > 0));
134  lonSignSame = ((position1.GetLongitude() > 0) == (position2.GetLongitude() > 0));
135  latSignSame = ((position1.GetLatitude() > 0) == (position2.GetLatitude() > 0));
136 
137  // check that is difference is ok
138  NS_TEST_ASSERT_MSG_LT(altDiff, 0.0001, "Altitude difference too big!");
139  NS_TEST_ASSERT_MSG_LT(lonDiff, 0.0001, "Longitude difference too big!");
140  NS_TEST_ASSERT_MSG_LT(latDiff, 0.0001, "Latitude difference too big!");
141 
142  NS_TEST_ASSERT_MSG_EQ(altSignSame, true, "Altitude signs are different.");
143  NS_TEST_ASSERT_MSG_EQ(lonSignSame, true, "Longitude signs are different.");
144  NS_TEST_ASSERT_MSG_EQ(latSignSame, true, "Latitude signs are different.");
145 }
146 
150 class GeoCoordinateTestSuite : public TestSuite
151 {
152  public:
154 };
155 
157  : TestSuite("geo-coordinate-test", Type::UNIT)
158 {
159  AddTestCase(new GeoCoordinateTestCase, TestCase::Duration::QUICK);
160 }
161 
162 // Do allocate an instance of this TestSuite
Test case to unit test that GeoCoordinate can be created with valid values.
void Validate(GeoCoordinate &coord1, GeoCoordinate &coord2)
Test suite for GeoCoordinate unit test cases.
GeoCoordinate class is used to store and operate with geodetic coordinates.
double GetAltitude() const
Gets altitude value of coordinate.
double GetLatitude() const
Gets latitude value of coordinate.
ReferenceEllipsoid_t GetRefEllipsoid()
Gets reference ellipsoid used by GeoCoordinate object.
double GetLongitude() const
Gets longitude value of coordinate.
Vector ToVector() const
Converts Geodetic coordinates to Cartesian coordinates.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.
static GeoCoordinateTestSuite geoCoordinateTestSuite