File: test_queue.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (260 lines) | stat: -rw-r--r-- 6,479 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "../../src/queue.h"
#include "../lib/runner.h"

/******************************************************************************
 *
 * Fixture with a single queue and a few test items that can be added to it.
 *
 *****************************************************************************/

struct item
{
    int value;
    queue queue;
};

struct fixture
{
    queue queue;
    struct item items[3];
};

static void *setUp(MUNIT_UNUSED const MunitParameter params[],
                   MUNIT_UNUSED void *user_data)
{
    struct fixture *f = munit_malloc(sizeof *f);
    QUEUE_INIT(&f->queue);
    return f;
}

static void tearDown(void *data)
{
    struct fixture *f = data;
    free(f);
}

/******************************************************************************
 *
 * Helper macros
 *
 *****************************************************************************/

/* Initialize and push the given number of fixture items to the fixture's
 * queue. Each item will have a value equal to its index plus one. */
#define PUSH(N)                                   \
    {                                             \
        int i_;                                   \
        for (i_ = 0; i_ < N; i_++) {              \
            struct item *item_ = &f->items[i_];   \
            item_->value = i_ + 1;                \
            QUEUE_PUSH(&f->queue, &item_->queue); \
        }                                         \
    }

/* Remove the i'th fixture item from the fixture queue. */
#define REMOVE(I) QUEUE_REMOVE(&f->items[I].queue)

/******************************************************************************
 *
 * Assertions
 *
 *****************************************************************************/

/* Assert that the item at the head of the fixture's queue has the given
 * value. */
#define ASSERT_HEAD(VALUE)                             \
    {                                                  \
        queue *head_ = QUEUE_HEAD(&f->queue);          \
        struct item *item_;                            \
        item_ = QUEUE_DATA(head_, struct item, queue); \
        munit_assert_int(item_->value, ==, VALUE);     \
    }

/* Assert that the item at the tail of the queue has the given value. */
#define ASSERT_TAIL(VALUE)                             \
    {                                                  \
        queue *tail_ = QUEUE_TAIL(&f->queue);          \
        struct item *item_;                            \
        item_ = QUEUE_DATA(tail_, struct item, queue); \
        munit_assert_int(item_->value, ==, VALUE);     \
    }

/* Assert that the fixture's queue is empty. */
#define ASSERT_EMPTY munit_assert_true(QUEUE_IS_EMPTY(&f->queue))

/* Assert that the fixture's queue is not empty. */
#define ASSERT_NOT_EMPTY munit_assert_false(QUEUE_IS_EMPTY(&f->queue))

/******************************************************************************
 *
 * QUEUE_IS_EMPTY
 *
 *****************************************************************************/

SUITE(QUEUE_IS_EMPTY)

TEST(QUEUE_IS_EMPTY, yes, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    ASSERT_EMPTY;
    return MUNIT_OK;
}

TEST(QUEUE_IS_EMPTY, no, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    PUSH(1);
    ASSERT_NOT_EMPTY;
    return MUNIT_OK;
}

/******************************************************************************
 *
 * QUEUE_PUSH
 *
 *****************************************************************************/

SUITE(QUEUE_PUSH)

TEST(QUEUE_PUSH, one, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    PUSH(1);
    ASSERT_HEAD(1);
    return MUNIT_OK;
}

TEST(QUEUE_PUSH, two, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    int i;
    PUSH(2);
    for (i = 0; i < 2; i++) {
        ASSERT_HEAD(i + 1);
        REMOVE(i);
    }
    ASSERT_EMPTY;
    return MUNIT_OK;
}

/******************************************************************************
 *
 * QUEUE_REMOVE
 *
 *****************************************************************************/

SUITE(QUEUE_REMOVE)

TEST(QUEUE_REMOVE, first, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    PUSH(3);
    REMOVE(0);
    ASSERT_HEAD(2);
    return MUNIT_OK;
}

TEST(QUEUE_REMOVE, second, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    PUSH(3);
    REMOVE(1);
    ASSERT_HEAD(1);
    return MUNIT_OK;
}

TEST(QUEUE_REMOVE, success, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    PUSH(3);
    REMOVE(2);
    ASSERT_HEAD(1);
    return MUNIT_OK;
}

/******************************************************************************
 *
 * QUEUE_TAIL
 *
 *****************************************************************************/

SUITE(QUEUE_TAIL)

TEST(QUEUE_TAIL, one, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    PUSH(1);
    ASSERT_TAIL(1);
    return MUNIT_OK;
}

TEST(QUEUE_TAIL, two, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    PUSH(2);
    ASSERT_TAIL(2);
    return MUNIT_OK;
}

TEST(QUEUE_TAIL, three, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    PUSH(3);
    ASSERT_TAIL(3);
    return MUNIT_OK;
}

/******************************************************************************
 *
 * QUEUE_FOREACH
 *
 *****************************************************************************/

SUITE(QUEUE_FOREACH)

/* Loop through a queue of zero items. */
TEST(QUEUE_FOREACH, zero, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    queue *head;
    int count = 0;
    QUEUE_FOREACH (head, &f->queue) {
        count++;
    }
    munit_assert_int(count, ==, 0);
    return MUNIT_OK;
}

/* Loop through a queue of one item. */
TEST(QUEUE_FOREACH, one, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    queue *head;
    int count = 0;
    PUSH(1);
    QUEUE_FOREACH (head, &f->queue) {
        count++;
    }
    munit_assert_int(count, ==, 1);
    return MUNIT_OK;
}

/* Loop through a queue of two items. The order of the loop is from the head to
 * the tail. */
TEST(QUEUE_FOREACH, two, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    queue *head;
    int values[2] = {0, 0};
    int i = 0;
    PUSH(2);
    QUEUE_FOREACH (head, &f->queue) {
        struct item *item;
        item = QUEUE_DATA(head, struct item, queue);
        values[i] = item->value;
        i++;
    }
    munit_assert_int(values[0], ==, 1);
    munit_assert_int(values[1], ==, 2);
    return MUNIT_OK;
}