Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test.hpp File Reference

unit test class More...

#include <stdio.h>
#include <string.h>
#include <string>
#include <list>
#include <iostream>
#include <utility>
#include <stdint.h>
Include dependency graph for test.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  cybozu::test::AutoRun
 

Namespaces

namespace  cybozu
 
namespace  cybozu::test
 

Macros

#define CYBOZU_TEST_ASSERT(x)
 
#define CYBOZU_TEST_EQUAL(x, y)
 
#define CYBOZU_TEST_NEAR(x, y, eps)
 
#define CYBOZU_TEST_EQUAL_POINTER(x, y)
 
#define CYBOZU_TEST_EQUAL_ARRAY(x, y, n)
 
#define CYBOZU_TEST_FAIL(msg)
 
#define CYBOZU_TEST_EXCEPTION_MESSAGE(statement, Exception, msg)
 
#define CYBOZU_TEST_EXCEPTION(statement, Exception)
 
#define CYBOZU_TEST_NO_EXCEPTION(statement)
 
#define CYBOZU_TEST_AUTO(name)
 
#define CYBOZU_TEST_AUTO_WITH_FIXTURE(name, Fixture)
 
#define CYBOZU_TEST_SETUP_FIXTURE(Fixture)
 

Functions

void cybozu::test::test (bool ret, const std::string &msg, const std::string &param, const char *file, int line)
 
template<typename T , typename U >
bool cybozu::test::isEqual (const T &lhs, const U &rhs)
 
bool cybozu::test::isEqual (size_t lhs, int rhs)
 
bool cybozu::test::isEqual (int lhs, size_t rhs)
 
bool cybozu::test::isEqual (const char *lhs, const char *rhs)
 
bool cybozu::test::isEqual (char *lhs, const char *rhs)
 
bool cybozu::test::isEqual (const char *lhs, char *rhs)
 
bool cybozu::test::isEqual (char *lhs, char *rhs)
 
bool cybozu::test::isEqual (float lhs, float rhs)
 
bool cybozu::test::isEqual (double lhs, double rhs)
 
int main (int argc, char *argv[])
 

Detailed Description

Copyright (C) 2008 Cybozu Labs, Inc., all rights reserved.

Definition in file test.hpp.

Macro Definition Documentation

◆ CYBOZU_TEST_ASSERT

#define CYBOZU_TEST_ASSERT ( x)
Value:
cybozu::test::test(!!(x), "CYBOZU_TEST_ASSERT", #x, __FILE__, __LINE__)
void test(bool ret, const std::string &msg, const std::string &param, const char *file, int line)
Definition test.hpp:117

alert if !x

Parameters
x[in]

Definition at line 194 of file test.hpp.

◆ CYBOZU_TEST_AUTO

#define CYBOZU_TEST_AUTO ( name)
Value:
void cybozu_test_ ## name(); \
struct cybozu_test_local_ ## name { \
cybozu_test_local_ ## name() \
{ \
cybozu::test::autoRun.append(#name, cybozu_test_ ## name); \
} \
} cybozu_test_local_instance_ ## name; \
void cybozu_test_ ## name()
std::string name
void append(const char *name, Func func)
Definition test.hpp:40

append auto unit test

Parameters
name[in] module name

Definition at line 324 of file test.hpp.

324#define CYBOZU_TEST_AUTO(name) \
325void cybozu_test_ ## name(); \
326struct cybozu_test_local_ ## name { \
327 cybozu_test_local_ ## name() \
328 { \
329 cybozu::test::autoRun.append(#name, cybozu_test_ ## name); \
330 } \
331} cybozu_test_local_instance_ ## name; \
332void cybozu_test_ ## name()

◆ CYBOZU_TEST_AUTO_WITH_FIXTURE

#define CYBOZU_TEST_AUTO_WITH_FIXTURE ( name,
Fixture )
Value:
void cybozu_test_ ## name(); \
void cybozu_test_real_ ## name() \
{ \
cybozu_test_ ## name(); \
} \
struct cybozu_test_local_ ## name { \
cybozu_test_local_ ## name() \
{ \
cybozu::test::autoRun.append(#name, cybozu_test_real_ ## name); \
} \
} cybozu_test_local_instance_ ## name; \
void cybozu_test_ ## name()

append auto unit test with fixture

Parameters
name[in] module name

Definition at line 338 of file test.hpp.

338#define CYBOZU_TEST_AUTO_WITH_FIXTURE(name, Fixture) \
339void cybozu_test_ ## name(); \
340void cybozu_test_real_ ## name() \
341{ \
342 Fixture f; \
343 cybozu_test_ ## name(); \
344} \
345struct cybozu_test_local_ ## name { \
346 cybozu_test_local_ ## name() \
347 { \
348 cybozu::test::autoRun.append(#name, cybozu_test_real_ ## name); \
349 } \
350} cybozu_test_local_instance_ ## name; \
351void cybozu_test_ ## name()

◆ CYBOZU_TEST_EQUAL

#define CYBOZU_TEST_EQUAL ( x,
y )
Value:
{ \
bool _cybozu_eq = cybozu::test::isEqual(x, y); \
cybozu::test::test(_cybozu_eq, "CYBOZU_TEST_EQUAL", #x ", " #y, __FILE__, __LINE__); \
if (!_cybozu_eq) { \
std::cout << "ctest: lhs=" << (x) << std::endl; \
std::cout << "ctest: rhs=" << (y) << std::endl; \
} \
}
bool isEqual(const T &lhs, const U &rhs)
Definition test.hpp:126

alert if x != y

Parameters
x[in]
y[in]

Definition at line 201 of file test.hpp.

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__); \
204 if (!_cybozu_eq) { \
205 std::cout << "ctest: lhs=" << (x) << std::endl; \
206 std::cout << "ctest: rhs=" << (y) << std::endl; \
207 } \
208}

◆ CYBOZU_TEST_EQUAL_ARRAY

#define CYBOZU_TEST_EQUAL_ARRAY ( x,
y,
n )
Value:
{ \
for (size_t _cybozu_test_i = 0, _cybozu_ie = (size_t)(n); _cybozu_test_i < _cybozu_ie; _cybozu_test_i++) { \
bool _cybozu_eq = cybozu::test::isEqual((x)[_cybozu_test_i], (y)[_cybozu_test_i]); \
cybozu::test::test(_cybozu_eq, "CYBOZU_TEST_EQUAL_ARRAY", #x ", " #y ", " #n, __FILE__, __LINE__); \
if (!_cybozu_eq) { \
std::cout << "ctest: i=" << _cybozu_test_i << std::endl; \
std::cout << "ctest: lhs=" << (x)[_cybozu_test_i] << std::endl; \
std::cout << "ctest: rhs=" << (y)[_cybozu_test_i] << std::endl; \
} \
} \
}

alert if x[] != y[]

Parameters
x[in]
y[in]
n[in]

Definition at line 237 of file test.hpp.

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__); \
241 if (!_cybozu_eq) { \
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; \
245 } \
246 } \
247}

◆ CYBOZU_TEST_EQUAL_POINTER

#define CYBOZU_TEST_EQUAL_POINTER ( x,
y )
Value:
{ \
bool _cybozu_eq = x == y; \
cybozu::test::test(_cybozu_eq, "CYBOZU_TEST_EQUAL_POINTER", #x ", " #y, __FILE__, __LINE__); \
if (!_cybozu_eq) { \
std::cout << "ctest: lhs=" << static_cast<const void*>(x) << std::endl; \
std::cout << "ctest: rhs=" << static_cast<const void*>(y) << std::endl; \
} \
}

Definition at line 223 of file test.hpp.

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__); \
226 if (!_cybozu_eq) { \
227 std::cout << "ctest: lhs=" << static_cast<const void*>(x) << std::endl; \
228 std::cout << "ctest: rhs=" << static_cast<const void*>(y) << std::endl; \
229 } \
230}

◆ CYBOZU_TEST_EXCEPTION

#define CYBOZU_TEST_EXCEPTION ( statement,
Exception )
Value:
{ \
int _cybozu_ret = 0; \
try { \
statement; \
_cybozu_ret = 1; \
} catch (const Exception&) { \
} catch (...) { \
_cybozu_ret = 2; \
} \
if (_cybozu_ret) { \
cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION", #statement ", " #Exception, __FILE__, __LINE__); \
if (_cybozu_ret == 1) { \
std::cout << "ctest: no exception" << std::endl; \
} else { \
std::cout << "ctest: unexpected exception" << std::endl; \
} \
} else { \
cybozu::test::autoRun.set(true); \
} \
}
void set(bool isOK)
Definition test.hpp:44

Definition at line 287 of file test.hpp.

287#define CYBOZU_TEST_EXCEPTION(statement, Exception) \
288{ \
289 int _cybozu_ret = 0; \
290 try { \
291 statement; \
292 _cybozu_ret = 1; \
293 } catch (const Exception&) { \
294 } catch (...) { \
295 _cybozu_ret = 2; \
296 } \
297 if (_cybozu_ret) { \
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; \
301 } else { \
302 std::cout << "ctest: unexpected exception" << std::endl; \
303 } \
304 } else { \
305 cybozu::test::autoRun.set(true); \
306 } \
307}

◆ CYBOZU_TEST_EXCEPTION_MESSAGE

#define CYBOZU_TEST_EXCEPTION_MESSAGE ( statement,
Exception,
msg )
Value:
{ \
int _cybozu_ret = 0; \
std::string _cybozu_errMsg; \
try { \
statement; \
_cybozu_ret = 1; \
} catch (const Exception& _cybozu_e) { \
_cybozu_errMsg = _cybozu_e.what(); \
if (_cybozu_errMsg.find(msg) == std::string::npos) { \
_cybozu_ret = 2; \
} \
} catch (...) { \
_cybozu_ret = 3; \
} \
if (_cybozu_ret) { \
cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION_MESSAGE", #statement ", " #Exception ", " #msg, __FILE__, __LINE__); \
if (_cybozu_ret == 1) { \
std::cout << "ctest: no exception" << std::endl; \
} else if (_cybozu_ret == 2) { \
std::cout << "ctest: bad exception msg:" << _cybozu_errMsg << std::endl; \
} else { \
std::cout << "ctest: unexpected exception" << std::endl; \
} \
} else { \
cybozu::test::autoRun.set(true); \
} \
}

verify message in exception

Definition at line 258 of file test.hpp.

258#define CYBOZU_TEST_EXCEPTION_MESSAGE(statement, Exception, msg) \
259{ \
260 int _cybozu_ret = 0; \
261 std::string _cybozu_errMsg; \
262 try { \
263 statement; \
264 _cybozu_ret = 1; \
265 } catch (const Exception& _cybozu_e) { \
266 _cybozu_errMsg = _cybozu_e.what(); \
267 if (_cybozu_errMsg.find(msg) == std::string::npos) { \
268 _cybozu_ret = 2; \
269 } \
270 } catch (...) { \
271 _cybozu_ret = 3; \
272 } \
273 if (_cybozu_ret) { \
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; \
279 } else { \
280 std::cout << "ctest: unexpected exception" << std::endl; \
281 } \
282 } else { \
283 cybozu::test::autoRun.set(true); \
284 } \
285}

◆ CYBOZU_TEST_FAIL

#define CYBOZU_TEST_FAIL ( msg)
Value:
cybozu::test::test(false, "CYBOZU_TEST_FAIL", msg, __FILE__, __LINE__)

always alert

Parameters
msg[in]

Definition at line 253 of file test.hpp.

◆ CYBOZU_TEST_NEAR

#define CYBOZU_TEST_NEAR ( x,
y,
eps )
Value:
{ \
bool _cybozu_isNear = fabs((x) - (y)) < eps; \
cybozu::test::test(_cybozu_isNear, "CYBOZU_TEST_NEAR", #x ", " #y, __FILE__, __LINE__); \
if (!_cybozu_isNear) { \
std::cout << "ctest: lhs=" << (x) << std::endl; \
std::cout << "ctest: rhs=" << (y) << std::endl; \
} \
}
void fabs()

alert if fabs(x, y) >= eps

Parameters
x[in]
y[in]

Definition at line 214 of file test.hpp.

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; \
220 } \
221}

◆ CYBOZU_TEST_NO_EXCEPTION

#define CYBOZU_TEST_NO_EXCEPTION ( statement)
Value:
try { \
statement; \
cybozu::test::autoRun.set(true); \
} catch (...) { \
cybozu::test::test(false, "CYBOZU_TEST_NO_EXCEPTION", #statement, __FILE__, __LINE__); \
}

verify statement does not throw

Definition at line 312 of file test.hpp.

312#define CYBOZU_TEST_NO_EXCEPTION(statement) \
313try { \
314 statement; \
315 cybozu::test::autoRun.set(true); \
316} catch (...) { \
317 cybozu::test::test(false, "CYBOZU_TEST_NO_EXCEPTION", #statement, __FILE__, __LINE__); \
318}

◆ CYBOZU_TEST_SETUP_FIXTURE

#define CYBOZU_TEST_SETUP_FIXTURE ( Fixture)
Value:
Fixture *cybozu_test_local_fixture; \
void cybozu_test_local_init() \
{ \
cybozu_test_local_fixture = new Fixture(); \
} \
void cybozu_test_local_term() \
{ \
delete cybozu_test_local_fixture; \
} \
struct cybozu_test_local_fixture_setup_ { \
cybozu_test_local_fixture_setup_() \
{ \
cybozu::test::autoRun.setup(cybozu_test_local_init, cybozu_test_local_term); \
} \
} cybozu_test_local_fixture_setup_instance_;
void setup(Func init, Func term)
Definition test.hpp:35

setup fixture

Parameters
Fixture[in] class name of fixture
Note
cstr of Fixture is called before test and dstr of Fixture is called after test

Definition at line 358 of file test.hpp.

358#define CYBOZU_TEST_SETUP_FIXTURE(Fixture) \
359Fixture *cybozu_test_local_fixture; \
360void cybozu_test_local_init() \
361{ \
362 cybozu_test_local_fixture = new Fixture(); \
363} \
364void cybozu_test_local_term() \
365{ \
366 delete cybozu_test_local_fixture; \
367} \
368struct cybozu_test_local_fixture_setup_ { \
369 cybozu_test_local_fixture_setup_() \
370 { \
371 cybozu::test::autoRun.setup(cybozu_test_local_init, cybozu_test_local_term); \
372 } \
373} cybozu_test_local_fixture_setup_instance_;

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 184 of file test.hpp.

185{
186 return cybozu::test::autoRun.run(argc, argv);
187}
int run(int, char *argv[])
Definition test.hpp:64
char ** argv
Here is the call graph for this function: