1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
11 #define SET_SIZE 1024*4
13 static int unsigned_compare(const void *a, const void *b) {
14 const unsigned *x = a, *y = b;
25 static void test_unsigned(void) {
26 unsigned buffer[SET_SIZE], i;
31 q = prioq_new(trivial_compare_func);
34 for (i = 0; i < ELEMENTSOF(buffer); i++) {
37 u = (unsigned) rand();
39 assert_se(prioq_put(q, UINT_TO_PTR(u), NULL) >= 0);
42 qsort(buffer, ELEMENTSOF(buffer), sizeof(buffer[0]), unsigned_compare);
44 for (i = 0; i < ELEMENTSOF(buffer); i++) {
47 assert_se(prioq_size(q) == ELEMENTSOF(buffer) - i);
49 u = PTR_TO_UINT(prioq_pop(q));
50 assert_se(buffer[i] == u);
53 assert_se(prioq_isempty(q));
62 static int test_compare(const void *a, const void *b) {
63 const struct test *x = a, *y = b;
65 if (x->value < y->value)
68 if (x->value > y->value)
74 static void test_hash(const void *a, struct siphash *state) {
75 const struct test *x = a;
77 siphash24_compress(&x->value, sizeof(x->value), state);
80 static const struct hash_ops test_hash_ops = {
82 .compare = test_compare
85 static void test_struct(void) {
88 unsigned previous = 0, i;
93 q = prioq_new(test_compare);
96 s = set_new(&test_hash_ops);
99 for (i = 0; i < SET_SIZE; i++) {
102 t = new0(struct test, 1);
104 t->value = (unsigned) rand();
106 r = prioq_put(q, t, &t->idx);
110 r = set_consume(s, t);
118 t = set_steal_first(s);
122 r = prioq_remove(q, t, &t->idx);
128 for (i = 0; i < SET_SIZE * 3 / 4; i++) {
131 assert_se(prioq_size(q) == (SET_SIZE * 3 / 4) - i);
136 assert_se(previous <= t->value);
141 assert_se(prioq_isempty(q));
144 assert_se(set_isempty(s));
148 int main(int argc, char* argv[]) {