Simpatico  v1.10
TestException.h
1 #ifndef TEST_EXCEPTION_H
2 #define TEST_EXCEPTION_H
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <iostream>
12 #include <string>
13 #include <sstream>
14 #include <cmath>
15 
20 {
21 
22 public:
23 
27  TestException();
28 
46  TestException(const char *function, const char *message,
47  const char *file, int line);
48 
56  TestException(const char *message, const char *file, int line);
57 
62 
68  void write(std::ostream &out);
69 
73  const std::string& message();
74 
75 
76 protected:
77 
78  std::string message_;
79 
80 };
81 
83 
84 /*
85 * Default constructor.
86 */
88  : message_()
89 {}
90 
91 /*
92 * Constructor for throwing.
93 */
94 TestException::TestException(const char *function, const char *message,
95  const char *file, int line)
96 {
97  message_ = " Function: ";
98  message_.append(function);
99  message_.append("\n");
100  message_.append(" Message : ");
101  message_.append(message);
102  message_.append("\n");
103  message_.append(" File : ");
104  message_.append(file);
105  message_.append("\n");
106  message_.append(" Line : ");
107  std::ostringstream s;
108  s << line;
109  message_.append(s.str());
110  message_.append("\n");
111 }
112 
113 /*
114 * Constructor without function name parameter.
115 */
116 TestException::TestException(const char *message, const char *file, int line)
117 {
118  message_ = " Message : ";
119  message_.append(message);
120  message_.append("\n");
121  message_.append(" File : ");
122  message_.append(file);
123  message_.append("\n");
124  message_.append(" Line : ");
125  std::ostringstream s;
126  s << line;
127  message_.append(s.str());
128  message_.append("\n");
129 }
130 
131 /*
132 * Destructor.
133 */
135 {}
136 
137 /*
138 * Write error message to output stream.
139 *
140 * \param out output stream
141 */
142 inline void TestException::write(std::ostream &out)
143 { out << message_; }
144 
145 /*
146 * Return the error message.
147 */
148 inline const std::string& TestException::message()
149 { return message_; }
150 
152 
156 #define TEST_FUNC __PRETTY_FUNCTION__
157 
161 #ifdef TEST_FUNC
162  #define TEST_ASSERT(expr) \
163  if (!(expr)) throw TestException(TEST_FUNC, #expr , __FILE__, __LINE__)
164 #else
165  #define TEST_ASSERT(expr) \
166  if (!(expr)) throw TestException( #expr , __FILE__, __LINE__)
167 #endif
168 
172 #ifdef TEST_FUNC
173  #define TEST_THROW(msg) \
174  throw TestException(TEST_FUNC, #msg , __FILE__, __LINE__)
175 #else
176  #define TEST_THROW(msg) \
177  throw TestException( #msg , __FILE__, __LINE__)
178 #endif
179 
180 #endif
TestException()
Default constructor.
Definition: TestException.h:87
std::string message_
Error message.
Definition: TestException.h:78
~TestException()
Destructor.
void write(std::ostream &out)
Write error message to output stream.
const std::string & message()
Return the error message.
An exception thrown by a failed unit test.
Definition: TestException.h:19