Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add MSG_host_get_storage_list() function
[simgrid.git] / include / xbt / cunit.h
1 /* cunit - A little C Unit facility                                         */
2
3 /* Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 /* This is partially inspirated from the OSSP ts (Test Suite Library)       */
10
11 #ifndef _XBT_CUNIT_H_
12 #define _XBT_CUNIT_H_
13
14 #include "xbt/sysdep.h"         /* XBT_GNU_PRINTF */
15 #include "xbt/ex.h"
16
17 SG_BEGIN_DECL()
18
19 /* note that the internals of testall, that follow, are not publicly documented */
20
21 /* test suite object type */
22 typedef struct s_xbt_test_suite *xbt_test_suite_t;
23
24 /* test object type */
25 typedef struct s_xbt_test_unit *xbt_test_unit_t;
26
27 /* test callback function type */
28 typedef void (*ts_test_cb_t) (void);
29
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, ...);
39
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.
42  *
43  * Each of them of form:
44  * 
45  * [-|+]suitename[:unitname[:testname]]
46  * 
47  * * First char: 
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.
54  */
55
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);
61
62
63 /** 
64  * @addtogroup XBT_cunit
65  * @brief Unit test mechanism (to test a set of functions)
66  *  
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?)
75  * 
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
80  * 
81  * Some more information on our unit testing is available in Section @ref inside_cmake_addtest_unit.  
82  * 
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
89  * welcome.
90  * 
91 @verbatim
92 #ifdef SIMGRID_TEST
93
94 <your code>
95
96 #endif  // SIMGRID_TEST
97 @endverbatim
98  * 
99  *
100  * @{ 
101  */
102 /** @brief Provide informations about the suite declared in this file
103  *  @hideinitializer
104  * 
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.
107  *
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
110  */
111 #define XBT_TEST_SUITE(suite_name,suite_title)
112
113 /** @brief Declare a new test units (containing individual tests)
114  *  @hideinitializer
115  *
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)
119  */
120 #define XBT_TEST_UNIT(name,func,title)    \
121     void func(void);  /*prototype*/       \
122     void func(void)
123
124
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,
130                                                                        4);
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
140  *  @hideinitializer 
141  * 
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.
144  */
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,   \
149                                                                 "%s", #cond)
150 #define _xbt_test_assert_ARGN(cond, ...) _xbt_test_assert_CHECK(cond,   \
151                                                                 __VA_ARGS__)
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__)
155
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);
158
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);
163
164 /** @} */
165
166 #ifdef XBT_USE_DEPRECATED
167
168 /* Kept for backward compatibility. */
169
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__)
176
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__)
183
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__)
190
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__)
197
198 #endif
199
200 SG_END_DECL()
201 #endif                          /* _TS_H_ */