This produces more useful test results.
With assert_true(streq(...)), we only see the line of code that
triggered the failure, while assert_string_equal() shows the contents
of the strings. See the following example:
alx@devuan:~/tmp$ cat cmocka.c
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
#define streq(a,b) (!strcmp(a,b))
static void a(void **)
{
const char *s = "foo";
assert_true(streq(s, "bar"));
}
static void b(void **)
{
const char *s = "foo";
assert_string_equal(s, "bar");
}
int
main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(a),
cmocka_unit_test(b),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
alx@devuan:~/tmp$ gcc cmocka.c -lcmocka
alx@devuan:~/tmp$ ./a.out
[==========] tests: Running 2 test(s).
[ RUN ] a
[ ERROR ] --- streq(s, "bar")
[ LINE ] --- cmocka.c:15: error: Failure!
[ FAILED ] a
[ RUN ] b
[ ERROR ] --- "foo" != "bar"
[ LINE ] --- cmocka.c:22: error: Failure!
[ FAILED ] b
[==========] tests: 2 test(s) run.
[ PASSED ] 0 test(s).
[ FAILED ] tests: 2 test(s), listed below:
[ FAILED ] a
[ FAILED ] b
2 FAILED TEST(S)
Tested-by: Silvan Mosberger <github@infinisil.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
../../lib/exit_if_null.c \
../../lib/shadowlog.c \
../../lib/alloc/malloc.c \
- ../../lib/string/strcmp/streq.c \
test_exit_if_null.c \
$(NULL)
test_exit_if_null_CFLAGS = \
#include <cmocka.h>
#include "sizeof.h"
-#include "string/strcmp/streq.h"
#define assert_unreachable() assert_true(0)
assert_unreachable();
break;
case EXIT_CALLED:
- assert_true(streq(p, "called"));
+ assert_string_equal(p, "called");
p = "test_ok";
break;
default:
break;
}
- assert_true(streq(p, "test_ok"));
+ assert_string_equal(p, "test_ok");
}