| 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 |  | 
|---|
| 38 | #pragma warning(push, 0) | 
|---|
| 39 | #include <stdlib.h> | 
|---|
| 40 | #pragma warning(pop) | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | /** @def PCUT_DEBUG(msg, ...) | 
|---|
| 44 | * Debug printing. | 
|---|
| 45 | * | 
|---|
| 46 | * By default, this macro does nothing. Define PCUT_DEBUG_BUILD to | 
|---|
| 47 | * actually print the messages to the console. | 
|---|
| 48 | * | 
|---|
| 49 | * @param msg Printf-like formatting message. | 
|---|
| 50 | * @param ... Extra arguments for printf. | 
|---|
| 51 | */ | 
|---|
| 52 | #ifdef PCUT_DEBUG_BUILD | 
|---|
| 53 | #pragma warning(push, 0) | 
|---|
| 54 | #include <stdio.h> | 
|---|
| 55 | #pragma warning(pop) | 
|---|
| 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") \ | 
|---|
| 62 | ) | 
|---|
| 63 | #else | 
|---|
| 64 | #define PCUT_DEBUG(...) (void)0 | 
|---|
| 65 | #endif | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | /** Mark a variable as unused. */ | 
|---|
| 69 | #define PCUT_UNUSED(x) ((void)x) | 
|---|
| 70 |  | 
|---|
| 71 | /** Forking mode for test execution. | 
|---|
| 72 | * | 
|---|
| 73 | * In this mode, each test is run in a separate process. | 
|---|
| 74 | * This ensures that even SIGSEGV does not stop the framework itself. | 
|---|
| 75 | */ | 
|---|
| 76 | #define PCUT_RUN_MODE_FORKING 1 | 
|---|
| 77 |  | 
|---|
| 78 | /** Single-process mode for test execution. | 
|---|
| 79 | * | 
|---|
| 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). | 
|---|
| 83 | */ | 
|---|
| 84 | #define PCUT_RUN_MODE_SINGLE 2 | 
|---|
| 85 |  | 
|---|
| 86 | extern int pcut_run_mode; | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 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); | 
|---|
| 92 |  | 
|---|
| 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); | 
|---|
| 97 |  | 
|---|
| 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); | 
|---|
| 101 |  | 
|---|
| 102 | int pcut_get_test_timeout(pcut_item_t *test); | 
|---|
| 103 |  | 
|---|
| 104 | void pcut_failed_assertion(const char *message); | 
|---|
| 105 | void pcut_print_fail_message(const char *msg); | 
|---|
| 106 |  | 
|---|
| 107 | /** Reporting callbacks structure. */ | 
|---|
| 108 | typedef struct pcut_report_ops pcut_report_ops_t; | 
|---|
| 109 |  | 
|---|
| 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. */ | 
|---|
| 115 | void (*done)(void); | 
|---|
| 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 *, | 
|---|
| 124 | const char *); | 
|---|
| 125 | }; | 
|---|
| 126 |  | 
|---|
| 127 | void pcut_report_register_handler(pcut_report_ops_t *ops); | 
|---|
| 128 |  | 
|---|
| 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); | 
|---|
| 139 |  | 
|---|
| 140 | /* OS-dependent functions. */ | 
|---|
| 141 |  | 
|---|
| 142 | /** Hook to execute before test starts. | 
|---|
| 143 | * | 
|---|
| 144 | * Useful for OS-specific preparations prior to launching the actual | 
|---|
| 145 | * test code (i. e. sandboxing the process more etc.). | 
|---|
| 146 | * | 
|---|
| 147 | * This function is not run by the launcher process that only | 
|---|
| 148 | * starts other tests in separate processes. | 
|---|
| 149 | * | 
|---|
| 150 | * @param test The test that is about to be executed. | 
|---|
| 151 | */ | 
|---|
| 152 | void pcut_hook_before_test(pcut_item_t *test); | 
|---|
| 153 |  | 
|---|
| 154 | /** Tell whether two strings start with the same prefix. | 
|---|
| 155 | * | 
|---|
| 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. | 
|---|
| 160 | */ | 
|---|
| 161 | int pcut_str_start_equals(const char *a, const char *b, int len); | 
|---|
| 162 |  | 
|---|
| 163 | /** Get size of string in bytes. | 
|---|
| 164 | * | 
|---|
| 165 | * @param s String in question. | 
|---|
| 166 | * @return Size of @p s in bytes. | 
|---|
| 167 | */ | 
|---|
| 168 | int pcut_str_size(const char *s); | 
|---|
| 169 |  | 
|---|
| 170 | /** Convert string to integer. | 
|---|
| 171 | * | 
|---|
| 172 | * @param s String with integer. | 
|---|
| 173 | * @return Converted integer. | 
|---|
| 174 | */ | 
|---|
| 175 | int pcut_str_to_int(const char *s); | 
|---|
| 176 |  | 
|---|
| 177 | /** Find character in a string. | 
|---|
| 178 | * | 
|---|
| 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. | 
|---|
| 183 | */ | 
|---|
| 184 | char *pcut_str_find_char(const char *haystack, const char needle); | 
|---|
| 185 |  | 
|---|
| 186 | /** Format string to a buffer. | 
|---|
| 187 | * | 
|---|
| 188 | */ | 
|---|
| 189 | int pcut_snprintf(char *dest, size_t size, const char *format, ...); | 
|---|
| 190 |  | 
|---|
| 191 | #endif | 
|---|