Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #65 from fabienchaix/master
[simgrid.git] / include / xbt / cunit.h
1 /* cunit - A little C Unit facility                                         */
2
3 /* Copyright (c) 2005-2014. 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 testing implementation (see @ref inside_tests_add_units)
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  * Unit testing is not intended to write integration tests.
77  * Please refer to \ref inside_tests_add_integration for that instead.
78  * 
79  * 
80  *
81  * @{ 
82  */
83 /** @brief Provide informations about the suite declared in this file
84  *  @hideinitializer
85  * 
86  * Actually, this macro is only used by the script extracting the test 
87  * units, but that should be transparent for you. 
88  *
89  * @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
90  * @param suite_title instructive title that testall should display when your suite is run
91  */
92 #define XBT_TEST_SUITE(suite_name,suite_title)
93
94 /** @brief Declare a new test units (containing individual tests)
95  *  @hideinitializer
96  *
97  * @param name the short name that will be used in test all to enable/disable this test
98  * @param func a valid function name that will be used to contain all code of this unit
99  * @param title human informative description of your test (displayed in testall)
100  */
101 #ifdef __cplusplus
102 #define XBT_TEST_UNIT(name,func,title)    \
103     extern "C" void func(void);  /*prototype*/ \
104     void func(void)
105 #else
106 #define XBT_TEST_UNIT(name,func,title)    \
107     void func(void);  /*prototype*/       \
108     void func(void)
109 #endif
110
111 /* test operations */
112 XBT_PUBLIC(void) _xbt_test_add(const char *file, int line, const char *fmt,
113                                ...) XBT_ATTRIB_PRINTF(3, 4);
114 XBT_PUBLIC(void) _xbt_test_fail(const char *file, int line,
115                                 const char *fmt, ...) XBT_ATTRIB_PRINTF(3,
116                                                                        4);
117 XBT_PUBLIC(void) _xbt_test_log(const char *file, int line, const char *fmt,
118                                ...) XBT_ATTRIB_PRINTF(3, 4);
119 /** @brief Declare that a new test begins (printf-like parameters, describing the test) 
120  *  @hideinitializer */
121 #define xbt_test_add(...)       _xbt_test_add(__FILE__, __LINE__, __VA_ARGS__)
122 /** @brief Declare that the lastly started test failed (printf-like parameters, describing failure cause) 
123  *  @hideinitializer */
124 #define xbt_test_fail(...)      _xbt_test_fail(__FILE__, __LINE__, __VA_ARGS__)
125 /** @brief The lastly started test is actually an assert
126  *  @hideinitializer 
127  * 
128  * - If provided a uniq parameter, this is assumed to be a condition that is expected to be true
129  * - 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.
130  */
131 #define xbt_test_assert(...)    _XBT_IF_ONE_ARG(_xbt_test_assert_ARG1,  \
132                                                 _xbt_test_assert_ARGN,  \
133                                                 __VA_ARGS__)(__VA_ARGS__)
134 #define _xbt_test_assert_ARG1(cond)      _xbt_test_assert_CHECK(cond,   \
135                                                                 "%s", #cond)
136 #define _xbt_test_assert_ARGN(cond, ...) _xbt_test_assert_CHECK(cond,   \
137                                                                 __VA_ARGS__)
138 #define _xbt_test_assert_CHECK(cond, ...)                       \
139   do { if (!(cond)) xbt_test_fail(__VA_ARGS__); } while (0)
140 /** @brief Report some details to help debugging when the test fails (shown only on failure)
141  *  @hideinitializer  */
142 #define xbt_test_log(...)       _xbt_test_log(__FILE__, __LINE__, __VA_ARGS__)
143
144 /** @brief Declare that the lastly started test failed because of the provided exception */
145 XBT_PUBLIC(void) xbt_test_exception(xbt_ex_t e);
146
147 /** @brief Declare that the lastly started test was expected to fail (and actually failed) */
148 XBT_PUBLIC(void) xbt_test_expect_failure(void);
149 /** @brief Declare that the lastly started test should be skiped today */
150 XBT_PUBLIC(void) xbt_test_skip(void);
151
152 /** @} */
153
154 #ifdef XBT_USE_DEPRECATED
155
156 /* Kept for backward compatibility. */
157
158 #define xbt_test_add0(...)      xbt_test_add(__VA_ARGS__)
159 #define xbt_test_add1(...)      xbt_test_add(__VA_ARGS__)
160 #define xbt_test_add2(...)      xbt_test_add(__VA_ARGS__)
161 #define xbt_test_add3(...)      xbt_test_add(__VA_ARGS__)
162 #define xbt_test_add4(...)      xbt_test_add(__VA_ARGS__)
163 #define xbt_test_add5(...)      xbt_test_add(__VA_ARGS__)
164
165 #define xbt_test_fail0(...)     xbt_test_fail(__VA_ARGS__)
166 #define xbt_test_fail1(...)     xbt_test_fail(__VA_ARGS__)
167 #define xbt_test_fail2(...)     xbt_test_fail(__VA_ARGS__)
168 #define xbt_test_fail3(...)     xbt_test_fail(__VA_ARGS__)
169 #define xbt_test_fail4(...)     xbt_test_fail(__VA_ARGS__)
170 #define xbt_test_fail5(...)     xbt_test_fail(__VA_ARGS__)
171
172 #define xbt_test_assert0(...)   xbt_test_assert(__VA_ARGS__)
173 #define xbt_test_assert1(...)   xbt_test_assert(__VA_ARGS__)
174 #define xbt_test_assert2(...)   xbt_test_assert(__VA_ARGS__)
175 #define xbt_test_assert3(...)   xbt_test_assert(__VA_ARGS__)
176 #define xbt_test_assert4(...)   xbt_test_assert(__VA_ARGS__)
177 #define xbt_test_assert5(...)   xbt_test_assert(__VA_ARGS__)
178
179 #define xbt_test_log0(...)      xbt_test_log(__VA_ARGS__)
180 #define xbt_test_log1(...)      xbt_test_log(__VA_ARGS__)
181 #define xbt_test_log2(...)      xbt_test_log(__VA_ARGS__)
182 #define xbt_test_log3(...)      xbt_test_log(__VA_ARGS__)
183 #define xbt_test_log4(...)      xbt_test_log(__VA_ARGS__)
184 #define xbt_test_log5(...)      xbt_test_log(__VA_ARGS__)
185
186 #endif
187
188 SG_END_DECL()
189 #endif                          /* _TS_H_ */