exception_test.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 #include <gtest/gtest.h>
35 #include <nav_core2/exceptions.h>
36 #include <string>
37 
38 TEST(Exceptions, direct_code_access)
39 {
40  // Make sure the caught exceptions have the same types as the thrown exception
41  // (This version does not create any copies of the exception)
42  try
43  {
45  }
47  {
48  EXPECT_EQ(x.getResultCode(), 12);
49  }
50 
51  try
52  {
54  }
56  {
57  EXPECT_EQ(x.getResultCode(), 12);
58  }
59 }
60 
61 TEST(Exceptions, indirect_code_access)
62 {
63  // Make sure the caught exceptions have the same types as the thrown exception
64  // (This version copies the exception to a new object with the parent type)
66  try
67  {
69  }
71  {
72  e = x;
73  }
74  EXPECT_EQ(e.getResultCode(), 12);
75 }
76 
77 TEST(Exceptions, rethrow)
78 {
79  // This version tests the ability to catch specific exceptions when rethrown
80  // A copy of the exception is made and rethrown, with the goal being able to catch the specific type
81  // and not the parent type.
83  try
84  {
86  }
88  {
89  e = std::current_exception();
90  }
91 
92  EXPECT_EQ(nav_core2::getResultCode(e), 12);
93 
94  try
95  {
96  std::rethrow_exception(e);
97  }
99  {
100  EXPECT_EQ(x.getResultCode(), 12);
101  SUCCEED();
102  }
103  catch (nav_core2::PlannerException& x)
104  {
105  FAIL() << "PlannerException caught instead of specific exception";
106  }
107 }
108 
109 TEST(Exceptions, weird_exception)
110 {
112 
113  // Check what happens with no exception
114  EXPECT_EQ(nav_core2::getResultCode(e), -1);
115 
116  // Check what happens with a non NavCore2Exception
117  try
118  {
119  std::string().at(1); // this generates an std::out_of_range
120  }
121  catch (...)
122  {
123  e = std::current_exception();
124  }
125 
126  EXPECT_EQ(nav_core2::getResultCode(e), -1);
127 }
128 
129 int main(int argc, char **argv)
130 {
131  testing::InitGoogleTest(&argc, argv);
132  return RUN_ALL_TESTS();
133 }
Parent type of all exceptions defined within.
Definition: exceptions.h:153
std::exception_ptr NavCore2ExceptionPtr
Definition: exceptions.h:88
TFSIMD_FORCE_INLINE const tfScalar & x() const
int SUCCEED
int main(int argc, char **argv)
Exception thrown when the global planner has spent too long looking for a path.
Definition: exceptions.h:287
TEST(Exceptions, direct_code_access)
int getResultCode(const NavCore2ExceptionPtr &e_ptr)
Handy function for getting the result code.
Definition: exceptions.h:93


nav_core2
Author(s):
autogenerated on Sun Jan 10 2021 04:08:27