-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqtest1.c
159 lines (149 loc) · 3.94 KB
/
qtest1.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
// vim : set noet
#include <stdlib.h>
#include <stdio.h>
#include "queue.h"
#include <libgen.h>
#include <string.h>
#include <ExtremeCUnit.h>
#include "queue.c"
#define LOG_ERROR(cmd) logError(#cmd, __LINE__, cmd)
static void logError(const char * cmd, int line, int rtn) {
if (LIBQUEUE_SUCCESS != rtn) {
printf("ERROR: cmd");
}
}
static void fillQueues(struct Queue *q) {
struct QueueData qd2;
char buffer[1024];
int i;
for (i = 0; i < 15; i++) {
qd2.v = buffer;
qd2.vlen= sprintf(buffer,"%d",i);
LOG_ERROR(queue_push(q, &qd2));
}
}
static void pop15(struct Queue *q) {
struct QueueData qd2;
char buffer[1024];
int i;
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);
}
}
void fill15_test(struct Queue *q) {
struct QueueData qd2;
char buffer[1024];
fillQueues(q);
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);
}
int i;
pop15(q);
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);
}
}
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);
}
}
TEST(TwoByteFiles) {
struct Queue *q;
char template[] = "/tmp/qtest_XXXXXX";
if (NULL == mkdtemp(template)) {
puts("failed to create temp dir for running tests");
return 1;
}
q = queue_open_with_options(template, "maxBinLogSize", 1,NULL);
fill15_test(q);
Assert(0 ==queue_close(q));
return 0;
}
TEST(OneByteFiles) {
struct Queue *q;
char template[] = "/tmp/qtest_XXXXXX";
if (NULL == mkdtemp(template)) {
puts("failed to create temp dir for running tests");
return 1;
}
q = queue_open_with_options(template, "maxBinLogSize", 2,NULL);
fill15_test(q);
if(queue_close(q) != 0)
puts("there was an error closing the queue!");
return 0;
}
TEST(Coruption1) {
struct Queue *q;
char template[] = "/tmp/qtest_XXXXXX";
if (NULL == mkdtemp(template)) {
puts("failed to create temp dir for running tests");
return 1;
}
q = queue_open_with_options(template, "maxBinLogSize", 2,NULL);
fillQueues(q);
fwrite(template, 1,1 ,q->write.binlogfd);
Assert(queue_close(q) == 0);
q = queue_open_with_options(template, "maxBinLogSize", 2,NULL);
pop15(q) ;
struct QueueData qd2;
Assert(LIBQUEUE_FAILURE == queue_pop(q, &qd2));
fillQueues(q);
pop15(q) ;
Assert(queue_close(q) == 0);
return 0;
}
TEST(Coruption1to2000) {
struct Queue *q;
char template[] = "/tmp/qtest_XXXXXX";
if (NULL == mkdtemp(template)) {
puts("failed to create temp dir for running tests");
return 1;
}
int i=1;
for (; i < 2000; i++) {
q = queue_open_with_options(template, "maxBinLogSize", 2,NULL);
fillQueues(q);
fwrite(template, i,1 ,q->write.binlogfd);
Assert(queue_close(q) == 0);
q = queue_open_with_options(template, "maxBinLogSize", 2,NULL);
pop15(q) ;
struct QueueData qd2;
Assert(LIBQUEUE_FAILURE == queue_pop(q, &qd2));
fillQueues(q);
pop15(q) ;
Assert(queue_close(q) == 0);
}
return 0;
}