-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_ring_queue.c
174 lines (131 loc) · 4.66 KB
/
test_ring_queue.c
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
#include <core/structures/fast_queue.h>
#include "test.h"
int main(int argc, char **argv)
{
BEGIN_TESTS();
{
/* test when inserting more than array size */
struct core_fast_queue fast_queue;
int i;
core_fast_queue_init(&fast_queue, sizeof(int));
int size;
size = 0;
TEST_INT_EQUALS(core_fast_queue_empty(&fast_queue), 1);
i = 16;
while (i--) {
TEST_INT_EQUALS(core_fast_queue_enqueue(&fast_queue, &i), 1);
++size;
TEST_INT_EQUALS(core_fast_queue_size(&fast_queue), size);
}
i = 16;
while (i--) {
TEST_INT_EQUALS(core_fast_queue_enqueue(&fast_queue, &i), 1);
++size;
TEST_INT_EQUALS(core_fast_queue_size(&fast_queue), size);
}
TEST_INT_EQUALS(core_fast_queue_full(&fast_queue), 0);
i = 16;
while (i--) {
int item;
TEST_INT_EQUALS(core_fast_queue_dequeue(&fast_queue, &item), 1);
--size;
TEST_INT_EQUALS(core_fast_queue_size(&fast_queue), size);
}
i = 16;
while (i--) {
int item;
TEST_INT_EQUALS(core_fast_queue_dequeue(&fast_queue, &item), 1);
--size;
TEST_INT_EQUALS(core_fast_queue_size(&fast_queue), size);
}
core_fast_queue_destroy(&fast_queue);
}
{
/* push 1000 elements, and verify them after */
struct core_fast_queue fast_queue;
int i;
int size;
size = 0;
core_fast_queue_init(&fast_queue, sizeof(int));
TEST_INT_EQUALS(core_fast_queue_empty(&fast_queue), 1);
i = 1000;
while (i--) {
TEST_INT_EQUALS(core_fast_queue_enqueue(&fast_queue, &i), 1);
++size;
TEST_INT_EQUALS(core_fast_queue_size(&fast_queue), size);
}
TEST_INT_EQUALS(core_fast_queue_full(&fast_queue), 0);
i = 1000;
while (i--) {
int item;
TEST_INT_EQUALS(core_fast_queue_dequeue(&fast_queue, &item), 1);
--size;
TEST_INT_EQUALS(core_fast_queue_size(&fast_queue), size);
TEST_INT_EQUALS(item, i);
/* printf("%i %i\n", item, i); */
}
TEST_INT_EQUALS(core_fast_queue_empty(&fast_queue), 1);
core_fast_queue_destroy(&fast_queue);
}
{
/* use array size of 1 and 2000 elements */
struct core_fast_queue fast_queue;
int i;
core_fast_queue_init(&fast_queue, sizeof(int));
TEST_INT_EQUALS(core_fast_queue_empty(&fast_queue), 1);
i = 2000;
while (i--) {
TEST_INT_EQUALS(core_fast_queue_enqueue(&fast_queue, &i), 1);
}
TEST_INT_EQUALS(core_fast_queue_full(&fast_queue), 0);
i = 2000;
while (i--) {
int item;
TEST_INT_EQUALS(core_fast_queue_dequeue(&fast_queue, &item), 1);
TEST_INT_EQUALS(item, i);
/* printf("%i %i\n", item, i); */
}
TEST_INT_EQUALS(core_fast_queue_empty(&fast_queue), 1);
core_fast_queue_destroy(&fast_queue);
}
{
/* stress test the code by inserting one element,
and then removing. */
struct core_fast_queue fast_queue;
int i;
core_fast_queue_init(&fast_queue, sizeof(int));
TEST_INT_EQUALS(core_fast_queue_empty(&fast_queue), 1);
i = 3000;
while (i--) {
TEST_INT_EQUALS(core_fast_queue_enqueue(&fast_queue, &i), 1);
TEST_INT_EQUALS(core_fast_queue_dequeue(&fast_queue, &i), 1);
TEST_INT_EQUALS(core_fast_queue_empty(&fast_queue), 1);
TEST_INT_EQUALS(core_fast_queue_enqueue(&fast_queue, &i), 1);
TEST_INT_EQUALS(core_fast_queue_dequeue(&fast_queue, &i), 1);
}
TEST_INT_EQUALS(core_fast_queue_full(&fast_queue), 0);
core_fast_queue_destroy(&fast_queue);
}
{
struct core_fast_queue fast_queue;
int i;
int j;
int value;
core_fast_queue_init(&fast_queue, sizeof(int));
TEST_INT_EQUALS(core_fast_queue_empty(&fast_queue), 1);
for (j = 0; j < 4; j++) {
i = 3000;
while (i--) {
value = i;
TEST_INT_EQUALS(core_fast_queue_enqueue(&fast_queue, &value), 1);
TEST_INT_EQUALS(core_fast_queue_enqueue(&fast_queue, &value), 1);
TEST_INT_EQUALS(core_fast_queue_dequeue(&fast_queue, &value), 1);
}
while (core_fast_queue_dequeue(&fast_queue, &value)) {
}
}
core_fast_queue_destroy(&fast_queue);
}
END_TESTS();
return 0;
}