libsir 2.2.6
Standard Incident Reporter
Loading...
Searching...
No Matches
tests_shared.h
1/*
2 * tests_shared.h
3 *
4 * Functions, types, and macros that are used by both the C and C++ test rigs.
5 *
6 * Version: 2.2.6
7 *
8 * ----------------------------------------------------------------------------
9 *
10 * SPDX-License-Identifier: MIT
11 *
12 * Copyright (c) 2018-2024 Ryan M. Lederman <lederman@gmail.com>
13 * Copyright (c) 2018-2024 Jeffrey H. Johnson <trnsz@pobox.com>
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a copy of
16 * this software and associated documentation files (the "Software"), to deal in
17 * the Software without restriction, including without limitation the rights to
18 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
19 * the Software, and to permit persons to whom the Software is furnished to do so,
20 * subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included in all
23 * copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
27 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
28 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
29 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 * ----------------------------------------------------------------------------
33 */
34
35#ifndef _SIR_TESTS_SHARED_H_INCLUDED
36# define _SIR_TESTS_SHARED_H_INCLUDED
37
38# include "sir.h"
39# include "sir/errors.h"
40# include "sir/internal.h"
41# include "sir/helpers.h"
42# include "sir/ansimacros.h"
43# include "sir/filesystem.h"
44
45# if !defined(__WIN__) || defined(__ORANGEC__)
46# if !defined(__ORANGEC__)
47# include <dirent.h>
48# endif
49# elif defined(__WIN__)
50# include <math.h>
51# endif
52
53# if defined(__cplusplus)
54extern "C" {
55# endif
56
62# define SIR_TESTLOGDIR "./logs/"
63
65# define MAKE_LOG_NAME(name) SIR_TESTLOGDIR name
66
68# define TEST_S(n) ((n) > 1 ? "tests" : "test")
69
71# define PRN_STR(str) ((str) ? (str) : SIR_RED("NULL"))
72
74# define PRN_PASS(pass) ((pass) ? SIR_GREENB("PASS") : SIR_REDB("FAIL"))
75
77# define INDENT_ITEM "\t " SIR_BULLET " "
78
80# define SIR_MAXTESTNAME 32
81
83# define SIR_CL_MAXFLAG 32
84
86# define SIR_CL_MAXUSAGE 256
87
92# define SIR_CL_PERFFLAG "--perf"
93# define SIR_CL_ONLYFLAG "--only"
94# define SIR_CL_LISTFLAG "--list"
95# define SIR_CL_LEAVELOGSFLAG "--leave-logs"
96# define SIR_CL_WAITFLAG "--wait"
97# define SIR_CL_VERSIONFLAG "--version"
98# define SIR_CL_HELPFLAG "--help"
99
104# define SIR_CL_ONLYUSAGE SIR_ULINE("name") " [, " SIR_ULINE("name") ", ...]"
105
108# define SIR_CL_PERFNAME "performance"
109
114# define SIR_CL_PERFDESC "Only run the performance measurement test"
115# define SIR_CL_ONLYDESC "Only run the test(s) specified"
116# define SIR_CL_LISTDESC "Prints a list of available test names for use with '" SIR_BOLD("--only") "'"
117# define SIR_CL_LEAVELOGSDESC "Log files are not deleted so that they may be examined"
118# define SIR_CL_WAITDESC "After running test(s), wait for a keypress before exiting"
119# define SIR_CL_VERSIONDESC "Prints the version of libsir that the test suite was built with"
120# define SIR_CL_HELPDESC "Shows this message"
121
123# define TEST_MSG(msg, ...) (void)printf("\t" msg SIR_EOL, __VA_ARGS__)
124
126# define TEST_MSG_0(msg) (void)printf("\t" msg SIR_EOL)
127
129# define ERROR_MSG(msg, ...) TEST_MSG(SIR_RED(msg), __VA_ARGS__)
130
132# define ERROR_MSG_0(msg) TEST_MSG_0(SIR_RED(msg))
133
135# if !defined(SIR_NO_TEXT_STYLING)
136# define PASSFAIL_MSG(expr, msg, ...) \
137 (void)printf(expr ? SIR_GREEN(msg) : SIR_RED(msg), __VA_ARGS__)
138# else
139# define PASSFAIL_MSG(expr, msg, ...) \
140 (void)printf(msg, __VA_ARGS__)
141# endif
142
144# define PRINT_EXPECTED_ERROR() (void)print_test_error(true, true)
145
151# define PRINT_RESULT_RETURN(pass) print_test_error(pass, false)
152
157# if !defined(__WIN__)
158# define HANDLE_OS_ERROR(clib, msg, ...) \
159 do { \
160 SIR_UNUSED(clib); \
161 (void)_sir_handleerr(errno); \
162 ERROR_MSG(msg ":", __VA_ARGS__); \
163 (void)print_test_error(false, false); \
164 } while (false)
165# else /* __WIN__ */
166# define HANDLE_OS_ERROR(clib, msg, ...) \
167 do { \
168 clib ? (void)_sir_handleerr(errno) : (void)_sir_handlewin32err(GetLastError()); \
169 ERROR_MSG(msg ":", __VA_ARGS__); \
170 (void)print_test_error(false, false); \
171 } while (false)
172# endif
173
179# if !defined(__cplusplus)
180typedef bool (*sir_test_fn)(void);
181# else
182typedef bool (*sir_test_fn)();
183# endif
184
189typedef struct {
190 const char* const name;
191 sir_test_fn fn;
192 bool run;
193 bool pass;
194} sir_test;
195
197typedef struct {
198 const char* const flag;
199 const char* const usage;
200 const char* const desc;
201} sir_cl_arg;
202
204typedef struct {
205 bool leave_logs;
206 bool wait;
207 bool only;
208 size_t to_run;
210
216void print_intro(size_t tgt_tests);
217
219void print_test_intro(size_t num, size_t tgt_tests, const char* const name);
220
222void print_test_outro(size_t num, size_t tgt_tests, const char* const name, bool pass);
223
225void print_test_summary(size_t tgt_tests, size_t passed, double elapsed);
226
228void print_failed_test_intro(size_t tgt_tests, size_t passed);
229
231void print_failed_test(const char* const name);
232
237bool mark_test_to_run(const char* const name, sir_test* tests, size_t num_tests);
238
240void print_test_list(const sir_test* tests, size_t num_tests);
241
243void print_usage_info(const sir_cl_arg* args, size_t num_args);
244
250bool print_test_error(bool result, bool expected);
251
255void print_libsir_version(void);
256
261const sir_cl_arg* find_cl_arg(const char* flag, const sir_cl_arg* args, size_t num_args);
262
268bool parse_cmd_line(int argc, char** argv, const sir_cl_arg* args, size_t num_args,
269 sir_test* tests, size_t num_tests, sir_cl_config* config);
270
272void wait_for_keypress(void);
273
275static inline
276void sir_timer_start(sir_time* timer) {
277 (void)_sir_msec_since(NULL, timer);
278}
279
281static inline
282double sir_timer_elapsed(const sir_time* timer) {
283 sir_time now;
284 return _sir_msec_since(timer, &now);
285}
286
288long sir_timer_getres(void);
289
291void sir_sleep_msec(uint32_t msec);
292
294size_t sir_readline(FILE* f, char* buf, size_t size);
295
297uint32_t getrand(uint32_t upper_bound);
298
300static inline
301bool getrand_bool(uint32_t upper_bound) {
302 return ((!upper_bound) ? true : getrand(upper_bound) % 2 == 0);
303}
304
309void rmfile(const char* filename, bool leave_logs);
310
315bool enumfiles(const char* path, const char* search, bool del, unsigned* count);
316
317# if defined(__cplusplus)
318}
319# endif
320
321#endif /* !_SIR_TESTS_SHARED_H_INCLUDED */
ANSI escape sequence macros.
Public interface to libsir.
A command line argument.
Definition tests_shared.h:197
const char *const usage
(e.g.
Definition tests_shared.h:199
const char *const desc
(e.g.
Definition tests_shared.h:200
Shared command line configuration.
Definition tests_shared.h:204
bool only
–wait
Definition tests_shared.h:207
size_t to_run
–only
Definition tests_shared.h:208
bool wait
–leave-logs
Definition tests_shared.h:206
Maps a test function to a human-readable name and some metadata.
Definition tests_shared.h:189
bool pass
Whether to run the test or not.
Definition tests_shared.h:193
bool run
The function pointer.
Definition tests_shared.h:192
sir_test_fn fn
The test's name.
Definition tests_shared.h:191
Internally-used time value type.
Definition types.h:281