diff options
author | Thomas Haller <thaller@redhat.com> | 2024-08-22 11:18:15 (GMT) |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2024-08-22 11:33:14 (GMT) |
commit | e76d5697f7c5b8658f36b0f7a66e2fc81e4f1fab (patch) | |
tree | 412def3795e26356abec03ce614b7b8e75ef8b16 | |
parent | d94a3e81e0a6eafc06f182e49d5124a3bffe2bc3 (diff) | |
download | libnl-e76d5697f7c5b8658f36b0f7a66e2fc81e4f1fab.zip libnl-e76d5697f7c5b8658f36b0f7a66e2fc81e4f1fab.tar.gz libnl-e76d5697f7c5b8658f36b0f7a66e2fc81e4f1fab.tar.bz2 |
tests: add _nltst_malloc0() and _nltst_sprintf() helpers
-rw-r--r-- | tests/nl-test-util.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/nl-test-util.h b/tests/nl-test-util.h index dab934d..f2fa697 100644 --- a/tests/nl-test-util.h +++ b/tests/nl-test-util.h @@ -102,11 +102,38 @@ _NL_AUTO_DEFINE_FCN_TYPED0(char **, _nltst_auto_strfreev_fcn, _nltst_strfreev); #define _nltst_assert_nonnull(x) __nltst_assert_nonnull(_NL_UNIQ, x) +/*****************************************************************************/ + static inline char *_nltst_strdup(const char *str) { return str ? _nltst_assert_nonnull(strdup(str)) : NULL; } +#define _nltst_malloc0(size) _nltst_assert_nonnull(calloc((size), 1u)) + +/*****************************************************************************/ + +#define _nltst_sprintf(buf, p_start, p_extra_len, ...) \ + do { \ + char *_buf = (buf); \ + size_t *_p_start = (p_start); \ + size_t *_p_extra_len = (p_extra_len); \ + int _r; \ + \ + _nltst_assert_nonnull(_buf); \ + _nltst_assert_nonnull(_p_start); \ + _nltst_assert_nonnull(_p_extra_len); \ + \ + _buf = &_buf[*_p_start]; \ + _r = snprintf(_buf, *_p_extra_len, __VA_ARGS__); \ + _nltst_assert(_r >= 0); \ + _nltst_assert((size_t)_r < *_p_extra_len); \ + _nltst_assert((size_t)_r == strlen(_buf)); \ + \ + *_p_start += (size_t)_r; \ + *_p_extra_len -= (size_t)_r; \ + } while (0) + /*****************************************************************************/ #define __nltst_sprintf_arr(uniq, arr, fmt, ...) \ |