xmscore  1.0
XmError.cpp
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
7 //------------------------------------------------------------------------------
8 
9 // 1. Precompiled header
10 
11 // 2. My header
12 #include <xmscore/misc/XmError.h>
13 
14 // 3. Standard Library Headers
15 // 4. External Library Headers
16 
17 // 5. Shared Headers
18 
19 // 6. Non-shared Headers
20 
21 #pragma warning(disable : 4127) // some tests use constant arguments to if() statements in macros
22 
23 namespace xms
24 {
25 int XM0 = 0;
26 int XM1 = 1;
27 
28 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 bool& xmAsserting()
33 {
34  static bool m_asserting = true;
35  return m_asserting;
36 } // xmAsserting
37 
38 } // namespace xms
39 
40 #ifdef CXX_TEST
41 // TESTS
44 
46 #include <cxxtest/TestSuite.h>
47 #include <xmscore/misc/XmError.t.h>
48 
49 namespace xms
50 {
51 namespace
52 { // unnamed namespace
53 
54 //------------------------------------------------------------------------------
56 //------------------------------------------------------------------------------
57 class XmAssertScopedDisable
58 {
59  bool const m_initialValue;
60 
61 public:
62  XmAssertScopedDisable() // remember then disable
63  : m_initialValue(xmAsserting())
64  {
65  xmAsserting() = false;
66  };
67  ~XmAssertScopedDisable() // restore
68  {
69  xmAsserting() = m_initialValue;
70  };
71 }; // XmAssertDisable
72 //------------------------------------------------------------------------------
74 //------------------------------------------------------------------------------
75 void iTest_XM_ENSURE_TRUE_VOID(bool a_assert)
76 {
77  char* foo = new char[123];
78  XM_ENSURE_TRUE_VOID(foo); // should pass
79  XM_ENSURE_TRUE_VOID_NO_ASSERT(foo); // should pass
80 
81  delete[] foo;
82  foo = nullptr;
83  if (a_assert)
84  {
85  XmAssertScopedDisable disable;
86  XM_ENSURE_TRUE_VOID(foo); // should return
87  }
88  else
89  {
90  XM_ENSURE_TRUE_VOID_NO_ASSERT(foo); // should return
91  }
92 
93  TS_FAIL("iTest_XM_ENSURE_TRUE_VOID"); // shouldn't get here
94 } // iTest_XM_ENSURE_TRUE_VOID
95 
96 //------------------------------------------------------------------------------
98 //------------------------------------------------------------------------------
99 void iTest_XM_ENSURE_TRUE_T(bool a_assert)
100 {
101  try
102  {
103  char* foo = new char[123];
104  XM_ENSURE_TRUE_T(foo, "1"); // should pass
105  XM_ENSURE_TRUE_T_NO_ASSERT(foo, "1"); // should pass
106 
107  delete[] foo;
108  foo = nullptr;
109  if (a_assert)
110  {
111  XmAssertScopedDisable disable;
112  XM_ENSURE_TRUE_T(foo, std::runtime_error("2")); // should throw
113  }
114  else
115  {
116  XM_ENSURE_TRUE_T_NO_ASSERT(foo, std::runtime_error("2")); // should throw
117  }
118 
119  TS_FAIL("iTest_XM_ENSURE_TRUE_T"); // shouldn't get here
120  }
121  catch (std::runtime_error& e)
122  {
123  std::string s = e.what();
124  TS_ASSERT_EQUALS(s, "2");
125  }
126 } // iTest_XM_ENSURE_TRUE_T
127 //------------------------------------------------------------------------------
129 //------------------------------------------------------------------------------
130 xmresult iTest_XM_ENSURE_TRUE_ptr(bool a_assert)
131 {
132  char* foo = new char[123];
133  XM_ENSURE_TRUE(foo, XR_FAILURE); // should pass
134  XM_ENSURE_TRUE_NO_ASSERT(foo, XR_FAILURE); // should pass
135 
136  delete[] foo;
137  foo = nullptr;
138  if (a_assert)
139  {
140  XmAssertScopedDisable disable;
141  XM_ENSURE_TRUE(foo, XR_FAILURE); // should return
142  }
143  else
144  {
145  XM_ENSURE_TRUE_NO_ASSERT(foo, XR_FAILURE); // should pass
146  }
147 
148  TS_FAIL("iTest_XM_ENSURE_TRUE_ptr"); // shouldn't get here
149  return XR_FAILURE;
150 } // iTest_XM_ENSURE_TRUE_ptr
151 //------------------------------------------------------------------------------
153 //------------------------------------------------------------------------------
154 xmresult iTest_XM_ENSURE_TRUE_boost_ptr(bool a_assert)
155 {
156  BSHP<char> foo(new char(123));
157  XM_ENSURE_TRUE(foo, XR_FAILURE); // should pass
158  XM_ENSURE_TRUE_NO_ASSERT(foo, XR_FAILURE); // should pass
159 
160  foo.reset();
161  if (a_assert)
162  {
163  XmAssertScopedDisable disable;
164  XM_ENSURE_TRUE(foo, XR_FAILURE); // should return
165  }
166  else
167  {
168  XM_ENSURE_TRUE_NO_ASSERT(foo, XR_FAILURE); // should pass
169  }
170 
171  TS_FAIL("iTest_XM_ENSURE_TRUE_boost_ptr"); // shouldn't get here
172  return XR_FAILURE;
173 } // iTest_XM_ENSURE_TRUE_boost_ptr
174 //------------------------------------------------------------------------------
176 //------------------------------------------------------------------------------
177 void iTest_XM_ENSURE_SUCCESS_VOID(bool a_assert)
178 {
179  xmresult foo = XR_SUCCESS;
180  XM_ENSURE_SUCCESS_VOID(foo); // should pass
181  XM_ENSURE_SUCCESS_VOID_NO_ASSERT(foo); // should pass
182 
183  foo = XR_FAILURE;
184  if (a_assert)
185  {
186  XmAssertScopedDisable disable;
187  XM_ENSURE_SUCCESS_VOID(foo); // should return
188  }
189  else
190  {
191  XM_ENSURE_SUCCESS_VOID_NO_ASSERT(foo); // should pass
192  }
193 
194  TS_FAIL("iTest_XM_ENSURE_SUCCESS_VOID"); // shouldn't get here
195 } // iTest_XM_ENSURE_SUCCESS_VOID
196 //------------------------------------------------------------------------------
198 //------------------------------------------------------------------------------
199 xmresult iTest_XM_ENSURE_SUCCESS(bool a_assert)
200 {
201  xmresult foo = XR_SUCCESS;
202  XM_ENSURE_SUCCESS(foo, XR_FAILURE); // should pass
203  XM_ENSURE_SUCCESS_NO_ASSERT(foo, XR_FAILURE); // should pass
204 
205  foo = XR_FAILURE;
206  if (a_assert)
207  {
208  XmAssertScopedDisable disable;
209  XM_ENSURE_SUCCESS(foo, XR_FAILURE); // should return
210  }
211  else
212  {
213  XM_ENSURE_SUCCESS_NO_ASSERT(foo, XR_FAILURE); // should pass
214  }
215 
216  TS_FAIL("iTest_XM_ENSURE_SUCCESS"); // shouldn't get here
217  return XR_FAILURE;
218 } // iTest_XM_ENSURE_SUCCESS
219 //------------------------------------------------------------------------------
221 //------------------------------------------------------------------------------
222 xmresult iTest_XM_ENSURE_SUCCESS_T(bool a_assert)
223 {
224  try
225  {
226  xmresult foo = XR_SUCCESS;
227  XM_ENSURE_SUCCESS_T(foo, XR_FAILURE); // should pass
228  XM_ENSURE_SUCCESS_T_NO_ASSERT(foo, XR_FAILURE); // should pass
229 
230  foo = XR_FAILURE;
231  if (a_assert)
232  {
233  XmAssertScopedDisable disable;
234  XM_ENSURE_SUCCESS_T(foo, XR_FAILURE); // should throw
235  }
236  else
237  {
238  XM_ENSURE_SUCCESS_T_NO_ASSERT(foo, XR_FAILURE); // should pass
239  }
240 
241  TS_FAIL("iTest_XM_ENSURE_SUCCESS"); // shouldn't get here
242  }
243  catch (xmresult& e)
244  {
245  TS_ASSERT_EQUALS(e, XR_FAILURE);
246  return XR_SUCCESS;
247  }
248  return XR_FAILURE;
249 } // iTest_XM_ENSURE_SUCCESS_T
250 //------------------------------------------------------------------------------
252 //------------------------------------------------------------------------------
253 xmresult iTest_XM_RETURN_XMRESULT_bool(bool a)
254 {
256 } // iTest_XM_RETURN_XMRESULT_bool
257 //------------------------------------------------------------------------------
259 //------------------------------------------------------------------------------
260 xmresult iTest_XM_RETURN_XMRESULT_boost_ptr_true()
261 {
262  BSHP<char> foo(new char(123));
263  XM_RETURN_XMRESULT(foo);
264 } // iTest_XM_RETURN_XMRESULT_boost_ptr_true
265 //------------------------------------------------------------------------------
267 //------------------------------------------------------------------------------
268 xmresult iTest_XM_RETURN_XMRESULT_boost_ptr_false()
269 {
270  BSHP<int> p;
272 } // iTest_XM_RETURN_XMRESULT_boost_ptr_false
273 
274 } // unnamed namespace
275 
276 } // namespace xms
277 
282 //------------------------------------------------------------------------------
284 //------------------------------------------------------------------------------
286 {
287  // TS_FAIL("XmErrorUnitTests::test1");
288 
289  // XM_ENSURE_TRUE
290 
291  xms::iTest_XM_ENSURE_TRUE_VOID(false);
292  xms::iTest_XM_ENSURE_TRUE_VOID(true);
293 
294  xms::xmresult rv = xms::iTest_XM_ENSURE_TRUE_ptr(false);
295  TS_ASSERT_EQUALS(rv, xms::XR_FAILURE);
296  rv = xms::iTest_XM_ENSURE_TRUE_ptr(true);
297  TS_ASSERT_EQUALS(rv, xms::XR_FAILURE);
298 
299  rv = xms::iTest_XM_ENSURE_TRUE_boost_ptr(false);
300  TS_ASSERT_EQUALS(rv, xms::XR_FAILURE);
301  rv = xms::iTest_XM_ENSURE_TRUE_boost_ptr(true);
302  TS_ASSERT_EQUALS(rv, xms::XR_FAILURE);
303 
304  // XM_ENSURE_SUCCESS
305 
306  xms::iTest_XM_ENSURE_SUCCESS_VOID(false);
307  xms::iTest_XM_ENSURE_SUCCESS_VOID(true);
308 
309  rv = xms::iTest_XM_ENSURE_SUCCESS(false);
310  TS_ASSERT_EQUALS(rv, xms::XR_FAILURE);
311  rv = xms::iTest_XM_ENSURE_SUCCESS(true);
312  TS_ASSERT_EQUALS(rv, xms::XR_FAILURE);
313 
314  xms::iTest_XM_ENSURE_SUCCESS_T(false);
315  xms::iTest_XM_ENSURE_SUCCESS_T(true);
316 
317  // XM_RETURN_RESULT
318 
319  rv = xms::iTest_XM_RETURN_XMRESULT_bool(true);
320  TS_ASSERT_EQUALS(rv, xms::XR_SUCCESS);
321 
322  rv = xms::iTest_XM_RETURN_XMRESULT_bool(false);
323  TS_ASSERT_EQUALS(rv, xms::XR_FAILURE);
324 
325  rv = xms::iTest_XM_RETURN_XMRESULT_boost_ptr_true();
326  TS_ASSERT_EQUALS(rv, xms::XR_SUCCESS);
327 
328  rv = xms::iTest_XM_RETURN_XMRESULT_boost_ptr_false();
329  TS_ASSERT_EQUALS(rv, xms::XR_FAILURE);
330 
331 } // XmErrorUnitTests::test1
332 //------------------------------------------------------------------------------
334 //------------------------------------------------------------------------------
336 {
337  using namespace xms;
338  // TS_FAIL("XmErrorUnitTests::test_XM_ASSERT");
339 
340  TS_ASSERT_EQUALS(xms::xmAsserting(), true); // this is the default
341  // XM_ASSERT(false); // this should assert. It did when it was not commented
342 
343  XmAssertScopedDisable disable;
344  XM_ASSERT(false); // now it should do nothing
345 } // XmErrorUnitTests::test_XM_ASSERT
346 
347 #endif
#define XM_ENSURE_TRUE_VOID(...)
Returns (void) if x evaluates to false, logs, and optionally asserts.
Definition: XmError.h:372
#define XM_ENSURE_SUCCESS_VOID_NO_ASSERT(...)
Returns (void) if x evaluates to xms::XR_FAILURE and logs.
Definition: XmError.h:223
bool & xmAsserting()
Stores whether we want XM_ASSERT to do something or not.
Definition: XmError.cpp:32
void test_XM_ASSERT()
Tests for XM_ASSERT.
Definition: XmError.cpp:335
#define XM_ENSURE_TRUE_NO_ASSERT(...)
Returns (void) if x evaluates to false and logs.
Definition: XmError.h:282
#define XM_ENSURE_SUCCESS_NO_ASSERT(...)
Returns ret if x evaluates to xms::XR_FAILURE and logs.
Definition: XmError.h:119
xmresult
Like Mozilla&#39;s nsresult.
Definition: XmError.h:39
Macros etc that make boost more convenient to use.
#define XM_ENSURE_SUCCESS(...)
Returns ret if x evaluates to xms::XR_FAILURE, logs, and optionally asserts.
Definition: XmError.h:112
#define XM_ENSURE_TRUE(...)
Returns (void) if x evaluates to false, logs, and optionally asserts.
Definition: XmError.h:276
#define XM_RETURN_XMRESULT(x)
Returns XR_SUCCESS if cond == true, else XR_FAILURE. Useful to convert a true/false return value into...
Definition: XmError.h:498
#define XM_ENSURE_TRUE_VOID_NO_ASSERT(...)
Returns (void) if x evaluates to false and logs.
Definition: XmError.h:379
void test1()
Tests for XmError.
Definition: XmError.cpp:285
#define XM_ASSERT(x)
Does a regular ASSERT if xmAsserting, otherwise does nothing.
Definition: XmError.h:69
#define XM_ENSURE_TRUE_T_NO_ASSERT(...)
Throws ret if x evaluates to false and logs.
Definition: XmError.h:331
#define XM_ENSURE_SUCCESS_T(...)
Throws ret if x evaluates to xms::XR_FAILURE, logs, and optionally asserts.
Definition: XmError.h:164
#define XM_ENSURE_SUCCESS_T_NO_ASSERT(...)
Throws ret if x evaluates to xms::XR_FAILURE and logs.
Definition: XmError.h:171
Functions and macros for assertion and checking for errors.
#define XM_ENSURE_SUCCESS_VOID(...)
Returns (void) if x evaluates to xms::XR_FAILURE, logs, and optionally asserts.
Definition: XmError.h:215
#define XM_ENSURE_TRUE_T(...)
Throws ret if x evaluates to false, logs, and optionally asserts.
Definition: XmError.h:325
int XM0
avoid warning
Definition: XmError.cpp:25
int XM1
avoid warning
Definition: XmError.cpp:26