]> git.proxmox.com Git - systemd.git/blame - src/test/test-prioq.c
New upstream version 240
[systemd.git] / src / test / test-prioq.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
663996b3
MS
2
3#include <stdlib.h>
4
db2df898 5#include "alloc-util.h"
663996b3 6#include "prioq.h"
db2df898 7#include "set.h"
60f067b4 8#include "siphash24.h"
db2df898 9#include "util.h"
663996b3
MS
10
11#define SET_SIZE 1024*4
12
6e866b33
MB
13static int unsigned_compare(const unsigned *a, const unsigned *b) {
14 return CMP(*a, *b);
663996b3
MS
15}
16
17static void test_unsigned(void) {
6e866b33
MB
18 _cleanup_(prioq_freep) Prioq *q = NULL;
19 unsigned buffer[SET_SIZE], i, u, n;
663996b3
MS
20
21 srand(0);
22
6e866b33 23 assert_se(q = prioq_new(trivial_compare_func));
663996b3
MS
24
25 for (i = 0; i < ELEMENTSOF(buffer); i++) {
663996b3
MS
26 u = (unsigned) rand();
27 buffer[i] = u;
28 assert_se(prioq_put(q, UINT_TO_PTR(u), NULL) >= 0);
6e866b33
MB
29
30 n = prioq_size(q);
31 assert_se(prioq_remove(q, UINT_TO_PTR(u), &n) == 0);
663996b3
MS
32 }
33
6e866b33 34 typesafe_qsort(buffer, ELEMENTSOF(buffer), unsigned_compare);
663996b3
MS
35
36 for (i = 0; i < ELEMENTSOF(buffer); i++) {
663996b3
MS
37 assert_se(prioq_size(q) == ELEMENTSOF(buffer) - i);
38
39 u = PTR_TO_UINT(prioq_pop(q));
40 assert_se(buffer[i] == u);
41 }
42
43 assert_se(prioq_isempty(q));
663996b3
MS
44}
45
46struct test {
47 unsigned value;
48 unsigned idx;
49};
50
6e866b33
MB
51static int test_compare(const struct test *x, const struct test *y) {
52 return CMP(x->value, y->value);
663996b3
MS
53}
54
6e866b33 55static void test_hash(const struct test *x, struct siphash *state) {
6300502b 56 siphash24_compress(&x->value, sizeof(x->value), state);
663996b3
MS
57}
58
6e866b33 59DEFINE_PRIVATE_HASH_OPS(test_hash_ops, struct test, test_hash, test_compare);
5eef597e 60
663996b3 61static void test_struct(void) {
6e866b33
MB
62 _cleanup_(prioq_freep) Prioq *q = NULL;
63 _cleanup_(set_freep) Set *s = NULL;
663996b3 64 unsigned previous = 0, i;
6e866b33 65 struct test *t;
663996b3
MS
66
67 srand(0);
68
6e866b33
MB
69 assert_se(q = prioq_new((compare_func_t) test_compare));
70 assert_se(s = set_new(&test_hash_ops));
663996b3
MS
71
72 for (i = 0; i < SET_SIZE; i++) {
6e866b33 73 assert_se(t = new0(struct test, 1));
663996b3
MS
74 t->value = (unsigned) rand();
75
6e866b33 76 assert_se(prioq_put(q, t, &t->idx) >= 0);
663996b3 77
6e866b33
MB
78 if (i % 4 == 0)
79 assert_se(set_consume(s, t) >= 0);
663996b3
MS
80 }
81
6e866b33
MB
82 while ((t = set_steal_first(s))) {
83 assert_se(prioq_remove(q, t, &t->idx) == 1);
84 assert_se(prioq_remove(q, t, &t->idx) == 0);
85 assert_se(prioq_remove(q, t, NULL) == 0);
663996b3
MS
86
87 free(t);
88 }
89
90 for (i = 0; i < SET_SIZE * 3 / 4; i++) {
663996b3
MS
91 assert_se(prioq_size(q) == (SET_SIZE * 3 / 4) - i);
92
6e866b33
MB
93 assert_se(t = prioq_pop(q));
94 assert_se(prioq_remove(q, t, &t->idx) == 0);
95 assert_se(prioq_remove(q, t, NULL) == 0);
663996b3 96 assert_se(previous <= t->value);
6e866b33 97
663996b3
MS
98 previous = t->value;
99 free(t);
100 }
101
102 assert_se(prioq_isempty(q));
663996b3 103 assert_se(set_isempty(s));
663996b3
MS
104}
105
106int main(int argc, char* argv[]) {
107
108 test_unsigned();
109 test_struct();
110
111 return 0;
112}