]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-prioq.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
10 #include "alloc-util.h"
13 #include "siphash24.h"
16 #define SET_SIZE 1024*4
18 static int unsigned_compare(const void *a
, const void *b
) {
19 const unsigned *x
= a
, *y
= b
;
30 static void test_unsigned(void) {
31 unsigned buffer
[SET_SIZE
], i
;
36 q
= prioq_new(trivial_compare_func
);
39 for (i
= 0; i
< ELEMENTSOF(buffer
); i
++) {
42 u
= (unsigned) rand();
44 assert_se(prioq_put(q
, UINT_TO_PTR(u
), NULL
) >= 0);
47 qsort(buffer
, ELEMENTSOF(buffer
), sizeof(buffer
[0]), unsigned_compare
);
49 for (i
= 0; i
< ELEMENTSOF(buffer
); i
++) {
52 assert_se(prioq_size(q
) == ELEMENTSOF(buffer
) - i
);
54 u
= PTR_TO_UINT(prioq_pop(q
));
55 assert_se(buffer
[i
] == u
);
58 assert_se(prioq_isempty(q
));
67 static int test_compare(const void *a
, const void *b
) {
68 const struct test
*x
= a
, *y
= b
;
70 if (x
->value
< y
->value
)
73 if (x
->value
> y
->value
)
79 static void test_hash(const void *a
, struct siphash
*state
) {
80 const struct test
*x
= a
;
82 siphash24_compress(&x
->value
, sizeof(x
->value
), state
);
85 static const struct hash_ops test_hash_ops
= {
87 .compare
= test_compare
90 static void test_struct(void) {
93 unsigned previous
= 0, i
;
98 q
= prioq_new(test_compare
);
101 s
= set_new(&test_hash_ops
);
104 for (i
= 0; i
< SET_SIZE
; i
++) {
107 t
= new0(struct test
, 1);
109 t
->value
= (unsigned) rand();
111 r
= prioq_put(q
, t
, &t
->idx
);
115 r
= set_consume(s
, t
);
123 t
= set_steal_first(s
);
127 r
= prioq_remove(q
, t
, &t
->idx
);
133 for (i
= 0; i
< SET_SIZE
* 3 / 4; i
++) {
136 assert_se(prioq_size(q
) == (SET_SIZE
* 3 / 4) - i
);
141 assert_se(previous
<= t
->value
);
146 assert_se(prioq_isempty(q
));
149 assert_se(set_isempty(s
));
153 int main(int argc
, char* argv
[]) {