-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqtest.c
214 lines (202 loc) · 5.81 KB
/
qtest.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
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
// vim : set noet
#include <stdlib.h>
#include <stdio.h>
#include "queue.h"
#include <libgen.h>
#include <string.h>
#include <pthread.h>
#define LOG_ERROR(cmd) logError(#cmd, __FILE__, __LINE__, cmd)
static void logError(const char * cmd, const char * file, int line, int rtn) {
if (LIBQUEUE_SUCCESS != rtn) {
fprintf(stdout, "ERROR %s:%d : %s\n", file, line, cmd);
}
}
void hdl_error(int rtn) {
if (0 > rtn) {
exit(EXIT_FAILURE);
}
}
void henry_push_test(struct Queue *q) {
struct QueueData qd = { "HENRY" , sizeof("HENRY") };
struct QueueData qd2;
hdl_error(puts("queue successfully opened!"));
if(queue_push(q, &qd) != 0)
hdl_error(puts("pushing HENRY failed!"));
if(queue_pop(q, &qd2) != 0) {
hdl_error(puts("popping HENRY failed!"));
} else {
hdl_error(puts("here's what popped!"));
hdl_error(puts((const char*)qd2.v));
free(qd2.v);
}
}
void int32_push_test(struct Queue *q) {
struct QueueData qd = { "HENRY" , sizeof("HENRY") };
struct QueueData qd2;
int32_t ft = 42;
qd.v = &ft;
qd.vlen = sizeof(int32_t);
LOG_ERROR(queue_push(q, &qd));
LOG_ERROR(queue_pop(q, &qd2));
printf("popping forty-two: %d\n", *(int32_t*)qd2.v);
if (qd2.v)
free(qd2.v);
}
void push_count(struct Queue *q, int count) {
struct QueueData qd2;
int i;
char buffer[1024];
for (i = 0; i < 15; i++) {
qd2.v = buffer;
qd2.vlen= sprintf(buffer,"%d",i);
LOG_ERROR(queue_push(q, &qd2));
}
}
void fill15_test(struct Queue *q) {
struct QueueData qd2;
int i;
char buffer[1024];
push_count(q, 15) ;
int64_t count;
if (0 != queue_count(q, &count)) {
}
if (15 != count) {
fprintf(stderr, "ERROR: there should be 15 in the queue but got back %lld\n", (long long unsigned)count);
}
for (i = 0; i < 15; i++) {
LOG_ERROR(queue_pop(q, &qd2));
int len = sprintf(buffer,"%d",i);
if (len != qd2.vlen || 0 != memcmp(buffer, qd2.v, qd2.vlen)) {
printf("pop value not expected at index %d: %s\n", i, (char *)qd2.v);
}
if (qd2.v)
free(qd2.v);
}
for (i = 0; i < 15; i++) {
qd2.v = buffer;
qd2.vlen= sprintf(buffer,"%d",i);
LOG_ERROR(queue_push(q, &qd2));
}
if (0 != queue_count(q, &count)) {
}
if (15 != count) {
fprintf(stderr, "ERROR: there should be 15 in the queue but got back %lld\n", (long long unsigned)count);
}
for (i = 0; i < 15; i++) {
LOG_ERROR(queue_peek(q, i,&qd2));
int len = sprintf(buffer,"%d",i);
if (len != qd2.vlen || 0 != memcmp(buffer, qd2.v, qd2.vlen)) {
printf("pop value not expected at index %d:expect %d actual %s\n", i, i, (char *)qd2.v);
}
if (qd2.vlen && qd2.v)
free(qd2.v);
}
for (i = 0; i < 15; i++) {
ssize_t count;
LOG_ERROR(queue_count(q, &count));
if (15- i != count) {
fprintf(stderr, "ERROR: while popping off queue, expected %d but got %d\n", (int) 15-i,(int)count );
}
LOG_ERROR(queue_pop(q, NULL));
}
if (0 != queue_count(q, &count)) {
}
if (0 != count) {
fprintf(stderr, "ERROR: there should be 0 in the queue but got back %lld\n", (long long unsigned)count);
}
}
void maxEntries (const char * template) {
struct Queue *q=queue_open_with_options(template,"maxEntries",4, NULL);
struct QueueData qd;
int i = 10;
qd.v = &i;
qd.vlen = sizeof(i);
if (LIBQUEUE_SUCCESS != queue_push(q, &qd))
fprintf(stdout,"%d queue push did not succeed", __LINE__);
if (LIBQUEUE_SUCCESS != queue_push(q, &qd))
fprintf(stdout,"%d queue push did not succeed", __LINE__);
if (LIBQUEUE_SUCCESS != queue_push(q, &qd))
fprintf(stdout,"%d queue push did not succeed", __LINE__);
if (LIBQUEUE_SUCCESS != queue_push(q, &qd))
fprintf(stdout,"%d queue push did not succeed", __LINE__);
if (LIBQUEUE_FAILURE!= queue_push(q, &qd))
fprintf(stdout,"%d queue push succeed when it shouldn't have", __LINE__);
if (LIBQUEUE_SUCCESS != queue_pop(q, &qd))
fprintf(stdout,"%d queue pop did not succeed", __LINE__);
if (LIBQUEUE_SUCCESS != queue_push(q, &qd))
fprintf(stdout,"%d queue push did not succeed", __LINE__);
if (qd.v)
free(qd.v);
if (LIBQUEUE_FAILURE!= queue_push(q, &qd)) {
LOG_ERROR(queue_close(q));
}
}
void *writerThread(void* templatep) {
const char * template = (const char *)templatep;
struct Queue *q;
q = queue_open(template);
push_count(q, 100) ;
pthread_yield();
push_count(q, 100);
LOG_ERROR(queue_close(q));
return NULL;
}
void threadTest(const char *template) {
pthread_t child;
pthread_create(&child,NULL, writerThread,(void *) template);
struct Queue *q;
q = queue_open(template);
void *rtn;
LOG_ERROR(queue_close(q));
pthread_join(child,&rtn);
}
int main(int argc, char **argv) {
struct Queue *q;
puts(argv[0]);
char template[] = "/tmp/qtest_XXXXXX";
if (NULL == mkdtemp(template)) {
puts("failed to create temp dir for running tests");
return 1;
}
q = queue_open(template);
if(0 == queue_is_opened(q)) {
fprintf(stderr,"Failed to open the queue:%s", queue_get_last_error(q));
LOG_ERROR(queue_close(q));
return EXIT_FAILURE;
}
LOG_ERROR(queue_close(q));
int i;
for (i=1; i < 20; i++ ) {
q = queue_open_with_options(template, "maxBinLogSize", i,NULL);
fill15_test(q);
LOG_ERROR(queue_close(q));
}
q = queue_open(template);
henry_push_test(q);
int32_push_test(q);
fill15_test(q);
maxEntries (template);
if(queue_close(q) != 0)
puts("there was an error closing the queue!");
puts("queue successfully closed!");
// lets delete the queue
char cmdline[1024];
sprintf(cmdline, "rm -rf %s", template);
int resultsOfDelete = system(cmdline);
if (EXIT_SUCCESS != resultsOfDelete) {
puts("there was an error running delete of temp dir command ");
return 1;
}
q=queue_open_with_options(template,"failIfMissing", NULL);
if (NULL == queue_get_last_error(q)) {
puts("queue was created when it shouldn't have been");
}
LOG_ERROR(queue_close(q));
if (0 != mkdir(template, 0777)) {
printf("failed to create temp dir (%s) for running tests\n", template);
return 1;
}
threadTest(template);
printf("done\n");
return 0;
}