I'm trying to write a parameterized test based on some string values using the Criterion framework. As an MRE, and following the example shown in the docs, I wrote the following:
我正在尝试使用Criterion框架编写一个基于一些字符串值的参数化测试。作为一个MRE,并遵循文档中显示的示例,我写了以下内容:
#include <criterion/criterion.h>
#include <criterion/new/assert.h>
#include <criterion/parameterized.h>
#include <stdio.h>
ParameterizedTestParameters(test_suite, test_str) {
static char* strings[] = {"Foo", "Bar"};
size_t strings_count = sizeof(strings)/sizeof(char*);
return cr_make_param_array(char*, strings, strings_count);
}
ParameterizedTest(char** pstring, test_suite, test_str) {
printf("%s\n", *pstring);
}
However, when I run the test, it crashes with the following output:
但是,当我运行测试时,它崩溃了,输出如下:
[----] tests/string_aparams.c:13: Unexpected signal caught below this line!
[FAIL] test_suite::test_str: CRASH!
[----] tests/string_aparams.c:13: Unexpected signal caught below this line!
[FAIL] test_suite::test_str: CRASH!
But when I tried writing a similar test based on an array of int
parameters, it worked fine:
但是,当我尝试基于一组int参数编写类似的测试时,它运行得很好:
ParameterizedTestParameters(test_suite, test_int) {
static int numbers[] = {1, 2};
size_t numbers_count = sizeof(numbers)/sizeof(int);
return cr_make_param_array(int, numbers, numbers_count);
}
ParameterizedTest(int* pnum, test_suite, test_int) {
printf("%d\n", *pnum);
}
[====] Running 2 tests from test_suite:
[RUN ] test_suite::test_int
[RUN ] test_suite::test_int
1
2
[PASS] test_suite::test_int: (0.00s)
[PASS] test_suite::test_int: (0.00s)
What could be the problem with first snippet of code?
第一段代码会有什么问题?
UPDATE:
After re-reading the docs a couple of times, the following passage from the same page made me think:
在重新阅读了几次这些文档后,同一页上的以下段落让我思考:
Any dynamic memory allocation done from a ParameterizedTestParameter
function must be done with cr_malloc, cr_calloc, or cr_realloc
Perhabs using "Foo"
and "Bar"
inside {"Foo", "Bar"}
is dynamically allocating space for the two strings and I should instead use cr_malloc
to allocate space for them. I'm not sure if that is the case, but this made me try another approach:
也许在{“foo”,“Bar”}中使用“foo”和“Bar”会为这两个字符串动态分配空间,而我应该使用cr_Malloc为它们分配空间。我不确定是不是这样,但这让我尝试了另一种方法:
#include <criterion/criterion.h>
#include <criterion/new/assert.h>
#include <criterion/parameterized.h>
#include <stdio.h>
#include <string.h>
#define STRINGS_COUNT 2
#define STRINGS (char*[]){"Foo", "Bar"}
ParameterizedTestParameters(test_suite, test_str) {
static char* strings[STRINGS_COUNT];
for(size_t k = 0; k < STRINGS_COUNT; k++) {
strings[k] = cr_malloc(strlen(STRINGS[k]) + 1);
strcpy(strings[k], STRINGS[k]);
}
return cr_make_param_array(char*, strings, STRINGS_COUNT);
}
ParameterizedTest(char* *pstring, test_suite, test_str) {
printf("%s\n", *pstring);
}
When running the above test, the following output is displayed:
运行上述测试时,将显示以下输出:
[RUN ] test_suite::test_str
Foo
[PASS] test_suite::test_str: (0.00s)
[RUN ] test_suite::test_str
Bar
Is my assumption that using the initializer {"Foo", "Bar"}
dynamically allocate space for the two strings correct? And, if not, why would the first test crash while the third succeed?
使用初始值设定项{“foo”,“Bar”}为两个字符串动态分配空间的假设是否正确?如果没有,为什么第一次测试失败,而第三次测试成功?
更多回答
@TedLyngmo Yes, I tried printing the result and it gave me 2 unfortunately.
@TedLyngmo是的,我试着打印结果,但不幸的是得到了2。
Sorry, I got confused :-) I'd prefer sizeof strings / sizeof *strings;
Btw, make it static const char* strings[] = {"Foo", "Bar"};
since you are not allowed to change the string literals anyway.
对不起,我搞错了:-)我更喜欢sizeof字符串/sizeof*字符串;顺便说一句,将其设置为静态常量char*string[]={“foo”,“bar”};因为无论如何都不允许更改字符串文字。
我是一名优秀的程序员,十分优秀!