diff options
author | Thomas Haller <thaller@redhat.com> | 2024-08-22 11:18:32 (GMT) |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2024-08-22 11:34:42 (GMT) |
commit | 7f099cf01ce37093f6a908282bb61fa4e63c07c0 (patch) | |
tree | abfeda02bfedff1e1eda7b014b6585720aaaa59f | |
parent | e76d5697f7c5b8658f36b0f7a66e2fc81e4f1fab (diff) | |
download | libnl-7f099cf01ce37093f6a908282bb61fa4e63c07c0.zip libnl-7f099cf01ce37093f6a908282bb61fa4e63c07c0.tar.gz libnl-7f099cf01ce37093f6a908282bb61fa4e63c07c0.tar.bz2 |
tests: add _nltst_objects_to_string() helper
-rw-r--r-- | tests/nl-test-util.c | 44 | ||||
-rw-r--r-- | tests/nl-test-util.h | 3 |
2 files changed, 47 insertions, 0 deletions
diff --git a/tests/nl-test-util.c b/tests/nl-test-util.c index 1503907..27cbb14 100644 --- a/tests/nl-test-util.c +++ b/tests/nl-test-util.c @@ -262,6 +262,50 @@ char *_nltst_object_to_string(const struct nl_object *obj) return s; } +char *_nltst_objects_to_string(const char *description, + struct nl_object *const *objs, ssize_t len) +{ + _nl_auto_free char *result = NULL; + size_t extra_len; + size_t start_idx; + size_t l; + size_t i; + + l = _nl_ptrarray_len(objs, len); + + start_idx = 0; + extra_len = 300 + (description ? strlen(description) : 0u); + + result = _nltst_malloc0(extra_len); + + _nltst_sprintf(result, &start_idx, &extra_len, ">>> [%zu] objects", l); + if (description) { + _nltst_sprintf(result, &start_idx, &extra_len, " (%s)", + description); + } + _nltst_sprintf(result, &start_idx, &extra_len, "\n"); + + for (i = 0; i < l; i++) { + _nl_auto_free char *s = NULL; + size_t slen; + size_t extra_len; + + s = _nltst_object_to_string(objs[i]); + slen = strlen(s); + + /* Don't bother to calculate the exact extra space. Just always + * reallocate enough. */ + extra_len = slen + 100; + result = _nltst_assert_nonnull( + realloc(result, start_idx + extra_len)); + + _nltst_sprintf(result, &start_idx, &extra_len, ">>> [%zu] %s\n", + i, s); + } + + return _nl_steal_pointer(&result); +} + struct cache_get_all_data { struct nl_object **arr; size_t len; diff --git a/tests/nl-test-util.h b/tests/nl-test-util.h index f2fa697..981228b 100644 --- a/tests/nl-test-util.h +++ b/tests/nl-test-util.h @@ -501,6 +501,9 @@ _nltst_select_route_equal(const NLTstSelectRoute *select_route1, char *_nltst_select_route_to_string(const NLTstSelectRoute *select_route); +char *_nltst_objects_to_string(const char *description, + struct nl_object *const *objs, ssize_t len); + void _nltst_select_route_parse(const char *str, NLTstSelectRoute *out_select_route); |