2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
25 #include "siphash24.h"
28 #define SET_SIZE 1024*4
30 static int unsigned_compare(const void *a, const void *b) {
31 const unsigned *x = a, *y = b;
42 static void test_unsigned(void) {
43 unsigned buffer[SET_SIZE], i;
48 q = prioq_new(trivial_compare_func);
51 for (i = 0; i < ELEMENTSOF(buffer); i++) {
54 u = (unsigned) rand();
56 assert_se(prioq_put(q, UINT_TO_PTR(u), NULL) >= 0);
59 qsort(buffer, ELEMENTSOF(buffer), sizeof(buffer[0]), unsigned_compare);
61 for (i = 0; i < ELEMENTSOF(buffer); i++) {
64 assert_se(prioq_size(q) == ELEMENTSOF(buffer) - i);
66 u = PTR_TO_UINT(prioq_pop(q));
67 assert_se(buffer[i] == u);
70 assert_se(prioq_isempty(q));
79 static int test_compare(const void *a, const void *b) {
80 const struct test *x = a, *y = b;
82 if (x->value < y->value)
85 if (x->value > y->value)
91 static void test_hash(const void *a, struct siphash *state) {
92 const struct test *x = a;
94 siphash24_compress(&x->value, sizeof(x->value), state);
97 static const struct hash_ops test_hash_ops = {
99 .compare = test_compare
102 static void test_struct(void) {
105 unsigned previous = 0, i;
110 q = prioq_new(test_compare);
113 s = set_new(&test_hash_ops);
116 for (i = 0; i < SET_SIZE; i++) {
119 t = new0(struct test, 1);
121 t->value = (unsigned) rand();
123 r = prioq_put(q, t, &t->idx);
127 r = set_consume(s, t);
135 t = set_steal_first(s);
139 r = prioq_remove(q, t, &t->idx);
145 for (i = 0; i < SET_SIZE * 3 / 4; i++) {
148 assert_se(prioq_size(q) == (SET_SIZE * 3 / 4) - i);
153 assert_se(previous <= t->value);
158 assert_se(prioq_isempty(q));
161 assert_se(set_isempty(s));
165 int main(int argc, char* argv[]) {