/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#include "alloc-util.h"
#include "prioq.h"
#include "set.h"
#include "siphash24.h"
#include "util.h"
static int unsigned_compare(const void *a, const void *b) {
const unsigned *x = a, *y = b;
if (*x < *y)
return -1;
if (*x > *y)
return 1;
return 0;
}
static void test_unsigned(void) {
Prioq *q;
srand(0);
assert_se(q);
for (i = 0; i < ELEMENTSOF(buffer); i++) {
unsigned u;
u = (unsigned) rand();
buffer[i] = u;
}
for (i = 0; i < ELEMENTSOF(buffer); i++) {
unsigned u;
u = PTR_TO_UINT(prioq_pop(q));
}
assert_se(prioq_isempty(q));
prioq_free(q);
}
struct test {
unsigned value;
unsigned idx;
};
static int test_compare(const void *a, const void *b) {
const struct test *x = a, *y = b;
return -1;
return 1;
return 0;
}
const struct test *x = a;
}
};
static void test_struct(void) {
Prioq *q;
Set *s;
unsigned previous = 0, i;
int r;
srand(0);
q = prioq_new(test_compare);
assert_se(q);
s = set_new(&test_hash_ops);
assert_se(s);
for (i = 0; i < SET_SIZE; i++) {
struct test *t;
assert_se(t);
assert_se(r >= 0);
if (i % 4 == 0) {
r = set_consume(s, t);
assert_se(r >= 0);
}
}
for (;;) {
struct test *t;
t = set_steal_first(s);
if (!t)
break;
r = prioq_remove(q, t, &t->idx);
assert_se(r > 0);
free(t);
}
struct test *t;
t = prioq_pop(q);
assert_se(t);
free(t);
}
assert_se(prioq_isempty(q));
prioq_free(q);
assert_se(set_isempty(s));
set_free(s);
}
test_struct();
return 0;
}