]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-prioq.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
11 #define SET_SIZE 1024*4
13 static int unsigned_compare(const unsigned *a
, const unsigned *b
) {
17 static void test_unsigned(void) {
18 _cleanup_(prioq_freep
) Prioq
*q
= NULL
;
19 unsigned buffer
[SET_SIZE
], i
, u
, n
;
23 assert_se(q
= prioq_new(trivial_compare_func
));
25 for (i
= 0; i
< ELEMENTSOF(buffer
); i
++) {
26 u
= (unsigned) rand();
28 assert_se(prioq_put(q
, UINT_TO_PTR(u
), NULL
) >= 0);
31 assert_se(prioq_remove(q
, UINT_TO_PTR(u
), &n
) == 0);
34 typesafe_qsort(buffer
, ELEMENTSOF(buffer
), unsigned_compare
);
36 for (i
= 0; i
< ELEMENTSOF(buffer
); i
++) {
37 assert_se(prioq_size(q
) == ELEMENTSOF(buffer
) - i
);
39 u
= PTR_TO_UINT(prioq_pop(q
));
40 assert_se(buffer
[i
] == u
);
43 assert_se(prioq_isempty(q
));
51 static int test_compare(const struct test
*x
, const struct test
*y
) {
52 return CMP(x
->value
, y
->value
);
55 static void test_hash(const struct test
*x
, struct siphash
*state
) {
56 siphash24_compress(&x
->value
, sizeof(x
->value
), state
);
59 DEFINE_PRIVATE_HASH_OPS(test_hash_ops
, struct test
, test_hash
, test_compare
);
61 static void test_struct(void) {
62 _cleanup_(prioq_freep
) Prioq
*q
= NULL
;
63 _cleanup_(set_freep
) Set
*s
= NULL
;
64 unsigned previous
= 0, i
;
69 assert_se(q
= prioq_new((compare_func_t
) test_compare
));
70 assert_se(s
= set_new(&test_hash_ops
));
72 assert_se(prioq_peek(q
) == NULL
);
73 assert_se(prioq_peek_by_index(q
, 0) == NULL
);
74 assert_se(prioq_peek_by_index(q
, 1) == NULL
);
75 assert_se(prioq_peek_by_index(q
, (unsigned) -1) == NULL
);
77 for (i
= 0; i
< SET_SIZE
; i
++) {
78 assert_se(t
= new0(struct test
, 1));
79 t
->value
= (unsigned) rand();
81 assert_se(prioq_put(q
, t
, &t
->idx
) >= 0);
84 assert_se(set_consume(s
, t
) >= 0);
87 for (i
= 0; i
< SET_SIZE
; i
++)
88 assert_se(prioq_peek_by_index(q
, i
));
89 assert_se(prioq_peek_by_index(q
, SET_SIZE
) == NULL
);
92 PRIOQ_FOREACH_ITEM(q
, t
) {
96 assert_se(count
== SET_SIZE
);
98 while ((t
= set_steal_first(s
))) {
99 assert_se(prioq_remove(q
, t
, &t
->idx
) == 1);
100 assert_se(prioq_remove(q
, t
, &t
->idx
) == 0);
101 assert_se(prioq_remove(q
, t
, NULL
) == 0);
106 for (i
= 0; i
< SET_SIZE
* 3 / 4; i
++) {
107 assert_se(prioq_size(q
) == (SET_SIZE
* 3 / 4) - i
);
109 assert_se(t
= prioq_pop(q
));
110 assert_se(prioq_remove(q
, t
, &t
->idx
) == 0);
111 assert_se(prioq_remove(q
, t
, NULL
) == 0);
112 assert_se(previous
<= t
->value
);
118 assert_se(prioq_isempty(q
));
119 assert_se(set_isempty(s
));
122 int main(int argc
, char* argv
[]) {