queue_test.c 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>

#define FAIL do {                                                       \
    printf("\n*** FAILED at %s line %d\n", __FILE__, __LINE__);         \
    pass = false;                                                       \
} while (0)

#define EQUAL(A, B) do {                                                \
    if ((A) != (B))                                                     \
        FAIL;                                                           \
} while (0)

typedef uint32_t Thing_t;       /* actual type doesn't matter */

static Thing_t things[MAX_QUEUE_SIZE];
static Thing_t thing1, thing2;

static bool matcher(void *wanted, void *candidate)
{
    return wanted == candidate;
}

int main(void)
{
    bool pass = true;
    queue_t queue;
    init_queue(&queue);

    for (int i = 0; i < MAX_QUEUE_SIZE; ++i)
    {
        if (!put_queue(&queue, &things[i]))
        {
            FAIL;
        }
    }

    /* queue is full */
    if (put_queue(&queue, &thing1))
        FAIL;

    Thing_t *p;
    for (int i = 0; i < MAX_QUEUE_SIZE; ++i)
    {
        p = get_queue(&queue);
        EQUAL(p, &things[i]);
    }

    /* queue is empty */
    p = get_queue(&queue);
    EQUAL(p, NULL);

    for (int i = 0; i < MAX_QUEUE_SIZE; ++i)
    {
        if (!put_queue(&queue, &things[i]))
        {
            FAIL;
        }
    }

    p = get_queue(&queue);
    EQUAL(p, &things[0]);

    p = get_queue(&queue);
    EQUAL(p, &things[1]);

    if (!requeue(&queue, &thing1))
        FAIL;
    if (!requeue(&queue, &thing2))
        FAIL;
    p = get_queue(&queue);
    EQUAL(p, &thing2);
    p = get_queue(&queue);
    EQUAL(p, &thing1);

    if (!requeue(&queue, &things[1]))
        FAIL;
    if (!requeue(&queue, &things[0]))
        FAIL;

    for (int i = 0; i < MAX_QUEUE_SIZE / 2; ++i)
    {
        p = get_queue(&queue);
        EQUAL(p, &things[i]);
    }

    for (int i = MAX_QUEUE_SIZE / 2; i < MAX_QUEUE_SIZE; ++i)
    {
        if (!put_queue(&queue, &things[i]))
            FAIL;
    }

    p = get_queue(&queue);
    EQUAL(p, &things[MAX_QUEUE_SIZE / 2]);
    p = get_queue(&queue);
    EQUAL(p, &things[MAX_QUEUE_SIZE / 2 + 1]);

    // ---- unqueue ----

    init_queue(&queue);
    for (int i = 0; i < MAX_QUEUE_SIZE; ++i)
    {
        if (!put_queue(&queue, &things[i]))
        {
            FAIL;
        }
    }
    for (int i = MAX_QUEUE_SIZE; --i >= 0;)
    {
        p = unqueue(&queue);
        EQUAL(p, &things[i]);
        EQUAL(queue.num_items, i);
    }
    EQUAL(queue.num_items, 0);
    if (!put_queue(&queue, &thing1))
        FAIL;
    if (!put_queue(&queue, &thing2))
        FAIL;
    EQUAL(queue.num_items, 2);
    p = get_queue(&queue);
    EQUAL(p, &thing1);
    p = get_queue(&queue);
    EQUAL(p, &thing2);

    // ---- unqueue_matching ----

    init_queue(&queue);

    // empty queue
131
    p = unqueue_matching(&queue, MAX_QUEUE_SIZE, matcher, &thing1);
132 133 134 135 136 137 138
    EQUAL(p, NULL);
    EQUAL(queue.num_items, 0);

    // one item in queue
    if (!put_queue(&queue, &thing1))
        FAIL;
    EQUAL(queue.num_items, 1);
139
    p = unqueue_matching(&queue, MAX_QUEUE_SIZE, matcher, &thing2);
140 141
    EQUAL(p, NULL);
    EQUAL(queue.num_items, 1);
142 143 144 145 146 147

    p = unqueue_matching(&queue, /*max_queue=*/ 0, matcher, &thing1);
    EQUAL(p, NULL);
    EQUAL(queue.num_items, 1);

    p = unqueue_matching(&queue, /*max_queue=*/ 1, matcher, &thing1);
148 149 150
    EQUAL(p, &thing1);
    EQUAL(queue.num_items, 0);

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    // more max_queue values
    for (int i = 0; i < MAX_QUEUE_SIZE; ++i)
    {
        if (!put_queue(&queue, &things[i]))
        {
            FAIL;
        }
    }
    p = unqueue_matching(&queue, /*max_queue=*/ 0, matcher, &things[MAX_QUEUE_SIZE - 1]);
    EQUAL(p, NULL);
    p = unqueue_matching(&queue, /*max_queue=*/ 1, matcher, &things[MAX_QUEUE_SIZE - 1]);
    EQUAL(p, &things[MAX_QUEUE_SIZE - 1]);
    EQUAL(queue.num_items, MAX_QUEUE_SIZE - 1);
    p = unqueue_matching(&queue, /*max_queue=*/ MAX_QUEUE_SIZE - 2, matcher, &things[0]);
    EQUAL(p, NULL);
    p = unqueue_matching(&queue, /*max_queue=*/ MAX_QUEUE_SIZE - 1, matcher, &things[0]);
    EQUAL(p, &things[0]);
    EQUAL(queue.num_items, MAX_QUEUE_SIZE - 2);

170
    // fill the queue then remove every other item
171
    init_queue(&queue);
172 173 174 175 176 177 178
    for (int i = 0; i < MAX_QUEUE_SIZE; ++i)
    {
        if (!put_queue(&queue, &things[i]))
        {
            FAIL;
        }
    }
179
    p = unqueue_matching(&queue, MAX_QUEUE_SIZE, matcher, &thing1);
180 181 182
    EQUAL(p, NULL);
    for (int i = MAX_QUEUE_SIZE - 1; i >= 0; i -= 2)
    {
183
        p = unqueue_matching(&queue, MAX_QUEUE_SIZE, matcher, &things[i]);
184 185 186
        EQUAL(p, &things[i]);
    }
    EQUAL(queue.num_items, MAX_QUEUE_SIZE / 2);
187
    p = unqueue_matching(&queue, MAX_QUEUE_SIZE, matcher, &thing1);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    EQUAL(p, NULL);
    for (int i = 0; i < MAX_QUEUE_SIZE; i += 2)
    {
        p = get_queue(&queue);
        EQUAL(p, &things[i]);
    }
    EQUAL(queue.num_items, 0);

    // fill the queue then remove every third item
    for (int i = 0; i < MAX_QUEUE_SIZE; ++i)
    {
        if (!put_queue(&queue, &things[i]))
        {
            FAIL;
        }
    }
204
    p = unqueue_matching(&queue, MAX_QUEUE_SIZE, matcher, &thing1);
205 206 207
    EQUAL(p, NULL);
    for (int i = 0; i < MAX_QUEUE_SIZE; i += 3)
    {
208
        p = unqueue_matching(&queue, MAX_QUEUE_SIZE, matcher, &things[i]);
209 210 211
        EQUAL(p, &things[i]);
    }
    EQUAL(queue.num_items, MAX_QUEUE_SIZE * 2 / 3);
212
    p = unqueue_matching(&queue, MAX_QUEUE_SIZE, matcher, &thing1);
213 214 215 216 217 218 219 220 221 222 223 224 225 226
    EQUAL(p, NULL);
    for (int i = 0; i < MAX_QUEUE_SIZE; ++i)
    {
        if (i % 3 == 0)
            continue;
        p = get_queue(&queue);
        EQUAL(p, &things[i]);
    }
    EQUAL(queue.num_items, 0);

    if (!pass)
        return EXIT_FAILURE;
    return EXIT_SUCCESS;
}