Interface Documentation
Version: invalid
types.hh
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
16 #include <iostream>
17 #include <sstream>
18 #include <string>
19 
20 #include "flecsi/util/demangle.hh"
21 #include "flecsi/util/unit/output.hh"
22 #include <flecsi/flog.hh>
23 
24 namespace flecsi {
25 
26 inline log::devel_tag unit_tag("unit");
27 
28 namespace util {
29 namespace unit {
30 
31 struct assert_handler_t;
32 
33 struct state_t {
34 
35  state_t(std::string name) {
36  name_ = name;
37  error_stream_.str(std::string());
38  } // initialize
39 
40  ~state_t() {
41  log::devel_guard guard(unit_tag);
42 
43  if(error_stream_.str().size()) {
44  std::stringstream stream;
45  stream << FLOG_OUTPUT_LTRED("TEST FAILED " << name_) << FLOG_COLOR_PLAIN
46  << std::endl;
47  stream << error_stream_.str();
48  flog(utility) << stream.str();
49  }
50  else {
51  flog(utility) << FLOG_OUTPUT_LTGREEN("TEST PASSED " << name_)
52  << FLOG_COLOR_PLAIN << std::endl;
53  } // if
54  } // process
55 
56  int & result() {
57  return result_;
58  }
59 
60  const std::string & name() const {
61  return name_;
62  }
63 
64  std::stringstream & stringstream() {
65  return error_stream_;
66  } // stream
67 
68  template<class F>
69  int operator->*(F f) { // highest binary precedence
70  f();
71  return result();
72  }
73 
74  // Allows 'return' before <<:
75  void operator>>=(const assert_handler_t &) const {}
76 
77 private:
78  int result_ = 0;
79  std::string name_;
80  std::stringstream error_stream_;
81 
82 }; // struct state_t
83 
85 
86  assert_handler_t(const char * condition,
87  const char * file,
88  int line,
89  state_t & runtime)
90  : runtime_(runtime) {
91  runtime_.result() = 1;
92  runtime_.stringstream()
93  << FLOG_OUTPUT_LTRED("ASSERT FAILED") << ": assertion '" << condition
94  << "' failed in " << FLOG_OUTPUT_BROWN(file << ":" << line)
95  << FLOG_COLOR_BROWN << " ";
96  } // assert_handler_t
97 
98  ~assert_handler_t() {
99  runtime_.stringstream() << FLOG_COLOR_PLAIN << std::endl;
100  std::stringstream stream;
101  stream << FLOG_OUTPUT_LTRED("TEST FAILED " << runtime_.name())
102  << FLOG_COLOR_PLAIN << std::endl;
103  stream << runtime_.stringstream().str();
104  } // ~assert_handler_t
105 
106  template<typename T>
107  assert_handler_t & operator<<(const T & value) {
108  runtime_.stringstream() << value;
109  return *this;
110  } // operator <<
111 
113  ::std::ostream & (*basic_manipulator)(::std::ostream & stream)) {
114  runtime_.stringstream() << basic_manipulator;
115  return *this;
116  } // operator <<
117 
118 private:
119  state_t & runtime_;
120 
121 }; // assert_handler_t
122 
124 
125  expect_handler_t(const char * condition,
126  const char * file,
127  int line,
128  state_t & runtime)
129  : runtime_(runtime) {
130  runtime_.result() = 1;
131  runtime_.stringstream()
132  << FLOG_OUTPUT_YELLOW("EXPECT FAILED") << ": unexpected '" << condition
133  << "' occurred in " << FLOG_OUTPUT_BROWN(file << ":" << line)
134  << FLOG_COLOR_BROWN << " ";
135  } // expect_handler_t
136 
137  ~expect_handler_t() {
138  runtime_.stringstream() << FLOG_COLOR_PLAIN << std::endl;
139  } // ~expect_handler_t
140 
141  template<typename T>
142  expect_handler_t & operator<<(const T & value) {
143  runtime_.stringstream() << value;
144  return *this;
145  } // operator <<
146 
148  ::std::ostream & (*basic_manipulator)(::std::ostream & stream)) {
149  runtime_.stringstream() << basic_manipulator;
150  return *this;
151  } // operator <<
152 
153 private:
154  state_t & runtime_;
155 
156 }; // expect_handler_t
157 
158 template<typename T1, typename T2>
159 inline bool
160 test_equal(const T1 & v1, const T2 & v2) {
161  return v1 == v2;
162 }
163 
164 template<typename T1, typename T2>
165 inline bool
166 test_less(const T1 & v1, const T2 & v2) {
167  return v1 < v2;
168 }
169 
170 template<typename T1, typename T2>
171 inline bool
172 test_greater(const T1 & v1, const T2 & v2) {
173  return v1 > v2;
174 }
175 
176 inline bool
177 string_compare(const char * lhs, const char * rhs) {
178  if(lhs == nullptr) {
179  return rhs == nullptr;
180  }
181  if(rhs == nullptr) {
182  return false;
183  }
184  return strcmp(lhs, rhs) == 0;
185 } // string_compare
186 
187 inline bool
188 string_case_compare(const char * lhs, const char * rhs) {
189  if(lhs == nullptr) {
190  return rhs == nullptr;
191  }
192  if(rhs == nullptr) {
193  return false;
194  }
195  return strcasecmp(lhs, rhs) == 0;
196 } // string_case_compare
197 
198 } // namespace unit
199 } // namespace util
200 } // namespace flecsi
201 
202 #define UNIT \
203  ::flecsi::log::flog_t::instance().config_stream().add_buffer( \
204  "flog", std::clog, true); \
205  ::flecsi::util::unit::state_t auto_unit_state(__func__); \
206  return auto_unit_state->*[&]() -> void
207 
208 #define UNIT_TYPE(name) ::flecsi::util::demangle((name))
209 
210 #define UNIT_TTYPE(type) ::flecsi::util::demangle(typeid(type).name())
211 
212 #define ASSERT_TRUE(condition) \
213  if(condition) \
214  ; \
215  else \
216  return auto_unit_state >>= ::flecsi::util::unit::assert_handler_t( \
217  #condition, __FILE__, __LINE__, auto_unit_state)
218 
219 #define EXPECT_TRUE(condition) \
220  if(condition) \
221  ; \
222  else \
223  ::flecsi::util::unit::expect_handler_t( \
224  #condition, __FILE__, __LINE__, auto_unit_state)
225 
226 #define ASSERT_FALSE(condition) \
227  if(!(condition)) \
228  ; \
229  else \
230  return auto_unit_state >>= ::flecsi::util::unit::assert_handler_t( \
231  #condition, __FILE__, __LINE__, auto_unit_state)
232 
233 #define EXPECT_FALSE(condition) \
234  if(!(condition)) \
235  ; \
236  else \
237  ::flecsi::util::unit::expect_handler_t( \
238  #condition, __FILE__, __LINE__, auto_unit_state)
239 
240 #define ASSERT_EQ(val1, val2) \
241  ASSERT_TRUE(::flecsi::util::unit::test_equal((val1), (val2)))
242 
243 #define EXPECT_EQ(val1, val2) \
244  EXPECT_TRUE(::flecsi::util::unit::test_equal((val1), (val2)))
245 
246 #define ASSERT_NE(val1, val2) \
247  ASSERT_TRUE(!::flecsi::util::unit::test_equal((val1), (val2)))
248 
249 #define EXPECT_NE(val1, val2) \
250  EXPECT_TRUE(!::flecsi::util::unit::test_equal((val1), (val2)))
251 
252 #define ASSERT_LT(val1, val2) \
253  ASSERT_TRUE(::flecsi::util::unit::test_less((val1), (val2)))
254 
255 #define EXPECT_LT(val1, val2) \
256  EXPECT_TRUE(::flecsi::util::unit::test_less((val1), (val2)))
257 
258 #define ASSERT_LE(val1, val2) \
259  ASSERT_TRUE(::flecsi::util::unit::test_greater((val2), (val1)))
260 
261 #define EXPECT_LE(val1, val2) \
262  EXPECT_TRUE(::flecsi::util::unit::test_greater((val2), (val1)))
263 
264 #define ASSERT_GT(val1, val2) \
265  ASSERT_TRUE(::flecsi::util::unit::test_greater((val1), (val2)))
266 
267 #define EXPECT_GT(val1, val2) \
268  EXPECT_TRUE(::flecsi::util::unit::test_greater((val1), (val2)))
269 
270 #define ASSERT_GE(val1, val2) \
271  ASSERT_TRUE(::flecsi::util::unit::test_less((val2), (val1)))
272 
273 #define EXPECT_GE(val1, val2) \
274  EXPECT_TRUE(::flecsi::util::unit::test_less((val2), (val1)))
275 
276 #define ASSERT_STREQ(str1, str2) \
277  if(::flecsi::util::unit::string_compare(str1, str2)) \
278  ; \
279  else \
280  return auto_unit_state >>= ::flecsi::util::unit::assert_handler_t( \
281  str1 " == " str2, __FILE__, __LINE__, auto_unit_state)
282 
283 #define EXPECT_STREQ(str1, str2) \
284  if(::flecsi::util::unit::string_compare(str1, str2)) \
285  ; \
286  else \
287  ::flecsi::util::unit::expect_handler_t( \
288  str1 " == " str2, __FILE__, __LINE__, auto_unit_state)
289 
290 #define ASSERT_STRNE(str1, str2) \
291  if(!::flecsi::util::unit::string_compare(str1, str2)) \
292  ; \
293  else \
294  return auto_unit_state >>= ::flecsi::util::unit::assert_handler_t( \
295  str1 " != " str2, __FILE__, __LINE__, auto_unit_state)
296 
297 #define EXPECT_STRNE(str1, str2) \
298  if(!::flecsi::util::unit::string_compare(str1, str2)) \
299  ; \
300  else \
301  ::flecsi::util::unit::expect_handler_t( \
302  str1 " != " str2, __FILE__, __LINE__, auto_unit_state)
303 
304 #define ASSERT_STRCASEEQ(str1, str2) \
305  if(::flecsi::util::unit::string_case_compare(str1, str2)) \
306  ; \
307  else \
308  return auto_unit_state >>= ::flecsi::util::unit::assert_handler_t(str1 \
309  " == " str2 " (case insensitive)", \
310  __FILE__, \
311  __LINE__, \
312  auto_unit_state)
313 
314 #define EXPECT_STRCASEEQ(str1, str2) \
315  if(::flecsi::util::unit::string_case_compare(str1, str2)) \
316  ; \
317  else \
318  ::flecsi::util::unit::expect_handler_t(str1 " == " str2 \
319  " (case insensitive)", \
320  __FILE__, \
321  __LINE__, \
322  auto_unit_state)
323 
324 #define ASSERT_STRCASENE(str1, str2) \
325  if(!::flecsi::util::unit::string_case_compare(str1, str2)) \
326  ; \
327  else \
328  return auto_unit_state >>= ::flecsi::util::unit::assert_handler_t(str1 \
329  " == " str2 " (case insensitive)", \
330  __FILE__, \
331  __LINE__, \
332  auto_unit_state)
333 
334 #define EXPECT_STRCASENE(str1, str2) \
335  if(!::flecsi::util::unit::string_case_compare(str1, str2)) \
336  ; \
337  else \
338  ::flecsi::util::unit::expect_handler_t(str1 " == " str2 \
339  " (case insensitive)", \
340  __FILE__, \
341  __LINE__, \
342  auto_unit_state)
343 
344 // Provide access to the output stream to allow user to capture output
345 #define UNIT_CAPTURE() \
346  ::flecsi::util::unit::test_output_t::instance().get_stream()
347 
348 // Return captured output as a std::string
349 #define UNIT_DUMP() ::flecsi::util::unit::test_output_t::instance().get_buffer()
350 
351 // Compare captured output to a blessed file
352 #define UNIT_EQUAL_BLESSED(f) \
353  ::flecsi::util::unit::test_output_t::instance().equal_blessed((f))
354 
355 // Write captured output to file
356 #define UNIT_WRITE(f) \
357  ::flecsi::util::unit::test_output_t::instance().to_file((f))
358 
359 // Dump captured output on failure
360 #if !defined(_MSC_VER)
361 #define UNIT_ASSERT(ASSERTION, ...) \
362  ASSERT_##ASSERTION(__VA_ARGS__) << UNIT_DUMP()
363 #else
364  // MSVC has a brain-dead preprocessor...
365 #define UNIT_ASSERT(ASSERTION, x, y) ASSERT_##ASSERTION(x, y) << UNIT_DUMP()
366 #endif
367 
368 // Dump captured output on failure
369 #if !defined(_MSC_VER)
370 #define UNIT_EXPECT(EXPECTATION, ...) \
371  EXPECT_##EXPECTATION(__VA_ARGS__) << UNIT_DUMP()
372 #else
373  // MSVC has a brain-dead preprocessor...
374 #define UNIT_EXPECT(EXPECTATION, x, y) EXPECT_##EXPECTATION(x, y) << UNIT_DUMP()
375 #endif
376 
377 // compare collections with varying levels of assertions
378 #define UNIT_CHECK_EQUAL_COLLECTIONS(...) \
379  ::flecsi::util::unit::CheckEqualCollections(__VA_ARGS__)
380 
381 #define UNIT_ASSERT_EQUAL_COLLECTIONS(...) \
382  ASSERT_TRUE(::flecsi::util::unit::CheckEqualCollections(__VA_ARGS__) << UNIT_DUMP()
383 
384 #define UNIT_EXPECT_EQUAL_COLLECTIONS(...) \
385  EXPECT_TRUE(::flecsi::util::unit::CheckEqualCollections(__VA_ARGS__)) \
386  << UNIT_DUMP()
Definition: types.hh:123
#define flog(severity)
Definition: flog.hh:136
std::ostream & operator<<(std::ostream &ostr, const filling_curve< D, T, DER > &k)
output for filling_curve using output_ function defined in the class
Definition: filling_curve.hh:206
Definition: flog.hh:82
Definition: types.hh:33
Definition: control.hh:31