Documentation
GitHub
Z / libzlang
0.29.0
C99 runtime library and the Zlang 0.29 source-to-source compiler
Toggle main menu visibility
Loading...
Searching...
No Matches
testing.h
Go to the documentation of this file.
1
#ifndef ZLANG_TESTING_TESTING_H
2
#define ZLANG_TESTING_TESTING_H
3
7
8
#include "
zlang/detail/inline.h
"
9
10
#include <inttypes.h>
11
#include <stddef.h>
12
#include <stdint.h>
13
#include <stdio.h>
14
#include <string.h>
15
17
typedef
struct
zlang_test_context_t
{
18
unsigned
int
failure_count_
;
19
}
zlang_test_context_t
;
20
22
typedef
void (*
zlang_test_fn_t
)(
zlang_test_context_t
*
const
context);
23
25
typedef
struct
zlang_test_case_t
{
26
const
char
*
name_
;
27
zlang_test_fn_t
run_
;
28
}
zlang_test_case_t
;
29
31
ZLANG_STATIC_INLINE
void
zlang_test_report_failure
(
zlang_test_context_t
*
const
context,
32
const
char
*
const
file,
const
unsigned
int
line,
33
const
char
*
const
expression) {
34
++context->
failure_count_
;
35
fprintf(stderr,
"%s:%u: check failed: %s\n"
, file, line, expression);
36
}
37
39
ZLANG_STATIC_INLINE
void
40
zlang_test_report_failure_uint
(
zlang_test_context_t
*
const
context,
const
char
*
const
file,
41
const
unsigned
int
line,
const
char
*
const
expression,
42
const
uintmax_t expected,
const
uintmax_t actual) {
43
++context->
failure_count_
;
44
fprintf(stderr,
"%s:%u: check failed: %s (expected %"
PRIuMAX
", got %"
PRIuMAX
")\n"
, file,
45
line, expression, expected, actual);
46
}
47
49
ZLANG_STATIC_INLINE
int
zlang_test_run_cases
(
const
char
*
const
suite,
50
const
zlang_test_case_t
*
const
cases,
51
const
size_t
case_count,
52
const
char
*
const
requested_case) {
53
size_t
_selected_count = 0U;
54
unsigned
int
_total_failures = 0U;
55
56
printf(
"%s: %zu test case(s) available\n"
, suite, case_count);
57
58
for
(
size_t
_index = 0U; _index < case_count; ++_index) {
59
const
zlang_test_case_t
*
const
_case_k = &cases[_index];
60
if
(requested_case != NULL && strcmp(_case_k->
name_
, requested_case) != 0) {
61
continue
;
62
}
63
++_selected_count;
64
65
zlang_test_context_t
_case_context = {0U};
66
printf(
" [ RUN ] %s\n"
, _case_k->
name_
);
67
_case_k->
run_
(&_case_context);
68
if
(_case_context.
failure_count_
== 0U) {
69
printf(
" [ OK ] %s\n"
, _case_k->
name_
);
70
}
else
{
71
printf(
" [ FAILED ] %s\n"
, _case_k->
name_
);
72
_total_failures += _case_context.
failure_count_
;
73
}
74
}
75
76
if
(requested_case != NULL && _selected_count == 0U) {
77
fprintf(stderr,
"%s: unknown test case: %s\n"
, suite, requested_case);
78
return
1;
79
}
80
if
(_total_failures != 0U) {
81
fprintf(stderr,
"%s: %u failure(s)\n"
, suite, _total_failures);
82
return
1;
83
}
84
85
printf(
"%s: all selected test cases passed\n"
, suite);
86
return
0;
87
}
88
90
ZLANG_STATIC_INLINE
int
zlang_test_main
(
const
int
argc,
char
*
const
*
const
argv,
91
const
char
*
const
suite,
92
const
zlang_test_case_t
*
const
cases,
93
const
size_t
case_count) {
94
const
char
*_requested_case = NULL;
95
if
(argc == 3 && strcmp(argv[1],
"--case"
) == 0) {
96
_requested_case = argv[2];
97
}
98
99
return
zlang_test_run_cases
(suite, cases, case_count, _requested_case);
100
}
101
103
#define ZLANG_CHECK(context, condition) \
104
do { \
105
if (!(condition)) { \
106
zlang_test_report_failure((context), __FILE__, (unsigned int)__LINE__, #condition); \
107
return; \
108
} \
109
} while (0)
110
112
#define ZLANG_CHECK_UINT(context, expected, actual) \
113
do { \
114
const uintmax_t _expected_k = (uintmax_t)(expected); \
115
const uintmax_t _actual_k = (uintmax_t)(actual); \
116
if (_expected_k != _actual_k) { \
117
zlang_test_report_failure_uint((context), __FILE__, (unsigned int)__LINE__, #actual, \
118
_expected_k, _actual_k); \
119
return; \
120
} \
121
} while (0)
122
123
#endif
/* ZLANG_TESTING_TESTING_H */
inline.h
ZLANG_STATIC_INLINE
#define ZLANG_STATIC_INLINE
Definition
inline.h:9
zlang_test_case_t
Definition
testing.h:25
zlang_test_case_t::name_
const char * name_
Definition
testing.h:26
zlang_test_case_t::run_
zlang_test_fn_t run_
Definition
testing.h:27
zlang_test_context_t
Definition
testing.h:17
zlang_test_context_t::failure_count_
unsigned int failure_count_
Definition
testing.h:18
zlang_test_run_cases
ZLANG_STATIC_INLINE int zlang_test_run_cases(const char *const suite, const zlang_test_case_t *const cases, const size_t case_count, const char *const requested_case)
Definition
testing.h:49
zlang_test_report_failure
ZLANG_STATIC_INLINE void zlang_test_report_failure(zlang_test_context_t *const context, const char *const file, const unsigned int line, const char *const expression)
Definition
testing.h:31
zlang_test_main
ZLANG_STATIC_INLINE int zlang_test_main(const int argc, char *const *const argv, const char *const suite, const zlang_test_case_t *const cases, const size_t case_count)
Definition
testing.h:90
zlang_test_fn_t
void(*) zlang_test_fn_t(zlang_test_context_t *const context)
Definition
testing.h:22
zlang_test_report_failure_uint
ZLANG_STATIC_INLINE void zlang_test_report_failure_uint(zlang_test_context_t *const context, const char *const file, const unsigned int line, const char *const expression, const uintmax_t expected, const uintmax_t actual)
Definition
testing.h:40
zlang
testing
testing.h
Zlang
GitHub
Generated by
1.18.0