Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AssertAndThrow_UnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_Assert.hpp"
12 
13 
14 namespace {
15 
16 
18 {
19  const int i = 5;
21  TEUCHOS_TEST_FOR_EXCEPTION(i != 5, std::logic_error, "Blah blah blah")
22  );
23 }
24 
25 
27 {
28  const int i = 5;
29  TEST_THROW(
30  TEUCHOS_TEST_FOR_EXCEPTION(i == 5, std::logic_error, "Blah blah blah"),
31  std::logic_error
32  );
33 }
34 
35 
37 {
38  const int i = 5;
39  std::string tfecfFuncName("someMethod");
41  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(i != 5, std::logic_error, "Blah blah blah")
42  );
43 }
44 
45 
47 {
48  const int i = 5;
49  std::string tfecfFuncName("someMethod");
50  TEST_THROW(
51  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(i == 5, std::logic_error, "Blah blah blah"),
52  std::logic_error);
53 }
54 
55 
57 {
58  const int i = 5;
60  TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(i != 5, std::logic_error, "Blah blah blah")
61  );
62 }
63 
64 
66 {
67  const int i = 5;
68  TEST_THROW(
69  TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(i == 5, std::logic_error, "Blah blah blah"),
70  std::logic_error);
71 }
72 
73 
75 {
76  const int i = 5;
79  );
80 }
81 
82 
84 {
85  const int i = 5;
86  TEST_THROW(
88  std::logic_error);
89 }
90 
91 
93 {
94  const int i = 5;
96  TEUCHOS_TEST_FOR_EXCEPT_MSG(i != 5, "Blah blah blah")
97  );
98 }
99 
100 
102 {
103  const int i = 5;
104  TEST_THROW(
105  TEUCHOS_TEST_FOR_EXCEPT_MSG(i == 5, "Blah blah blah"),
106  std::logic_error);
107 }
108 
109 
111 {
112  const int i = 5;
113  TEST_NOTHROW(
114  TEUCHOS_TEST_FOR_EXCEPTION_PRINT(i != 5, std::logic_error, "Blah blah blah", &out)
115  );
116 }
117 
118 
120 {
121  const int i = 5;
122  std::ostringstream oss;
123  TEST_THROW(
124  TEUCHOS_TEST_FOR_EXCEPTION_PRINT(i == 5, std::logic_error, "Blah blah blah", &oss),
125  std::logic_error);
126  TEST_INEQUALITY(oss.str().find("Throwing an std::exception of type"), std::string::npos);
127  TEST_INEQUALITY(oss.str().find("logic_error"), std::string::npos);
128  TEST_INEQUALITY(oss.str().find("AssertAndThrow_UnitTests.cpp"), std::string::npos);
129  TEST_INEQUALITY(oss.str().find("Throw number ="), std::string::npos);
130  TEST_INEQUALITY(oss.str().find("Throw test that evaluated to true"), std::string::npos);
131  TEST_INEQUALITY(oss.str().find("i == 5"), std::string::npos);
132  TEST_INEQUALITY(oss.str().find("Blah blah blah"), std::string::npos);
133  // NOTE: The above test asserts that the exception message is being built
134  // somewhat correctly!
135 }
136 
137 
139 {
140  const int i = 5;
141  TEST_NOTHROW(
142  TEUCHOS_TEST_FOR_EXCEPT_PRINT(i != 5, &out)
143  );
144 }
145 
146 
148 {
149  const int i = 5;
150  std::ostringstream oss;
151  TEST_THROW(
152  TEUCHOS_TEST_FOR_EXCEPT_PRINT(i == 5, &oss),
153  std::logic_error);
154  TEST_INEQUALITY(oss.str().find("AssertAndThrow_UnitTests.cpp"), std::string::npos);
155  TEST_INEQUALITY(oss.str().find("i == 5"), std::string::npos);
156 }
157 
158 
160 {
161  std::logic_error localExcept("Blah blah blah");
162  TEST_THROW(
163  TEUCHOS_TRACE(localExcept),
164  std::runtime_error);
165 }
166 
167 
168 } // namespace
#define TEST_INEQUALITY(v1, v2)
Assert the inequality of v1 and v2.
#define TEST_NOTHROW(code)
Asserr that the statement 'code' does not thrown any excpetions.
#define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg)
Macro for throwing an exception from within a class method with breakpointing to ease debugging...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
#define TEST_THROW(code, ExceptType)
Assert that the statement 'code' throws the exception 'ExceptType' (otherwise the test fails)...
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
#define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
Unit testing support.
#define TEUCHOS_TEST_FOR_EXCEPT_PRINT(throw_exception_test, out_ptr)
This macro is the same as TEUCHOS_TEST_FOR_EXCEPT() except that the exception will be caught...
#define TEUCHOS_TEST_FOR_EXCEPTION_PRINT(throw_exception_test, Exception, msg, out_ptr)
This macro is the same as TEUCHOS_TEST_FOR_EXCEPTION() except that the exception will be caught...
#define TEUCHOS_TRACE(exc)
This macro intercepts an exception, prints a standardized message including the current filename and ...
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.