]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
tests/unit/test_xaprintf.c: Use assert_string_equal()
authorAlejandro Colomar <alx@kernel.org>
Tue, 4 Nov 2025 12:39:26 +0000 (13:39 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 28 Nov 2025 14:41:54 +0000 (08:41 -0600)
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>
tests/unit/Makefile.am
tests/unit/test_exit_if_null.c

index 9a6387ebe2796a917d907be2140778e6494d8b2e..250004ef6aa7746797e5f4f1863c5f35113868c6 100644 (file)
@@ -133,7 +133,6 @@ test_exit_if_null_SOURCES = \
     ../../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 = \
index 4a66e870da36195f87c562be8bf358981f414d7a..ce6b898585012548a77dbb44d91cc73945251d3e 100644 (file)
@@ -16,7 +16,6 @@
 #include <cmocka.h>
 
 #include "sizeof.h"
-#include "string/strcmp/streq.h"
 
 
 #define assert_unreachable()  assert_true(0)
@@ -66,7 +65,7 @@ test_exit_if_null_exit(void **state)
                assert_unreachable();
                break;
        case EXIT_CALLED:
-               assert_true(streq(p, "called"));
+               assert_string_equal(p, "called");
                p = "test_ok";
                break;
        default:
@@ -74,7 +73,7 @@ test_exit_if_null_exit(void **state)
                break;
        }
 
-       assert_true(streq(p, "test_ok"));
+       assert_string_equal(p, "test_ok");
 }