Skip to content

Commit

Permalink
tests: add REQUIRE_MEMEQ() macro
Browse files Browse the repository at this point in the history
Actually prints out the offending memory regions. Very useful for
debugging, to avoid having to attach a (gdb) instance and poke at memory
regions every single time.
  • Loading branch information
haasn committed Feb 14, 2023
1 parent 9613df0 commit 55c8585
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/tests/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,45 @@ static inline bool feq(float a, float b, float epsilon)
} \
} while (0)

static inline void log_array(const uint8_t *a, const uint8_t *ref, size_t size)
{
for (size_t n = 0; n < size; n++) {
const char *prefix = "", *suffix = "";
char terminator = ' ';
if (a[n] != ref[n]) {
prefix = "\033[31;1m";
suffix = "\033[0m";
}
if (n+1 == size || n % 16 == 15)
terminator = '\n';
fprintf(stderr, "%s%02"PRIx8"%s%c", prefix, a[n], suffix, terminator);
}
}

static inline void require_memeq(const void *aptr, const void *bptr, size_t size,
const char *astr, const char *bstr,
const char *sizestr, const char *file, int line)
{
const uint8_t *a = aptr, *b = bptr;
for (size_t i = 0; i < size; i++) {
if (a[i] == b[i])
continue;

fprintf(stderr, "=== FAILED: memcmp(%s, %s, %s) == 0 at %s:%d\n"
"at position %zu: 0x%02"PRIx8" != 0x%02"PRIx8"\n\n",
astr, bstr, sizestr, file, line, i, a[i], b[i]);

const size_t logsize = PL_MIN(size, PL_MAX(i+2, 512));
fprintf(stderr, "first %zu bytes of '%s':\n", logsize, astr);
log_array(a, b, logsize);
fprintf(stderr, "\nfirst %zu bytes of '%s':\n", logsize, bstr);
log_array(b, a, logsize);
exit(1);
}
}

#define REQUIRE_MEMEQ(a, b, size) require_memeq(a, b, size, #a, #b, #size, __FILE__, __LINE__)

#define REQUIRE_HANDLE(shmem, type) \
switch (type) { \
case PL_HANDLE_FD: \
Expand Down

0 comments on commit 55c8585

Please sign in to comment.