Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
secp256k1
src
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
49
typedef
void (*
test_fn
)(void);
50
51
struct
tf_test_entry
{
52
const
char
*
name
;
53
test_fn
func
;
54
};
55
56
struct
tf_test_module
{
57
const
char
*
name
;
58
const
struct
tf_test_entry
*
data
;
59
int
size
;
60
};
61
62
typedef
int (*
setup_ctx_fn
)(void);
63
typedef
int (*
teardown_fn
)(void);
64
typedef
void (*
run_test_fn
)(
const
struct
tf_test_entry
*);
65
66
struct
tf_targets
{
67
/* Target tests indexes */
68
const
struct
tf_test_entry
*
slots
[
MAX_ARGS
];
69
/* Next available slot */
70
int
size
;
71
};
72
73
/* --- Command-line args --- */
74
struct
tf_args
{
75
/* 0 => sequential; 1..MAX_SUBPROCESSES => parallel workers */
76
int
num_processes
;
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 */
82
int
list_tests
;
83
/* Target tests indexes */
84
struct
tf_targets
targets
;
85
/* Enable test execution logging */
86
int
logging
;
87
};
88
89
/* --------------------------------------------------------- */
90
/* Public API */
91
/* --------------------------------------------------------- */
92
93
struct
tf_framework
{
94
/* Command-line args */
95
struct
tf_args
args
;
96
/* Test modules registry */
97
const
struct
tf_test_module
*
registry_modules
;
98
/* Num of modules */
99
int
num_modules
;
100
/* Registry for tests that require no RNG init */
101
const
struct
tf_test_module
*
registry_no_rng
;
102
/* Specific context setup and teardown functions */
103
setup_ctx_fn
fn_setup
;
104
teardown_fn
fn_teardown
;
105
/* Test runner function (can be customized) */
106
run_test_fn
fn_run_test
;
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
*/
130
static
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
*/
143
static
int
tf_run
(
struct
tf_framework
* tf);
144
145
#endif
/* SECP256K1_UNIT_TEST_H */
tf_args
Definition
unit_test.h:74
tf_args::help
int help
Definition
unit_test.h:80
tf_args::list_tests
int list_tests
Definition
unit_test.h:82
tf_args::targets
struct tf_targets targets
Definition
unit_test.h:84
tf_args::num_processes
int num_processes
Definition
unit_test.h:76
tf_args::custom_seed
const char * custom_seed
Definition
unit_test.h:78
tf_args::logging
int logging
Definition
unit_test.h:86
tf_framework
Definition
unit_test.h:93
tf_framework::fn_teardown
teardown_fn fn_teardown
Definition
unit_test.h:104
tf_framework::fn_run_test
run_test_fn fn_run_test
Definition
unit_test.h:106
tf_framework::fn_setup
setup_ctx_fn fn_setup
Definition
unit_test.h:103
tf_framework::registry_no_rng
const struct tf_test_module * registry_no_rng
Definition
unit_test.h:101
tf_framework::registry_modules
const struct tf_test_module * registry_modules
Definition
unit_test.h:97
tf_framework::args
struct tf_args args
Definition
unit_test.h:95
tf_framework::num_modules
int num_modules
Definition
unit_test.h:99
tf_targets
Definition
unit_test.h:66
tf_targets::size
int size
Definition
unit_test.h:70
tf_targets::slots
const struct tf_test_entry * slots[MAX_ARGS]
Definition
unit_test.h:68
tf_test_entry
Definition
unit_test.h:51
tf_test_entry::name
const char * name
Definition
unit_test.h:52
tf_test_entry::func
test_fn func
Definition
unit_test.h:53
tf_test_module
Definition
unit_test.h:56
tf_test_module::size
int size
Definition
unit_test.h:59
tf_test_module::name
const char * name
Definition
unit_test.h:57
tf_test_module::data
const struct tf_test_entry * data
Definition
unit_test.h:58
MAX_ARGS
#define MAX_ARGS
Definition
unit_test.h:16
tf_init
static int tf_init(struct tf_framework *tf, int argc, char **argv)
run_test_fn
void(* run_test_fn)(const struct tf_test_entry *)
Definition
unit_test.h:64
tf_run
static int tf_run(struct tf_framework *tf)
teardown_fn
int(* teardown_fn)(void)
Definition
unit_test.h:63
setup_ctx_fn
int(* setup_ctx_fn)(void)
Definition
unit_test.h:62
test_fn
void(* test_fn)(void)
Definition
unit_test.h:49
Generated on
for Bitcoin Core by
1.17.0