summaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authorIntegral <integral@member.fsf.org>2024-10-26 00:10:22 +0200
committerGitHub <noreply@github.com>2024-10-26 00:10:22 +0200
commitddb8a639d5d1f9a0ebfa790805703cf381a42459 (patch)
treeee6fcdfcecd1ab5c5e4840d41e901d73f1ec38a2 /src/test
parentMerge pull request #34884 from poettering/run0-disconnect-fix (diff)
downloadsystemd-ddb8a639d5d1f9a0ebfa790805703cf381a42459.tar.xz
systemd-ddb8a639d5d1f9a0ebfa790805703cf381a42459.zip
tree-wide: replace for loop with FOREACH_ELEMENT or FOREACH_ARRAY macros (#34893)
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-bootspec.c12
-rw-r--r--src/test/test-chase-manual.c4
-rw-r--r--src/test/test-fileio.c6
-rw-r--r--src/test/test-hashmap-plain.c22
-rw-r--r--src/test/test-job-type.c4
-rw-r--r--src/test/test-list.c30
-rw-r--r--src/test/test-load-fragment.c21
-rw-r--r--src/test/test-ordered-set.c8
-rw-r--r--src/test/test-prioq.c8
-rw-r--r--src/test/test-set.c8
-rw-r--r--src/test/test-string-util.c6
-rw-r--r--src/test/test-terminal-util.c4
-rw-r--r--src/test/test-tpm2.c14
13 files changed, 72 insertions, 75 deletions
diff --git a/src/test/test-bootspec.c b/src/test/test-bootspec.c
index 88faa1efb2..1c3873114a 100644
--- a/src/test/test-bootspec.c
+++ b/src/test/test-bootspec.c
@@ -65,13 +65,13 @@ TEST_RET(bootspec_sort) {
ASSERT_OK(mkdtemp_malloc("/tmp/bootspec-testXXXXXX", &d));
- for (size_t i = 0; i < ELEMENTSOF(entries); i++) {
+ FOREACH_ELEMENT(entry, entries) {
_cleanup_free_ char *j = NULL;
- j = path_join(d, "/loader/entries/", entries[i].fname);
+ j = path_join(d, "/loader/entries/", entry->fname);
assert_se(j);
- ASSERT_OK(write_string_file(j, entries[i].contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755));
+ ASSERT_OK(write_string_file(j, entry->contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755));
}
ASSERT_OK(boot_config_load(&config, d, NULL));
@@ -176,13 +176,13 @@ TEST_RET(bootspec_boot_config_find_entry) {
assert_se(mkdtemp_malloc("/tmp/bootspec-testXXXXXX", &d) >= 0);
- for (size_t i = 0; i < ELEMENTSOF(entries); i++) {
+ FOREACH_ELEMENT(entry, entries) {
_cleanup_free_ char *j = NULL;
- j = path_join(d, "/loader/entries/", entries[i].fname);
+ j = path_join(d, "/loader/entries/", entry->fname);
assert_se(j);
- assert_se(write_string_file(j, entries[i].contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) >= 0);
+ assert_se(write_string_file(j, entry->contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) >= 0);
}
assert_se(boot_config_load(&config, d, NULL) >= 0);
diff --git a/src/test/test-chase-manual.c b/src/test/test-chase-manual.c
index 475f089fa8..03c0a245bd 100644
--- a/src/test/test-chase-manual.c
+++ b/src/test/test-chase-manual.c
@@ -46,8 +46,8 @@ static int parse_argv(int argc, char *argv[]) {
" %s [OPTION...] path...\n"
"Options:\n"
, argv[0]);
- for (size_t i = 0; i < ELEMENTSOF(options) - 1; i++)
- printf(" --%s\n", options[i].name);
+ FOREACH_ARRAY(option, options, ELEMENTSOF(options) - 1)
+ printf(" --%s\n", option->name);
return 0;
case ARG_ROOT:
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
index d76ef679a1..7c222b0932 100644
--- a/src/test/test-fileio.c
+++ b/src/test/test-fileio.c
@@ -892,14 +892,14 @@ TEST(read_line4) {
int r;
- for (size_t i = 0; i < ELEMENTSOF(eof_endings); i++) {
+ FOREACH_ELEMENT(ending, eof_endings) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *s = NULL;
- assert_se(f = fmemopen_unlocked((void*) eof_endings[i].string, eof_endings[i].length, "r"));
+ assert_se(f = fmemopen_unlocked((void*) ending->string, ending->length, "r"));
r = read_line(f, SIZE_MAX, &s);
- assert_se((size_t) r == eof_endings[i].length);
+ assert_se((size_t) r == ending->length);
ASSERT_STREQ(s, "foo");
assert_se(read_line(f, SIZE_MAX, NULL) == 0); /* Ensure we hit EOF */
diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c
index cb45c15087..e1485a84d4 100644
--- a/src/test/test-hashmap-plain.c
+++ b/src/test/test-hashmap-plain.c
@@ -688,7 +688,7 @@ static const struct hash_ops crippled_hashmap_ops = {
TEST(hashmap_many) {
Hashmap *h;
- unsigned i, j;
+ unsigned i;
void *v, *k;
bool slow = slow_tests_enabled();
const struct {
@@ -702,24 +702,24 @@ TEST(hashmap_many) {
log_info("/* %s (%s) */", __func__, slow ? "slow" : "fast");
- for (j = 0; j < ELEMENTSOF(tests); j++) {
+ FOREACH_ELEMENT(test, tests) {
usec_t ts = now(CLOCK_MONOTONIC), n;
- assert_se(h = hashmap_new(tests[j].ops));
+ assert_se(h = hashmap_new(test->ops));
- for (i = 1; i < tests[j].n_entries*3; i+=3) {
+ for (i = 1; i < test->n_entries*3; i+=3) {
assert_se(hashmap_put(h, UINT_TO_PTR(i), UINT_TO_PTR(i)) >= 0);
assert_se(PTR_TO_UINT(hashmap_get(h, UINT_TO_PTR(i))) == i);
}
- for (i = 1; i < tests[j].n_entries*3; i++)
+ for (i = 1; i < test->n_entries*3; i++)
assert_se(hashmap_contains(h, UINT_TO_PTR(i)) == (i % 3 == 1));
log_info("%s %u <= %u * 0.8 = %g",
- tests[j].title, hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.8);
+ test->title, hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.8);
assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.8);
- assert_se(hashmap_size(h) == tests[j].n_entries);
+ assert_se(hashmap_size(h) == test->n_entries);
while (!hashmap_isempty(h)) {
k = hashmap_first_key(h);
@@ -754,9 +754,9 @@ TEST(hashmap_free) {
log_info("/* %s (%s, %u entries) */", __func__, slow ? "slow" : "fast", n_entries);
- for (unsigned j = 0; j < ELEMENTSOF(tests); j++) {
+ FOREACH_ELEMENT(test, tests) {
ts = now(CLOCK_MONOTONIC);
- assert_se(h = hashmap_new(tests[j].ops));
+ assert_se(h = hashmap_new(test->ops));
custom_counter = 0;
for (unsigned i = 0; i < n_entries; i++) {
@@ -774,9 +774,9 @@ TEST(hashmap_free) {
hashmap_free(h);
n = now(CLOCK_MONOTONIC);
- log_info("%s test took %s", tests[j].title, FORMAT_TIMESPAN(n - ts, 0));
+ log_info("%s test took %s", test->title, FORMAT_TIMESPAN(n - ts, 0));
- assert_se(custom_counter == tests[j].expect_counter);
+ assert_se(custom_counter == test->expect_counter);
}
}
diff --git a/src/test/test-job-type.c b/src/test/test-job-type.c
index 519fff71d5..1589b7e597 100644
--- a/src/test/test-job-type.c
+++ b/src/test/test-job-type.c
@@ -11,12 +11,12 @@ int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
- for (size_t i = 0; i < ELEMENTSOF(test_states); i++) {
+ FOREACH_ELEMENT(state, test_states) {
/* fake a unit */
Service s = {
.meta.load_state = UNIT_LOADED,
.type = SERVICE_SIMPLE,
- .state = test_states[i],
+ .state = *state,
};
Unit *u = UNIT(&s);
diff --git a/src/test/test-list.c b/src/test/test-list.c
index 6c64070284..c409e08ea4 100644
--- a/src/test/test-list.c
+++ b/src/test/test-list.c
@@ -20,10 +20,10 @@ int main(int argc, const char *argv[]) {
ASSERT_NULL(head);
ASSERT_NULL(head2);
- for (i = 0; i < ELEMENTSOF(items); i++) {
- LIST_INIT(item_list, &items[i]);
- assert_se(LIST_JUST_US(item_list, &items[i]));
- assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]);
+ FOREACH_ELEMENT(item, items) {
+ LIST_INIT(item_list, item);
+ assert_se(LIST_JUST_US(item_list, item));
+ assert_se(LIST_PREPEND(item_list, head, item) == item);
}
i = 0;
@@ -183,9 +183,9 @@ int main(int argc, const char *argv[]) {
ASSERT_NULL(head);
- for (i = 0; i < ELEMENTSOF(items); i++) {
- assert_se(LIST_JUST_US(item_list, &items[i]));
- assert_se(LIST_APPEND(item_list, head, &items[i]) == &items[i]);
+ FOREACH_ELEMENT(item, items) {
+ assert_se(LIST_JUST_US(item_list, item));
+ assert_se(LIST_APPEND(item_list, head, item) == item);
}
assert_se(!LIST_JUST_US(item_list, head));
@@ -200,15 +200,15 @@ int main(int argc, const char *argv[]) {
assert_se(items[2].item_list_prev == &items[1]);
assert_se(items[3].item_list_prev == &items[2]);
- for (i = 0; i < ELEMENTSOF(items); i++)
- assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]);
+ FOREACH_ELEMENT(item, items)
+ assert_se(LIST_REMOVE(item_list, head, item) == item);
ASSERT_NULL(head);
- for (i = 0; i < ELEMENTSOF(items) / 2; i++) {
- LIST_INIT(item_list, &items[i]);
- assert_se(LIST_JUST_US(item_list, &items[i]));
- assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]);
+ FOREACH_ARRAY(item, items, ELEMENTSOF(items) / 2) {
+ LIST_INIT(item_list, item);
+ assert_se(LIST_JUST_US(item_list, item));
+ assert_se(LIST_PREPEND(item_list, head, item) == item);
}
for (i = ELEMENTSOF(items) / 2; i < ELEMENTSOF(items); i++) {
@@ -244,8 +244,8 @@ int main(int argc, const char *argv[]) {
ASSERT_NULL(head2);
assert_se(head);
- for (i = 0; i < ELEMENTSOF(items); i++)
- assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]);
+ FOREACH_ELEMENT(item, items)
+ assert_se(LIST_REMOVE(item_list, head, item) == item);
ASSERT_NULL(head);
diff --git a/src/test/test-load-fragment.c b/src/test/test-load-fragment.c
index d1a51756ed..038165aa17 100644
--- a/src/test/test-load-fragment.c
+++ b/src/test/test-load-fragment.c
@@ -912,22 +912,21 @@ TEST(config_parse_memory_limit) {
{ "MemoryMax", "10", &c.memory_max, 10 },
{ "MemoryMax", "infinity", &c.memory_max, CGROUP_LIMIT_MAX },
};
- size_t i;
int r;
- for (i = 0; i < ELEMENTSOF(limit_tests); i++) {
+ FOREACH_ELEMENT(test, limit_tests) {
c.memory_min = CGROUP_LIMIT_DUMMY;
c.memory_low = CGROUP_LIMIT_DUMMY;
c.memory_high = CGROUP_LIMIT_DUMMY;
c.memory_max = CGROUP_LIMIT_DUMMY;
r = config_parse_memory_limit(NULL, "fake", 1, "section", 1,
- limit_tests[i].limit, 1,
- limit_tests[i].value, &c, NULL);
+ test->limit, 1,
+ test->value, &c, NULL);
log_info("%s=%s\t%"PRIu64"==%"PRIu64,
- limit_tests[i].limit, limit_tests[i].value,
- *limit_tests[i].result, limit_tests[i].expected);
+ test->limit, test->value,
+ *test->result, test->expected);
assert_se(r >= 0);
- assert_se(*limit_tests[i].result == limit_tests[i].expected);
+ assert_se(*test->result == test->expected);
}
}
@@ -1029,12 +1028,12 @@ TEST(config_parse_log_filter_patterns) {
if (ERRNO_IS_NOT_SUPPORTED(dlopen_pcre2()))
return (void) log_tests_skipped("PCRE2 support is not available");
- for (size_t i = 0; i < ELEMENTSOF(regex_tests); i++) {
+ FOREACH_ELEMENT(test, regex_tests) {
assert_se(config_parse_log_filter_patterns(NULL, "fake", 1, "section", 1, "LogFilterPatterns", 1,
- regex_tests[i].regex, &c, NULL) >= 0);
+ test->regex, &c, NULL) >= 0);
- assert_se(set_size(c.log_filter_allowed_patterns) == regex_tests[i].allowed_patterns_count);
- assert_se(set_size(c.log_filter_denied_patterns) == regex_tests[i].denied_patterns_count);
+ assert_se(set_size(c.log_filter_allowed_patterns) == test->allowed_patterns_count);
+ assert_se(set_size(c.log_filter_denied_patterns) == test->denied_patterns_count);
/* Ensure `~` is properly removed */
const char *p;
diff --git a/src/test/test-ordered-set.c b/src/test/test-ordered-set.c
index bb1eefb083..45e4157296 100644
--- a/src/test/test-ordered-set.c
+++ b/src/test/test-ordered-set.c
@@ -46,11 +46,11 @@ TEST(set_free_with_hash_ops) {
assert_se(m = ordered_set_new(&item_hash_ops));
- for (size_t i = 0; i < ELEMENTSOF(items) - 1; i++)
- assert_se(ordered_set_put(m, items + i) == 1);
+ FOREACH_ARRAY(item, items, ELEMENTSOF(items) - 1)
+ assert_se(ordered_set_put(m, item) == 1);
- for (size_t i = 0; i < ELEMENTSOF(items) - 1; i++)
- assert_se(ordered_set_put(m, items + i) == 0); /* We get 0 here, because we use trivial hash
+ FOREACH_ARRAY(item, items, ELEMENTSOF(items) - 1)
+ assert_se(ordered_set_put(m, item) == 0); /* We get 0 here, because we use trivial hash
* ops. Also see below... */
m = ordered_set_free(m);
diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c
index 92c22d3b20..89c2b73e32 100644
--- a/src/test/test-prioq.c
+++ b/src/test/test-prioq.c
@@ -17,15 +17,15 @@ static int unsigned_compare(const unsigned *a, const unsigned *b) {
TEST(unsigned) {
_cleanup_(prioq_freep) Prioq *q = NULL;
- unsigned buffer[SET_SIZE], i, u, n;
+ unsigned buffer[SET_SIZE], u, n;
srand(0);
assert_se(q = prioq_new(trivial_compare_func));
- for (i = 0; i < ELEMENTSOF(buffer); i++) {
+ FOREACH_ELEMENT(i, buffer) {
u = (unsigned) rand();
- buffer[i] = u;
+ *i = u;
assert_se(prioq_put(q, UINT_TO_PTR(u), NULL) >= 0);
n = prioq_size(q);
@@ -34,7 +34,7 @@ TEST(unsigned) {
typesafe_qsort(buffer, ELEMENTSOF(buffer), unsigned_compare);
- for (i = 0; i < ELEMENTSOF(buffer); i++) {
+ for (unsigned i = 0; i < ELEMENTSOF(buffer); i++) {
assert_se(prioq_size(q) == ELEMENTSOF(buffer) - i);
u = PTR_TO_UINT(prioq_pop(q));
diff --git a/src/test/test-set.c b/src/test/test-set.c
index 23bac0255a..f1e6e9314b 100644
--- a/src/test/test-set.c
+++ b/src/test/test-set.c
@@ -37,8 +37,8 @@ TEST(set_free_with_destructor) {
struct Item items[4] = {};
assert_se(m = set_new(NULL));
- for (size_t i = 0; i < ELEMENTSOF(items) - 1; i++)
- assert_se(set_put(m, items + i) == 1);
+ FOREACH_ARRAY(item, items, ELEMENTSOF(items) - 1)
+ assert_se(set_put(m, item) == 1);
m = set_free_with_destructor(m, item_seen);
assert_se(items[0].seen == 1);
@@ -54,8 +54,8 @@ TEST(set_free_with_hash_ops) {
struct Item items[4] = {};
assert_se(m = set_new(&item_hash_ops));
- for (size_t i = 0; i < ELEMENTSOF(items) - 1; i++)
- assert_se(set_put(m, items + i) == 1);
+ FOREACH_ARRAY(item, items, ELEMENTSOF(items) - 1)
+ assert_se(set_put(m, item) == 1);
m = set_free(m);
assert_se(items[0].seen == 1);
diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c
index 4bf7548ac1..999d3bacb8 100644
--- a/src/test/test-string-util.c
+++ b/src/test/test-string-util.c
@@ -80,10 +80,10 @@ TEST(free_and_strndup) {
_cleanup_free_ char *t = NULL;
const char *prev_expected = t;
- for (unsigned i = 0; i < ELEMENTSOF(cases); i++) {
+ FOREACH_ELEMENT(c, cases) {
test_free_and_strndup_one(&t,
- cases[i].src, cases[i].len, cases[i].expected,
- !streq_ptr(cases[i].expected, prev_expected));
+ c->src, c->len, c->expected,
+ !streq_ptr(c->expected, prev_expected));
prev_expected = t;
}
}
diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c
index 9e87c4d195..ad800a4111 100644
--- a/src/test/test-terminal-util.c
+++ b/src/test/test-terminal-util.c
@@ -126,8 +126,8 @@ static const Color colors[] = {
};
TEST(colors) {
- for (size_t i = 0; i < ELEMENTSOF(colors); i++)
- printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal());
+ FOREACH_ELEMENT(color, colors)
+ printf("<%s%s%s>\n", colors->func(), color->name, ansi_normal());
}
TEST(text) {
diff --git a/src/test/test-tpm2.c b/src/test/test-tpm2.c
index 8caf51b260..e4f1f861b4 100644
--- a/src/test/test-tpm2.c
+++ b/src/test/test-tpm2.c
@@ -152,8 +152,8 @@ static void _test_pcr_selection_mask_hash(uint32_t mask, TPMI_ALG_HASH hash) {
uint32_t test_masks[] = {
0x0, 0x1, 0x100, 0x10000, 0xf0f0f0, 0xaaaaaa, 0xffffff,
};
- for (unsigned i = 0; i < ELEMENTSOF(test_masks); i++) {
- uint32_t test_mask = test_masks[i];
+ FOREACH_ELEMENT(i, test_masks) {
+ uint32_t test_mask = *i;
TPMS_PCR_SELECTION a = POISON_TPMS, b = POISON_TPMS, test_s = POISON_TPMS;
tpm2_tpms_pcr_selection_from_mask(test_mask, hash, &test_s);
@@ -181,11 +181,11 @@ static void _test_pcr_selection_mask_hash(uint32_t mask, TPMI_ALG_HASH hash) {
TEST(tpms_pcr_selection_mask_and_hash) {
TPMI_ALG_HASH HASH_ALGS[] = { TPM2_ALG_SHA1, TPM2_ALG_SHA256, };
- for (unsigned i = 0; i < ELEMENTSOF(HASH_ALGS); i++)
+ FOREACH_ELEMENT(hash, HASH_ALGS)
for (uint32_t m2 = 0; m2 <= 0xffffff; m2 += 0x50000)
for (uint32_t m1 = 0; m1 <= 0xffff; m1 += 0x500)
for (uint32_t m0 = 0; m0 <= 0xff; m0 += 0x5)
- _test_pcr_selection_mask_hash(m0 | m1 | m2, HASH_ALGS[i]);
+ _test_pcr_selection_mask_hash(m0 | m1 | m2, *hash);
}
static void _test_tpms_sw(
@@ -1191,11 +1191,9 @@ static int check_calculate_seal(Tpm2Context *c) {
calculate_seal_and_unseal(c, TPM2_SRK_HANDLE, srk_public);
TPMI_ALG_ASYM test_algs[] = { TPM2_ALG_RSA, TPM2_ALG_ECC, };
- for (unsigned i = 0; i < ELEMENTSOF(test_algs); i++) {
- TPMI_ALG_ASYM alg = test_algs[i];
-
+ FOREACH_ELEMENT(alg, test_algs) {
TPM2B_PUBLIC template = { .size = sizeof(TPMT_PUBLIC), };
- assert_se(tpm2_get_srk_template(alg, &template.publicArea) >= 0);
+ assert_se(tpm2_get_srk_template(*alg, &template.publicArea) >= 0);
_cleanup_free_ TPM2B_PUBLIC *public = NULL;
_cleanup_(tpm2_handle_freep) Tpm2Handle *handle = NULL;