tests: add ASSERT_MEMEQ_BYTE() and ASSERT_MEMEQ_ZERO()

Assert that a memory region consists entirely of a given byte (or zero)
and, on mismatch, show which bytes diverge via hexdump_memcmp_byte().
More useful than ASSERT_TRUE(memeqbyte(...)) / ASSERT_TRUE(memeqzero(...)).
This commit is contained in:
Yu Watanabe committed 2026-11-05 03:21:31 +09:00
1 parent 7ff3f83104
commit fe2987979b
2 files changed
+28

No files matched your search

+18
View File
@@ -613,6 +613,24 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
})
#endif
#ifdef __COVERITY__
# define ASSERT_MEMEQ_BYTE(byte, data, length) __coverity_check__(memeqbyte((byte), (data), (length)))
#else
# define ASSERT_MEMEQ_BYTE(byte, data, length) \
({ \
uint8_t _byte = (byte); \
const void *_data = (data); \
size_t _length = (length); \
if (!memeqbyte(_byte, _data, _length)) { \
_cleanup_free_ char *_dump = hexdump_memcmp_byte(_byte, _data, _length); \
log_test_failed("Expected all %zu bytes of \"%s\" to be 0x%02x:\n%s", \
_length, #data, _byte, strnull(_dump)); \
} \
})
#endif
#define ASSERT_MEMEQ_ZERO(data, length) ASSERT_MEMEQ_BYTE(0x00, data, length)
/* DECIMAL_STR_FMT() uses _Generic which cannot be used in string concatenation so we have to format the
* input into strings first and then format those into the final assertion message. */
+10
View File
@@ -168,6 +168,16 @@ TEST(ASSERT) {
ASSERT_IOVEC_EQ(&IOVEC_MAKE(m1, sizeof(m1)), &IOVEC_MAKE(m2, sizeof(m2)));
ASSERT_SIGNAL(ASSERT_IOVEC_EQ(&IOVEC_MAKE(m1, sizeof(m1)), &IOVEC_MAKE(m3, sizeof(m3))), SIGABRT);
ASSERT_SIGNAL(ASSERT_IOVEC_EQ(&IOVEC_MAKE(m1, sizeof(m1)), &IOVEC_MAKE(m1, sizeof(m1) - 1)), SIGABRT);
static const uint8_t allx[] = { 'x', 'x', 'x', 'x' }, mixed[] = { 'x', 'x', 'y', 'x' }, zeros[4] = {};
ASSERT_MEMEQ_BYTE('x', allx, sizeof(allx));
ASSERT_MEMEQ_BYTE('x', allx, 0);
ASSERT_SIGNAL(ASSERT_MEMEQ_BYTE('x', mixed, sizeof(mixed)), SIGABRT);
ASSERT_MEMEQ_ZERO(zeros, sizeof(zeros));
ASSERT_MEMEQ_ZERO(NULL, 0);
ASSERT_SIGNAL(ASSERT_MEMEQ_ZERO(allx, sizeof(allx)), SIGABRT);
}
TEST(ASSERT_OK_OR) {