diff options
author | Adrian Vovk <adrianvovk@gmail.com> | 2024-01-19 02:48:24 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-02-14 09:35:36 +0100 |
commit | c425c8854f52b099e0166aae4a8ea358d827848f (patch) | |
tree | 3096b6746b89640050f5276d485e3f722ec1ff6e /src/test/test-hashmap-plain.c | |
parent | sd-journal: drop unused function prototype (diff) | |
download | systemd-c425c8854f52b099e0166aae4a8ea358d827848f.tar.xz systemd-c425c8854f52b099e0166aae4a8ea358d827848f.zip |
hashmap: Add helper to dump sorted keys
Currently, hashmap_dump_sorted sorts by key and then returns the values
in order sorted by key. This commit adds another helper that does the
same but returns the sorted keys instead
Diffstat (limited to 'src/test/test-hashmap-plain.c')
-rw-r--r-- | src/test/test-hashmap-plain.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c index 152b1c03e6..0d1f845794 100644 --- a/src/test/test-hashmap-plain.c +++ b/src/test/test-hashmap-plain.c @@ -965,6 +965,8 @@ TEST(string_strv_hashmap) { TEST(hashmap_dump_sorted) { static void * const expected[] = { UINT_TO_PTR(123U), UINT_TO_PTR(12U), UINT_TO_PTR(345U), }; + static const char *expected_keys[] = { "key 0", "key 1", "key 2", }; + static void * const expected_keys2[] = { UINT_TO_PTR(111U), UINT_TO_PTR(222U), UINT_TO_PTR(333U), }; _cleanup_hashmap_free_ Hashmap *m = NULL; _cleanup_free_ void **vals = NULL; size_t n; @@ -984,6 +986,13 @@ TEST(hashmap_dump_sorted) { assert_se(memcmp(vals, expected, n * sizeof(void*)) == 0); vals = mfree(vals); + + assert_se(hashmap_dump_keys_sorted(m, &vals, &n) >= 0); + assert_se(n == ELEMENTSOF(expected_keys)); + for (size_t i = 0; i < n; i++) + assert_se(streq(vals[i], expected_keys[i])); + + vals = mfree(vals); m = hashmap_free(m); assert_se(m = hashmap_new(NULL)); @@ -999,6 +1008,12 @@ TEST(hashmap_dump_sorted) { assert_se(hashmap_dump_sorted(m, &vals, &n) >= 0); assert_se(n == ELEMENTSOF(expected)); assert_se(memcmp(vals, expected, n * sizeof(void*)) == 0); + + vals = mfree(vals); + + assert_se(hashmap_dump_keys_sorted(m, &vals, &n) >= 0); + assert_se(n == ELEMENTSOF(expected_keys2)); + assert_se(memcmp(vals, expected_keys2, n * sizeof(void*)) == 0); } /* Signal to test-hashmap.c that tests from this compilation unit were run. */ |