Vector_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 "Vector.hpp"
40 #include "TestUtil.hpp"
41 #include <iostream>
42 
43 
44 class Vector_T
45 {
46  public:
47 
48  /* Tests the initialization of Vector objects */
50  {
51  gnsstk::TestUtil testFramework("Vector","initializationTest",__FILE__,__LINE__);
52  std::string failMesg;
53 
54  int badCount=0;
55 
56  gnsstk::Vector<double> v1(250,1.0);
57  gnsstk::Vector<double> v2(1000,5.0);
58  gnsstk::Vector<double> st(250); //Initialize without values
59  gnsstk::Vector<double> Compare(v1); //Copy Constructor
60 
61  std::valarray<double> valarray1(10.0, 250);
62  gnsstk::Vector<double> val(valarray1);
63 
64  int i = 0;
65 
66  for(i=0; i<v1.size(); i++)
67  {
68  failMesg = "Were the array values set to expectation?";
69  if (1.0 != v1[i]) {badCount++;}
70  if (1.0 != v1(i)) {badCount++;}
71  }
72  testFramework.assert(badCount==0, failMesg, __LINE__);
73  badCount = 0; // Reset error counter
74 
75  failMesg = "Was the size set to expectation?";
76  testFramework.assert(i == v1.size(), failMesg, __LINE__);
77 
78  for(i=0; i<v2.size(); i++)
79  {
80  failMesg = "Were the array values set to expectation?";
81  if (5.0 != v2[i]) {badCount++;}
82  if (5.0 != v2(i)) {badCount++;}
83  }
84  testFramework.assert(badCount==0, failMesg, __LINE__);
85  badCount = 0; // Reset error counter
86 
87  failMesg = "Was the size set to expectation?";
88  testFramework.assert(i == v2.size(), failMesg, __LINE__);
89 
90  failMesg = "Was the size set to expectation?";
91  testFramework.assert(250 == st.size(), failMesg, __LINE__);
92 
93  for(i=0; i<Compare.size(); i++)
94  {
95  failMesg = "Were the array values set to expectation?";
96  if (1.0 != Compare[i]) {badCount++;}
97  if (1.0 != Compare(i)) {badCount++;}
98  }
99  testFramework.assert(badCount==0, failMesg, __LINE__);
100  badCount = 0; // Reset error counter
101 
102  failMesg = "Was the size set to expectation?";
103  testFramework.assert(i == Compare.size(), failMesg, __LINE__);
104 
105  for(i=0; i<val.size(); i++)
106  {
107  failMesg = "Were the array values set to expectation?";
108  if (10.0 != val[i]) {badCount++;}
109  if (10.0 != val(i)) {badCount++;}
110  }
111  testFramework.assert(badCount==0, failMesg, __LINE__);
112  badCount = 0; // Reset error counter
113 
114  failMesg = "Was the size set to expectation?";
115  testFramework.assert(i == val.size(), failMesg, __LINE__);
116 
117 //===============================================================
118 // Add test for VectorBase and subvector constructors
119 //===============================================================
120 
121  return testFramework.countFails();
122  }
123 
124  /* Tests addition and subtraction between vector objects */
125  int operatorTest(void)
126  {
127  gnsstk::TestUtil testFramework( "Vector", "== Operator", __FILE__, __LINE__);
128  std::string failMesg;
129 
130  gnsstk::Vector<double> v1(3,1.0);
131  gnsstk::Vector<double> v2(12,3.0);
132  gnsstk::Vector<double> v3 = v2;
133  gnsstk::Vector<double> v4 = -v1;
134 
135  int badCount = 0;
136 
137  for(int i = 0; i < v2.size(); i++)
138  {
139  failMesg = "Are equivalent objects equivalent?";
140  if (v2[i] != v3[i]) {badCount++;}
141  }
142  testFramework.assert(badCount==0, failMesg, __LINE__);
143 
144  for(int i = 0; i < v1.size(); i++)
145  {
146  failMesg = "Are equivalent objects equivalent?";
147  if (-v1[i] != v4[i]) {badCount++;}
148  }
149  testFramework.assert(badCount==0, failMesg, __LINE__);
150 
151  testFramework.changeSourceMethod("+= Operator");
152  v2 += v3; // 6 6 6 ...
153  v2 += 2; // 8 8 8 ...
154 
155  for(int i = 0; i < v2.size(); i++)
156  {
157  failMesg = "Were the previous addition operators successful?";
158  if (8. != v2[i]) {badCount++;}
159  }
160  testFramework.assert(badCount==0, failMesg, __LINE__);
161 
162  testFramework.changeSourceMethod("+= Operator");
163  v2 -= v3; // 5 5 5 ...
164  v2 -= 4; // 1 1 1 ...
165 
166  for(int i = 0; i < v1.size(); i++)
167  {
168  failMesg = "Were the previous subtraction operators successful?";
169  if (v1[i] != v2[i]) {badCount++;} //sizes mismatch, check till v1 ends
170  }
171  testFramework.assert(badCount==0, failMesg, __LINE__);
172 
173  v2 += 2; // 3 3 3 ...
174 
175  v1 = v1&&v2; // 3 3 3 3 3 3 ...
176 
177  failMesg = "Was the previous && operators successful in joining similar vectors?";
178  testFramework.assert((size_t)15==v1.size(), failMesg, __LINE__);
179 
180  v1 = v1&&v3;
181 
182  failMesg = "Was the previous && operators successful in joining different vectors?";
183  testFramework.assert((size_t)27==v1.size(), failMesg, __LINE__);
184 
185  return testFramework.countFails();
186  }
187 };
188 
189 int main()
190 {
191  int check, errorCounter = 0;
192  Vector_T testClass;
193 
194  check = testClass.initializationTest();
195  errorCounter += check;
196 
197  check = testClass.operatorTest();
198 
199  std::cout << "Total Failures for " << __FILE__ << ": " << errorCounter << std::endl;
200 
201  return errorCounter; //Return the total number of errors
202 }
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
Vector_T::initializationTest
int initializationTest(void)
Definition: Vector_T.cpp:49
main
int main()
Definition: Vector_T.cpp:189
gnsstk::TestUtil::changeSourceMethod
void changeSourceMethod(const std::string &newMethod)
Definition: TestUtil.hpp:785
TestUtil.hpp
gnsstk::Vector< double >
Vector_T::operatorTest
int operatorTest(void)
Definition: Vector_T.cpp:125
gnsstk::Vector::size
size_t size() const
STL size.
Definition: Vector.hpp:207
Vector_T
Definition: Vector_T.cpp:44
gnsstk::TestUtil
Definition: TestUtil.hpp:265
Vector.hpp


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