summaryrefslogtreecommitdiff
path: root/t/helper
diff options
context:
space:
mode:
authorDerrick Stolee <stolee@gmail.com>2018-11-01 13:46:17 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-11-02 03:14:21 (GMT)
commitaca4240f6a32423df5f95de6a9354b524fe57ec5 (patch)
tree06a2b1efc6f6d9d0a66088e13965238bfb36e86b /t/helper
parent2d3b1c576c85b7f5db1f418907af00ab88e0c303 (diff)
downloadgit-aca4240f6a32423df5f95de6a9354b524fe57ec5.zip
git-aca4240f6a32423df5f95de6a9354b524fe57ec5.tar.gz
git-aca4240f6a32423df5f95de6a9354b524fe57ec5.tar.bz2
prio-queue: add 'peek' operation
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>
Diffstat (limited to 't/helper')
-rw-r--r--t/helper/test-prio-queue.c26
1 files changed, 18 insertions, 8 deletions
diff --git a/t/helper/test-prio-queue.c b/t/helper/test-prio-queue.c
index 9807b64..5bc9c46 100644
--- a/t/helper/test-prio-queue.c
+++ b/t/helper/test-prio-queue.c
@@ -22,14 +22,24 @@ int cmd__prio_queue(int argc, const char **argv)
struct prio_queue pq = { intcmp };
while (*++argv) {
- if (!strcmp(*argv, "get"))
- show(prio_queue_get(&pq));
- else if (!strcmp(*argv, "dump")) {
- int *v;
- while ((v = prio_queue_get(&pq)))
- show(v);
- }
- else {
+ 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);