15#if defined(_MSC_VER) && (MSC_VER <= 1500)
16 #include <cybozu/inttype.hpp>
24 typedef void (*Func)();
25 typedef std::list<std::pair<const char*, Func> > UnitTestList;
42 list_.push_back(std::make_pair(
name, func));
55 const char sep =
'\\';
59 size_t pos =
name.find_last_of(sep);
60 std::string
ret =
name.substr(pos + 1);
62 return ret.substr(0, pos);
69 for (UnitTestList::const_iterator i = list_.begin(), ie = list_.end(); i != ie; ++i) {
70 std::cout <<
"ctest:module=" << i->first << std::endl;
73 }
catch (std::exception& e) {
75 std::cout <<
"ctest: " << i->first <<
" is stopped by exception " << e.what() << std::endl;
78 std::cout <<
"ctest: " << i->first <<
" is stopped by unknown exception" << std::endl;
82 }
catch (std::exception& e) {
83 msg = std::string(
"ctest:err:") + e.what();
85 msg =
"ctest:err: catch unknown exception";
90 <<
", module=" << list_.size()
91 <<
", total=" << (okCount_ + ngCount_ + exceptionCount_)
92 <<
", ok=" << okCount_
93 <<
", ng=" << ngCount_
94 <<
", exception=" << exceptionCount_ << std::endl;
97 std::cout << msg << std::endl;
117inline void test(
bool ret,
const std::string& msg,
const std::string& param,
const char *file,
int line)
121 printf(
"%s(%d):ctest:%s(%s);\n", file, line, msg.c_str(), param.c_str());
125template<
typename T,
typename U>
134 return lhs == size_t(rhs);
138 return size_t(lhs) == rhs;
140inline bool isEqual(
const char *lhs,
const char *rhs)
142 return strcmp(lhs, rhs) == 0;
144inline bool isEqual(
char *lhs,
const char *rhs)
146 return strcmp(lhs, rhs) == 0;
148inline bool isEqual(
const char *lhs,
char *rhs)
150 return strcmp(lhs, rhs) == 0;
154 return strcmp(lhs, rhs) == 0;
165 return lfi.i == rfi.i;
176 return ldi.i == rdi.i;
181#ifndef CYBOZU_TEST_DISABLE_AUTO_RUN
184 return cybozu::test::autoRun.
run(argc,
argv);
192#define CYBOZU_TEST_ASSERT(x) cybozu::test::test(!!(x), "CYBOZU_TEST_ASSERT", #x, __FILE__, __LINE__)
199#define CYBOZU_TEST_EQUAL(x, y) { \
200 bool eq = cybozu::test::isEqual(x, y); \
201 cybozu::test::test(eq, "CYBOZU_TEST_EQUAL", #x ", " #y, __FILE__, __LINE__); \
203 std::cout << "ctest: lhs=" << (x) << std::endl; \
204 std::cout << "ctest: rhs=" << (y) << std::endl; \
212#define CYBOZU_TEST_NEAR(x, y, eps) { \
213 bool isNear = fabs((x) - (y)) < eps; \
214 cybozu::test::test(isNear, "CYBOZU_TEST_NEAR", #x ", " #y, __FILE__, __LINE__); \
216 std::cout << "ctest: lhs=" << (x) << std::endl; \
217 std::cout << "ctest: rhs=" << (y) << std::endl; \
221#define CYBOZU_TEST_EQUAL_POINTER(x, y) { \
223 cybozu::test::test(eq, "CYBOZU_TEST_EQUAL_POINTER", #x ", " #y, __FILE__, __LINE__); \
225 std::cout << "ctest: lhs=" << static_cast<const void*>(x) << std::endl; \
226 std::cout << "ctest: rhs=" << static_cast<const void*>(y) << std::endl; \
235#define CYBOZU_TEST_EQUAL_ARRAY(x, y, n) { \
236 for (size_t i = 0, ie = (size_t)(n); i < ie; i++) { \
237 bool eq = cybozu::test::isEqual(x, y); \
238 cybozu::test::test(eq, "CYBOZU_TEST_EQUAL_ARRAY", #x ", " #y, __FILE__, __LINE__); \
240 std::cout << "ctest: i=" << i << std::endl; \
241 std::cout << "ctest: lhs=" << (x) << std::endl; \
242 std::cout << "ctest: rhs=" << (y) << std::endl; \
251#define CYBOZU_TEST_FAIL(msg) cybozu::test::test(false, "CYBOZU_TEST_FAIL", msg, __FILE__, __LINE__)
256#define CYBOZU_TEST_EXCEPTION_MESSAGE(statement, Exception, msg) \
259 std::string errMsg; \
263 } catch (const Exception& e) { \
265 if (errMsg.find(msg) == std::string::npos) { \
272 cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION_MESSAGE", #statement ", " #Exception ", " #msg, __FILE__, __LINE__); \
274 std::cout << "ctest: no exception" << std::endl; \
275 } else if (ret == 2) { \
276 std::cout << "ctest: bad exception msg:" << errMsg << std::endl; \
278 std::cout << "ctest: unexpected exception" << std::endl; \
281 cybozu::test::autoRun.set(true); \
285#define CYBOZU_TEST_EXCEPTION(statement, Exception) \
291 } catch (const Exception&) { \
296 cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION", #statement ", " #Exception, __FILE__, __LINE__); \
298 std::cout << "ctest: no exception" << std::endl; \
300 std::cout << "ctest: unexpected exception" << std::endl; \
303 cybozu::test::autoRun.set(true); \
310#define CYBOZU_TEST_NO_EXCEPTION(statement) \
313 cybozu::test::autoRun.set(true); \
315 cybozu::test::test(false, "CYBOZU_TEST_NO_EXCEPTION", #statement, __FILE__, __LINE__); \
322#define CYBOZU_TEST_AUTO(name) \
323void cybozu_test_ ## name(); \
324struct cybozu_test_local_ ## name { \
325 cybozu_test_local_ ## name() \
327 cybozu::test::autoRun.append(#name, cybozu_test_ ## name); \
329} cybozu_test_local_instance_ ## name; \
330void cybozu_test_ ## name()
336#define CYBOZU_TEST_AUTO_WITH_FIXTURE(name, Fixture) \
337void cybozu_test_ ## name(); \
338void cybozu_test_real_ ## name() \
341 cybozu_test_ ## name(); \
343struct cybozu_test_local_ ## name { \
344 cybozu_test_local_ ## name() \
346 cybozu::test::autoRun.append(#name, cybozu_test_real_ ## name); \
348} cybozu_test_local_instance_ ## name; \
349void cybozu_test_ ## name()
356#define CYBOZU_TEST_SETUP_FIXTURE(Fixture) \
357Fixture *cybozu_test_local_fixture; \
358void cybozu_test_local_init() \
360 cybozu_test_local_fixture = new Fixture(); \
362void cybozu_test_local_term() \
364 delete cybozu_test_local_fixture; \
366struct cybozu_test_local_fixture_setup_ { \
367 cybozu_test_local_fixture_setup_() \
369 cybozu::test::autoRun.setup(cybozu_test_local_init, cybozu_test_local_term); \
371} cybozu_test_local_fixture_setup_instance_;
void append(const char *name, Func func)
std::string getBaseName(const std::string &name) const
void setup(Func init, Func term)
static AutoRun & getInstance()
int run(int, char *argv[])
bool isEqual(const T &lhs, const U &rhs)
#define T(meth, val, expected)
unsigned __int64 uint64_t