]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-prioq.c
89c2b73e320ef699d73d060bdbc07ce6a8601b18
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
12 #define SET_SIZE 1024*4
14 static int unsigned_compare(const unsigned *a
, const unsigned *b
) {
19 _cleanup_(prioq_freep
) Prioq
*q
= NULL
;
20 unsigned buffer
[SET_SIZE
], u
, n
;
24 assert_se(q
= prioq_new(trivial_compare_func
));
26 FOREACH_ELEMENT(i
, buffer
) {
27 u
= (unsigned) rand();
29 assert_se(prioq_put(q
, UINT_TO_PTR(u
), NULL
) >= 0);
32 assert_se(prioq_remove(q
, UINT_TO_PTR(u
), &n
) == 0);
35 typesafe_qsort(buffer
, ELEMENTSOF(buffer
), unsigned_compare
);
37 for (unsigned i
= 0; i
< ELEMENTSOF(buffer
); i
++) {
38 assert_se(prioq_size(q
) == ELEMENTSOF(buffer
) - i
);
40 u
= PTR_TO_UINT(prioq_pop(q
));
41 assert_se(buffer
[i
] == u
);
44 assert_se(prioq_isempty(q
));
52 static int test_compare(const struct test
*x
, const struct test
*y
) {
53 return CMP(x
->value
, y
->value
);
56 static void test_hash(const struct test
*x
, struct siphash
*state
) {
57 siphash24_compress_typesafe(x
->value
, state
);
60 DEFINE_PRIVATE_HASH_OPS(test_hash_ops
, struct test
, test_hash
, test_compare
);
63 _cleanup_(prioq_freep
) Prioq
*q
= NULL
;
64 _cleanup_set_free_ Set
*s
= NULL
;
65 unsigned previous
= 0, i
;
70 assert_se(q
= prioq_new((compare_func_t
) test_compare
));
71 assert_se(s
= set_new(&test_hash_ops
));
73 ASSERT_NULL(prioq_peek(q
));
74 ASSERT_NULL(prioq_peek_by_index(q
, 0));
75 ASSERT_NULL(prioq_peek_by_index(q
, 1));
76 ASSERT_NULL(prioq_peek_by_index(q
, UINT_MAX
));
78 for (i
= 0; i
< SET_SIZE
; i
++) {
79 assert_se(t
= new0(struct test
, 1));
80 t
->value
= (unsigned) rand();
82 assert_se(prioq_put(q
, t
, &t
->idx
) >= 0);
85 assert_se(set_consume(s
, t
) >= 0);
88 for (i
= 0; i
< SET_SIZE
; i
++)
89 assert_se(prioq_peek_by_index(q
, i
));
90 ASSERT_NULL(prioq_peek_by_index(q
, SET_SIZE
));
93 PRIOQ_FOREACH_ITEM(q
, t
) {
97 assert_se(count
== SET_SIZE
);
99 while ((t
= set_steal_first(s
))) {
100 assert_se(prioq_remove(q
, t
, &t
->idx
) == 1);
101 assert_se(prioq_remove(q
, t
, &t
->idx
) == 0);
102 assert_se(prioq_remove(q
, t
, NULL
) == 0);
107 for (i
= 0; i
< SET_SIZE
* 3 / 4; i
++) {
108 assert_se(prioq_size(q
) == (SET_SIZE
* 3 / 4) - i
);
110 assert_se(t
= prioq_pop(q
));
111 assert_se(prioq_remove(q
, t
, &t
->idx
) == 0);
112 assert_se(prioq_remove(q
, t
, NULL
) == 0);
113 assert_se(previous
<= t
->value
);
119 assert_se(prioq_isempty(q
));
120 assert_se(set_isempty(s
));
123 DEFINE_TEST_MAIN(LOG_INFO
);