Skip to content

Commit a806b99

Browse files
committed
Test case is able to convert to parameterized by cutest_case_convert_parameterized()
1 parent ca6197c commit a806b99

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1. Automatic disable thread support if `Threads` not found.
1111
2. You can dynamic register test cases by `cutest_register_case()`.
1212
3. Test case can be unregistered by `cutest_unregister_case()`.
13+
4. Test case is able to convert to parameterized by `cutest_case_convert_parameterized()`.
1314

1415

1516
## v3.0.3 (2024/04/23)

include/cutest.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ extern "C" {
115115
/**
116116
* @brief Development version.
117117
*/
118-
#define CUTEST_VERSION_PREREL 5
118+
#define CUTEST_VERSION_PREREL 6
119119

120120
/**
121121
* @brief Ensure the api is exposed as C function.
@@ -218,18 +218,15 @@ extern "C" {
218218
#define TEST_PARAMETERIZED_DEFINE(fixture, test, TYPE, ...) \
219219
static void cutest_usertest_parameterized_register_##fixture##_##test(void (*cb)(TYPE*, unsigned long)) {\
220220
static TYPE s_parameterized_userdata[] = { __VA_ARGS__ };\
221-
static cutest_case_t s_tests[sizeof(s_parameterized_userdata) / sizeof(s_parameterized_userdata[0])];\
222-
const unsigned long number_of_parameterized_data = sizeof(s_tests) / sizeof(s_tests[0]);\
221+
static cutest_case_t s_tests[TEST_ARRAY_SIZE(s_parameterized_userdata)];\
223222
unsigned long i = 0;\
224-
for (i = 0; i < number_of_parameterized_data; i++) {\
223+
for (i = 0; i < TEST_ARRAY_SIZE(s_tests); i++) {\
225224
cutest_case_init(&s_tests[i], #fixture, #test,\
226225
s_cutest_fixture_setup_##fixture,\
227226
s_cutest_fixture_teardown_##fixture,\
228227
(void(*)(void*, unsigned long))cb);\
229-
s_tests[i].parameterized.type_name = #TYPE;\
230-
s_tests[i].parameterized.test_data_cstr = TEST_STRINGIFY(__VA_ARGS__);\
231-
s_tests[i].parameterized.param_data = s_parameterized_userdata;\
232-
s_tests[i].parameterized.param_idx = i;\
228+
cutest_case_convert_parameterized(&s_tests[i],\
229+
#TYPE, TEST_STRINGIFY(__VA_ARGS__), (void*)s_parameterized_userdata, i);\
233230
cutest_register_case(&s_tests[i]);\
234231
}\
235232
}\
@@ -270,11 +267,7 @@ extern "C" {
270267
TEST_C_API void u_cutest_body_##fixture##_##test(\
271268
u_cutest_parameterized_type_##fixture##_##test*, unsigned long);\
272269
TEST_INITIALIZER(cutest_usertest_interface_##fixture##_##test) {\
273-
static unsigned char s_token = 0;\
274-
if (s_token == 0) {\
275-
s_token = 1;\
276-
cutest_usertest_parameterized_register_##fixture##_##test(u_cutest_body_##fixture##_##test);\
277-
}\
270+
cutest_usertest_parameterized_register_##fixture##_##test(u_cutest_body_##fixture##_##test);\
278271
}\
279272
TEST_C_API void u_cutest_body_##fixture##_##test(\
280273
u_cutest_parameterized_type_##fixture##_##test* _test_parameterized_data,\
@@ -444,6 +437,8 @@ extern "C" {
444437
#define TEST_JOIN(a, b) TEST_JOIN2(a, b)
445438
#define TEST_JOIN2(a, b) a##b
446439

440+
#define TEST_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
441+
447442
/** @endcond */
448443

449444
/**
@@ -714,6 +709,17 @@ typedef struct cutest_case
714709
void cutest_case_init(cutest_case_t* tc, const char* fixture_name, const char* case_name,
715710
cutest_test_case_setup_fn setup, cutest_test_case_teardown_fn teardown, cutest_test_case_body_fn body);
716711

712+
/**
713+
* @brief Convert normal test case to parameterized test case.
714+
* @param[in,out] tc - Test case.
715+
* @param[in] type - User type name.
716+
* @param[in] commit - The C string of user test data.
717+
* @param[in] data - Data passed to #cutest_case_t::stage::body
718+
* @param[in] size - Index passed to #cutest_case_t::stage::body
719+
*/
720+
void cutest_case_convert_parameterized(cutest_case_t* tc, const char* type,
721+
const char* commit, void* data, unsigned long size);
722+
717723
/**
718724
* @brief Register test case.
719725
*

src/cutest.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
#include "cutest.h"
66

7-
/**
8-
* @brief Get static array element count.
9-
* @param[in] arr Static array.
10-
*/
11-
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
12-
137
/*
148
* Before Visual Studio 2015, there is a bug that a `do { } while (0)` will triger C4127 warning
159
* https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4127
@@ -863,7 +857,7 @@ static void _cutest_initlize_color_unix(void)
863857
}
864858

865859
unsigned long i;
866-
for (i = 0; i < ARRAY_SIZE(support_color_term_list); i++)
860+
for (i = 0; i < TEST_ARRAY_SIZE(support_color_term_list); i++)
867861
{
868862
if (cutest_porting_strcmp(term, support_color_term_list[i]) == 0)
869863
{
@@ -3366,6 +3360,15 @@ void cutest_case_init(cutest_case_t* tc, const char* fixture_name, const char* c
33663360
tc->stage.body = body;
33673361
}
33683362

3363+
void cutest_case_convert_parameterized(cutest_case_t* tc, const char* type,
3364+
const char* commit, void* data, unsigned long size)
3365+
{
3366+
tc->parameterized.type_name = type;
3367+
tc->parameterized.test_data_cstr = commit;
3368+
tc->parameterized.param_data = data;
3369+
tc->parameterized.param_idx = size;
3370+
}
3371+
33693372
int cutest_run_tests(int argc, char* argv[], FILE* out, const cutest_hook_t* hook)
33703373
{
33713374
int ret = 0;

0 commit comments

Comments
 (0)