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 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
45 #include "Teuchos_Assert.hpp"
46 
47 
48 namespace {
49 
50 
52 {
53  const int i = 5;
55  TEUCHOS_TEST_FOR_EXCEPTION(i != 5, std::logic_error, "Blah blah blah")
56  );
57 }
58 
59 
61 {
62  const int i = 5;
63  TEST_THROW(
64  TEUCHOS_TEST_FOR_EXCEPTION(i == 5, std::logic_error, "Blah blah blah"),
65  std::logic_error
66  );
67 }
68 
69 
71 {
72  const int i = 5;
73  std::string tfecfFuncName("someMethod");
75  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(i != 5, std::logic_error, "Blah blah blah")
76  );
77 }
78 
79 
81 {
82  const int i = 5;
83  std::string tfecfFuncName("someMethod");
84  TEST_THROW(
85  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(i == 5, std::logic_error, "Blah blah blah"),
86  std::logic_error);
87 }
88 
89 
91 {
92  const int i = 5;
94  TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(i != 5, std::logic_error, "Blah blah blah")
95  );
96 }
97 
98 
100 {
101  const int i = 5;
102  TEST_THROW(
103  TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(i == 5, std::logic_error, "Blah blah blah"),
104  std::logic_error);
105 }
106 
107 
109 {
110  const int i = 5;
111  TEST_NOTHROW(
113  );
114 }
115 
116 
118 {
119  const int i = 5;
120  TEST_THROW(
121  TEUCHOS_TEST_FOR_EXCEPT(i == 5),
122  std::logic_error);
123 }
124 
125 
127 {
128  const int i = 5;
129  TEST_NOTHROW(
130  TEUCHOS_TEST_FOR_EXCEPT_MSG(i != 5, "Blah blah blah")
131  );
132 }
133 
134 
136 {
137  const int i = 5;
138  TEST_THROW(
139  TEUCHOS_TEST_FOR_EXCEPT_MSG(i == 5, "Blah blah blah"),
140  std::logic_error);
141 }
142 
143 
145 {
146  const int i = 5;
147  TEST_NOTHROW(
148  TEUCHOS_TEST_FOR_EXCEPTION_PRINT(i != 5, std::logic_error, "Blah blah blah", &out)
149  );
150 }
151 
152 
154 {
155  const int i = 5;
156  std::ostringstream oss;
157  TEST_THROW(
158  TEUCHOS_TEST_FOR_EXCEPTION_PRINT(i == 5, std::logic_error, "Blah blah blah", &oss),
159  std::logic_error);
160  TEST_INEQUALITY(oss.str().find("Throwing an std::exception of type"), std::string::npos);
161  TEST_INEQUALITY(oss.str().find("logic_error"), std::string::npos);
162  TEST_INEQUALITY(oss.str().find("AssertAndThrow_UnitTests.cpp"), std::string::npos);
163  TEST_INEQUALITY(oss.str().find("Throw number ="), std::string::npos);
164  TEST_INEQUALITY(oss.str().find("Throw test that evaluated to true"), std::string::npos);
165  TEST_INEQUALITY(oss.str().find("i == 5"), std::string::npos);
166  TEST_INEQUALITY(oss.str().find("Blah blah blah"), std::string::npos);
167  // NOTE: The above test asserts that the exception message is being built
168  // somewhat correctly!
169 }
170 
171 
173 {
174  const int i = 5;
175  TEST_NOTHROW(
176  TEUCHOS_TEST_FOR_EXCEPT_PRINT(i != 5, &out)
177  );
178 }
179 
180 
182 {
183  const int i = 5;
184  std::ostringstream oss;
185  TEST_THROW(
186  TEUCHOS_TEST_FOR_EXCEPT_PRINT(i == 5, &oss),
187  std::logic_error);
188  TEST_INEQUALITY(oss.str().find("AssertAndThrow_UnitTests.cpp"), std::string::npos);
189  TEST_INEQUALITY(oss.str().find("i == 5"), std::string::npos);
190 }
191 
192 
194 {
195  std::logic_error localExcept("Blah blah blah");
196  TEST_THROW(
197  TEUCHOS_TRACE(localExcept),
198  std::runtime_error);
199 }
200 
201 
202 } // 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.