1 /* cunit - A little C Unit facility */
3 /* Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 /* This is partially inspirated from the OSSP ts (Test Suite Library) */
14 #include "xbt/sysdep.h" /* XBT_GNU_PRINTF */
19 /* note that the internals of testall, that follow, are not publicly documented */
21 /* test suite object type */
22 typedef struct s_xbt_test_suite *xbt_test_suite_t;
24 /* test object type */
25 typedef struct s_xbt_test_unit *xbt_test_unit_t;
27 /* test callback function type */
28 typedef void (*ts_test_cb_t) (void);
30 /* test suite operations */
31 XBT_PUBLIC(xbt_test_suite_t) xbt_test_suite_new(const char *name,
32 const char *fmt, ...);
33 XBT_PUBLIC(xbt_test_suite_t) xbt_test_suite_by_name(const char *name,
34 const char *fmt, ...);
35 XBT_PUBLIC(void) xbt_test_suite_dump(xbt_test_suite_t suite);
36 XBT_PUBLIC(void) xbt_test_suite_push(xbt_test_suite_t suite,
37 const char *name, ts_test_cb_t func,
38 const char *fmt, ...);
40 /* Run all the specified tests. what_to_do allows to disable some tests.
41 * It is a coma (,) separated list of directives. They are applied from left to right.
43 * Each of them of form:
45 * [-|+]suitename[:unitname[:testname]]
48 * if it's a '-', the directive disables something
49 * if it's a '+', the directive enables something
50 * By default, everything is enabled, but you can disable a suite and reenable some parts
51 * * Suitename: the suite on which the directive acts
52 * * unitname: if given, the unit on which the directive acts. If not, acts on any units.
53 * * testname: if given, the test on which the directive acts. If not, acts on any tests.
56 XBT_PUBLIC(int) xbt_test_run(char *selection, int verbosity);
57 /* Show information about the selection of tests */
58 XBT_PUBLIC(void) xbt_test_dump(char *selection);
59 /* Cleanup the mess */
60 XBT_PUBLIC(void) xbt_test_exit(void);
64 * @addtogroup XBT_cunit
65 * @brief Unit test mechanism (to test a set of functions)
67 * This module is mainly intended to allow the tests of SimGrid
68 * itself and may lack the level of genericity that you would expect
69 * as a user. Only use it in external projects at your own risk (but
70 * it work rather well for us). We play with the idea of migrating
71 * to an external solution for our unit tests, possibly offering
72 * more features, but having absolutely no dependencies is a nice
73 * feature of SimGrid (and this code is sufficient to cover our
74 * needs, actually, so why should we bother switching?)
76 * Note that if you want to test a full binary (such as an example),
77 * you want to use our integration testing mechanism, not our unit
78 * testing one. Please refer to Section \ref
79 * inside_cmake_addtest_integration
81 * Some more information on our unit testing is available in Section @ref inside_cmake_addtest_unit.
83 * All code intended to be executed as a unit test will be extracted
84 * by a script (tools/sg_unit_extract.pl), and must thus be protected
85 * between preprocessor definitions, as follows. Note that
86 * SIMGRID_TEST string must appear on the endif line too for the
87 * script to work, and that this script does not allow to have more
88 * than one suite per file. For now, but patches are naturally
96 #endif // SIMGRID_TEST
102 /** @brief Provide informations about the suite declared in this file
105 * Actually, this macro is not used by C, but by the script
106 * extracting the test units, but that should be transparent for you.
108 * @param suite_name the short name of this suite, to be used in the --tests argument of testall afterward. Avoid spaces and any other strange chars
109 * @param suite_title instructive title that testall should display when your suite is run
111 #define XBT_TEST_SUITE(suite_name,suite_title)
113 /** @brief Declare a new test units (containing individual tests)
116 * @param name the short name that will be used in test all to enable/disable this test
117 * @param func a valid function name that will be used to contain all code of this unit
118 * @param title human informative description of your test (displayed in testall)
120 #define XBT_TEST_UNIT(name,func,title) \
121 void func(void); /*prototype*/ \
125 /* test operations */
126 XBT_PUBLIC(void) _xbt_test_add(const char *file, int line, const char *fmt,
127 ...) _XBT_GNUC_PRINTF(3, 4);
128 XBT_PUBLIC(void) _xbt_test_fail(const char *file, int line,
129 const char *fmt, ...) _XBT_GNUC_PRINTF(3,
131 XBT_PUBLIC(void) _xbt_test_log(const char *file, int line, const char *fmt,
132 ...) _XBT_GNUC_PRINTF(3, 4);
133 /** @brief Declare that a new test begins (printf-like parameters, describing the test)
134 * @hideinitializer */
135 #define xbt_test_add(...) _xbt_test_add(__FILE__, __LINE__, __VA_ARGS__)
136 /** @brief Declare that the lastly started test failed (printf-like parameters, describing failure cause)
137 * @hideinitializer */
138 #define xbt_test_fail(...) _xbt_test_fail(__FILE__, __LINE__, __VA_ARGS__)
139 /** @brief The lastly started test is actually an assert
142 * - If provided a uniq parameter, this is assumed to be a condition that is expected to be true
143 * - If provided more parameters, the first one is a condition, and the other ones are printf-like arguments that are to be displayed when the condition fails.
145 #define xbt_test_assert(...) _XBT_IF_ONE_ARG(_xbt_test_assert_ARG1, \
146 _xbt_test_assert_ARGN, \
147 __VA_ARGS__)(__VA_ARGS__)
148 #define _xbt_test_assert_ARG1(cond) _xbt_test_assert_CHECK(cond, \
150 #define _xbt_test_assert_ARGN(cond, ...) _xbt_test_assert_CHECK(cond, \
152 #define _xbt_test_assert_CHECK(cond, ...) \
153 do { if (!(cond)) xbt_test_fail(__VA_ARGS__); } while (0)
154 #define xbt_test_log(...) _xbt_test_log(__FILE__, __LINE__, __VA_ARGS__)
156 /** @brief Declare that the lastly started test failed because of the provided exception */
157 XBT_PUBLIC(void) xbt_test_exception(xbt_ex_t e);
159 /** @brief Declare that the lastly started test was expected to fail (and actually failed) */
160 XBT_PUBLIC(void) xbt_test_expect_failure(void);
161 /** @brief Declare that the lastly started test should be skiped today */
162 XBT_PUBLIC(void) xbt_test_skip(void);
166 #ifdef XBT_USE_DEPRECATED
168 /* Kept for backward compatibility. */
170 #define xbt_test_add0(...) xbt_test_add(__VA_ARGS__)
171 #define xbt_test_add1(...) xbt_test_add(__VA_ARGS__)
172 #define xbt_test_add2(...) xbt_test_add(__VA_ARGS__)
173 #define xbt_test_add3(...) xbt_test_add(__VA_ARGS__)
174 #define xbt_test_add4(...) xbt_test_add(__VA_ARGS__)
175 #define xbt_test_add5(...) xbt_test_add(__VA_ARGS__)
177 #define xbt_test_fail0(...) xbt_test_fail(__VA_ARGS__)
178 #define xbt_test_fail1(...) xbt_test_fail(__VA_ARGS__)
179 #define xbt_test_fail2(...) xbt_test_fail(__VA_ARGS__)
180 #define xbt_test_fail3(...) xbt_test_fail(__VA_ARGS__)
181 #define xbt_test_fail4(...) xbt_test_fail(__VA_ARGS__)
182 #define xbt_test_fail5(...) xbt_test_fail(__VA_ARGS__)
184 #define xbt_test_assert0(...) xbt_test_assert(__VA_ARGS__)
185 #define xbt_test_assert1(...) xbt_test_assert(__VA_ARGS__)
186 #define xbt_test_assert2(...) xbt_test_assert(__VA_ARGS__)
187 #define xbt_test_assert3(...) xbt_test_assert(__VA_ARGS__)
188 #define xbt_test_assert4(...) xbt_test_assert(__VA_ARGS__)
189 #define xbt_test_assert5(...) xbt_test_assert(__VA_ARGS__)
191 #define xbt_test_log0(...) xbt_test_log(__VA_ARGS__)
192 #define xbt_test_log1(...) xbt_test_log(__VA_ARGS__)
193 #define xbt_test_log2(...) xbt_test_log(__VA_ARGS__)
194 #define xbt_test_log3(...) xbt_test_log(__VA_ARGS__)
195 #define xbt_test_log4(...) xbt_test_log(__VA_ARGS__)
196 #define xbt_test_log5(...) xbt_test_log(__VA_ARGS__)