]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added priorityq_items() for getting all items from a priority queue.
authorTimo Sirainen <tss@iki.fi>
Mon, 22 Jun 2009 01:41:18 +0000 (21:41 -0400)
committerTimo Sirainen <tss@iki.fi>
Mon, 22 Jun 2009 01:41:18 +0000 (21:41 -0400)
--HG--
branch : HEAD

src/lib/priorityq.c
src/lib/priorityq.h
src/lib/test-priorityq.c

index 79e862129be6d9040d0cb1e30fcbda4607f8d447..47c261b8470799895b4de0f9f28128099441ac0e 100644 (file)
@@ -158,3 +158,11 @@ struct priorityq_item *priorityq_pop(struct priorityq *pq)
                priorityq_remove_idx(pq, 0);
        return item;
 }
+
+struct priorityq_item *const *priorityq_items(struct priorityq *pq)
+{
+       if (array_count(&pq->items) == 0)
+               return NULL;
+
+       return array_idx(&pq->items, 0);
+}
index 7e80107fd43bf26c08721626145b9ed97be1ee16..aa38914fc9fbfa36d4ea5b3495f64422de2d9834 100644 (file)
@@ -31,5 +31,9 @@ void priorityq_remove(struct priorityq *pq, struct priorityq_item *item);
 struct priorityq_item *priorityq_peek(struct priorityq *pq);
 /* Like priorityq_peek(), but also remove the returned item from the queue. */
 struct priorityq_item *priorityq_pop(struct priorityq *pq);
+/* Returns array containing all the priorityq_items. Only the first item is
+   guaranteed to be the highest priority item, the rest can't be assumed to
+   be in any order. */
+struct priorityq_item *const *priorityq_items(struct priorityq *pq);
 
 #endif
index 686b7acad402c6d1db5e3b8441e2063bdc2e483f..6369bf095661cff0a9973a0e48b82ca327c541f6 100644 (file)
@@ -30,46 +30,51 @@ void test_priorityq(void)
                1, 2, 3, 4, 5, 6, 7, 8
        };
        struct pq_test_item *item, items[PQ_MAX_ITEMS];
+       struct priorityq_item *const *all_items;
        unsigned int i, j;
        struct priorityq *pq;
        pool_t pool;
        int prev;
-       bool success = TRUE;
 
        pool = pool_alloconly_create("priorityq items", 1024);
 
        /* simple tests with popping only */
+       test_begin("priorityq");
        for (i = 0; input[i] != -1; i++) {
                p_clear(pool);
                pq = priorityq_init(cmp_int, 1);
                for (j = 0; input[i] != -1; i++, j++) {
-                       if (priorityq_count(pq) != j)
-                               success = FALSE;
+                       test_assert(priorityq_count(pq) == j);
                        item = p_new(pool, struct pq_test_item, 1);
                        item->num = input[i];
                        priorityq_add(pq, &item->item);
                }
+               all_items = priorityq_items(pq);
+               test_assert(priorityq_count(pq) == N_ELEMENTS(output));
+               item = (struct pq_test_item *)all_items[0];
+               test_assert(item->num == output[0]);
+               for (j = 1; j < N_ELEMENTS(output); j++) {
+                       item = (struct pq_test_item *)all_items[j];
+                       test_assert(item->num > output[0]);
+                       test_assert(item->num <= output[N_ELEMENTS(output)-1]);
+               }
                for (j = 0; j < N_ELEMENTS(output); j++) {
-                       if (priorityq_count(pq) != N_ELEMENTS(output) - j)
-                               success = FALSE;
+                       test_assert(priorityq_count(pq) == N_ELEMENTS(output) - j);
 
                        item = (struct pq_test_item *)priorityq_peek(pq);
-                       if (output[j] != item->num)
-                               success = FALSE;
+                       test_assert(output[j] == item->num);
                        item = (struct pq_test_item *)priorityq_pop(pq);
-                       if (output[j] != item->num)
-                               success = FALSE;
+                       test_assert(output[j] == item->num);
                }
-               if (priorityq_count(pq) != 0)
-                       success = FALSE;
-               if (priorityq_peek(pq) != NULL || priorityq_pop(pq) != NULL)
-                       success = FALSE;
+               test_assert(priorityq_count(pq) == 0);
+               test_assert(priorityq_peek(pq) == NULL);
+               test_assert(priorityq_pop(pq) == NULL);
                priorityq_deinit(&pq);
        }
-       test_out("priorityq(1)", success);
+       test_end();
 
        /* randomized tests, remove elements */
-       success = TRUE;
+       test_begin("priorityq randomized");
        for (i = 0; i < 100; i++) {
                pq = priorityq_init(cmp_int, 1);
                for (j = 0; j < PQ_MAX_ITEMS; j++) {
@@ -85,17 +90,15 @@ void test_priorityq(void)
                prev = 0;
                while (priorityq_count(pq) > 0) {
                        item = (struct pq_test_item *)priorityq_pop(pq);
-                       if (item->num < 0 || prev > item->num)
-                               success = FALSE;
+                       test_assert(item->num >= 0 && prev <= item->num);
                        prev = item->num;
                        item->num = -1;
                }
                for (j = 0; j < PQ_MAX_ITEMS; j++) {
-                       if (items[j].num != -1)
-                               success = FALSE;
+                       test_assert(items[j].num == -1);
                }
                priorityq_deinit(&pq);
        }
-       test_out("priorityq(2)", success);
+       test_end();
        pool_unref(&pool);
 }