source: mainline/uspace/lib/pcut/src/run.c@ c280d7e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c280d7e was 193d280c, checked in by Martin Decky <martin@…>, 10 years ago

cstyle improvements
replace traditional K&R-style function declarations and definitions

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[01579ad]1/*
2 * Copyright (c) 2012-2013 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file
30 *
31 * Test execution routines.
32 */
33
34#include "internal.h"
35#ifndef PCUT_NO_LONG_JUMP
36#include <setjmp.h>
37#endif
38
39#ifndef PCUT_NO_LONG_JUMP
40/** Long-jump buffer. */
41static jmp_buf start_test_jump;
42#endif
43
44/** Whether to run a tear-down function on a failure.
45 *
46 * Used to determine whether we are already in a tear-down context.
47 */
48static int execute_teardown_on_failure;
49
50/** Whether to report test result at all.
51 *
52 * Used to determine whether we are the forked or the parent process.
53 */
54static int report_test_result;
55
56/** Whether to print test error.
57 *
58 * Used to determine whether we are the forked or the parent process.
59 */
60static int print_test_error;
61
62/** Whether leaving a test means a process exit. */
63static int leave_means_exit;
64
65/** Pointer to currently running test. */
66static pcut_item_t *current_test = NULL;
67
68/** Pointer to current test suite. */
69static pcut_item_t *current_suite = NULL;
70
71/** A NULL-like suite. */
[9b20126]72static pcut_item_t default_suite;
73static int default_suite_initialized = 0;
74
[193d280c]75static void init_default_suite_when_needed(void)
76{
77 if (default_suite_initialized)
[9b20126]78 return;
[193d280c]79
[9b20126]80 default_suite.id = -1;
81 default_suite.kind = PCUT_KIND_TESTSUITE;
82 default_suite.previous = NULL;
83 default_suite.next = NULL;
84 default_suite.name = "Default";
85 default_suite.setup_func = NULL;
86 default_suite.teardown_func = NULL;
87}
[01579ad]88
89/** Find the suite given test belongs to.
90 *
91 * @param it The test.
92 * @return Always a valid test suite item.
93 */
[193d280c]94static pcut_item_t *pcut_find_parent_suite(pcut_item_t *it)
95{
[01579ad]96 while (it != NULL) {
[193d280c]97 if (it->kind == PCUT_KIND_TESTSUITE)
[01579ad]98 return it;
[193d280c]99
[01579ad]100 it = it->previous;
101 }
[193d280c]102
[9b20126]103 init_default_suite_when_needed();
[01579ad]104 return &default_suite;
105}
106
107/** Run a set-up (tear-down) function.
108 *
109 * @param func Function to run (can be NULL).
110 */
[193d280c]111static void run_setup_teardown(pcut_setup_func_t func)
112{
113 if (func != NULL)
[01579ad]114 func();
115}
116
117/** Terminate current test with given outcome.
118 *
119 * @warning This function may execute a long jump or terminate
120 * current process.
121 *
122 * @param outcome Outcome of the current test.
123 */
[193d280c]124static void leave_test(int outcome)
125{
[9b20126]126 PCUT_DEBUG("leave_test(outcome=%d), will_exit=%s", outcome,
[193d280c]127 leave_means_exit ? "yes" : "no");
128 if (leave_means_exit)
[01579ad]129 exit(outcome);
[193d280c]130
[01579ad]131#ifndef PCUT_NO_LONG_JUMP
132 longjmp(start_test_jump, 1);
133#endif
134}
135
136/** Process a failed assertion.
137 *
138 * @warning This function calls leave_test() and typically will not
139 * return.
140 *
141 * @param message Message describing the failure.
142 */
[193d280c]143void pcut_failed_assertion(const char *message)
144{
[01579ad]145 static const char *prev_message = NULL;
[193d280c]146
[01579ad]147 /*
148 * The assertion failed. We need to abort the current test,
149 * inform the user and perform some clean-up. That could
150 * include running the tear-down routine.
151 */
[193d280c]152 if (print_test_error)
[01579ad]153 pcut_print_fail_message(message);
[193d280c]154
[01579ad]155 if (execute_teardown_on_failure) {
156 execute_teardown_on_failure = 0;
157 prev_message = message;
[9b20126]158 run_setup_teardown(current_suite->teardown_func);
[193d280c]159
[01579ad]160 /* Tear-down was okay. */
161 if (report_test_result) {
162 pcut_report_test_done(current_test, TEST_OUTCOME_FAIL,
163 message, NULL, NULL);
164 }
165 } else {
166 if (report_test_result) {
167 pcut_report_test_done(current_test, TEST_OUTCOME_FAIL,
168 prev_message, message, NULL);
169 }
170 }
[193d280c]171
[01579ad]172 prev_message = NULL;
[193d280c]173
[01579ad]174 leave_test(TEST_OUTCOME_FAIL); /* No return. */
175}
176
177/** Run a test.
178 *
179 * @param test Test to execute.
180 * @return Error status (zero means success).
181 */
[193d280c]182static int run_test(pcut_item_t *test)
183{
[01579ad]184 /*
185 * Set here as the returning point in case of test failure.
186 * If we get here, it means something failed during the
187 * test execution.
188 */
[193d280c]189
[01579ad]190#ifndef PCUT_NO_LONG_JUMP
191 int test_finished = setjmp(start_test_jump);
[193d280c]192 if (test_finished)
[01579ad]193 return 1;
194#endif
[193d280c]195
196 if (report_test_result)
[01579ad]197 pcut_report_test_start(test);
[193d280c]198
[01579ad]199 current_suite = pcut_find_parent_suite(test);
200 current_test = test;
[193d280c]201
[9b20126]202 pcut_hook_before_test(test);
[193d280c]203
[01579ad]204 /*
205 * If anything goes wrong, execute the tear-down function
206 * as well.
207 */
208 execute_teardown_on_failure = 1;
[193d280c]209
[01579ad]210 /*
211 * Run the set-up function.
212 */
[9b20126]213 run_setup_teardown(current_suite->setup_func);
[193d280c]214
[01579ad]215 /*
216 * The setup function was performed, it is time to run
217 * the actual test.
218 */
[9b20126]219 test->test_func();
[193d280c]220
[01579ad]221 /*
222 * Finally, run the tear-down function. We need to clear
223 * the flag to prevent endless loop.
224 */
225 execute_teardown_on_failure = 0;
[9b20126]226 run_setup_teardown(current_suite->teardown_func);
[193d280c]227
[01579ad]228 /*
229 * If we got here, it means everything went well with
230 * this test.
231 */
[193d280c]232 if (report_test_result)
[01579ad]233 pcut_report_test_done(current_test, TEST_OUTCOME_PASS,
[193d280c]234 NULL, NULL, NULL);
235
[01579ad]236 return 0;
237}
238
239/** Run a test in a forked mode.
240 *
241 * Forked mode means that the caller of the test is already a new
242 * process running this test only.
243 *
244 * @param test Test to execute.
245 * @return Error status (zero means success).
246 */
[193d280c]247int pcut_run_test_forked(pcut_item_t *test)
248{
[01579ad]249 report_test_result = 0;
250 print_test_error = 1;
251 leave_means_exit = 1;
[193d280c]252
253 int rc = run_test(test);
254
[01579ad]255 current_test = NULL;
256 current_suite = NULL;
[193d280c]257
[01579ad]258 return rc;
259}
260
261/** Run a test in a single mode.
262 *
263 * Single mode means that the test is called in the context of the
264 * parent process, that is no new process is forked.
265 *
266 * @param test Test to execute.
267 * @return Error status (zero means success).
268 */
[193d280c]269int pcut_run_test_single(pcut_item_t *test)
270{
[01579ad]271 report_test_result = 1;
272 print_test_error = 0;
273 leave_means_exit = 0;
[193d280c]274
275 int rc = run_test(test);
276
[01579ad]277 current_test = NULL;
278 current_suite = NULL;
[193d280c]279
[01579ad]280 return rc;
281}
282
[134ac5d]283/** Tells time-out length for a given test.
284 *
285 * @param test Test for which the time-out is questioned.
286 * @return Timeout in seconds.
287 */
[193d280c]288int pcut_get_test_timeout(pcut_item_t *test)
289{
[134ac5d]290 int timeout = PCUT_DEFAULT_TEST_TIMEOUT;
[9b20126]291 pcut_extra_t *extras = test->extras;
[193d280c]292
[134ac5d]293 while (extras->type != PCUT_EXTRA_LAST) {
[193d280c]294 if (extras->type == PCUT_EXTRA_TIMEOUT)
[134ac5d]295 timeout = extras->timeout;
[193d280c]296
[134ac5d]297 extras++;
298 }
[193d280c]299
[134ac5d]300 return timeout;
301}
Note: See TracBrowser for help on using the repository browser.