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 | * Common definitions internally used in PCUT.
|
---|
31 | */
|
---|
32 |
|
---|
33 | #ifndef PCUT_INTERNAL_H_GUARD
|
---|
34 | #define PCUT_INTERNAL_H_GUARD
|
---|
35 |
|
---|
36 | #include <pcut/pcut.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /** @def PCUT_DEBUG(msg, ...)
|
---|
41 | * Debug printing.
|
---|
42 | *
|
---|
43 | * By default, this macro does nothing. Define PCUT_DEBUG_BUILD to
|
---|
44 | * actually print the messages to the console.
|
---|
45 | *
|
---|
46 | * @param msg Printf-like formatting message.
|
---|
47 | * @param ... Extra arguments for printf.
|
---|
48 | */
|
---|
49 | #ifdef PCUT_DEBUG_BUILD
|
---|
50 | #include <stdio.h>
|
---|
51 | #define PCUT_DEBUG_INTERNAL(msg, ...) \
|
---|
52 | fprintf(stderr, "[PCUT %s:%d]: " msg "%s", __FILE__, __LINE__, __VA_ARGS__)
|
---|
53 | #define PCUT_DEBUG(...) \
|
---|
54 | PCUT_DEBUG_INTERNAL( \
|
---|
55 | PCUT_VARG_GET_FIRST(__VA_ARGS__, this_arg_is_ignored), \
|
---|
56 | PCUT_VARG_SKIP_FIRST(__VA_ARGS__, "\n") \
|
---|
57 | )
|
---|
58 | #else
|
---|
59 | #define PCUT_DEBUG(...) (void)0
|
---|
60 | #endif
|
---|
61 |
|
---|
62 |
|
---|
63 | /** Mark a variable as unused. */
|
---|
64 | #define PCUT_UNUSED(x) ((void)x)
|
---|
65 |
|
---|
66 | /** Forking mode for test execution.
|
---|
67 | *
|
---|
68 | * In this mode, each test is run in a separate process.
|
---|
69 | * This ensures that even SIGSEGV does not stop the framework itself.
|
---|
70 | */
|
---|
71 | #define PCUT_RUN_MODE_FORKING 1
|
---|
72 |
|
---|
73 | /** Single-process mode for test execution.
|
---|
74 | *
|
---|
75 | * This mode is used when new process is launched when in forking-mode or
|
---|
76 | * this mode can be used if we are sure that no test would fail
|
---|
77 | * fatally (that is causing an unexpected program exit).
|
---|
78 | */
|
---|
79 | #define PCUT_RUN_MODE_SINGLE 2
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Use sprintf_s in Windows but only with Microsoft compiler.
|
---|
83 | * Namely, let MinGW use snprintf.
|
---|
84 | */
|
---|
85 | #if (defined(__WIN64) || defined(__WIN32) || defined(_WIN32)) && defined(_MSC_VER)
|
---|
86 | #define snprintf sprintf_s
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | extern int pcut_run_mode;
|
---|
90 |
|
---|
91 |
|
---|
92 | pcut_item_t *pcut_fix_list_get_real_head(pcut_item_t *last);
|
---|
93 | int pcut_count_tests(pcut_item_t *it);
|
---|
94 | void pcut_print_items(pcut_item_t *first);
|
---|
95 |
|
---|
96 | pcut_item_t *pcut_get_real_next(pcut_item_t *item);
|
---|
97 | pcut_item_t *pcut_get_real(pcut_item_t *item);
|
---|
98 | void pcut_print_tests(pcut_item_t *first);
|
---|
99 | int pcut_is_arg_with_number(const char *arg, const char *opt, int *value);
|
---|
100 |
|
---|
101 | int pcut_run_test_forking(const char *self_path, pcut_item_t *test);
|
---|
102 | int pcut_run_test_forked(pcut_item_t *test);
|
---|
103 | int pcut_run_test_single(pcut_item_t *test);
|
---|
104 |
|
---|
105 | int pcut_get_test_timeout(pcut_item_t *test);
|
---|
106 |
|
---|
107 | void pcut_failed_assertion(const char *message);
|
---|
108 | void pcut_print_fail_message(const char *msg);
|
---|
109 |
|
---|
110 | /** Reporting callbacks structure. */
|
---|
111 | typedef struct pcut_report_ops pcut_report_ops_t;
|
---|
112 |
|
---|
113 | /** @copydoc pcut_report_ops_t */
|
---|
114 | struct pcut_report_ops {
|
---|
115 | /** Initialize the reporting, given all tests. */
|
---|
116 | void (*init)(pcut_item_t *);
|
---|
117 | /** Finalize the reporting. */
|
---|
118 | void (*done)(void);
|
---|
119 | /** Test suite just started. */
|
---|
120 | void (*suite_start)(pcut_item_t *);
|
---|
121 | /** Test suite completed. */
|
---|
122 | void (*suite_done)(pcut_item_t *);
|
---|
123 | /** Test is about to start. */
|
---|
124 | void (*test_start)(pcut_item_t *);
|
---|
125 | /** Test completed. */
|
---|
126 | void (*test_done)(pcut_item_t *, int, const char *, const char *,
|
---|
127 | const char *);
|
---|
128 | };
|
---|
129 |
|
---|
130 | void pcut_report_register_handler(pcut_report_ops_t *ops);
|
---|
131 |
|
---|
132 | void pcut_report_init(pcut_item_t *all_items);
|
---|
133 | void pcut_report_suite_start(pcut_item_t *suite);
|
---|
134 | void pcut_report_suite_done(pcut_item_t *suite);
|
---|
135 | void pcut_report_test_start(pcut_item_t *test);
|
---|
136 | void pcut_report_test_done(pcut_item_t *test, int outcome,
|
---|
137 | const char *error_message, const char *teardown_error_message,
|
---|
138 | const char *extra_output);
|
---|
139 | void pcut_report_test_done_unparsed(pcut_item_t *test, int outcome,
|
---|
140 | const char *unparsed_output, size_t unparsed_output_size);
|
---|
141 | void pcut_report_done(void);
|
---|
142 |
|
---|
143 | /* OS-dependent functions. */
|
---|
144 |
|
---|
145 | /** Hook to execute before test starts.
|
---|
146 | *
|
---|
147 | * Useful for OS-specific preparations prior to launching the actual
|
---|
148 | * test code (i. e. sandboxing the process more etc.).
|
---|
149 | *
|
---|
150 | * This function is not run by the launcher process that only
|
---|
151 | * starts other tests in separate processes.
|
---|
152 | *
|
---|
153 | * @param test The test that is about to be executed.
|
---|
154 | */
|
---|
155 | void pcut_hook_before_test(pcut_item_t *test);
|
---|
156 |
|
---|
157 | /** Tell whether two strings start with the same prefix.
|
---|
158 | *
|
---|
159 | * @param a First string.
|
---|
160 | * @param b Second string.
|
---|
161 | * @param len Length of common prefix.
|
---|
162 | * @return Whether first @p len characters of @p a are the same as in @p b.
|
---|
163 | */
|
---|
164 | int pcut_str_start_equals(const char *a, const char *b, int len);
|
---|
165 |
|
---|
166 | /** Get size of string in bytes.
|
---|
167 | *
|
---|
168 | * @param s String in question.
|
---|
169 | * @return Size of @p s in bytes.
|
---|
170 | */
|
---|
171 | int pcut_str_size(const char *s);
|
---|
172 |
|
---|
173 | /** Convert string to integer.
|
---|
174 | *
|
---|
175 | * @param s String with integer.
|
---|
176 | * @return Converted integer.
|
---|
177 | */
|
---|
178 | int pcut_str_to_int(const char *s);
|
---|
179 |
|
---|
180 | /** Find character in a string.
|
---|
181 | *
|
---|
182 | * @param haystack Where to look for the @p needle.
|
---|
183 | * @param needle Character to find.
|
---|
184 | * @return String starting with @p needle.
|
---|
185 | * @retval NULL there is no @p needle in @p haystack.
|
---|
186 | */
|
---|
187 | char *pcut_str_find_char(const char *haystack, const char needle);
|
---|
188 |
|
---|
189 |
|
---|
190 | #endif
|
---|