src/cx/test.h

changeset 985
68754c7de906
parent 959
0e1bf3c199bf
equal deleted inserted replaced
984:e8f354a25ac8 985:68754c7de906
94 94
95 /** Type for the CxTestSuite. */ 95 /** Type for the CxTestSuite. */
96 typedef struct CxTestSuite CxTestSuite; 96 typedef struct CxTestSuite CxTestSuite;
97 97
98 /** Pointer to a test function. */ 98 /** Pointer to a test function. */
99 cx_attr_nonnull
99 typedef void(*CxTest)(CxTestSuite *, void *, cx_write_func); 100 typedef void(*CxTest)(CxTestSuite *, void *, cx_write_func);
100 101
101 /** Type for the internal list of test cases. */ 102 /** Type for the internal list of test cases. */
102 typedef struct CxTestSet CxTestSet; 103 typedef struct CxTestSet CxTestSet;
103 104
135 /** 136 /**
136 * Creates a new test suite. 137 * Creates a new test suite.
137 * @param name optional name of the suite 138 * @param name optional name of the suite
138 * @return a new test suite 139 * @return a new test suite
139 */ 140 */
141 cx_attr_nonnull
142 cx_attr_nodiscard
143 cx_attr_cstr_arg(1)
144 cx_attr_malloc
140 static inline CxTestSuite* cx_test_suite_new(const char *name) { 145 static inline CxTestSuite* cx_test_suite_new(const char *name) {
141 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite)); 146 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite));
142 if (suite != NULL) { 147 if (suite != NULL) {
143 suite->name = name; 148 suite->name = name;
144 suite->success = 0; 149 suite->success = 0;
152 /** 157 /**
153 * Destroys a test suite. 158 * Destroys a test suite.
154 * @param suite the test suite to destroy 159 * @param suite the test suite to destroy
155 */ 160 */
156 static inline void cx_test_suite_free(CxTestSuite* suite) { 161 static inline void cx_test_suite_free(CxTestSuite* suite) {
162 if (suite == NULL) return;
157 CxTestSet *l = suite->tests; 163 CxTestSet *l = suite->tests;
158 while (l != NULL) { 164 while (l != NULL) {
159 CxTestSet *e = l; 165 CxTestSet *e = l;
160 l = l->next; 166 l = l->next;
161 free(e); 167 free(e);
168 * 174 *
169 * @param suite the suite, the test function shall be added to 175 * @param suite the suite, the test function shall be added to
170 * @param test the test function to register 176 * @param test the test function to register
171 * @return zero on success or non-zero on failure 177 * @return zero on success or non-zero on failure
172 */ 178 */
179 cx_attr_nonnull
173 static inline int cx_test_register(CxTestSuite* suite, CxTest test) { 180 static inline int cx_test_register(CxTestSuite* suite, CxTest test) {
174 CxTestSet *t = (CxTestSet*) malloc(sizeof(CxTestSet)); 181 CxTestSet *t = (CxTestSet*) malloc(sizeof(CxTestSet));
175 if (t) { 182 if (t) {
176 t->test = test; 183 t->test = test;
177 t->next = NULL; 184 t->next = NULL;
194 * Runs a test suite and writes the test log to the specified stream. 201 * Runs a test suite and writes the test log to the specified stream.
195 * @param suite the test suite to run 202 * @param suite the test suite to run
196 * @param out_target the target buffer or file to write the output to 203 * @param out_target the target buffer or file to write the output to
197 * @param out_writer the write function writing to \p out_target 204 * @param out_writer the write function writing to \p out_target
198 */ 205 */
206 cx_attr_nonnull
199 static inline void cx_test_run(CxTestSuite *suite, 207 static inline void cx_test_run(CxTestSuite *suite,
200 void *out_target, cx_write_func out_writer) { 208 void *out_target, cx_write_func out_writer) {
201 if (suite->name == NULL) { 209 if (suite->name == NULL) {
202 out_writer("*** Test Suite ***\n", 1, 19, out_target); 210 out_writer("*** Test Suite ***\n", 1, 19, out_target);
203 } else { 211 } else {

mercurial