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";
89 int err = ngCount_ + exceptionCount_;
90 int total = okCount_ + err;
92 <<
", module=" << list_.size()
93 <<
", total=" << total
94 <<
", ok=" << okCount_
95 <<
", ng=" << ngCount_
96 <<
", exception=" << exceptionCount_ << std::endl;
97 return err > 0 ? 1 : 0;
99 std::cout << msg << std::endl;
119inline void test(
bool ret,
const std::string& msg,
const std::string& param,
const char *file,
int line)
123 printf(
"%s(%d):ctest:%s(%s);\n", file, line, msg.c_str(), param.c_str());
127template<
typename T,
typename U>
134inline bool isEqual(
size_t lhs,
int rhs)
136 return lhs == size_t(rhs);
138inline bool isEqual(
int lhs,
size_t rhs)
140 return size_t(lhs) == rhs;
142inline bool isEqual(
const char *lhs,
const char *rhs)
144 return strcmp(lhs, rhs) == 0;
146inline bool isEqual(
char *lhs,
const char *rhs)
148 return strcmp(lhs, rhs) == 0;
150inline bool isEqual(
const char *lhs,
char *rhs)
152 return strcmp(lhs, rhs) == 0;
154inline bool isEqual(
char *lhs,
char *rhs)
156 return strcmp(lhs, rhs) == 0;
159inline bool isEqual(
float lhs,
float rhs)
167 return lfi.i == rfi.i;
170inline bool isEqual(
double lhs,
double rhs)
178 return ldi.i == rdi.i;
183#ifndef CYBOZU_TEST_DISABLE_AUTO_RUN
186 return cybozu::test::autoRun.
run(argc,
argv);
194#define CYBOZU_TEST_ASSERT(x) cybozu::test::test(!!(x), "CYBOZU_TEST_ASSERT", #x, __FILE__, __LINE__)
201#define CYBOZU_TEST_EQUAL(x, y) { \
202 bool _cybozu_eq = cybozu::test::isEqual(x, y); \
203 cybozu::test::test(_cybozu_eq, "CYBOZU_TEST_EQUAL", #x ", " #y, __FILE__, __LINE__); \
205 std::cout << "ctest: lhs=" << (x) << std::endl; \
206 std::cout << "ctest: rhs=" << (y) << std::endl; \
214#define CYBOZU_TEST_NEAR(x, y, eps) { \
215 bool _cybozu_isNear = fabs((x) - (y)) < eps; \
216 cybozu::test::test(_cybozu_isNear, "CYBOZU_TEST_NEAR", #x ", " #y, __FILE__, __LINE__); \
217 if (!_cybozu_isNear) { \
218 std::cout << "ctest: lhs=" << (x) << std::endl; \
219 std::cout << "ctest: rhs=" << (y) << std::endl; \
223#define CYBOZU_TEST_EQUAL_POINTER(x, y) { \
224 bool _cybozu_eq = x == y; \
225 cybozu::test::test(_cybozu_eq, "CYBOZU_TEST_EQUAL_POINTER", #x ", " #y, __FILE__, __LINE__); \
227 std::cout << "ctest: lhs=" << static_cast<const void*>(x) << std::endl; \
228 std::cout << "ctest: rhs=" << static_cast<const void*>(y) << std::endl; \
237#define CYBOZU_TEST_EQUAL_ARRAY(x, y, n) { \
238 for (size_t _cybozu_test_i = 0, _cybozu_ie = (size_t)(n); _cybozu_test_i < _cybozu_ie; _cybozu_test_i++) { \
239 bool _cybozu_eq = cybozu::test::isEqual((x)[_cybozu_test_i], (y)[_cybozu_test_i]); \
240 cybozu::test::test(_cybozu_eq, "CYBOZU_TEST_EQUAL_ARRAY", #x ", " #y ", " #n, __FILE__, __LINE__); \
242 std::cout << "ctest: i=" << _cybozu_test_i << std::endl; \
243 std::cout << "ctest: lhs=" << (x)[_cybozu_test_i] << std::endl; \
244 std::cout << "ctest: rhs=" << (y)[_cybozu_test_i] << std::endl; \
253#define CYBOZU_TEST_FAIL(msg) cybozu::test::test(false, "CYBOZU_TEST_FAIL", msg, __FILE__, __LINE__)
258#define CYBOZU_TEST_EXCEPTION_MESSAGE(statement, Exception, msg) \
260 int _cybozu_ret = 0; \
261 std::string _cybozu_errMsg; \
265 } catch (const Exception& _cybozu_e) { \
266 _cybozu_errMsg = _cybozu_e.what(); \
267 if (_cybozu_errMsg.find(msg) == std::string::npos) { \
274 cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION_MESSAGE", #statement ", " #Exception ", " #msg, __FILE__, __LINE__); \
275 if (_cybozu_ret == 1) { \
276 std::cout << "ctest: no exception" << std::endl; \
277 } else if (_cybozu_ret == 2) { \
278 std::cout << "ctest: bad exception msg:" << _cybozu_errMsg << std::endl; \
280 std::cout << "ctest: unexpected exception" << std::endl; \
283 cybozu::test::autoRun.set(true); \
287#define CYBOZU_TEST_EXCEPTION(statement, Exception) \
289 int _cybozu_ret = 0; \
293 } catch (const Exception&) { \
298 cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION", #statement ", " #Exception, __FILE__, __LINE__); \
299 if (_cybozu_ret == 1) { \
300 std::cout << "ctest: no exception" << std::endl; \
302 std::cout << "ctest: unexpected exception" << std::endl; \
305 cybozu::test::autoRun.set(true); \
312#define CYBOZU_TEST_NO_EXCEPTION(statement) \
315 cybozu::test::autoRun.set(true); \
317 cybozu::test::test(false, "CYBOZU_TEST_NO_EXCEPTION", #statement, __FILE__, __LINE__); \
324#define CYBOZU_TEST_AUTO(name) \
325void cybozu_test_ ## name(); \
326struct cybozu_test_local_ ## name { \
327 cybozu_test_local_ ## name() \
329 cybozu::test::autoRun.append(#name, cybozu_test_ ## name); \
331} cybozu_test_local_instance_ ## name; \
332void cybozu_test_ ## name()
338#define CYBOZU_TEST_AUTO_WITH_FIXTURE(name, Fixture) \
339void cybozu_test_ ## name(); \
340void cybozu_test_real_ ## name() \
343 cybozu_test_ ## name(); \
345struct cybozu_test_local_ ## name { \
346 cybozu_test_local_ ## name() \
348 cybozu::test::autoRun.append(#name, cybozu_test_real_ ## name); \
350} cybozu_test_local_instance_ ## name; \
351void cybozu_test_ ## name()
358#define CYBOZU_TEST_SETUP_FIXTURE(Fixture) \
359Fixture *cybozu_test_local_fixture; \
360void cybozu_test_local_init() \
362 cybozu_test_local_fixture = new Fixture(); \
364void cybozu_test_local_term() \
366 delete cybozu_test_local_fixture; \
368struct cybozu_test_local_fixture_setup_ { \
369 cybozu_test_local_fixture_setup_() \
371 cybozu::test::autoRun.setup(cybozu_test_local_init, cybozu_test_local_term); \
373} 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[])
static const Reg16 di(Operand::DI)
bool isEqual(const T &lhs, const U &rhs)
@ test
Unit testing utility error code.
#define T(meth, val, expected)
unsigned __int64 uint64_t