]>
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
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
26 #include "siphash24.h"
29 #define SET_SIZE 1024*4
31 static int unsigned_compare(const void *a
, const void *b
) {
32 const unsigned *x
= a
, *y
= b
;
43 static void test_unsigned(void) {
44 unsigned buffer
[SET_SIZE
], i
;
49 q
= prioq_new(trivial_compare_func
);
52 for (i
= 0; i
< ELEMENTSOF(buffer
); i
++) {
55 u
= (unsigned) rand();
57 assert_se(prioq_put(q
, UINT_TO_PTR(u
), NULL
) >= 0);
60 qsort(buffer
, ELEMENTSOF(buffer
), sizeof(buffer
[0]), unsigned_compare
);
62 for (i
= 0; i
< ELEMENTSOF(buffer
); i
++) {
65 assert_se(prioq_size(q
) == ELEMENTSOF(buffer
) - i
);
67 u
= PTR_TO_UINT(prioq_pop(q
));
68 assert_se(buffer
[i
] == u
);
71 assert_se(prioq_isempty(q
));
80 static int test_compare(const void *a
, const void *b
) {
81 const struct test
*x
= a
, *y
= b
;
83 if (x
->value
< y
->value
)
86 if (x
->value
> y
->value
)
92 static void test_hash(const void *a
, struct siphash
*state
) {
93 const struct test
*x
= a
;
95 siphash24_compress(&x
->value
, sizeof(x
->value
), state
);
98 static const struct hash_ops test_hash_ops
= {
100 .compare
= test_compare
103 static void test_struct(void) {
106 unsigned previous
= 0, i
;
111 q
= prioq_new(test_compare
);
114 s
= set_new(&test_hash_ops
);
117 for (i
= 0; i
< SET_SIZE
; i
++) {
120 t
= new0(struct test
, 1);
122 t
->value
= (unsigned) rand();
124 r
= prioq_put(q
, t
, &t
->idx
);
128 r
= set_consume(s
, t
);
136 t
= set_steal_first(s
);
140 r
= prioq_remove(q
, t
, &t
->idx
);
146 for (i
= 0; i
< SET_SIZE
* 3 / 4; i
++) {
149 assert_se(prioq_size(q
) == (SET_SIZE
* 3 / 4) - i
);
154 assert_se(previous
<= t
->value
);
159 assert_se(prioq_isempty(q
));
162 assert_se(set_isempty(s
));
166 int main(int argc
, char* argv
[]) {