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