mirror of
https://github.com/git/git
synced 2024-11-05 18:59:29 +00:00
aca4240f6a
When consuming a priority queue, it can be convenient to inspect the next object that will be dequeued without actually dequeueing it. Our existing library did not have such a 'peek' operation, so add it as prio_queue_peek(). Add a reference-level comparison in t/helper/test-prio-queue.c so this method is exercised by t0009-prio-queue.sh. Further, add a test that checks the behavior when the compare function is NULL (i.e. the queue becomes a stack). Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
50 lines
973 B
C
50 lines
973 B
C
#include "test-tool.h"
|
|
#include "cache.h"
|
|
#include "prio-queue.h"
|
|
|
|
static int intcmp(const void *va, const void *vb, void *data)
|
|
{
|
|
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);
|
|
}
|
|
|
|
int cmd__prio_queue(int argc, const char **argv)
|
|
{
|
|
struct prio_queue pq = { intcmp };
|
|
|
|
while (*++argv) {
|
|
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 {
|
|
int *v = malloc(sizeof(*v));
|
|
*v = atoi(*argv);
|
|
prio_queue_put(&pq, v);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|