test: Use TEST macro in more cases

This converts to TEST macro in less trivial cases. This is mostly
due to having an intro or outro before/after the actual tests.

Some notable changes:
 - add a "test" to make sure the hashmap and ordered_hashmap tests
   from different compilation units are actually run in test-hashmap.c
 - make root arg a global var in test-install-root.c
 - slightly rework an EFI specific test in test-proc-cmdline.c
 - usage of saved_argv/saved_argc in test-process-util.c
 - splitting test-rlimit-util.c into several tests
 - moving the hwdb open check into intro in test-sd-hwdb.c
 - condense several "tests" into one in test-udev-util.c
This commit is contained in:
Jan Janssen
2021-11-25 15:03:10 +01:00
parent 4f7452a8eb
commit c462e63eea
13 changed files with 299 additions and 668 deletions
+37 -133
View File
@@ -10,14 +10,10 @@
#include "time-util.h"
#include "tests.h"
void test_hashmap_funcs(void);
static void test_hashmap_replace(void) {
TEST(hashmap_replace) {
Hashmap *m;
char *val1, *val2, *val3, *val4, *val5, *r;
log_info("/* %s */", __func__);
m = hashmap_new(&string_hash_ops);
val1 = strdup("val1");
@@ -52,12 +48,10 @@ static void test_hashmap_replace(void) {
hashmap_free(m);
}
static void test_hashmap_copy(void) {
TEST(hashmap_copy) {
Hashmap *m, *copy;
char *val1, *val2, *val3, *val4, *r;
log_info("/* %s */", __func__);
val1 = strdup("val1");
assert_se(val1);
val2 = strdup("val2");
@@ -89,13 +83,11 @@ static void test_hashmap_copy(void) {
hashmap_free(m);
}
static void test_hashmap_get_strv(void) {
TEST(hashmap_get_strv) {
Hashmap *m;
char **strv;
char *val1, *val2, *val3, *val4;
log_info("/* %s */", __func__);
val1 = strdup("val1");
assert_se(val1);
val2 = strdup("val2");
@@ -128,12 +120,10 @@ static void test_hashmap_get_strv(void) {
hashmap_free(m);
}
static void test_hashmap_move_one(void) {
TEST(hashmap_move_one) {
Hashmap *m, *n;
char *val1, *val2, *val3, *val4, *r;
log_info("/* %s */", __func__);
val1 = strdup("val1");
assert_se(val1);
val2 = strdup("val2");
@@ -169,12 +159,10 @@ static void test_hashmap_move_one(void) {
hashmap_free_free(n);
}
static void test_hashmap_move(void) {
TEST(hashmap_move) {
Hashmap *m, *n;
char *val1, *val2, *val3, *val4, *r;
log_info("/* %s */", __func__);
val1 = strdup("val1");
assert_se(val1);
val2 = strdup("val2");
@@ -213,12 +201,10 @@ static void test_hashmap_move(void) {
hashmap_free_free(n);
}
static void test_hashmap_update(void) {
TEST(hashmap_update) {
Hashmap *m;
char *val1, *val2, *r;
log_info("/* %s */", __func__);
m = hashmap_new(&string_hash_ops);
val1 = strdup("old_value");
assert_se(val1);
@@ -242,15 +228,13 @@ static void test_hashmap_update(void) {
hashmap_free(m);
}
static void test_hashmap_put(void) {
TEST(hashmap_put) {
Hashmap *m = NULL;
int valid_hashmap_put;
void *val1 = (void*) "val 1";
void *val2 = (void*) "val 2";
_cleanup_free_ char* key1 = NULL;
log_info("/* %s */", __func__);
assert_se(hashmap_ensure_allocated(&m, &string_hash_ops) == 1);
assert_se(m);
@@ -265,12 +249,10 @@ static void test_hashmap_put(void) {
hashmap_free(m);
}
static void test_hashmap_remove(void) {
TEST(hashmap_remove1) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
char *r;
log_info("/* %s */", __func__);
r = hashmap_remove(NULL, "key 1");
assert_se(r == NULL);
@@ -291,7 +273,7 @@ static void test_hashmap_remove(void) {
assert_se(!hashmap_get(m, "key 1"));
}
static void test_hashmap_remove2(void) {
TEST(hashmap_remove2) {
_cleanup_hashmap_free_free_free_ Hashmap *m = NULL;
char key1[] = "key 1";
char key2[] = "key 2";
@@ -299,8 +281,6 @@ static void test_hashmap_remove2(void) {
char val2[] = "val 2";
void *r, *r2;
log_info("/* %s */", __func__);
r = hashmap_remove2(NULL, "key 1", &r2);
assert_se(r == NULL);
@@ -324,15 +304,13 @@ static void test_hashmap_remove2(void) {
assert_se(!hashmap_get(m, key1));
}
static void test_hashmap_remove_value(void) {
TEST(hashmap_remove_value) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
char *r;
char val1[] = "val 1";
char val2[] = "val 2";
log_info("/* %s */", __func__);
r = hashmap_remove_value(NULL, "key 1", val1);
assert_se(r == NULL);
@@ -360,13 +338,11 @@ static void test_hashmap_remove_value(void) {
assert_se(!hashmap_get(m, "key 1"));
}
static void test_hashmap_remove_and_put(void) {
TEST(hashmap_remove_and_put) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
int valid;
char *r;
log_info("/* %s */", __func__);
m = hashmap_new(&string_hash_ops);
assert_se(m);
@@ -392,7 +368,7 @@ static void test_hashmap_remove_and_put(void) {
assert_se(valid == -EEXIST);
}
static void test_hashmap_remove_and_replace(void) {
TEST(hashmap_remove_and_replace) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
int valid;
void *key1 = UINT_TO_PTR(1);
@@ -401,8 +377,6 @@ static void test_hashmap_remove_and_replace(void) {
void *r;
int i, j;
log_info("/* %s */", __func__);
m = hashmap_new(&trivial_hash_ops);
assert_se(m);
@@ -450,12 +424,10 @@ static void test_hashmap_remove_and_replace(void) {
}
}
static void test_hashmap_ensure_allocated(void) {
TEST(hashmap_ensure_allocated) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
int r;
log_info("/* %s */", __func__);
r = hashmap_ensure_allocated(&m, &string_hash_ops);
assert_se(r == 1);
@@ -467,7 +439,7 @@ static void test_hashmap_ensure_allocated(void) {
assert_se(r == 0);
}
static void test_hashmap_foreach_key(void) {
TEST(hashmap_foreach_key) {
Hashmap *m;
bool key_found[] = { false, false, false, false };
const char *s;
@@ -478,8 +450,6 @@ static void test_hashmap_foreach_key(void) {
"key 3\0"
"key 4\0";
log_info("/* %s */", __func__);
m = hashmap_new(&string_hash_ops);
NULSTR_FOREACH(key, key_table)
@@ -503,14 +473,12 @@ static void test_hashmap_foreach_key(void) {
hashmap_free(m);
}
static void test_hashmap_foreach(void) {
TEST(hashmap_foreach) {
Hashmap *m;
bool value_found[] = { false, false, false, false };
char *val1, *val2, *val3, *val4, *s;
unsigned count;
log_info("/* %s */", __func__);
val1 = strdup("my val1");
assert_se(val1);
val2 = strdup("my val2");
@@ -556,12 +524,10 @@ static void test_hashmap_foreach(void) {
hashmap_free_free(m);
}
static void test_hashmap_merge(void) {
TEST(hashmap_merge) {
Hashmap *m, *n;
char *val1, *val2, *val3, *val4, *r;
log_info("/* %s */", __func__);
val1 = strdup("my val1");
assert_se(val1);
val2 = strdup("my val2");
@@ -591,12 +557,10 @@ static void test_hashmap_merge(void) {
hashmap_free_free(m);
}
static void test_hashmap_contains(void) {
TEST(hashmap_contains) {
Hashmap *m;
char *val1;
log_info("/* %s */", __func__);
val1 = strdup("my val");
assert_se(val1);
@@ -613,12 +577,10 @@ static void test_hashmap_contains(void) {
hashmap_free_free(m);
}
static void test_hashmap_isempty(void) {
TEST(hashmap_isempty) {
Hashmap *m;
char *val1;
log_info("/* %s */", __func__);
val1 = strdup("my val");
assert_se(val1);
@@ -632,12 +594,10 @@ static void test_hashmap_isempty(void) {
hashmap_free_free(m);
}
static void test_hashmap_size(void) {
TEST(hashmap_size) {
Hashmap *m;
char *val1, *val2, *val3, *val4;
log_info("/* %s */", __func__);
val1 = strdup("my val");
assert_se(val1);
val2 = strdup("my val");
@@ -663,13 +623,11 @@ static void test_hashmap_size(void) {
hashmap_free_free(m);
}
static void test_hashmap_get(void) {
TEST(hashmap_get) {
Hashmap *m;
char *r;
char *val;
log_info("/* %s */", __func__);
val = strdup("my val");
assert_se(val);
@@ -690,15 +648,13 @@ static void test_hashmap_get(void) {
hashmap_free_free(m);
}
static void test_hashmap_get2(void) {
TEST(hashmap_get2) {
Hashmap *m;
char *r;
char *val;
char key_orig[] = "Key 1";
void *key_copy;
log_info("/* %s */", __func__);
val = strdup("my val");
assert_se(val);
@@ -734,7 +690,7 @@ static const struct hash_ops crippled_hashmap_ops = {
.compare = trivial_compare_func,
};
static void test_hashmap_many(void) {
TEST(hashmap_many) {
Hashmap *h;
unsigned i, j;
void *v, *k;
@@ -785,7 +741,7 @@ static void test_hashmap_many(void) {
extern unsigned custom_counter;
extern const struct hash_ops boring_hash_ops, custom_hash_ops;
static void test_hashmap_free(void) {
TEST(hashmap_free) {
Hashmap *h;
bool slow = slow_tests_enabled();
usec_t ts, n;
@@ -835,13 +791,11 @@ static void item_seen(Item *item) {
item->seen++;
}
static void test_hashmap_free_with_destructor(void) {
TEST(hashmap_free_with_destructor) {
Hashmap *m;
struct Item items[4] = {};
unsigned i;
log_info("/* %s */", __func__);
assert_se(m = hashmap_new(NULL));
for (i = 0; i < ELEMENTSOF(items) - 1; i++)
assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1);
@@ -853,11 +807,9 @@ static void test_hashmap_free_with_destructor(void) {
assert_se(items[3].seen == 0);
}
static void test_hashmap_first(void) {
TEST(hashmap_first) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
log_info("/* %s */", __func__);
m = hashmap_new(&string_hash_ops);
assert_se(m);
@@ -872,11 +824,9 @@ static void test_hashmap_first(void) {
#endif
}
static void test_hashmap_first_key(void) {
TEST(hashmap_first_key) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
log_info("/* %s */", __func__);
m = hashmap_new(&string_hash_ops);
assert_se(m);
@@ -891,11 +841,9 @@ static void test_hashmap_first_key(void) {
#endif
}
static void test_hashmap_steal_first_key(void) {
TEST(hashmap_steal_first_key) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
log_info("/* %s */", __func__);
m = hashmap_new(&string_hash_ops);
assert_se(m);
@@ -906,13 +854,11 @@ static void test_hashmap_steal_first_key(void) {
assert_se(hashmap_isempty(m));
}
static void test_hashmap_steal_first(void) {
TEST(hashmap_steal_first) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
int seen[3] = {};
char *val;
log_info("/* %s */", __func__);
m = hashmap_new(&string_hash_ops);
assert_se(m);
@@ -928,11 +874,9 @@ static void test_hashmap_steal_first(void) {
assert_se(hashmap_isempty(m));
}
static void test_hashmap_clear_free_free(void) {
TEST(hashmap_clear_free_free) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
log_info("/* %s */", __func__);
m = hashmap_new(&string_hash_ops);
assert_se(m);
@@ -954,11 +898,9 @@ static void test_hashmap_clear_free_free(void) {
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(test_hash_ops_key, char, string_hash_func, string_compare_func, free);
DEFINE_PRIVATE_HASH_OPS_FULL(test_hash_ops_full, char, string_hash_func, string_compare_func, free, char, free);
static void test_hashmap_clear_free_with_destructor(void) {
TEST(hashmap_clear_free_with_destructor) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
log_info("/* %s */", __func__);
m = hashmap_new(&test_hash_ops_key);
assert_se(m);
@@ -981,11 +923,9 @@ static void test_hashmap_clear_free_with_destructor(void) {
assert_se(hashmap_isempty(m));
}
static void test_hashmap_reserve(void) {
TEST(hashmap_reserve) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
log_info("/* %s */", __func__);
m = hashmap_new(&string_hash_ops);
assert_se(hashmap_reserve(m, 1) == 0);
@@ -1000,11 +940,9 @@ static void test_hashmap_reserve(void) {
assert_se(hashmap_reserve(m, UINT_MAX - 1) == -ENOMEM);
}
static void test_path_hashmap(void) {
TEST(path_hashmap) {
_cleanup_hashmap_free_ Hashmap *h = NULL;
log_info("/* %s */", __func__);
assert_se(h = hashmap_new(&path_hash_ops));
assert_se(hashmap_put(h, "foo", INT_TO_PTR(1)) >= 0);
@@ -1035,12 +973,10 @@ static void test_path_hashmap(void) {
assert_se(hashmap_get(h, "foo././//ba.r////.quux///.//.") == INT_TO_PTR(9));
}
static void test_string_strv_hashmap(void) {
TEST(string_strv_hashmap) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
char **s;
log_info("/* %s */", __func__);
assert_se(string_strv_hashmap_put(&m, "foo", "bar") == 1);
assert_se(string_strv_hashmap_put(&m, "foo", "bar") == 0);
assert_se(string_strv_hashmap_put(&m, "foo", "BAR") == 1);
@@ -1062,40 +998,8 @@ static void test_string_strv_hashmap(void) {
assert_se(strv_equal(s, STRV_MAKE("bar", "BAR")));
}
void test_hashmap_funcs(void) {
log_info("/************ %s ************/", __func__);
test_hashmap_copy();
test_hashmap_get_strv();
test_hashmap_move_one();
test_hashmap_move();
test_hashmap_replace();
test_hashmap_update();
test_hashmap_put();
test_hashmap_remove();
test_hashmap_remove2();
test_hashmap_remove_value();
test_hashmap_remove_and_put();
test_hashmap_remove_and_replace();
test_hashmap_ensure_allocated();
test_hashmap_foreach();
test_hashmap_foreach_key();
test_hashmap_contains();
test_hashmap_merge();
test_hashmap_isempty();
test_hashmap_get();
test_hashmap_get2();
test_hashmap_size();
test_hashmap_many();
test_hashmap_free();
test_hashmap_free_with_destructor();
test_hashmap_first();
test_hashmap_first_key();
test_hashmap_steal_first_key();
test_hashmap_steal_first();
test_hashmap_clear_free_free();
test_hashmap_clear_free_with_destructor();
test_hashmap_reserve();
test_path_hashmap();
test_string_strv_hashmap();
/* Signal to test-hashmap.c that tests from this compilation unit were run. */
extern int n_extern_tests_run;
TEST(ensure_extern_hashmap_tests) {
n_extern_tests_run++;
}