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:

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 192 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 322 of file test.hpp.

322#define CYBOZU_TEST_AUTO(name) \
323void cybozu_test_ ## name(); \
324struct cybozu_test_local_ ## name { \
325 cybozu_test_local_ ## name() \
326 { \
327 cybozu::test::autoRun.append(#name, cybozu_test_ ## name); \
328 } \
329} cybozu_test_local_instance_ ## name; \
330void 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 336 of file test.hpp.

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

◆ CYBOZU_TEST_EQUAL

#define CYBOZU_TEST_EQUAL ( x,
y )
Value:
{ \
bool eq = cybozu::test::isEqual(x, y); \
cybozu::test::test(eq, "CYBOZU_TEST_EQUAL", #x ", " #y, __FILE__, __LINE__); \
if (!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 199 of file test.hpp.

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

◆ CYBOZU_TEST_EQUAL_ARRAY

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

alert if x[] != y[]

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

Definition at line 235 of file test.hpp.

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__); \
239 if (!eq) { \
240 std::cout << "ctest: i=" << i << std::endl; \
241 std::cout << "ctest: lhs=" << (x) << std::endl; \
242 std::cout << "ctest: rhs=" << (y) << std::endl; \
243 } \
244 } \
245}

◆ CYBOZU_TEST_EQUAL_POINTER

#define CYBOZU_TEST_EQUAL_POINTER ( x,
y )
Value:
{ \
bool eq = x == y; \
cybozu::test::test(eq, "CYBOZU_TEST_EQUAL_POINTER", #x ", " #y, __FILE__, __LINE__); \
if (!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 221 of file test.hpp.

221#define CYBOZU_TEST_EQUAL_POINTER(x, y) { \
222 bool eq = x == y; \
223 cybozu::test::test(eq, "CYBOZU_TEST_EQUAL_POINTER", #x ", " #y, __FILE__, __LINE__); \
224 if (!eq) { \
225 std::cout << "ctest: lhs=" << static_cast<const void*>(x) << std::endl; \
226 std::cout << "ctest: rhs=" << static_cast<const void*>(y) << std::endl; \
227 } \
228}

◆ CYBOZU_TEST_EXCEPTION

#define CYBOZU_TEST_EXCEPTION ( statement,
Exception )
Value:
{ \
int ret = 0; \
try { \
statement; \
ret = 1; \
} catch (const Exception&) { \
} catch (...) { \
ret = 2; \
} \
if (ret) { \
cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION", #statement ", " #Exception, __FILE__, __LINE__); \
if (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
CK_RV ret

Definition at line 285 of file test.hpp.

285#define CYBOZU_TEST_EXCEPTION(statement, Exception) \
286{ \
287 int ret = 0; \
288 try { \
289 statement; \
290 ret = 1; \
291 } catch (const Exception&) { \
292 } catch (...) { \
293 ret = 2; \
294 } \
295 if (ret) { \
296 cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION", #statement ", " #Exception, __FILE__, __LINE__); \
297 if (ret == 1) { \
298 std::cout << "ctest: no exception" << std::endl; \
299 } else { \
300 std::cout << "ctest: unexpected exception" << std::endl; \
301 } \
302 } else { \
303 cybozu::test::autoRun.set(true); \
304 } \
305}

◆ CYBOZU_TEST_EXCEPTION_MESSAGE

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

verify message in exception

Definition at line 256 of file test.hpp.

256#define CYBOZU_TEST_EXCEPTION_MESSAGE(statement, Exception, msg) \
257{ \
258 int ret = 0; \
259 std::string errMsg; \
260 try { \
261 statement; \
262 ret = 1; \
263 } catch (const Exception& e) { \
264 errMsg = e.what(); \
265 if (errMsg.find(msg) == std::string::npos) { \
266 ret = 2; \
267 } \
268 } catch (...) { \
269 ret = 3; \
270 } \
271 if (ret) { \
272 cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION_MESSAGE", #statement ", " #Exception ", " #msg, __FILE__, __LINE__); \
273 if (ret == 1) { \
274 std::cout << "ctest: no exception" << std::endl; \
275 } else if (ret == 2) { \
276 std::cout << "ctest: bad exception msg:" << errMsg << std::endl; \
277 } else { \
278 std::cout << "ctest: unexpected exception" << std::endl; \
279 } \
280 } else { \
281 cybozu::test::autoRun.set(true); \
282 } \
283}

◆ 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 251 of file test.hpp.

◆ CYBOZU_TEST_NEAR

#define CYBOZU_TEST_NEAR ( x,
y,
eps )
Value:
{ \
bool isNear = fabs((x) - (y)) < eps; \
cybozu::test::test(isNear, "CYBOZU_TEST_NEAR", #x ", " #y, __FILE__, __LINE__); \
if (!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 212 of file test.hpp.

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__); \
215 if (!isNear) { \
216 std::cout << "ctest: lhs=" << (x) << std::endl; \
217 std::cout << "ctest: rhs=" << (y) << std::endl; \
218 } \
219}

◆ 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 310 of file test.hpp.

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

◆ 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 356 of file test.hpp.

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

Function Documentation

◆ main()

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

Definition at line 182 of file test.hpp.

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