Bitcoin Core 30.99.0
P2P Digital Currency
unit_test.h
Go to the documentation of this file.
1/***********************************************************************
2 * Distributed under the MIT software license, see the accompanying *
3 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
4 ***********************************************************************/
5
6#ifndef SECP256K1_UNIT_TEST_H
7#define SECP256K1_UNIT_TEST_H
8
9/* --------------------------------------------------------- */
10/* Configurable constants */
11/* --------------------------------------------------------- */
12
13/* Maximum number of command-line arguments.
14 * Must be at least as large as the total number of tests
15 * to allow specifying all tests individually. */
16#define MAX_ARGS 150
17/* Maximum number of parallel jobs */
18#define MAX_SUBPROCESSES 16
19
20/* --------------------------------------------------------- */
21/* Test Framework Registry Macros */
22/* --------------------------------------------------------- */
23
24#define CASE(name) { #name, run_##name }
25#define CASE1(name) { #name, name }
26
27#define MAKE_TEST_MODULE(name) { \
28 #name, \
29 tests_##name, \
30 sizeof(tests_##name) / sizeof(tests_##name[0]) \
31}
32
33/* Macro to wrap a test internal function with a COUNT loop (iterations number) */
34#define REPEAT_TEST(fn) REPEAT_TEST_MULT(fn, 1)
35#define REPEAT_TEST_MULT(fn, multiplier) \
36 static void fn(void) { \
37 int i; \
38 int repeat = COUNT * (multiplier); \
39 for (i = 0; i < repeat; i++) \
40 fn##_internal(); \
41 }
42
43
44
45/* --------------------------------------------------------- */
46/* Test Framework API */
47/* --------------------------------------------------------- */
48
49typedef void (*test_fn)(void);
50
52 const char* name;
54};
55
57 const char* name;
58 const struct tf_test_entry* data;
59 int size;
60};
61
62typedef int (*setup_ctx_fn)(void);
63typedef int (*teardown_fn)(void);
64typedef void (*run_test_fn)(const struct tf_test_entry*);
65
66struct tf_targets {
67 /* Target tests indexes */
69 /* Next available slot */
70 int size;
71};
72
73/* --- Command-line args --- */
74struct tf_args {
75 /* 0 => sequential; 1..MAX_SUBPROCESSES => parallel workers */
77 /* Specific RNG seed */
78 const char* custom_seed;
79 /* Whether to print the help msg */
80 int help;
81 /* Whether to print the tests list msg */
83 /* Target tests indexes */
85 /* Enable test execution logging */
87};
88
89/* --------------------------------------------------------- */
90/* Public API */
91/* --------------------------------------------------------- */
92
94 /* Command-line args */
95 struct tf_args args;
96 /* Test modules registry */
98 /* Num of modules */
100 /* Registry for tests that require no RNG init */
102 /* Specific context setup and teardown functions */
105 /* Test runner function (can be customized) */
107};
108
109/*
110 * Initialize the test framework.
111 *
112 * Must be called before tf_run() and before any output is performed to
113 * stdout or stderr, because this function disables buffering on both
114 * streams to ensure reliable diagnostic output.
115 *
116 * Parses command-line arguments and configures the framework context.
117 * The caller must initialize the following members of 'tf' before calling:
118 * - tf->registry_modules
119 * - tf->num_modules
120 *
121 * Side effects:
122 * - stdout and stderr are set to unbuffered mode via setbuf().
123 * This allows immediate flushing of diagnostic messages but may
124 * affect performance for other output operations.
125 *
126 * Returns:
127 * EXIT_SUCCESS (0) on success,
128 * EXIT_FAILURE (non-zero) on error.
129 */
130static int tf_init(struct tf_framework* tf, int argc, char** argv);
131
132/*
133 * Run tests based on the provided test framework context.
134 *
135 * This function uses the configuration stored in the tf_framework
136 * (targets, number of processes, iteration count, etc.) to determine
137 * which tests to execute and how to execute them.
138 *
139 * Returns:
140 * EXIT_SUCCESS (0) if all tests passed,
141 * EXIT_FAILURE (non-zero) otherwise.
142 */
143static int tf_run(struct tf_framework* tf);
144
145#endif /* SECP256K1_UNIT_TEST_H */
int help
Definition: unit_test.h:80
int list_tests
Definition: unit_test.h:82
struct tf_targets targets
Definition: unit_test.h:84
int num_processes
Definition: unit_test.h:76
const char * custom_seed
Definition: unit_test.h:78
int logging
Definition: unit_test.h:86
teardown_fn fn_teardown
Definition: unit_test.h:104
run_test_fn fn_run_test
Definition: unit_test.h:106
setup_ctx_fn fn_setup
Definition: unit_test.h:103
const struct tf_test_module * registry_no_rng
Definition: unit_test.h:101
const struct tf_test_module * registry_modules
Definition: unit_test.h:97
struct tf_args args
Definition: unit_test.h:95
int num_modules
Definition: unit_test.h:99
int size
Definition: unit_test.h:70
const struct tf_test_entry * slots[MAX_ARGS]
Definition: unit_test.h:68
Definition: unit_test.h:51
const char * name
Definition: unit_test.h:52
test_fn func
Definition: unit_test.h:53
const char * name
Definition: unit_test.h:57
const struct tf_test_entry * data
Definition: unit_test.h:58
#define MAX_ARGS
Definition: unit_test.h:16
static int tf_init(struct tf_framework *tf, int argc, char **argv)
void(* run_test_fn)(const struct tf_test_entry *)
Definition: unit_test.h:64
static int tf_run(struct tf_framework *tf)
int(* teardown_fn)(void)
Definition: unit_test.h:63
int(* setup_ctx_fn)(void)
Definition: unit_test.h:62
void(* test_fn)(void)
Definition: unit_test.h:49