2018-03-24 15:44:50 +08:00
|
|
|
#include "test-tool.h"
|
2013-06-07 10:13:50 +08:00
|
|
|
#include "prio-queue.h"
|
|
|
|
|
2023-02-24 14:39:27 +08:00
|
|
|
static int intcmp(const void *va, const void *vb, void *data UNUSED)
|
2013-06-07 10:13:50 +08: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-29 04:57:25 +08:00
|
|
|
int cmd__prio_queue(int argc UNUSED, const char **argv)
|
2013-06-07 10:13:50 +08:00
|
|
|
{
|
|
|
|
struct prio_queue pq = { intcmp };
|
|
|
|
|
|
|
|
while (*++argv) {
|
2018-11-01 21:46:17 +08: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 21:48:14 +08:00
|
|
|
int *v = xmalloc(sizeof(*v));
|
2013-06-07 10:13:50 +08:00
|
|
|
*v = atoi(*argv);
|
|
|
|
prio_queue_put(&pq, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 18:01:31 +08:00
|
|
|
clear_prio_queue(&pq);
|
|
|
|
|
2013-06-07 10:13:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|