summaryrefslogtreecommitdiffstats
path: root/src/test/test-prioq.c
blob: 4bfa18195e5abccd1f2b38a9db903de5c4f18733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <stdlib.h>

#include "alloc-util.h"
#include "prioq.h"
#include "set.h"
#include "siphash24.h"
#include "sort-util.h"

#define SET_SIZE 1024*4

static int unsigned_compare(const unsigned *a, const unsigned *b) {
        return CMP(*a, *b);
}

static void test_unsigned(void) {
        _cleanup_(prioq_freep) Prioq *q = NULL;
        unsigned buffer[SET_SIZE], i, u, n;

        srand(0);

        assert_se(q = prioq_new(trivial_compare_func));

        for (i = 0; i < ELEMENTSOF(buffer); i++) {
                u = (unsigned) rand();
                buffer[i] = u;
                assert_se(prioq_put(q, UINT_TO_PTR(u), NULL) >= 0);

                n = prioq_size(q);
                assert_se(prioq_remove(q, UINT_TO_PTR(u), &n) == 0);
        }

        typesafe_qsort(buffer, ELEMENTSOF(buffer), unsigned_compare);

        for (i = 0; i < ELEMENTSOF(buffer); i++) {
                assert_se(prioq_size(q) == ELEMENTSOF(buffer) - i);

                u = PTR_TO_UINT(prioq_pop(q));
                assert_se(buffer[i] == u);
        }

        assert_se(prioq_isempty(q));
}

struct test {
        unsigned value;
        unsigned idx;
};

static int test_compare(const struct test *x, const struct test *y) {
        return CMP(x->value, y->value);
}

static void test_hash(const struct test *x, struct siphash *state) {
        siphash24_compress(&x->value, sizeof(x->value), state);
}

DEFINE_PRIVATE_HASH_OPS(test_hash_ops, struct test, test_hash, test_compare);

static void test_struct(void) {
        _cleanup_(prioq_freep) Prioq *q = NULL;
        _cleanup_set_free_ Set *s = NULL;
        unsigned previous = 0, i;
        struct test *t;

        srand(0);

        assert_se(q = prioq_new((compare_func_t) test_compare));
        assert_se(s = set_new(&test_hash_ops));

        assert_se(prioq_peek(q) == NULL);
        assert_se(prioq_peek_by_index(q, 0) == NULL);
        assert_se(prioq_peek_by_index(q, 1) == NULL);
        assert_se(prioq_peek_by_index(q, (unsigned) -1) == NULL);

        for (i = 0; i < SET_SIZE; i++) {
                assert_se(t = new0(struct test, 1));
                t->value = (unsigned) rand();

                assert_se(prioq_put(q, t, &t->idx) >= 0);

                if (i % 4 == 0)
                        assert_se(set_consume(s, t) >= 0);
        }

        for (i = 0; i < SET_SIZE; i++)
                assert_se(prioq_peek_by_index(q, i));
        assert_se(prioq_peek_by_index(q, SET_SIZE) == NULL);

        unsigned count = 0;
        PRIOQ_FOREACH_ITEM(q, t) {
                assert_se(t);
                count++;
        }
        assert_se(count == SET_SIZE);

        while ((t = set_steal_first(s))) {
                assert_se(prioq_remove(q, t, &t->idx) == 1);
                assert_se(prioq_remove(q, t, &t->idx) == 0);
                assert_se(prioq_remove(q, t, NULL) == 0);

                free(t);
        }

        for (i = 0; i < SET_SIZE * 3 / 4; i++) {
                assert_se(prioq_size(q) == (SET_SIZE * 3 / 4) - i);

                assert_se(t = prioq_pop(q));
                assert_se(prioq_remove(q, t, &t->idx) == 0);
                assert_se(prioq_remove(q, t, NULL) == 0);
                assert_se(previous <= t->value);

                previous = t->value;
                free(t);
        }

        assert_se(prioq_isempty(q));
        assert_se(set_isempty(s));
}

int main(int argc, char* argv[]) {

        test_unsigned();
        test_struct();

        return 0;
}