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
#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
p = unqueue_matching(&queue, matcher, &thing1);
EQUAL(p, NULL);
EQUAL(queue.num_items, 0);
// one item in queue
if (!put_queue(&queue, &thing1))
FAIL;
EQUAL(queue.num_items, 1);
p = unqueue_matching(&queue, matcher, &thing2);
EQUAL(p, NULL);
EQUAL(queue.num_items, 1);
p = unqueue_matching(&queue, matcher, &thing1);
EQUAL(p, &thing1);
EQUAL(queue.num_items, 0);
// fill the queue then remove every other item
for (int i = 0; i < MAX_QUEUE_SIZE; ++i)
{
if (!put_queue(&queue, &things[i]))
{
FAIL;
}
}
p = unqueue_matching(&queue, matcher, &thing1);
EQUAL(p, NULL);
for (int i = MAX_QUEUE_SIZE - 1; i >= 0; i -= 2)
{
p = unqueue_matching(&queue, matcher, &things[i]);
EQUAL(p, &things[i]);
}
EQUAL(queue.num_items, MAX_QUEUE_SIZE / 2);
p = unqueue_matching(&queue, matcher, &thing1);
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;
}
}
p = unqueue_matching(&queue, matcher, &thing1);
EQUAL(p, NULL);
for (int i = 0; i < MAX_QUEUE_SIZE; i += 3)
{
p = unqueue_matching(&queue, matcher, &things[i]);
EQUAL(p, &things[i]);
}
EQUAL(queue.num_items, MAX_QUEUE_SIZE * 2 / 3);
p = unqueue_matching(&queue, matcher, &thing1);
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;
}