2018-03-24 07:44:50 +00:00
|
|
|
#include "test-tool.h"
|
2013-06-07 02:13:50 +00:00
|
|
|
#include "prio-queue.h"
|
|
|
|
|
2023-02-24 06:39:27 +00:00
|
|
|
static int intcmp(const void *va, const void *vb, void *data UNUSED)
|
2013-06-07 02:13:50 +00:00
|
|
|
{
|
|
|
|
const int *a = va, *b = vb;
|
|
|
|
return *a - *b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show(int *v)
|
|
|
|
{
|
|
|
|
if (!v)
|
|
|
|
printf("NULL\n");
|
|
|
|
else
|
|
|
|
printf("%d\n", *v);
|
|
|
|
free(v);
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:57:25 +00:00
|
|
|
int cmd__prio_queue(int argc UNUSED, const char **argv)
|
2013-06-07 02:13:50 +00:00
|
|
|
{
|
|
|
|
struct prio_queue pq = { intcmp };
|
|
|
|
|
|
|
|
while (*++argv) {
|
2018-11-01 13:46:17 +00:00
|
|
|
if (!strcmp(*argv, "get")) {
|
|
|
|
void *peek = prio_queue_peek(&pq);
|
|
|
|
void *get = prio_queue_get(&pq);
|
|
|
|
if (peek != get)
|
|
|
|
BUG("peek and get results do not match");
|
|
|
|
show(get);
|
|
|
|
} else if (!strcmp(*argv, "dump")) {
|
|
|
|
void *peek;
|
|
|
|
void *get;
|
|
|
|
while ((peek = prio_queue_peek(&pq))) {
|
|
|
|
get = prio_queue_get(&pq);
|
|
|
|
if (peek != get)
|
|
|
|
BUG("peek and get results do not match");
|
|
|
|
show(get);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(*argv, "stack")) {
|
|
|
|
pq.compare = NULL;
|
|
|
|
} else {
|
2019-04-11 13:48:14 +00:00
|
|
|
int *v = xmalloc(sizeof(*v));
|
2013-06-07 02:13:50 +00:00
|
|
|
*v = atoi(*argv);
|
|
|
|
prio_queue_put(&pq, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 10:01:31 +00:00
|
|
|
clear_prio_queue(&pq);
|
|
|
|
|
2013-06-07 02:13:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|