Skip to content

Commit 2171559

Browse files
Deomid Ryabkovcesantabot
Deomid Ryabkov
authored andcommitted
Commonize unit test main()
CL: none PUBLISHED_FROM=ee76407d89f3fcbe3da23810117a374d06af0a25
1 parent 43b73dc commit 2171559

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

common/test_util.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
extern "C" {
2828
#endif
2929

30-
extern int num_tests;
30+
extern int g_num_tests;
3131

3232
#ifdef MG_TEST_ABORT_ON_FAIL
3333
#define MG_TEST_ABORT abort()
@@ -48,7 +48,7 @@ void _strfail(const char *a, const char *e, int len);
4848

4949
#define ASSERT(expr) \
5050
do { \
51-
num_tests++; \
51+
g_num_tests++; \
5252
if (!(expr)) FAIL(#expr, __LINE__); \
5353
} while (0)
5454
#define ASSERT_TRUE(expr) ASSERT(expr)
@@ -71,7 +71,6 @@ void _strfail(const char *a, const char *e, int len);
7171
elapsed = cs_time() - elapsed; \
7272
printf(" [%.3f] %s\n", elapsed, test_name); \
7373
fflush(stdout); \
74-
*total_elapsed += elapsed; \
7574
} \
7675
if (msg) return msg; \
7776
} while (0)
@@ -89,7 +88,7 @@ void _strfail(const char *a, const char *e, int len);
8988
*/
9089
#define ASSERT_EQ(actual, expected) \
9190
do { \
92-
num_tests++; \
91+
g_num_tests++; \
9392
if (!((actual) == (expected))) { \
9493
printf("%f != %f\n", AS_DOUBLE(actual), AS_DOUBLE(expected)); \
9594
FAIL(#actual " == " #expected, __LINE__); \
@@ -99,7 +98,7 @@ void _strfail(const char *a, const char *e, int len);
9998
/* "Less than" assertion. */
10099
#define ASSERT_LT(a, b) \
101100
do { \
102-
num_tests++; \
101+
g_num_tests++; \
103102
if (!((a) < (b))) { \
104103
printf("%f >= %f\n", AS_DOUBLE(a), AS_DOUBLE(b)); \
105104
FAIL(#a " < " #b, __LINE__); \
@@ -109,7 +108,7 @@ void _strfail(const char *a, const char *e, int len);
109108
/* "Greater than" assertion. */
110109
#define ASSERT_GT(a, b) \
111110
do { \
112-
num_tests++; \
111+
g_num_tests++; \
113112
if (!((a) > (b))) { \
114113
printf("%f <= %f\n", AS_DOUBLE(a), AS_DOUBLE(b)); \
115114
FAIL(#a " > " #b, __LINE__); \
@@ -119,7 +118,7 @@ void _strfail(const char *a, const char *e, int len);
119118
/* Assert that actual == expected, where both are NUL-terminated. */
120119
#define ASSERT_STREQ(actual, expected) \
121120
do { \
122-
num_tests++; \
121+
g_num_tests++; \
123122
if (!_assert_streq(actual, expected)) { \
124123
FAIL("ASSERT_STREQ(" #actual ", " #expected ")", __LINE__); \
125124
} \
@@ -128,7 +127,7 @@ void _strfail(const char *a, const char *e, int len);
128127
/* Assert that actual == expected, where both are pointers */
129128
#define ASSERT_PTREQ(actual, expected) \
130129
do { \
131-
num_tests++; \
130+
g_num_tests++; \
132131
if (actual != expected) { \
133132
printf("%p != %p\n", actual, expected); \
134133
FAIL("ASSERT_PTREQ(" #actual ", " #expected ")", __LINE__); \
@@ -138,7 +137,7 @@ void _strfail(const char *a, const char *e, int len);
138137
/* Assert that actual != expected, where both are pointers */
139138
#define ASSERT_PTRNEQ(actual, expected) \
140139
do { \
141-
num_tests++; \
140+
g_num_tests++; \
142141
if (actual == expected) { \
143142
printf("%p == %p\n", actual, expected); \
144143
FAIL("ASSERT_PTRNEQ(" #actual ", " #expected ")", __LINE__); \
@@ -148,15 +147,15 @@ void _strfail(const char *a, const char *e, int len);
148147
/* Same as STREQ, but only expected is NUL-terminated. */
149148
#define ASSERT_STREQ_NZ(actual, expected) \
150149
do { \
151-
num_tests++; \
150+
g_num_tests++; \
152151
if (!_assert_streq_nz(actual, expected)) { \
153152
FAIL("ASSERT_STREQ_NZ(" #actual ", " #expected ")", __LINE__); \
154153
} \
155154
} while (0)
156155

157156
#define ASSERT_MG_STREQ(actual, expected) \
158157
do { \
159-
num_tests++; \
158+
g_num_tests++; \
160159
if ((actual).len != strlen(expected) || \
161160
memcmp((actual).p, expected, (actual).len) != 0) { \
162161
printf("'%.*s' (%d) != '%s'\n", (int)(actual).len, (actual).p, \

mjs/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ PROG = $(BUILD_DIR)/mjs
5151
all: $(PROG)
5252

5353
TESTUTIL_FILES = $(REPO_ROOT)/common/cs_dirent.c \
54-
$(REPO_ROOT)/common/cs_time.c \
54+
$(REPO_ROOT)/common/cs_time.c \
55+
$(REPO_ROOT)/common/test_main.c \
5556
$(REPO_ROOT)/common/test_util.c
5657

5758
mjs.c: $(TOP_SOURCES) mjs.h Makefile

mjs/tests/unit_test.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern "C" {
1111
#define STRINGIFY(x) #x
1212
#define MJS_ENABLE_DEBUG 1
1313
#include "common/cs_dirent.h"
14+
#include "common/test_main.h"
1415
#include "common/test_util.h"
1516
#include "mjs.c"
1617

@@ -3767,9 +3768,10 @@ const char *test_parser(struct mjs *mjs) {
37673768
return NULL;
37683769
}
37693770

3770-
static const char *run_all_tests(const char *filter, double *total_elapsed) {
3771-
cs_log_set_level(2);
3771+
void tests_setup(void) {
3772+
}
37723773

3774+
const char *tests_run(const char *filter) {
37733775
RUN_TEST_MJS(test_parser);
37743776
RUN_TEST_MJS(test_arithmetic);
37753777
RUN_TEST_MJS(test_block);
@@ -3820,11 +3822,7 @@ static const char *run_all_tests(const char *filter, double *total_elapsed) {
38203822
return NULL;
38213823
}
38223824

3823-
int main(void) {
3824-
double seconds = 0.0;
3825-
const char *msg = run_all_tests("", &seconds);
3826-
printf("%s, run %d in %.3fs\n", msg ? "FAIL" : "PASS", num_tests, seconds);
3827-
return msg == NULL ? EXIT_SUCCESS : EXIT_FAILURE;
3825+
void tests_teardown(void) {
38283826
}
38293827

38303828
#if defined(__cplusplus)

0 commit comments

Comments
 (0)