diff --git a/tests/unit/io.cpp b/tests/unit/io.cpp index c04ef6c02..16651b992 100644 --- a/tests/unit/io.cpp +++ b/tests/unit/io.cpp @@ -35,7 +35,6 @@ #include #include #include -#include using namespace villas; @@ -44,7 +43,7 @@ extern void init_memory(); #define NUM_VALUES 10 struct param { - char *fmt; + std::basic_string, criterion::allocator> fmt; int cnt; int bits; }; @@ -206,16 +205,7 @@ void cr_assert_eq_sample_raw(struct sample *a, struct sample *b, int flags, int ParameterizedTestParameters(io, lowlevel) { - for (int i = 0; i < ARRAY_LEN(params); i++) { - struct param *p = ¶ms[i]; - - char *fmt = cr_malloc(strlen(p->fmt) + 1); - strcpy(fmt, p->fmt); - - p->fmt = fmt; - } - - return cr_make_param_array(struct param, params, ARRAY_LEN(params)); + return criterion_test_params(params); } ParameterizedTest(struct param *p, io, lowlevel, .init = init_memory) @@ -251,8 +241,8 @@ ParameterizedTest(struct param *p, io, lowlevel, .init = init_memory) fill_sample_data(&signals, smps, p->cnt); - f = format_type_lookup(p->fmt); - cr_assert_not_null(f, "Format '%s' does not exist", p->fmt); + f = format_type_lookup(p->fmt.c_str()); + cr_assert_not_null(f, "Format '%s' does not exist", p->fmt.c_str()); ret = io_init(&io, f, &signals, SAMPLE_HAS_ALL); cr_assert_eq(ret, 0); @@ -284,17 +274,7 @@ ParameterizedTest(struct param *p, io, lowlevel, .init = init_memory) ParameterizedTestParameters(io, highlevel) { - for (int i = 0; i < ARRAY_LEN(params); i++) { - struct param *p = ¶ms[i]; - - char *fmt = cr_malloc(strlen(p->fmt) + 1); - strcpy(fmt, p->fmt); - - p->fmt = fmt; - } - - - return cr_make_param_array(struct param, params, ARRAY_LEN(params)); + return criterion_test_params(params); } ParameterizedTest(struct param *p, io, highlevel, .init = init_memory) @@ -342,8 +322,8 @@ ParameterizedTest(struct param *p, io, highlevel, .init = init_memory) ret = asprintf(&fn, "%s/file", dir); cr_assert_gt(ret, 0); - f = format_type_lookup(p->fmt); - cr_assert_not_null(f, "Format '%s' does not exist", p->fmt); + f = format_type_lookup(p->fmt.c_str()); + cr_assert_not_null(f, "Format '%s' does not exist", p->fmt.c_str()); ret = io_init(&io, f, &signals, SAMPLE_HAS_ALL); cr_assert_eq(ret, 0); diff --git a/tests/unit/json.cpp b/tests/unit/json.cpp index 375b69abc..4cb6771dd 100644 --- a/tests/unit/json.cpp +++ b/tests/unit/json.cpp @@ -27,9 +27,11 @@ #include #include +using str = std::basic_string, criterion::allocator>; + struct param { - const char *argv[32]; - const char *json; + std::vector> argv; + str json; }; ParameterizedTestParameters(json, json_load_cli) @@ -37,56 +39,42 @@ ParameterizedTestParameters(json, json_load_cli) static struct param params[] = { // Combined long option { - .argv = { "dummy", "--option=value", nullptr }, + .argv = { "dummy", "--option=value" }, .json = "{ \"option\" : \"value\" }" }, // Separated long option { - .argv = { "dummy", "--option", "value", nullptr }, + .argv = { "dummy", "--option", "value" }, .json = "{ \"option\" : \"value\" }" }, // All kinds of data types { - .argv = { "dummy", "--integer", "1", "--real", "1.1", "--bool", "true", "--null", "null", "--string", "hello world", nullptr }, + .argv = { "dummy", "--integer", "1", "--real", "1.1", "--bool", "true", "--null", "null", "--string", "hello world" }, .json = "{ \"integer\" : 1, \"real\" : 1.1, \"bool\" : true, \"null\" : null, \"string\" : \"hello world\" }" }, // Array generation { - .argv = { "dummy", "--bool", "true", "--bool", "false", nullptr }, + .argv = { "dummy", "--bool", "true", "--bool", "false" }, .json = "{ \"bool\" : [ true, false ] }" }, // Dots in the option name generate sub objects { - .argv = { "dummy", "--sub.option", "value", nullptr }, + .argv = { "dummy", "--sub.option", "value" }, .json = "{ \"sub\" : { \"option\" : \"value\" } }" }, // Nesting is possible { - .argv = { "dummy", "--sub.sub.option", "value", nullptr }, + .argv = { "dummy", "--sub.sub.option", "value" }, .json = "{ \"sub\" : { \"sub\" : { \"option\" : \"value\" } } }" }, // Multiple subgroups are merged { - .argv = { "dummy", "--sub.sub.option", "value1", "--sub.option", "value2", nullptr }, + .argv = { "dummy", "--sub.sub.option", "value1", "--sub.option", "value2" }, .json = "{ \"sub\" : { \"option\" : \"value2\", \"sub\" : { \"option\" : \"value1\" } } }" } }; - for (int i = 0; i < ARRAY_LEN(params); i++) { - struct param *p = ¶ms[i]; - - char *json = cr_malloc(strlen(p->json) + 1); - strcpy(json, p->json); - p->json = json; - - for (char **a = p->argv; *a; a++) { - char *argv = cr_malloc(strlen(*a) + 1); - strcpy(argv , *a); - *a = argv; - } - } - - return cr_make_param_array(struct param, params, ARRAY_LEN(params)); + return criterion_test_params(params); } ParameterizedTest(struct param *p, json, json_load_cli) @@ -94,15 +82,15 @@ ParameterizedTest(struct param *p, json, json_load_cli) json_error_t err; json_t *json, *cli; - /* Calculate argc */ - int argc = 0; - while (p->argv[argc]) - argc++; - - json = json_loads(p->json, 0, &err); + json = json_loads(p->json.c_str(), 0, &err); cr_assert_not_null(json); - cli = json_load_cli(argc, p->argv); + auto argv = new const char*[p->argv.size() + 1]; + for (int i = 0; i < p->argv.size(); i++) + argv[i] = p->argv[i].c_str(); + argv[p->argv.size()] = nullptr; + + cli = json_load_cli(p->argv.size(), argv); cr_assert_not_null(cli); //json_dumpf(json, stdout, JSON_INDENT(2)); putc('\n', stdout);