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//-V::522
39# include "sir.h"
40# include "sir/errors.h"
41# include "sir/internal.h"
42# include "sir/helpers.h"
43# include "sir/ansimacros.h"
44# include "sir/filesystem.h"
45
46# if !defined(__WIN__) || defined(__ORANGEC__)
47# if !defined(__ORANGEC__)
48# include <dirent.h>
49# endif
50# elif defined(__WIN__)
51# include <math.h>
52# endif
53
54# if defined(__cplusplus)
55extern "C" {
56# endif
57
61
63# define SIR_TESTLOGDIR "./logs/"
64
66# define MAKE_LOG_NAME(name) SIR_TESTLOGDIR name
67
69# define TEST_S(n) ((n) > 1 ? "tests" : "test")
70
72# define PRN_STR(str) ((str) ? (str) : SIR_RED("NULL"))
73
75# define PRN_PASS(pass) ((pass) ? SIR_GREENB("PASS") : SIR_REDB("FAIL"))
76
78# define INDENT_ITEM "\t " SIR_BULLET " "
79
81# define SIR_MAXTESTNAME 32
82
84# define SIR_CL_MAXFLAG 32
85
87# define SIR_CL_MAXUSAGE 256
88
92
93# define SIR_CL_PERFFLAG "--perf"
94# define SIR_CL_ONLYFLAG "--only"
95# define SIR_CL_LISTFLAG "--list"
96# define SIR_CL_LEAVELOGSFLAG "--leave-logs"
97# define SIR_CL_WAITFLAG "--wait"
98# define SIR_CL_VERSIONFLAG "--version"
99# define SIR_CL_HELPFLAG "--help"
100
104
105# define SIR_CL_ONLYUSAGE SIR_ULINE("name") " [, " SIR_ULINE("name") ", ...]"
106
109# define SIR_CL_PERFNAME "performance"
110
114
115# define SIR_CL_PERFDESC "Only run the performance measurement test"
116# define SIR_CL_ONLYDESC "Only run the test(s) specified"
117# define SIR_CL_LISTDESC "Prints a list of available test names for use with '" SIR_BOLD("--only") "'"
118# define SIR_CL_LEAVELOGSDESC "Log files are not deleted so that they may be examined"
119# define SIR_CL_WAITDESC "After running test(s), wait for a keypress before exiting"
120# define SIR_CL_VERSIONDESC "Prints the version of libsir that the test suite was built with"
121# define SIR_CL_HELPDESC "Shows this message"
122
124# define TEST_MSG(msg, ...) (void)printf("\t" msg SIR_EOL, __VA_ARGS__)
125
127# define TEST_MSG_0(msg) (void)printf("\t" msg SIR_EOL)
128
130# define ERROR_MSG(msg, ...) TEST_MSG(SIR_RED(msg), __VA_ARGS__)
131
133# define ERROR_MSG_0(msg) TEST_MSG_0(SIR_RED(msg))
134
136# if !defined(SIR_NO_TEXT_STYLING)
137# define PASSFAIL_MSG(expr, msg, ...) \
138 (void)printf(expr ? SIR_GREEN(msg) : SIR_RED(msg), __VA_ARGS__)
139# else
140# define PASSFAIL_MSG(expr, msg, ...) \
141 (void)printf(msg, __VA_ARGS__)
142# endif
143
145# define PRINT_EXPECTED_ERROR() (void)print_test_error(true, true)
146
152# define PRINT_RESULT_RETURN(pass) print_test_error(pass, false)
153
158# if !defined(__WIN__)
159# define HANDLE_OS_ERROR(clib, msg, ...) \
160 do { \
161 SIR_UNUSED(clib); \
162 (void)_sir_handleerr(errno); \
163 ERROR_MSG(msg ":", __VA_ARGS__); \
164 (void)print_test_error(false, false); \
165 } while (false)
166# else /* __WIN__ */
167# define HANDLE_OS_ERROR(clib, msg, ...) \
168 do { \
169 clib ? (void)_sir_handleerr(errno) : (void)_sir_handlewin32err(GetLastError()); \
170 ERROR_MSG(msg ":", __VA_ARGS__); \
171 (void)print_test_error(false, false); \
172 } while (false)
173# endif
174
178
180# if !defined(__cplusplus)
181typedef bool (*sir_test_fn)(void);
182# else
183typedef bool (*sir_test_fn)();
184# endif
185
190typedef struct {
191 const char* const name;
192 sir_test_fn fn;
193 bool run;
194 bool pass;
195} sir_test;
196
198typedef struct {
199 const char* const flag;
200 const char* const usage;
201 const char* const desc;
202} sir_cl_arg;
203
205typedef struct {
206 bool leave_logs;
207 bool wait;
208 bool only;
209 size_t to_run;
211
215
217void print_intro(size_t tgt_tests);
218
220void print_test_intro(size_t num, size_t tgt_tests, const char* const name);
221
223void print_test_outro(size_t num, size_t tgt_tests, const char* const name, bool pass);
224
226void print_test_summary(size_t tgt_tests, size_t passed, double elapsed);
227
229void print_failed_test_intro(size_t tgt_tests, size_t passed);
230
232void print_failed_test(const char* const name);
233
238bool mark_test_to_run(const char* const name, sir_test* tests, size_t num_tests);
239
241void print_test_list(const sir_test* tests, size_t num_tests);
242
244void print_usage_info(const sir_cl_arg* args, size_t num_args);
245
251bool print_test_error(bool result, bool expected);
252
256void print_libsir_version(void);
257
262const sir_cl_arg* find_cl_arg(const char* flag, const sir_cl_arg* args, size_t num_args);
263
269bool parse_cmd_line(int argc, char** argv, const sir_cl_arg* args, size_t num_args,
270 sir_test* tests, size_t num_tests, sir_cl_config* config);
271
273void wait_for_keypress(void);
274
276static inline
277void sir_timer_start(sir_time* timer) {
278 (void)_sir_msec_since(NULL, timer);
279}
280
282static inline
283double sir_timer_elapsed(const sir_time* timer) {
284 sir_time now;
285 return _sir_msec_since(timer, &now);
286}
287
289long sir_timer_getres(void);
290
292void sir_sleep_msec(uint32_t msec);
293
295size_t sir_readline(FILE* f, char* buf, size_t size);
296
298uint32_t getrand(uint32_t upper_bound);
299
301static inline
302bool getrand_bool(uint32_t upper_bound) {
303 return ((!upper_bound) ? true : getrand(upper_bound) % 2 == 0);
304}
305
310void rmfile(const char* filename, bool leave_logs);
311
316bool enumfiles(const char* path, const char* search, bool del, unsigned* count);
317
318# if defined(__cplusplus)
319}
320# endif
321
322#endif /* !_SIR_TESTS_SHARED_H_INCLUDED */
ANSI escape sequence macros.
Public interface to libsir.
A command line argument.
Definition tests_shared.h:198
const char *const usage
(e.g.
Definition tests_shared.h:200
const char *const desc
(e.g.
Definition tests_shared.h:201
Shared command line configuration.
Definition tests_shared.h:205
bool only
–wait
Definition tests_shared.h:208
size_t to_run
–only
Definition tests_shared.h:209
bool wait
–leave-logs
Definition tests_shared.h:207
Maps a test function to a human-readable name and some metadata.
Definition tests_shared.h:190
bool pass
Whether to run the test or not.
Definition tests_shared.h:194
bool run
The function pointer.
Definition tests_shared.h:193
sir_test_fn fn
The test's name.
Definition tests_shared.h:192
Internally-used time value type.
Definition types.h:281