我正在定义我自己的ASSERT宏--比如说ASSERT_FOO。
它非常复杂,它根据宏定义等改变其行为。我想创建测试来测试它。我需要像ASSERT_ASSERT(ASSERT_FOO(foo()))这样的东西;
为了实现这一点,我需要像测试重置这样的东西,它可以消除累积的断言,并使测试成功。
if (::testing::Test::HasFatalFailure()) {
// reset the test ?
} else {
FAIL();
}我怎样才能做到这一点呢?
发布于 2014-12-26 19:43:56
// Substitute non-verbose assert() macro
// http://stackoverflow.com/questions/27640730/
#ifndef assert
#include <cstdlib> // abort()
static int assert_count = 0; // count assertion failures
const unsigned ASSERT_COUNT = 15; // when to stop assertion executions
/** (cond ? () : cout << "cond" ) */
#define assert(cond) \
do { \
if (!(cond)) { \
std::cout << ":" << __LINE__ << " fail-> " #cond "\n"; \
assert_count++; \
if ( ASSERT_COUNT==assert_count ) { abort(); } \
} \
} while (false)
#endif
/** "C:/DIR/SubDir/file_name.ext" ==> "file_name.ext" */
const char* remove_dir_name( const char* str ) {
const char *p = str; /* p = str+strlen(str); */
while ( *p != 0 ) { ++p; }
while ( *p != '/' && *p != '\\' && *p != ':' ) {
if ( p == str ) { return str; }
--p;
}
return p+1;
}https://stackoverflow.com/questions/27640730
复制相似问题