Stl_helpers_T.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #include "stl_helpers.hpp"
40 #include "TestUtil.hpp"
41 #include <iostream>
42 
44 {
45 public:
46  Stl_helpers_T(){ eps = 1E-12;}// Default Constructor, set the precision value
47  ~Stl_helpers_T() {} // Default Desructor
48 
49 //========================================================================================
50 // maxAndMinTest will check if max and min correctly identify maximum and minimum
51 //========================================================================================
52  int maxAndMinTest(void)
53  {
54  gnsstk::TestUtil testFramework("stl_helpers", "Max and Min", __FILE__, __LINE__);
55  std::string testMesg;
56 
57  std::list<int> ilist1;
58  std::list<int> ilist2;
59  std::list<float> flist1;
60  std::list<float> flist2;
61 
62  for (int i=0; i<5; i++) {ilist1.push_back(i+1);};
63 
64  ilist2.push_back(54);
65  ilist2.push_back(-90);
66  ilist2.push_back(12);
67  ilist2.push_back(100);
68  ilist2.push_back(65);
69 
70  for (float i=0; i<5; i++) {flist1.push_back(i+1);};
71 
72  flist2.push_back(54);
73  flist2.push_back(-90);
74  flist2.push_back(12);
75  flist2.push_back(100);
76  flist2.push_back(65);
77 
78 
79  //---------------------------------------------------------------------------
80  //Verify max method functions correctly
81  //---------------------------------------------------------------------------
82  testMesg = "gnsstk::max() did not return the maximum value of a integer";
83  testFramework.assert(gnsstk::max(ilist1) == 5, testMesg, __LINE__);
84  testFramework.assert(gnsstk::max(ilist2) == 100, testMesg, __LINE__);
85  testMesg = "gnsstk::max() did not return the maximum value of a float";
86  testFramework.assert(fabs(gnsstk::max(flist1) - 5) < eps, testMesg, __LINE__);
87  testFramework.assert(fabs(gnsstk::max(flist2) - 100) < eps, testMesg, __LINE__);
88 
89  //---------------------------------------------------------------------------
90  //Verify min method functions correctly
91  //---------------------------------------------------------------------------
92  testMesg = "gnsstk::min() did not return the minimum value of a integer";
93  testFramework.assert(gnsstk::min(ilist1) == 1, testMesg, __LINE__);
94  testFramework.assert(gnsstk::min(ilist2) == -90, testMesg, __LINE__);
95  testMesg = "gnsstk::min() did not return the minimum value of a float";
96  testFramework.assert(fabs(gnsstk::min(flist1) - 1) < eps, testMesg, __LINE__);
97  testFramework.assert(fabs(gnsstk::min(flist2) + 90) < eps, testMesg, __LINE__);
98 
99  return testFramework.countFails();
100  }
101 
102 //=========================================================================================
103 // statsTest will check if the stats methods correctly identify maximum and minimum
104 //=========================================================================================
105  int statsTest(void)
106  {
107  gnsstk::TestUtil testFramework("stl_helpers", "Stats", __FILE__, __LINE__);
108  std::string testMesg;
109 
110 
111  std::list<int> ilist1;
112  std::list<float> flist1;
113 
114  for (int i=0; i<5; i++) {ilist1.push_back(i+1);};
115  for (float i=0; i<5; i++) {flist1.push_back(i+1);};
116 
117  gnsstk::stats<int> (ilist1);
118  gnsstk::stats<float> (flist1);
119 
120  float expectedN = 5.0;
121  float expectedMean = 3.0;
122  float expectedSigma = 2.5;
123  expectedSigma = std::sqrt(2.5);
124 
125  //---------------------------------------------------------------------------
126  //Verify n was calculated correctly
127  //---------------------------------------------------------------------------
128  testMesg = "The computed n value is incorrect for a integer";
129  testFramework.assert(gnsstk::stats<int>(ilist1).n == 5, testMesg, __LINE__);
130  testMesg = "The computed n value is incorrect for a float";
131  testFramework.assert(fabs(gnsstk::stats<float>(flist1).n - expectedN) < eps, testMesg, __LINE__);
132 
133  //---------------------------------------------------------------------------
134  //Verify the mean was calculated correctly
135  //---------------------------------------------------------------------------
136  testMesg = "The computed mean value is incorrect for a integer";
137  testFramework.assert(gnsstk::stats<int>(ilist1).mean == 3, testMesg, __LINE__);
138  testMesg = "The computed mean value is incorrect for a float";
139  testFramework.assert(fabs(gnsstk::stats<float>(flist1).mean - expectedMean) < eps, testMesg, __LINE__);
140 
141  //---------------------------------------------------------------------------
142  //Verify sigma was calculated correctly
143  //---------------------------------------------------------------------------
144  testMesg = "The computed sigma value is incorrect for a integer";
145  testFramework.assert(gnsstk::stats<int>(ilist1).sigma == 1, testMesg, __LINE__);
146  testMesg = "The computed sigma value is incorrect for a float";
147  testFramework.assert(fabs(gnsstk::stats<float>(flist1).sigma - expectedSigma) < eps, testMesg, __LINE__);
148 
149  return testFramework.countFails();
150  }
151 //==================================================================================================
152 // vectorIndexTest will check if vectorIndex can find the index of the first index of a element
153 //==================================================================================================
154  int vectorIndexTest(void)
155  {
156  gnsstk::TestUtil testFramework("stl_helpers", "VectorIndex", __FILE__, __LINE__);
157  std::string testMesg;
158 
159  std::vector<int> iOneTime(5);
160  for (int i=0; i<5; i++) {iOneTime[i] = i;};
161  std::vector<int> iTwoTimes(5);
162  iTwoTimes[0] = 5;
163  iTwoTimes[1] = 2;
164  iTwoTimes[2] = 3;
165  iTwoTimes[3] = 2;
166  iTwoTimes[4] = 3;
167  std::vector<int> iNone(5);
168  for (int i=10; i<15; i++) {iNone[i-10] = i;};
169 
170  std::vector<float> fOneTime(5);
171  for (float i=0; i<5; i++) {fOneTime[i] = i;};
172  std::vector<float> fTwoTimes(5);
173  fTwoTimes[0] = 5;
174  fTwoTimes[1] = 2;
175  fTwoTimes[2] = 3;
176  fTwoTimes[3] = 2;
177  fTwoTimes[4] = 3;
178  std::vector<float> fNone(5);
179  for (float i=10; i<15; i++) {iNone[i-10] = i;};
180 
181 
182  int expectedIndexOneTime = 3;
183  int expectedIndexTwoTimes = 2;
184  int expectedIndexNone = -1;
185 
186  testMesg = "VectorIndex() did not find the first instance of a given integer element";
187  testFramework.assert(gnsstk::vectorindex<int>(iOneTime, 3) == expectedIndexOneTime, testMesg, __LINE__);
188  testFramework.assert(gnsstk::vectorindex<int>(iTwoTimes, 3) == expectedIndexTwoTimes, testMesg, __LINE__);
189  testFramework.assert(gnsstk::vectorindex<int>(iNone, 3) == expectedIndexNone, testMesg, __LINE__);
190 
191  testMesg = "VectorIndex() did not find the first instance of a given float element";
192  testFramework.assert(gnsstk::vectorindex<float>(fOneTime, 3) == expectedIndexOneTime, testMesg, __LINE__);
193  testFramework.assert(gnsstk::vectorindex<float>(fTwoTimes, 3) == expectedIndexTwoTimes, testMesg, __LINE__);
194  testFramework.assert(gnsstk::vectorindex<float>(fNone, 3) == expectedIndexNone, testMesg, __LINE__);
195 
196  return testFramework.countFails();
197  }
198 
199 private:
200  double eps;
201 
202 };
203 
204 
205 int main() //Main function to initialize and run all tests above
206 {
207  Stl_helpers_T testClass;
208  int check, errorCounter = 0;
209 
210  check = testClass.maxAndMinTest();
211  errorCounter += check;
212 
213  check = testClass.statsTest();
214  errorCounter += check;
215 
216  check = testClass.vectorIndexTest();
217  errorCounter += check;
218 
219  std::cout << "Total Failures for " << __FILE__ << ": " << errorCounter << std::endl;
220 
221  return errorCounter; //Return the total number of errors
222 }
gnsstk::TestUtil::countFails
int countFails(void)
Definition: TestUtil.hpp:771
gnsstk::TestUtil::assert
void assert(bool testExpression, const std::string &testMsg, const int lineNumber)
Definition: TestUtil.hpp:607
stl_helpers.hpp
gnsstk::max
T max(const SparseMatrix< T > &SM)
Maximum element - return 0 if empty.
Definition: SparseMatrix.hpp:881
Stl_helpers_T::eps
double eps
Definition: Stl_helpers_T.cpp:200
main
int main()
Definition: Stl_helpers_T.cpp:205
TestUtil.hpp
gnsstk::min
T min(const SparseMatrix< T > &SM)
Maximum element - return 0 if empty.
Definition: SparseMatrix.hpp:858
Stl_helpers_T::vectorIndexTest
int vectorIndexTest(void)
Definition: Stl_helpers_T.cpp:154
Stl_helpers_T::statsTest
int statsTest(void)
Definition: Stl_helpers_T.cpp:105
Stl_helpers_T
Definition: Stl_helpers_T.cpp:43
Stl_helpers_T::~Stl_helpers_T
~Stl_helpers_T()
Definition: Stl_helpers_T.cpp:47
Stl_helpers_T::maxAndMinTest
int maxAndMinTest(void)
Definition: Stl_helpers_T.cpp:52
gnsstk::TestUtil
Definition: TestUtil.hpp:265
Stl_helpers_T::Stl_helpers_T
Stl_helpers_T()
Definition: Stl_helpers_T.cpp:46


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:41