Skip to content

Commit

Permalink
Merge pull request #103 from SanderMertens/test_params
Browse files Browse the repository at this point in the history
Add parametrizable tests
  • Loading branch information
SanderMertens committed Apr 20, 2024
2 parents 72e9851 + 423829b commit bcce194
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 134 deletions.
3 changes: 2 additions & 1 deletion drivers/lang/c/src/gcc/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ void gcc_add_std(
ut_strbuf_appendstr(cmd, " -Wuninitialized");
ut_strbuf_appendstr(cmd, " -Wmissing-field-initializers");
ut_strbuf_appendstr(cmd, " -Wundef");
ut_strbuf_appendstr(cmd, " -Wunused-function");

if (is_clang(lang)) {
ut_strbuf_appendstr(cmd, " -Wno-unknown-warning-option");
Expand Down Expand Up @@ -379,7 +380,7 @@ void gcc_compile_src(
* add the precompiled header options for the current project header. */
bool own_source = true;
char *relative_src = &source[strlen(project->path)];
if (strncmp(relative_src, "deps"UT_OS_PS, 5)) {
if (!strncmp(relative_src, "deps"UT_OS_PS, 5)) {
own_source = false;
}

Expand Down
12 changes: 12 additions & 0 deletions drivers/test/include/bake_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ typedef struct bake_test_case {
void (*function)(void);
} bake_test_case;

typedef struct bake_test_param {
const char *name;
char **values;
int32_t value_count;
int32_t value_cur;
} bake_test_param;

typedef struct bake_test_suite {
const char *id;
void (*setup)(void);
void (*teardown)(void);
uint32_t testcase_count;
bake_test_case *testcases;
uint32_t param_count;
bake_test_param *params;
uint32_t assert_count;
} bake_test_suite;

Expand Down Expand Up @@ -122,6 +131,9 @@ void test_expect_abort(void);
BAKE_TEST_API
void test_abort(void);

BAKE_TEST_API
const char* test_param(const char *name);

#define test_assert(cond) _test_assert(cond, #cond, __FILE__, __LINE__)
#define test_bool(v1, v2) _test_bool(v1, v2, #v1, #v2, __FILE__, __LINE__)
#define test_true(v) _test_bool(v, true, #v, "true", __FILE__, __LINE__)
Expand Down
77 changes: 75 additions & 2 deletions drivers/test/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,67 @@ int generate_suite_testcases(
return 0;
}

static
int generate_suite_params(
ut_code *src,
JSON_Array *suites)
{
size_t i, count = json_array_get_count(suites);

/* The JSON structure has already been validated, so no need to do error
* checking again. */

for (i = 0; i < count; i ++) {
JSON_Object *suite = json_array_get_object(suites, i);
const char *id = json_object_get_string(suite, "id");

JSON_Object *params = json_object_get_object(suite, "params");
if (!params) {
continue;
}

/* First generate arrays with parameter values */
for (size_t p = 0; p < json_object_get_count(params); p ++) {
const char *name = json_object_get_name(params, p);
ut_code_write(src, "const char* %s_%s_param[] = {", id, name);

JSON_Array *param_values = json_object_get_array(params, name);
for (size_t v = 0; v < json_array_get_count(param_values); v ++) {
const char *value = json_array_get_string(param_values, v);
if (v) {
ut_code_write(src, ", ");
}
ut_code_write(src, "\"%s\"", value);
}

ut_code_write(src, "};\n");
}

ut_code_write(src, "bake_test_param %s_params[] = {\n", id);
ut_code_indent(src);

for (size_t p = 0; p < json_object_get_count(params); p ++) {
if (p) {
ut_code_write(src, ",\n");
}

const char *name = json_object_get_name(params, p);
JSON_Array *param_values = json_object_get_array(params, name);
ut_code_write(src, "{\"%s\", (char**)%s_%s_param", name, id, name);
ut_code_write(src, ", %d}", json_array_get_count(param_values));
}

ut_code_write(src, "\n");
ut_code_dedent(src);
ut_code_write(src, "};\n");
}

ut_code_write(src, "\n");

return 0;
}


static
int generate_suite_data(
ut_code *src,
Expand Down Expand Up @@ -159,7 +220,17 @@ int generate_suite_data(
size_t t_count = json_array_get_count(testcases);

ut_code_write(src, "%d,\n", t_count);
ut_code_write(src, "%s_testcases\n", id);
ut_code_write(src, "%s_testcases", id);

JSON_Object *params = json_object_get_object(suite, "params");
if (params) {
size_t p_count = json_object_get_count(params);
ut_code_write(src, ",\n");
ut_code_write(src, "%d,\n", p_count);
ut_code_write(src, "%s_params\n", id);
} else {
ut_code_write(src, "\n");
}

ut_code_dedent(src);
ut_code_write(src, "}");
Expand All @@ -175,6 +246,7 @@ int generate_testmain(
bake_driver_api *driver,
bake_config *config,
bake_project *project,
JSON_Object *jo,
JSON_Array *suites)
{
(void)driver;
Expand Down Expand Up @@ -207,6 +279,7 @@ int generate_testmain(

generate_testcase_fwd_decls(src, suites);
generate_suite_testcases(src, suites);
generate_suite_params(src, suites);

ut_code_write(src, "static bake_test_suite suites[] = {\n");
ut_code_indent(src);
Expand Down Expand Up @@ -349,7 +422,7 @@ void generate(
}
}

generate_testmain(driver, config, project, suites);
generate_testmain(driver, config, project, jo, suites);
} else {
fprintf(stderr, "no 'testsuites' array in test configuration\n");
project->error = true;
Expand Down
Loading

0 comments on commit bcce194

Please sign in to comment.