2 * Copyright (c) 2012-2013 Vojtech Horky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 * Common definitions internally used in PCUT.
33 #ifndef PCUT_INTERNAL_H_GUARD
34 #define PCUT_INTERNAL_H_GUARD
36 #include <pcut/pcut.h>
38 #pragma warning(push, 0)
43 /** @def PCUT_DEBUG(msg, ...)
46 * By default, this macro does nothing. Define PCUT_DEBUG_BUILD to
47 * actually print the messages to the console.
49 * @param msg Printf-like formatting message.
50 * @param ... Extra arguments for printf.
52 #ifdef PCUT_DEBUG_BUILD
53 #pragma warning(push, 0)
56 #define PCUT_DEBUG_INTERNAL(msg, ...) \
57 fprintf(stderr, "[PCUT %s:%d]: " msg "%s", __FILE__, __LINE__, __VA_ARGS__)
58 #define PCUT_DEBUG(...) \
59 PCUT_DEBUG_INTERNAL( \
60 PCUT_VARG_GET_FIRST(__VA_ARGS__, this_arg_is_ignored), \
61 PCUT_VARG_SKIP_FIRST(__VA_ARGS__, "\n") \
64 #define PCUT_DEBUG(...) (void)0
68 /** Mark a variable as unused. */
69 #define PCUT_UNUSED(x) ((void)x)
71 /** Forking mode for test execution.
73 * In this mode, each test is run in a separate process.
74 * This ensures that even SIGSEGV does not stop the framework itself.
76 #define PCUT_RUN_MODE_FORKING 1
78 /** Single-process mode for test execution.
80 * This mode is used when new process is launched when in forking-mode or
81 * this mode can be used if we are sure that no test would fail
82 * fatally (that is causing an unexpected program exit).
84 #define PCUT_RUN_MODE_SINGLE 2
86 extern int pcut_run_mode
;
89 pcut_item_t
*pcut_fix_list_get_real_head(pcut_item_t
*last
);
90 int pcut_count_tests(pcut_item_t
*it
);
91 void pcut_print_items(pcut_item_t
*first
);
93 pcut_item_t
*pcut_get_real_next(pcut_item_t
*item
);
94 pcut_item_t
*pcut_get_real(pcut_item_t
*item
);
95 void pcut_print_tests(pcut_item_t
*first
);
96 int pcut_is_arg_with_number(const char *arg
, const char *opt
, int *value
);
98 int pcut_run_test_forking(const char *self_path
, pcut_item_t
*test
);
99 int pcut_run_test_forked(pcut_item_t
*test
);
100 int pcut_run_test_single(pcut_item_t
*test
);
102 int pcut_get_test_timeout(pcut_item_t
*test
);
104 void pcut_failed_assertion(const char *message
);
105 void pcut_print_fail_message(const char *msg
);
107 /** Reporting callbacks structure. */
108 typedef struct pcut_report_ops pcut_report_ops_t
;
110 /** @copydoc pcut_report_ops_t */
111 struct pcut_report_ops
{
112 /** Initialize the reporting, given all tests. */
113 void (*init
)(pcut_item_t
*);
114 /** Finalize the reporting. */
116 /** Test suite just started. */
117 void (*suite_start
)(pcut_item_t
*);
118 /** Test suite completed. */
119 void (*suite_done
)(pcut_item_t
*);
120 /** Test is about to start. */
121 void (*test_start
)(pcut_item_t
*);
122 /** Test completed. */
123 void (*test_done
)(pcut_item_t
*, int, const char *, const char *,
127 void pcut_report_register_handler(pcut_report_ops_t
*ops
);
129 void pcut_report_init(pcut_item_t
*all_items
);
130 void pcut_report_suite_start(pcut_item_t
*suite
);
131 void pcut_report_suite_done(pcut_item_t
*suite
);
132 void pcut_report_test_start(pcut_item_t
*test
);
133 void pcut_report_test_done(pcut_item_t
*test
, int outcome
,
134 const char *error_message
, const char *teardown_error_message
,
135 const char *extra_output
);
136 void pcut_report_test_done_unparsed(pcut_item_t
*test
, int outcome
,
137 const char *unparsed_output
, size_t unparsed_output_size
);
138 void pcut_report_done(void);
140 /* OS-dependent functions. */
142 /** Hook to execute before test starts.
144 * Useful for OS-specific preparations prior to launching the actual
145 * test code (i. e. sandboxing the process more etc.).
147 * This function is not run by the launcher process that only
148 * starts other tests in separate processes.
150 * @param test The test that is about to be executed.
152 void pcut_hook_before_test(pcut_item_t
*test
);
154 /** Tell whether two strings start with the same prefix.
156 * @param a First string.
157 * @param b Second string.
158 * @param len Length of common prefix.
159 * @return Whether first @p len characters of @p a are the same as in @p b.
161 int pcut_str_start_equals(const char *a
, const char *b
, int len
);
163 /** Get size of string in bytes.
165 * @param s String in question.
166 * @return Size of @p s in bytes.
168 int pcut_str_size(const char *s
);
170 /** Convert string to integer.
172 * @param s String with integer.
173 * @return Converted integer.
175 int pcut_str_to_int(const char *s
);
177 /** Find character in a string.
179 * @param haystack Where to look for the @p needle.
180 * @param needle Character to find.
181 * @return String starting with @p needle.
182 * @retval NULL there is no @p needle in @p haystack.
184 char *pcut_str_find_char(const char *haystack
, const char needle
);
186 /** Format string to a buffer.
189 int pcut_snprintf(char *dest
, size_t size
, const char *format
, ...);