summaryrefslogtreecommitdiff
path: root/src/test/test-prioq.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2018-10-16 22:17:04 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2018-10-16 22:17:08 +0900
commitd13b5f5a85f6070586e59f6dd2a8b2c9358d5c7e (patch)
tree17695872187f15dd1b82265b971a3ea705fb4d87 /src/test/test-prioq.c
parentbbddd2b8d9b06bfef8b3eb58f53144fa64995083 (diff)
downloadsystemd-d13b5f5a85f6070586e59f6dd2a8b2c9358d5c7e.tar.gz
util,test: introduce cleanup function prioq_freep()
This also simplifies test-prioq.c.
Diffstat (limited to 'src/test/test-prioq.c')
-rw-r--r--src/test/test-prioq.c61
1 files changed, 17 insertions, 44 deletions
diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c
index 57147a166f..8aff66978e 100644
--- a/src/test/test-prioq.c
+++ b/src/test/test-prioq.c
@@ -15,17 +15,14 @@ static int unsigned_compare(const unsigned *a, const unsigned *b) {
}
static void test_unsigned(void) {
- unsigned buffer[SET_SIZE], i;
- Prioq *q;
+ _cleanup_(prioq_freep) Prioq *q = NULL;
+ unsigned buffer[SET_SIZE], i, u;
srand(0);
- q = prioq_new(trivial_compare_func);
- assert_se(q);
+ assert_se(q = prioq_new(trivial_compare_func));
for (i = 0; i < ELEMENTSOF(buffer); i++) {
- unsigned u;
-
u = (unsigned) rand();
buffer[i] = u;
assert_se(prioq_put(q, UINT_TO_PTR(u), NULL) >= 0);
@@ -34,8 +31,6 @@ static void test_unsigned(void) {
typesafe_qsort(buffer, ELEMENTSOF(buffer), unsigned_compare);
for (i = 0; i < ELEMENTSOF(buffer); i++) {
- unsigned u;
-
assert_se(prioq_size(q) == ELEMENTSOF(buffer) - i);
u = PTR_TO_UINT(prioq_pop(q));
@@ -43,7 +38,6 @@ static void test_unsigned(void) {
}
assert_se(prioq_isempty(q));
- prioq_free(q);
}
struct test {
@@ -69,66 +63,45 @@ static const struct hash_ops test_hash_ops = {
};
static void test_struct(void) {
- Prioq *q;
- Set *s;
+ _cleanup_(prioq_freep) Prioq *q = NULL;
+ _cleanup_(set_freep) Set *s = NULL;
unsigned previous = 0, i;
- int r;
+ struct test *t;
srand(0);
- q = prioq_new(test_compare);
- assert_se(q);
-
- s = set_new(&test_hash_ops);
- assert_se(s);
+ assert_se(q = prioq_new(test_compare));
+ assert_se(s = set_new(&test_hash_ops));
for (i = 0; i < SET_SIZE; i++) {
- struct test *t;
-
- t = new0(struct test, 1);
- assert_se(t);
+ assert_se(t = new0(struct test, 1));
t->value = (unsigned) rand();
- r = prioq_put(q, t, &t->idx);
- assert_se(r >= 0);
+ assert_se(prioq_put(q, t, &t->idx) >= 0);
- if (i % 4 == 0) {
- r = set_consume(s, t);
- assert_se(r >= 0);
- }
+ if (i % 4 == 0)
+ assert_se(set_consume(s, t) >= 0);
}
- for (;;) {
- struct test *t;
-
- t = set_steal_first(s);
- if (!t)
- break;
-
- r = prioq_remove(q, t, &t->idx);
- assert_se(r > 0);
+ while ((t = set_steal_first(s))) {
+ assert_se(prioq_remove(q, t, &t->idx) == 1);
+ assert_se(prioq_remove(q, t, &t->idx) == 0);
free(t);
}
for (i = 0; i < SET_SIZE * 3 / 4; i++) {
- struct test *t;
-
assert_se(prioq_size(q) == (SET_SIZE * 3 / 4) - i);
- t = prioq_pop(q);
- assert_se(t);
-
+ assert_se(t = prioq_pop(q));
assert_se(previous <= t->value);
+
previous = t->value;
free(t);
}
assert_se(prioq_isempty(q));
- prioq_free(q);
-
assert_se(set_isempty(s));
- set_free(s);
}
int main(int argc, char* argv[]) {