]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-prioq.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Lennart Poettering
8 #include "alloc-util.h"
11 #include "siphash24.h"
14 #define SET_SIZE 1024*4
16 static int unsigned_compare(const void *a
, const void *b
) {
17 const unsigned *x
= a
, *y
= b
;
28 static void test_unsigned(void) {
29 unsigned buffer
[SET_SIZE
], i
;
34 q
= prioq_new(trivial_compare_func
);
37 for (i
= 0; i
< ELEMENTSOF(buffer
); i
++) {
40 u
= (unsigned) rand();
42 assert_se(prioq_put(q
, UINT_TO_PTR(u
), NULL
) >= 0);
45 qsort(buffer
, ELEMENTSOF(buffer
), sizeof(buffer
[0]), unsigned_compare
);
47 for (i
= 0; i
< ELEMENTSOF(buffer
); i
++) {
50 assert_se(prioq_size(q
) == ELEMENTSOF(buffer
) - i
);
52 u
= PTR_TO_UINT(prioq_pop(q
));
53 assert_se(buffer
[i
] == u
);
56 assert_se(prioq_isempty(q
));
65 static int test_compare(const void *a
, const void *b
) {
66 const struct test
*x
= a
, *y
= b
;
68 if (x
->value
< y
->value
)
71 if (x
->value
> y
->value
)
77 static void test_hash(const void *a
, struct siphash
*state
) {
78 const struct test
*x
= a
;
80 siphash24_compress(&x
->value
, sizeof(x
->value
), state
);
83 static const struct hash_ops test_hash_ops
= {
85 .compare
= test_compare
88 static void test_struct(void) {
91 unsigned previous
= 0, i
;
96 q
= prioq_new(test_compare
);
99 s
= set_new(&test_hash_ops
);
102 for (i
= 0; i
< SET_SIZE
; i
++) {
105 t
= new0(struct test
, 1);
107 t
->value
= (unsigned) rand();
109 r
= prioq_put(q
, t
, &t
->idx
);
113 r
= set_consume(s
, t
);
121 t
= set_steal_first(s
);
125 r
= prioq_remove(q
, t
, &t
->idx
);
131 for (i
= 0; i
< SET_SIZE
* 3 / 4; i
++) {
134 assert_se(prioq_size(q
) == (SET_SIZE
* 3 / 4) - i
);
139 assert_se(previous
<= t
->value
);
144 assert_se(prioq_isempty(q
));
147 assert_se(set_isempty(s
));
151 int main(int argc
, char* argv
[]) {