diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2017-11-28 12:35:49 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2017-11-28 21:30:30 +0100 |
commit | 224b0e7ad0116579f830b7dd387b97c3facb65d9 (patch) | |
tree | d769049843011ec955d86af31e2f0d62c4521f38 /src/test/test-set.c | |
parent | ec1d290903538df3183dac2771b5db429b6f7688 (diff) | |
download | systemd-224b0e7ad0116579f830b7dd387b97c3facb65d9.tar.gz |
Add set/hashmap helpers for non-trivial freeing and use where straighforward
A macro is needed because otherwise we couldn't ensure type safety.
Some simple tests are included.
No functional change intended.
Diffstat (limited to 'src/test/test-set.c')
-rw-r--r-- | src/test/test-set.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/test/test-set.c b/src/test/test-set.c index 7bae95bd2b..0a29a62621 100644 --- a/src/test/test-set.c +++ b/src/test/test-set.c @@ -40,6 +40,29 @@ static void test_set_steal_first(void) { assert_se(set_isempty(m)); } +typedef struct Item { + int seen; +} Item; +static void item_seen(Item *item) { + item->seen++; +} + +static void test_set_free_with_destructor(void) { + Set *m; + struct Item items[4] = {}; + unsigned i; + + assert_se(m = set_new(NULL)); + for (i = 0; i < ELEMENTSOF(items) - 1; i++) + assert_se(set_put(m, items + i) == 1); + + m = set_free_with_destructor(m, item_seen); + assert_se(items[0].seen == 1); + assert_se(items[1].seen == 1); + assert_se(items[2].seen == 1); + assert_se(items[3].seen == 0); +} + static void test_set_put(void) { _cleanup_set_free_ Set *m = NULL; @@ -102,6 +125,7 @@ static void test_set_make(void) { int main(int argc, const char *argv[]) { test_set_steal_first(); + test_set_free_with_destructor(); test_set_put(); test_set_make(); |