Exception_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 "TestUtil.hpp"
40 #include "Exception.hpp"
41 #include <iostream>
42 #include <cmath>
43 #include <typeinfo>
44 
45 using namespace std;
46 
47 // part of the test for this macro
48 NEW_EXCEPTION_CLASS(TestExceptionClass, gnsstk::InvalidParameter);
49 
51 {
52 public:
55  int testThrowTypes();
74  int testErrorSeverity();
80  int testMacros();
81 
82 private:
84  template <class EXC>
85  int throwType();
86 
88  void throwSomething(gnsstk::TestUtil& testFramework);
90  void rethrowSomething(gnsstk::TestUtil& testFramework);
91 
92  int cowLine1, cowLine2;
93 };
94 
95 
98 {
99  int total = 0;
100  total += throwType<gnsstk::Exception>();
101  total += throwType<gnsstk::InvalidParameter>();
102  total += throwType<gnsstk::InvalidRequest>();
103  total += throwType<gnsstk::AssertionFailure>();
104  total += throwType<gnsstk::AccessError>();
105  total += throwType<gnsstk::IndexOutOfBoundsException>();
106  total += throwType<gnsstk::InvalidArgumentException>();
107  total += throwType<gnsstk::ConfigurationException>();
108  total += throwType<gnsstk::FileMissingException>();
109  total += throwType<gnsstk::SystemSemaphoreException>();
110  total += throwType<gnsstk::SystemPipeException>();
111  total += throwType<gnsstk::SystemQueueException>();
112  total += throwType<gnsstk::OutOfMemory>();
113  total += throwType<gnsstk::ObjectNotFound>();
114  total += throwType<gnsstk::NullPointerException>();
115  total += throwType<gnsstk::UnimplementedException>();
116  return total;
117 }
118 
119 
120 int Exception_T ::
122 {
123  TUDEF("Exception", "getErrorId()");
124  unsigned loc1Line = 0, loc2Line = 0;
125  int locFails = 0;
126  std::string fn("testErrorSeverity");
127  try
128  {
130  // do not separate these statements
131  loc1Line = __LINE__; GNSSTK_THROW(exc);
132  testFramework.changeSourceMethod("GNSSTK_THROW");
133  TUFAIL("Did not throw an exception when expected");
134  }
135  catch (gnsstk::Exception &exc)
136  {
137  testFramework.changeSourceMethod("GNSSTK_THROW");
138  TUPASS("GNSSTK_THROW");
139  testFramework.changeSourceMethod("getErrorId");
140  TUASSERTE(unsigned long, 1234, exc.getErrorId());
141  testFramework.changeSourceMethod("isRecoverable");
142  testFramework.assert(exc.isRecoverable(), "Incorrect severity", __LINE__);
143  testFramework.changeSourceMethod("getLocationCount");
144  TUASSERTE(size_t, 1, exc.getLocationCount());
145  testFramework.changeSourceMethod("getTextCount");
146  TUASSERTE(size_t, 1, exc.getTextCount());
147  testFramework.changeSourceMethod("addText");
148  exc.addText("another text");
149  TUASSERTE(size_t, 2, exc.getTextCount());
150  testFramework.changeSourceMethod("addLocation");
151  // do not separate these statements
152  loc2Line = __LINE__; gnsstk::ExceptionLocation here(__FILE__,fn,__LINE__);
153  exc.addLocation(here);
154  TUASSERTE(size_t, 2, exc.getLocationCount());
155  if (exc.getLocationCount() == 2)
156  {
157  gnsstk::TestUtil test2("ExceptionLocation", "", __FILE__, __LINE__);
160  // can't change class in gnsstk::TestUtil, oh well
161  testFramework.changeSourceMethod("getLineNumber");
162  TUASSERTE(unsigned long, loc1Line, loc1.getLineNumber());
163  TUASSERTE(unsigned long, loc2Line, loc2.getLineNumber());
164  testFramework.changeSourceMethod("getFileName");
165  TUASSERTE(std::string, std::string(__FILE__), loc1.getFileName());
166  TUASSERTE(std::string, std::string(__FILE__), loc2.getFileName());
167  testFramework.changeSourceMethod("getFunctionName");
168 #if defined ( __FUNCTION__ )
169  TUASSERTE(std::string, std::string(__FUNCTION__), loc1.getFunctionName());
170 #else
171  TUASSERTE(std::string, std::string(""), loc1.getFunctionName());
172 #endif
173  TUASSERTE(std::string, fn, loc2.getFunctionName());
174  }
175  }
176  catch (...)
177  {
178  testFramework.changeSourceMethod("GNSSTK_THROW");
179  TUFAIL("Threw an unexpected exception type");
180  }
181  try
182  {
183  // Initialize error id as 5678 then change it using
184  // setErrorId to make sure that method works. Likewise with
185  // setSeverity()
187  exc.setErrorId(9012);
189  GNSSTK_THROW(exc);
190  testFramework.changeSourceMethod("GNSSTK_THROW");
191  TUFAIL("Did not throw an exception when expected");
192  }
193  catch (gnsstk::Exception &exc)
194  {
195  testFramework.changeSourceMethod("GNSSTK_THROW");
196  TUPASS("GNSSTK_THROW");
197  testFramework.changeSourceMethod("getErrorId");
198  TUASSERTE(unsigned long, 9012, exc.getErrorId());
199  testFramework.changeSourceMethod("isRecoverable");
200  testFramework.assert(!exc.isRecoverable(), "Incorrect severity", __LINE__);
201  }
202  catch (...)
203  {
204  testFramework.changeSourceMethod("GNSSTK_THROW");
205  TUFAIL("Threw an unexpected exception type");
206  }
207  return testFramework.countFails();
208 }
209 
210 
211 int Exception_T ::
213 {
214  TUDEF("Exception", "macros");
215 
216  // test GNSSTK_ASSERT
217  testFramework.changeSourceMethod("GNSSTK_ASSERT");
218  try
219  {
220  GNSSTK_ASSERT(false);
221  TUFAIL("Did not throw AssertionFailure exception");
222  }
223  catch (gnsstk::AssertionFailure &exc)
224  {
225  TUPASS("GNSSTK_ASSERT");
226  }
227  catch (...)
228  {
229  TUFAIL("Threw a different exception from AssertionFailure");
230  }
231 
232  // test NEW_EXCEPTION_CLASS
233  testFramework.changeSourceMethod("NEW_EXCEPTION_CLASS");
234  try
235  {
236  TestExceptionClass exc("moo");
237  GNSSTK_THROW(exc);
238  TUFAIL("Did not throw TestExceptionClass");
239  }
240  catch (gnsstk::InvalidParameter)
241  {
242  // make sure the exception class is a child of InvalidParameter
243  TUPASS("NEW_EXCEPTION_CLASS");
244  }
245  catch (...)
246  {
247  TUFAIL("Threw a different exception from InvalidParameter");
248  }
249 
250  // test GNSSTK_RETHROW
251  testFramework.changeSourceMethod("GNSSTK_RETHROW");
252  try
253  {
254  rethrowSomething(testFramework);
255  TUFAIL("rethrowSomething didn't throw exception");
256  }
257  catch(gnsstk::InvalidRequest& exc)
258  {
259  TUASSERTE(size_t, 2, exc.getLocationCount());
260  if (exc.getLocationCount() == 2)
261  {
262  gnsstk::ExceptionLocation loc1 = exc.getLocation();
263  gnsstk::ExceptionLocation loc2 = exc.getLocation(1);
264  TUASSERTE(unsigned long, cowLine1, loc1.getLineNumber());
265  TUASSERTE(unsigned long, cowLine2, loc2.getLineNumber());
266  }
267  }
268  catch (...)
269  {
270  TUFAIL("rethrowSomething threw unexpected exception type");
271  }
272 
273  return testFramework.countFails();
274 }
275 
276 
277 template <class EXC>
278 int Exception_T ::
280 {
281  TUDEF(typeid(EXC).name(), "GNSSTK_THROW");
282  try
283  {
284  EXC exc("fail");
285  GNSSTK_THROW(exc);
286  TUFAIL("Did not throw an exception when expected");
287  }
288  catch (gnsstk::Exception &exc)
289  {
290  // all Exception classes are expected to be children of
291  // gnsstk::Exception
292  TUPASS("GNSSTK_THROW");
293  // Removed - clearly not working and probably not being used.
294  // Should the interface be removed from the Exception
295  // implementation as well?
296  //testFramework.changeSourceMethod("getName");
297  //TUASSERTE(std::string, std::string(typeid(EXC).name()), exc.getName());
298  }
299  catch (...)
300  {
301  TUFAIL("Threw an unexpected exception type");
302  }
303  return testFramework.countFails();
304 }
305 
306 
307 void Exception_T ::
309 {
310  gnsstk::InvalidRequest exc("cow");
311  cowLine1 = __LINE__; GNSSTK_THROW(exc);
312 }
313 
314 
315 void Exception_T ::
317 {
318  try
319  {
320  throwSomething(testFramework);
321  TUFAIL("throwSomething didn't throw exception");
322  }
323  catch (gnsstk::InvalidRequest &exc)
324  {
325  TUPASS("throwSomething");
326  cowLine2 = __LINE__; GNSSTK_RETHROW(exc);
327  }
328  catch (...)
329  {
330  TUFAIL("throwSomething threw unexpected exception type");
331  }
332 }
333 
334 
335 int main(int argc, char *argv[])
336 {
337  int errorTotal = 0;
338  Exception_T testClass;
339 
340  errorTotal += testClass.testThrowTypes();
341  errorTotal += testClass.testErrorSeverity();
342  errorTotal += testClass.testMacros();
343 
344  cout << "Total Failures for " << __FILE__ << ": " << errorTotal << endl;
345 
346  return errorTotal;
347 }
gnsstk::Exception::setSeverity
Exception & setSeverity(const Severity &sever)
Definition: Exception.hpp:262
Exception_T::cowLine2
int cowLine2
Definition: Exception_T.cpp:92
gnsstk::Exception::isRecoverable
bool isRecoverable() const
Definition: Exception.hpp:254
NEW_EXCEPTION_CLASS
NEW_EXCEPTION_CLASS(TestExceptionClass, gnsstk::InvalidParameter)
main
int main(int argc, char *argv[])
Definition: Exception_T.cpp:335
Exception_T::throwType
int throwType()
Template function used by throwTypes()
Definition: Exception_T.cpp:279
TUASSERTE
#define TUASSERTE(TYPE, EXP, GOT)
Definition: TestUtil.hpp:81
TUFAIL
#define TUFAIL(MSG)
Definition: TestUtil.hpp:228
gnsstk::Exception::getTextCount
size_t getTextCount() const
Returns the number of text strings in the exception text stack.
Definition: Exception.cpp:152
Exception_T::testErrorSeverity
int testErrorSeverity()
Definition: Exception_T.cpp:121
gnsstk::ExceptionLocation::getLineNumber
unsigned long getLineNumber() const
Accessor for line of source file where exception occurred.
Definition: Exception.hpp:97
Exception_T::throwSomething
void throwSomething(gnsstk::TestUtil &testFramework)
Throw an exception for testMacros.
Definition: Exception_T.cpp:308
gnsstk::Exception::addLocation
Exception & addLocation(const ExceptionLocation &location)
Definition: Exception.cpp:108
gnsstk::Exception
Definition: Exception.hpp:151
TestUtil.hpp
gnsstk::Exception::getLocationCount
size_t getLocationCount() const
Definition: Exception.cpp:128
TUPASS
#define TUPASS(MSG)
Definition: TestUtil.hpp:230
TUDEF
#define TUDEF(CLASS, METHOD)
Definition: TestUtil.hpp:56
Exception_T::testThrowTypes
int testThrowTypes()
Definition: Exception_T.cpp:97
GNSSTK_RETHROW
#define GNSSTK_RETHROW(exc)
Definition: Exception.hpp:369
gnsstk::Exception::setErrorId
Exception & setErrorId(const unsigned long &errId)
Definition: Exception.hpp:220
gnsstk::Exception::recoverable
@ recoverable
Definition: Exception.hpp:158
Exception_T::testMacros
int testMacros()
Definition: Exception_T.cpp:212
GNSSTK_ASSERT
#define GNSSTK_ASSERT(CONDITION)
Provide an "ASSERT" type macro.
Definition: Exception.hpp:373
Exception_T
Definition: Exception_T.cpp:50
Exception_T::rethrowSomething
void rethrowSomething(gnsstk::TestUtil &testFramework)
Throw an exception for testMacros.
Definition: Exception_T.cpp:316
Exception.hpp
std
Definition: Angle.hpp:142
gnsstk::Exception::getErrorId
unsigned long getErrorId() const
Returns the error ID of the exception.
Definition: Exception.hpp:212
gnsstk::Exception::unrecoverable
@ unrecoverable
Definition: Exception.hpp:157
gnsstk::Exception::getLocation
const ExceptionLocation getLocation(const size_t &index=0) const
Definition: Exception.cpp:115
gnsstk::Exception::addText
Exception & addText(const std::string &errorText)
Definition: Exception.cpp:133
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
example5.fn
string fn
Definition: example5.py:10
gnsstk::ExceptionLocation::getFileName
std::string getFileName() const
Accessor for name of source file where exception occurred.
Definition: Exception.hpp:91
gnsstk::ExceptionLocation
Definition: Exception.hpp:69
gnsstk::TestUtil
Definition: TestUtil.hpp:265
gnsstk::ExceptionLocation::getFunctionName
std::string getFunctionName() const
Accessor for name of function where exception occurred.
Definition: Exception.hpp:94


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