From 39dcab9062328227fb796a1316c62767eae53805 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Fri, 27 Jan 2023 09:47:24 +0100 Subject: test: shorten a bit --- src/test/test-bitmap.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/test') diff --git a/src/test/test-bitmap.c b/src/test/test-bitmap.c index 655b823c7f..6a84508b12 100644 --- a/src/test/test-bitmap.c +++ b/src/test/test-bitmap.c @@ -10,8 +10,7 @@ int main(int argc, const char *argv[]) { assert_se(b); assert_se(bitmap_ensure_allocated(&b) == 0); - bitmap_free(b); - b = NULL; + b = bitmap_free(b); assert_se(bitmap_ensure_allocated(&b) == 0); assert_se(bitmap_isset(b, 0) == false); @@ -89,13 +88,11 @@ int main(int argc, const char *argv[]) { bitmap_clear(b); assert_se(bitmap_isclear(b) == true); assert_se(bitmap_equal(b, b2) == false); - bitmap_free(b2); - b2 = NULL; + b2 = bitmap_free(b2); assert_se(bitmap_set(b, UINT_MAX) == -ERANGE); - bitmap_free(b); - b = NULL; + b = bitmap_free(b); assert_se(bitmap_ensure_allocated(&b) == 0); assert_se(bitmap_ensure_allocated(&b2) == 0); -- cgit v1.2.1 From 925c51d95c1e28db9f1e75922e427a9398c1888b Mon Sep 17 00:00:00 2001 From: David Tardon Date: Fri, 21 Apr 2023 16:18:43 +0200 Subject: test-fdset: use _cleanup_ --- src/test/test-fdset.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src/test') diff --git a/src/test/test-fdset.c b/src/test/test-fdset.c index 67c99d8501..aa136a633f 100644 --- a/src/test/test-fdset.c +++ b/src/test/test-fdset.c @@ -93,7 +93,7 @@ TEST(fdset_close_others) { TEST(fdset_remove) { _cleanup_close_ int fd = -EBADF; - FDSet *fdset = NULL; + _cleanup_fdset_free_ FDSet *fdset = NULL; char name[] = "/tmp/test-fdset_remove.XXXXXX"; fd = mkostemp_safe(name); @@ -104,7 +104,6 @@ TEST(fdset_remove) { assert_se(fdset_put(fdset, fd) >= 0); assert_se(fdset_remove(fdset, fd) >= 0); assert_se(!fdset_contains(fdset, fd)); - fdset_free(fdset); assert_se(fcntl(fd, F_GETFD) >= 0); @@ -113,7 +112,7 @@ TEST(fdset_remove) { TEST(fdset_iterate) { int fd = -EBADF; - FDSet *fdset = NULL; + _cleanup_fdset_free_ FDSet *fdset = NULL; char name[] = "/tmp/test-fdset_iterate.XXXXXX"; int c = 0; int a; @@ -133,8 +132,6 @@ TEST(fdset_iterate) { } assert_se(c == 1); - fdset_free(fdset); - unlink(name); } -- cgit v1.2.1 From 596b44b178757b72975e7d577896f29b71c731ff Mon Sep 17 00:00:00 2001 From: David Tardon Date: Fri, 21 Apr 2023 16:25:31 +0200 Subject: test: use _cleanup_ for temp. files --- src/test/test-acl-util.c | 4 ++-- src/test/test-copy.c | 28 ++++++++-------------------- src/test/test-fd-util.c | 14 ++++---------- src/test/test-fdset.c | 33 +++++++++------------------------ src/test/test-hostname-setup.c | 5 ++--- src/test/test-stat-util.c | 14 ++++---------- src/test/test-terminal-util.c | 5 ++--- 7 files changed, 31 insertions(+), 72 deletions(-) (limited to 'src/test') diff --git a/src/test/test-acl-util.c b/src/test/test-acl-util.c index 2c406893df..093eaaa01b 100644 --- a/src/test/test-acl-util.c +++ b/src/test/test-acl-util.c @@ -9,13 +9,14 @@ #include "errno-util.h" #include "fd-util.h" #include "format-util.h" +#include "fs-util.h" #include "string-util.h" #include "tests.h" #include "tmpfile-util.h" #include "user-util.h" TEST_RET(add_acls_for_user) { - char fn[] = "/tmp/test-empty.XXXXXX"; + _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-empty.XXXXXX"; _cleanup_close_ int fd = -EBADF; char *cmd; uid_t uid; @@ -65,7 +66,6 @@ TEST_RET(add_acls_for_user) { cmd = strjoina("getfacl -p ", fn); assert_se(system(cmd) == 0); - (void) unlink(fn); return 0; } diff --git a/src/test/test-copy.c b/src/test/test-copy.c index df08b7ed61..38855fd45e 100644 --- a/src/test/test-copy.c +++ b/src/test/test-copy.c @@ -24,8 +24,8 @@ TEST(copy_file) { _cleanup_free_ char *buf = NULL; - char fn[] = "/tmp/test-copy_file.XXXXXX"; - char fn_copy[] = "/tmp/test-copy_file.XXXXXX"; + _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-copy_file.XXXXXX"; + _cleanup_(unlink_tempfilep) char fn_copy[] = "/tmp/test-copy_file.XXXXXX"; size_t sz = 0; int fd; @@ -44,9 +44,6 @@ TEST(copy_file) { assert_se(read_full_file(fn_copy, &buf, &sz) == 0); assert_se(streq(buf, "foo bar bar bar foo\n")); assert_se(sz == 20); - - unlink(fn); - unlink(fn_copy); } static bool read_file_at_and_streq(int dir_fd, const char *path, const char *expected) { @@ -108,8 +105,8 @@ TEST(copy_tree_replace_dirs) { } TEST(copy_file_fd) { - char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; - char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; + _cleanup_(unlink_tempfilep) char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; + _cleanup_(unlink_tempfilep) char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; _cleanup_close_ int in_fd = -EBADF, out_fd = -EBADF; const char *text = "boohoo\nfoo\n\tbar\n"; char buf[64] = {}; @@ -126,9 +123,6 @@ TEST(copy_file_fd) { assert_se(read(out_fd, buf, sizeof buf) == (ssize_t) strlen(text)); assert_se(streq(buf, text)); - - unlink(in_fn); - unlink(out_fn); } TEST(copy_tree) { @@ -293,8 +287,8 @@ TEST(copy_bytes) { } static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, uint64_t max_bytes) { - char fn2[] = "/tmp/test-copy-file-XXXXXX"; - char fn3[] = "/tmp/test-copy-file-XXXXXX"; + _cleanup_(unlink_tempfilep) char fn2[] = "/tmp/test-copy-file-XXXXXX"; + _cleanup_(unlink_tempfilep) char fn3[] = "/tmp/test-copy-file-XXXXXX"; _cleanup_close_ int fd = -EBADF, fd2 = -EBADF, fd3 = -EBADF; int r; struct stat buf, buf2, buf3; @@ -343,9 +337,6 @@ static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, assert_se(buf3.st_size == buf2.st_size); else assert_se((uint64_t) buf3.st_size == max_bytes); - - unlink(fn2); - unlink(fn3); } TEST(copy_bytes_regular_file) { @@ -392,8 +383,8 @@ TEST(copy_proc) { } TEST_RET(copy_holes) { - char fn[] = "/var/tmp/test-copy-hole-fd-XXXXXX"; - char fn_copy[] = "/var/tmp/test-copy-hole-fd-XXXXXX"; + _cleanup_(unlink_tempfilep) char fn[] = "/var/tmp/test-copy-hole-fd-XXXXXX"; + _cleanup_(unlink_tempfilep) char fn_copy[] = "/var/tmp/test-copy-hole-fd-XXXXXX"; struct stat stat; off_t blksz; int r, fd, fd_copy; @@ -441,9 +432,6 @@ TEST_RET(copy_holes) { close(fd); close(fd_copy); - unlink(fn); - unlink(fn_copy); - return 0; } diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c index c902f83784..20d412f5c6 100644 --- a/src/test/test-fd-util.c +++ b/src/test/test-fd-util.c @@ -30,9 +30,9 @@ TEST(close_many) { int fds[3]; - char name0[] = "/tmp/test-close-many.XXXXXX"; - char name1[] = "/tmp/test-close-many.XXXXXX"; - char name2[] = "/tmp/test-close-many.XXXXXX"; + _cleanup_(unlink_tempfilep) char name0[] = "/tmp/test-close-many.XXXXXX"; + _cleanup_(unlink_tempfilep) char name1[] = "/tmp/test-close-many.XXXXXX"; + _cleanup_(unlink_tempfilep) char name2[] = "/tmp/test-close-many.XXXXXX"; fds[0] = mkostemp_safe(name0); fds[1] = mkostemp_safe(name1); @@ -45,22 +45,16 @@ TEST(close_many) { assert_se(fcntl(fds[2], F_GETFD) >= 0); safe_close(fds[2]); - - unlink(name0); - unlink(name1); - unlink(name2); } TEST(close_nointr) { - char name[] = "/tmp/test-test-close_nointr.XXXXXX"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-test-close_nointr.XXXXXX"; int fd; fd = mkostemp_safe(name); assert_se(fd >= 0); assert_se(close_nointr(fd) >= 0); assert_se(close_nointr(fd) < 0); - - unlink(name); } TEST(same_fd) { diff --git a/src/test/test-fdset.c b/src/test/test-fdset.c index aa136a633f..e2b8f94833 100644 --- a/src/test/test-fdset.c +++ b/src/test/test-fdset.c @@ -5,6 +5,7 @@ #include "fd-util.h" #include "fdset.h" +#include "fs-util.h" #include "macro.h" #include "tests.h" #include "tmpfile-util.h" @@ -12,21 +13,19 @@ TEST(fdset_new_fill) { int fd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; - char name[] = "/tmp/test-fdset_new_fill.XXXXXX"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_new_fill.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); assert_se(fdset_new_fill(&fdset) >= 0); assert_se(fdset_contains(fdset, fd)); - - unlink(name); } TEST(fdset_put_dup) { _cleanup_close_ int fd = -EBADF; int copyfd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; - char name[] = "/tmp/test-fdset_put_dup.XXXXXX"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_put_dup.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); @@ -37,15 +36,13 @@ TEST(fdset_put_dup) { assert_se(copyfd >= 0 && copyfd != fd); assert_se(fdset_contains(fdset, copyfd)); assert_se(!fdset_contains(fdset, fd)); - - unlink(name); } TEST(fdset_cloexec) { int fd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; int flags = -1; - char name[] = "/tmp/test-fdset_cloexec.XXXXXX"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_cloexec.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); @@ -63,8 +60,6 @@ TEST(fdset_cloexec) { flags = fcntl(fd, F_GETFD); assert_se(flags >= 0); assert_se(flags & FD_CLOEXEC); - - unlink(name); } TEST(fdset_close_others) { @@ -72,7 +67,7 @@ TEST(fdset_close_others) { int copyfd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; int flags = -1; - char name[] = "/tmp/test-fdset_close_others.XXXXXX"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_close_others.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); @@ -87,14 +82,12 @@ TEST(fdset_close_others) { assert_se(flags < 0); flags = fcntl(copyfd, F_GETFD); assert_se(flags >= 0); - - unlink(name); } TEST(fdset_remove) { _cleanup_close_ int fd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; - char name[] = "/tmp/test-fdset_remove.XXXXXX"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_remove.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); @@ -106,14 +99,12 @@ TEST(fdset_remove) { assert_se(!fdset_contains(fdset, fd)); assert_se(fcntl(fd, F_GETFD) >= 0); - - unlink(name); } TEST(fdset_iterate) { int fd = -EBADF; _cleanup_fdset_free_ FDSet *fdset = NULL; - char name[] = "/tmp/test-fdset_iterate.XXXXXX"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_iterate.XXXXXX"; int c = 0; int a; @@ -131,14 +122,12 @@ TEST(fdset_iterate) { assert_se(a == fd); } assert_se(c == 1); - - unlink(name); } TEST(fdset_isempty) { int fd; _cleanup_fdset_free_ FDSet *fdset = NULL; - char name[] = "/tmp/test-fdset_isempty.XXXXXX"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_isempty.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); @@ -149,14 +138,12 @@ TEST(fdset_isempty) { assert_se(fdset_isempty(fdset)); assert_se(fdset_put(fdset, fd) >= 0); assert_se(!fdset_isempty(fdset)); - - unlink(name); } TEST(fdset_steal_first) { int fd; _cleanup_fdset_free_ FDSet *fdset = NULL; - char name[] = "/tmp/test-fdset_steal_first.XXXXXX"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_steal_first.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); @@ -169,8 +156,6 @@ TEST(fdset_steal_first) { assert_se(fdset_steal_first(fdset) == fd); assert_se(fdset_steal_first(fdset) < 0); assert_se(fdset_put(fdset, fd) >= 0); - - unlink(name); } TEST(fdset_new_array) { diff --git a/src/test/test-hostname-setup.c b/src/test/test-hostname-setup.c index 241b197f47..94e5ece560 100644 --- a/src/test/test-hostname-setup.c +++ b/src/test/test-hostname-setup.c @@ -4,13 +4,14 @@ #include "alloc-util.h" #include "fileio.h" +#include "fs-util.h" #include "hostname-setup.h" #include "string-util.h" #include "tests.h" #include "tmpfile-util.h" TEST(read_etc_hostname) { - char path[] = "/tmp/hostname.XXXXXX"; + _cleanup_(unlink_tempfilep) char path[] = "/tmp/hostname.XXXXXX"; char *hostname; int fd; @@ -54,8 +55,6 @@ TEST(read_etc_hostname) { /* nonexisting file */ assert_se(read_etc_hostname("/non/existing", &hostname) == -ENOENT); assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */ - - unlink(path); } TEST(hostname_setup) { diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c index 6b0f2d8033..f79b05a4d1 100644 --- a/src/test/test-stat-util.c +++ b/src/test/test-stat-util.c @@ -44,8 +44,8 @@ TEST(null_or_empty_path_with_root) { TEST(files_same) { _cleanup_close_ int fd = -EBADF; - char name[] = "/tmp/test-files_same.XXXXXX"; - char name_alias[] = "/tmp/test-files_same.alias"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-files_same.XXXXXX"; + _cleanup_(unlink_tempfilep) char name_alias[] = "/tmp/test-files_same.alias"; fd = mkostemp_safe(name); assert_se(fd >= 0); @@ -55,14 +55,11 @@ TEST(files_same) { assert_se(files_same(name, name, AT_SYMLINK_NOFOLLOW)); assert_se(files_same(name, name_alias, 0)); assert_se(!files_same(name, name_alias, AT_SYMLINK_NOFOLLOW)); - - unlink(name); - unlink(name_alias); } TEST(is_symlink) { - char name[] = "/tmp/test-is_symlink.XXXXXX"; - char name_link[] = "/tmp/test-is_symlink.link"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-is_symlink.XXXXXX"; + _cleanup_(unlink_tempfilep) char name_link[] = "/tmp/test-is_symlink.link"; _cleanup_close_ int fd = -EBADF; fd = mkostemp_safe(name); @@ -72,9 +69,6 @@ TEST(is_symlink) { assert_se(is_symlink(name) == 0); assert_se(is_symlink(name_link) == 1); assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0); - - unlink(name); - unlink(name_link); } TEST(path_is_fs_type) { diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c index 8d5b24e502..a2b7101573 100644 --- a/src/test/test-terminal-util.c +++ b/src/test/test-terminal-util.c @@ -8,6 +8,7 @@ #include "alloc-util.h" #include "fd-util.h" +#include "fs-util.h" #include "macro.h" #include "path-util.h" #include "strv.h" @@ -40,7 +41,7 @@ TEST(read_one_char) { _cleanup_fclose_ FILE *file = NULL; char r; bool need_nl; - char name[] = "/tmp/test-read_one_char.XXXXXX"; + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-read_one_char.XXXXXX"; assert_se(fmkostemp_safe(name, "r+", &file) == 0); @@ -60,8 +61,6 @@ TEST(read_one_char) { assert_se(fputs("\n", file) >= 0); rewind(file); assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0); - - assert_se(unlink(name) >= 0); } TEST(getttyname_malloc) { -- cgit v1.2.1 From 7a9f8b90536ec56ada20fb39aae5373a8273be9d Mon Sep 17 00:00:00 2001 From: David Tardon Date: Mon, 24 Apr 2023 20:44:29 +0200 Subject: test-calendarspec: use _cleanup_ --- src/test/test-calendarspec.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'src/test') diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index 564983b699..db64142f01 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -8,7 +8,7 @@ #include "tests.h" static void _test_one(int line, const char *input, const char *output) { - CalendarSpec *c; + _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; _cleanup_free_ char *p = NULL, *q = NULL; usec_t u; int r; @@ -28,18 +28,17 @@ static void _test_one(int line, const char *input, const char *output) { u = now(CLOCK_REALTIME); r = calendar_spec_next_usec(c, u, &u); log_info("Next: %s", r < 0 ? STRERROR(r) : FORMAT_TIMESTAMP(u)); - calendar_spec_free(c); + c = calendar_spec_free(c); assert_se(calendar_spec_from_string(p, &c) >= 0); assert_se(calendar_spec_to_string(c, &q) >= 0); - calendar_spec_free(c); assert_se(streq(q, p)); } #define test_one(input, output) _test_one(__LINE__, input, output) static void _test_next(int line, const char *input, const char *new_tz, usec_t after, usec_t expect) { - CalendarSpec *c; + _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; usec_t u; char *old_tz; int r; @@ -66,8 +65,6 @@ static void _test_next(int line, const char *input, const char *new_tz, usec_t a else assert_se(r == -ENOENT); - calendar_spec_free(c); - assert_se(set_unset_env("TZ", old_tz, true) == 0); tzset(); } @@ -76,7 +73,7 @@ static void _test_next(int line, const char *input, const char *new_tz, usec_t a TEST(timestamp) { char buf[FORMAT_TIMESTAMP_MAX]; _cleanup_free_ char *t = NULL; - CalendarSpec *c; + _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; usec_t x, y; /* Ensure that a timestamp is also a valid calendar specification. Convert forth and back */ @@ -87,7 +84,6 @@ TEST(timestamp) { log_info("%s", buf); assert_se(calendar_spec_from_string(buf, &c) >= 0); assert_se(calendar_spec_to_string(c, &t) >= 0); - calendar_spec_free(c); log_info("%s", t); assert_se(parse_timestamp(t, &y) >= 0); @@ -95,7 +91,7 @@ TEST(timestamp) { } TEST(hourly_bug_4031) { - CalendarSpec *c; + _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; usec_t n, u, w; int r; @@ -113,8 +109,6 @@ TEST(hourly_bug_4031) { assert_se(u <= n + USEC_PER_HOUR); assert_se(u < w); assert_se(w <= u + USEC_PER_HOUR); - - calendar_spec_free(c); } TEST(calendar_spec_one) { -- cgit v1.2.1 From 8f25d740f19a7475efe5cd60aafe5d6231e8be8b Mon Sep 17 00:00:00 2001 From: David Tardon Date: Mon, 24 Apr 2023 20:48:33 +0200 Subject: test-set: use _cleanup_ --- src/test/test-set.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/test') diff --git a/src/test/test-set.c b/src/test/test-set.c index 304e3db6c9..259ffa7ce2 100644 --- a/src/test/test-set.c +++ b/src/test/test-set.c @@ -145,7 +145,8 @@ TEST(set_ensure_allocated) { } TEST(set_copy) { - Set *s, *copy; + _cleanup_(set_freep) Set *s = NULL; + _cleanup_(set_free_freep) Set *copy = NULL; char *key1, *key2, *key3, *key4; key1 = strdup("key1"); @@ -169,9 +170,6 @@ TEST(set_copy) { assert_se(copy); assert_se(set_equal(s, copy)); - - set_free(s); - set_free_free(copy); } TEST(set_ensure_put) { -- cgit v1.2.1 From 5b87bccc001be4aed1435b49225e95eabeeed720 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Mon, 24 Apr 2023 20:58:19 +0200 Subject: test-hashmap-plain: use _cleanup_ --- src/test/test-hashmap-plain.c | 79 ++++++++++++------------------------------- 1 file changed, 22 insertions(+), 57 deletions(-) (limited to 'src/test') diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c index fea20d82a6..d5e5dcaa68 100644 --- a/src/test/test-hashmap-plain.c +++ b/src/test/test-hashmap-plain.c @@ -11,8 +11,9 @@ #include "tests.h" TEST(hashmap_replace) { - Hashmap *m; - char *val1, *val2, *val3, *val4, *val5, *r; + _cleanup_(hashmap_freep) Hashmap *m = NULL; + _cleanup_free_ char *val1 = NULL, *val2 = NULL, *val3 = NULL, *val4 = NULL, *val5 = NULL; + char *r; m = hashmap_new(&string_hash_ops); @@ -39,17 +40,11 @@ TEST(hashmap_replace) { hashmap_replace(m, "key 5", val5); r = hashmap_get(m, "key 5"); assert_se(streq(r, "val5")); - - free(val1); - free(val2); - free(val3); - free(val4); - free(val5); - hashmap_free(m); } TEST(hashmap_copy) { - Hashmap *m, *copy; + _cleanup_(hashmap_freep) Hashmap *m = NULL; + _cleanup_(hashmap_free_freep) Hashmap *copy = NULL; char *val1, *val2, *val3, *val4, *r; val1 = strdup("val1"); @@ -78,14 +73,11 @@ TEST(hashmap_copy) { assert_se(streq(r, "val3")); r = hashmap_get(copy, "key 4"); assert_se(streq(r, "val4")); - - hashmap_free_free(copy); - hashmap_free(m); } TEST(hashmap_get_strv) { - Hashmap *m; - char **strv; + _cleanup_(hashmap_freep) Hashmap *m = NULL; + _cleanup_(strv_freep) char **strv = NULL; char *val1, *val2, *val3, *val4; val1 = strdup("val1"); @@ -114,14 +106,10 @@ TEST(hashmap_get_strv) { assert_se(streq(strv[1], "val2")); assert_se(streq(strv[2], "val3")); assert_se(streq(strv[3], "val4")); - - strv_free(strv); - - hashmap_free(m); } TEST(hashmap_move_one) { - Hashmap *m, *n; + _cleanup_(hashmap_free_freep) Hashmap *m = NULL, *n = NULL; char *val1, *val2, *val3, *val4, *r; val1 = strdup("val1"); @@ -154,13 +142,10 @@ TEST(hashmap_move_one) { assert_se(!r); assert_se(hashmap_move_one(n, m, "key 3") == -EEXIST); - - hashmap_free_free(m); - hashmap_free_free(n); } TEST(hashmap_move) { - Hashmap *m, *n; + _cleanup_(hashmap_free_freep) Hashmap *m = NULL, *n = NULL; char *val1, *val2, *val3, *val4, *r; val1 = strdup("val1"); @@ -196,14 +181,12 @@ TEST(hashmap_move) { assert_se(r && streq(r, "val3")); r = hashmap_get(n, "key 4"); assert_se(r && streq(r, "val4")); - - hashmap_free_free(m); - hashmap_free_free(n); } TEST(hashmap_update) { - Hashmap *m; - char *val1, *val2, *r; + _cleanup_(hashmap_freep) Hashmap *m = NULL; + _cleanup_free_ char *val1 = NULL, *val2 = NULL; + char *r; m = hashmap_new(&string_hash_ops); val1 = strdup("old_value"); @@ -222,14 +205,10 @@ TEST(hashmap_update) { assert_se(hashmap_update(m, "key 1", val2) == 0); r = hashmap_get(m, "key 1"); assert_se(streq(r, "new_value")); - - free(val1); - free(val2); - hashmap_free(m); } TEST(hashmap_put) { - Hashmap *m = NULL; + _cleanup_(hashmap_freep) Hashmap *m = NULL; int valid_hashmap_put; void *val1 = (void*) "val 1"; void *val2 = (void*) "val 2"; @@ -245,8 +224,6 @@ TEST(hashmap_put) { key1 = strdup("key 1"); assert_se(hashmap_put(m, key1, val1) == 0); assert_se(hashmap_put(m, key1, val2) == -EEXIST); - - hashmap_free(m); } TEST(hashmap_remove1) { @@ -440,7 +417,7 @@ TEST(hashmap_ensure_allocated) { } TEST(hashmap_foreach_key) { - Hashmap *m; + _cleanup_(hashmap_freep) Hashmap *m = NULL; bool key_found[] = { false, false, false, false }; const char *s; const char *key; @@ -469,12 +446,10 @@ TEST(hashmap_foreach_key) { assert_se(m); assert_se(key_found[0] && key_found[1] && key_found[2] && !key_found[3]); - - hashmap_free(m); } TEST(hashmap_foreach) { - Hashmap *m; + _cleanup_(hashmap_free_freep) Hashmap *m = NULL; bool value_found[] = { false, false, false, false }; char *val1, *val2, *val3, *val4, *s; unsigned count; @@ -488,8 +463,6 @@ TEST(hashmap_foreach) { val4 = strdup("my val4"); assert_se(val4); - m = NULL; - count = 0; HASHMAP_FOREACH(s, m) count++; @@ -520,12 +493,11 @@ TEST(hashmap_foreach) { assert_se(m); assert_se(value_found[0] && value_found[1] && value_found[2] && value_found[3]); - - hashmap_free_free(m); } TEST(hashmap_merge) { - Hashmap *m, *n; + _cleanup_(hashmap_free_freep) Hashmap *m = NULL; + _cleanup_(hashmap_freep) Hashmap *n = NULL; char *val1, *val2, *val3, *val4, *r; val1 = strdup("my val1"); @@ -553,12 +525,10 @@ TEST(hashmap_merge) { assert_se(m); assert_se(n); - hashmap_free(n); - hashmap_free_free(m); } TEST(hashmap_contains) { - Hashmap *m; + _cleanup_(hashmap_free_freep) Hashmap *m = NULL; char *val1; val1 = strdup("my val"); @@ -574,11 +544,10 @@ TEST(hashmap_contains) { assert_se(!hashmap_contains(NULL, "Key 1")); assert_se(m); - hashmap_free_free(m); } TEST(hashmap_isempty) { - Hashmap *m; + _cleanup_(hashmap_free_freep) Hashmap *m = NULL; char *val1; val1 = strdup("my val"); @@ -591,11 +560,10 @@ TEST(hashmap_isempty) { assert_se(!hashmap_isempty(m)); assert_se(m); - hashmap_free_free(m); } TEST(hashmap_size) { - Hashmap *m; + _cleanup_(hashmap_free_freep) Hashmap *m = NULL; char *val1, *val2, *val3, *val4; val1 = strdup("my val"); @@ -620,11 +588,10 @@ TEST(hashmap_size) { assert_se(m); assert_se(hashmap_size(m) == 4); assert_se(hashmap_buckets(m) >= 4); - hashmap_free_free(m); } TEST(hashmap_get) { - Hashmap *m; + _cleanup_(hashmap_free_freep) Hashmap *m = NULL; char *r; char *val; @@ -645,11 +612,10 @@ TEST(hashmap_get) { assert_se(r == NULL); assert_se(m); - hashmap_free_free(m); } TEST(hashmap_get2) { - Hashmap *m; + _cleanup_(hashmap_free_free_freep) Hashmap *m = NULL; char *r; char *val; char key_orig[] = "Key 1"; @@ -678,7 +644,6 @@ TEST(hashmap_get2) { assert_se(r == NULL); assert_se(m); - hashmap_free_free_free(m); } static void crippled_hashmap_func(const void *p, struct siphash *state) { -- cgit v1.2.1