Add tests for asserting pointers with message

Signed-off-by: Friedrich Schwedler <fschwedler@emlix.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Friedrich Schwedler 2024-03-12 13:57:17 +01:00 committed by Andreas Schneider
parent 614175f145
commit dadc4c7e0c
3 changed files with 103 additions and 0 deletions

View File

@ -38,6 +38,8 @@ set(CMOCKA_TESTS
test_assert_memory_fail
test_assert_ptr
test_assert_ptr_fail
test_assert_ptr_msg
test_assert_ptr_msg_fail
test_assert_u_int
test_assert_u_int_fail
test_assert_range
@ -139,6 +141,13 @@ set_tests_properties(
"\\[ FAILED \\] ptr_tests: 4 test"
)
set_tests_properties(
test_assert_ptr_msg_fail
PROPERTIES
PASS_REGULAR_EXPRESSION
"\\[ FAILED \\] ptr_msg_tests: 4 test"
)
set_tests_properties(
test_assert_u_int_fail
PROPERTIES

View File

@ -0,0 +1,47 @@
#include "config.h"
#include <setjmp.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <cmocka.h>
static void test_ptr_equal_msg(void **state)
{
(void)state; /* unused */
const char *my_pointer = "wurst";
assert_ptr_equal_msg(my_pointer, my_pointer, "my_pointer should be equal to itself");
}
static void test_ptr_not_equal_msg(void **state)
{
(void)state; /* unused */
assert_ptr_not_equal_msg("wurst", "brot", "\"wurst\" should be a different pointer then \"brot\"");
}
static void test_null_msg(void **state)
{
(void)state; /* unused */
void *my_pointer = NULL;
assert_null_msg(my_pointer, "my_pointer should be a NULL pointer");
}
static void test_non_null_msg(void **state)
{
(void)state; /* unused */
const char *my_pointer = "hello world";
assert_non_null_msg(my_pointer, "my_pointer should not be a NULL pointer");
}
int main(void)
{
const struct CMUnitTest ptr_msg_tests[] = {
cmocka_unit_test(test_ptr_equal_msg),
cmocka_unit_test(test_ptr_not_equal_msg),
cmocka_unit_test(test_null_msg),
cmocka_unit_test(test_non_null_msg),
};
return cmocka_run_group_tests(ptr_msg_tests, NULL, NULL);
}

View File

@ -0,0 +1,47 @@
#include "config.h"
#include <setjmp.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <cmocka.h>
static void test_ptr_equal_msg_fail(void **state)
{
(void)state; /* unused */
const char *my_pointer = "wurst";
assert_ptr_equal_msg(my_pointer, my_pointer + 1, "my_pointer should be equal to itself plus one");
}
static void test_ptr_not_equal_msg_fail(void **state)
{
(void)state; /* unused */
const char *my_pointer = "wurst";
assert_ptr_not_equal_msg(my_pointer, my_pointer, "my_pointer should not be equal to itself");
}
static void test_null_msg_fail(void **state)
{
(void)state; /* unused */
assert_null_msg("wurst", "\"wurst\" should be a NULL pointer");
}
static void test_non_null_msg_fail(void **state)
{
(void)state; /* unused */
const char *my_pointer = NULL;
assert_non_null_msg(my_pointer, "my_pointer should not be a NULL pointer");
}
int main(void)
{
const struct CMUnitTest ptr_msg_tests[] = {
cmocka_unit_test(test_ptr_equal_msg_fail),
cmocka_unit_test(test_ptr_not_equal_msg_fail),
cmocka_unit_test(test_null_msg_fail),
cmocka_unit_test(test_non_null_msg_fail),
};
return cmocka_run_group_tests(ptr_msg_tests, NULL, NULL);
}