|
| 1 | +#include "test-lib.h" |
| 2 | +#include "prio-queue.h" |
| 3 | + |
| 4 | +static int intcmp(const void *va, const void *vb, void *data UNUSED) |
| 5 | +{ |
| 6 | + const int *a = va, *b = vb; |
| 7 | + return *a - *b; |
| 8 | +} |
| 9 | + |
| 10 | + |
| 11 | +#define MISSING -1 |
| 12 | +#define DUMP -2 |
| 13 | +#define STACK -3 |
| 14 | +#define GET -4 |
| 15 | +#define REVERSE -5 |
| 16 | + |
| 17 | +static int show(int *v) |
| 18 | +{ |
| 19 | + return v ? *v : MISSING; |
| 20 | +} |
| 21 | + |
| 22 | +static void test_prio_queue(int *input, int *result, size_t input_size) |
| 23 | +{ |
| 24 | + struct prio_queue pq = { intcmp }; |
| 25 | + |
| 26 | + for (int i = 0, j = 0; i < input_size; i++) { |
| 27 | + void *peek, *get; |
| 28 | + switch(input[i]) { |
| 29 | + case GET: |
| 30 | + peek = prio_queue_peek(&pq); |
| 31 | + get = prio_queue_get(&pq); |
| 32 | + if (!check(peek == get)) |
| 33 | + return; |
| 34 | + if(!check_int(result[j++], ==, show(get))) |
| 35 | + test_msg("failed at result[] index %d", j-1); |
| 36 | + break; |
| 37 | + case DUMP: |
| 38 | + while ((peek = prio_queue_peek(&pq))) { |
| 39 | + get = prio_queue_get(&pq); |
| 40 | + if (!check(peek == get)) |
| 41 | + return; |
| 42 | + if(!check_int(result[j++], ==, show(get))) |
| 43 | + test_msg("failed at result[] index %d", j-1); |
| 44 | + } |
| 45 | + break; |
| 46 | + case STACK: |
| 47 | + pq.compare = NULL; |
| 48 | + break; |
| 49 | + case REVERSE: |
| 50 | + prio_queue_reverse(&pq); |
| 51 | + break; |
| 52 | + default: |
| 53 | + prio_queue_put(&pq, &input[i]); |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + clear_prio_queue(&pq); |
| 58 | +} |
| 59 | + |
| 60 | +#define BASIC_INPUT 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP |
| 61 | +#define BASIC_RESULT 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 |
| 62 | + |
| 63 | +#define MIXED_PUT_GET_INPUT 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP |
| 64 | +#define MIXED_PUT_GET_RESULT 2, 3, 4, 1, 5, 6 |
| 65 | + |
| 66 | +#define EMPTY_QUEUE_INPUT 1, 2, GET, GET, GET, 1, 2, GET, GET, GET |
| 67 | +#define EMPTY_QUEUE_RESULT 1, 2, MISSING, 1, 2, MISSING |
| 68 | + |
| 69 | +#define STACK_INPUT STACK, 8, 1, 5, 4, 6, 2, 3, DUMP |
| 70 | +#define STACK_RESULT 3, 2, 6, 4, 5, 1, 8 |
| 71 | + |
| 72 | +#define REVERSE_STACK_INPUT STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP |
| 73 | +#define REVERSE_STACK_RESULT 1, 2, 3, 4, 5, 6 |
| 74 | + |
| 75 | +#define TEST_INPUT(INPUT, RESULT, name) \ |
| 76 | + static void test_##name(void) \ |
| 77 | +{ \ |
| 78 | + int input[] = {INPUT}; \ |
| 79 | + int result[] = {RESULT}; \ |
| 80 | + test_prio_queue(input, result, ARRAY_SIZE(input)); \ |
| 81 | +} |
| 82 | + |
| 83 | +TEST_INPUT(BASIC_INPUT, BASIC_RESULT, basic) |
| 84 | +TEST_INPUT(MIXED_PUT_GET_INPUT, MIXED_PUT_GET_RESULT, mixed) |
| 85 | +TEST_INPUT(EMPTY_QUEUE_INPUT, EMPTY_QUEUE_RESULT, empty) |
| 86 | +TEST_INPUT(STACK_INPUT, STACK_RESULT, stack) |
| 87 | +TEST_INPUT(REVERSE_STACK_INPUT, REVERSE_STACK_RESULT, reverse) |
| 88 | + |
| 89 | +int cmd_main(int argc, const char **argv) |
| 90 | +{ |
| 91 | + TEST(test_basic(), "prio-queue works for basic input"); |
| 92 | + TEST(test_mixed(), "prio-queue works for mixed put & get commands"); |
| 93 | + TEST(test_empty(), "prio-queue works when queue is empty"); |
| 94 | + TEST(test_stack(), "prio-queue works when used as a LIFO stack"); |
| 95 | + TEST(test_reverse(), "prio-queue works when LIFO stack is reversed"); |
| 96 | + |
| 97 | + return test_done(); |
| 98 | +} |
0 commit comments