-
Notifications
You must be signed in to change notification settings - Fork 90
/
pipe_test.c
463 lines (372 loc) · 11.3 KB
/
pipe_test.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* pipe_test.c - The pipe's unit tests. This does not need to be linked unless
* you plan on testing the pipe.
*
* The MIT License
* Copyright (c) 2011 Clark Gaebel <cg.wowus.cg@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "pipe.h"
#include "pipe_util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define UNUSED_PARAMETER(var) (var) = (var)
// All this hackery is just to get asserts to work in release build.
#ifdef NDEBUG
#define NDEBUG_WAS_DEFINED
#undef NDEBUG
#endif
#include <assert.h>
#ifdef NDEBUG_WAS_DEFINED
#undef NDEBUG_WAS_DEFINED
#define NDEBUG
#endif
#define DEF_TEST(name) \
static void test_##name()
#define countof(a) (sizeof(a)/sizeof(*(a)))
#define array_eq(a1, a2) \
(sizeof(a1) == sizeof(a2) \
? memcmp(a1, a2, sizeof(a1) == 0) \
: 0)
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#endif
#define array_eq_len(a1, a2, len2) \
(sizeof(a1) == sizeof((a2)[0])*(len2) \
? memcmp(a1, a2, sizeof(a1)) == 0 \
: 0)
// This test answers the question: "Can we use a pipe like a normal queue?"
DEF_TEST(basic_storage)
{
pipe_t* pipe = pipe_new(sizeof(int), 0);
pipe_producer_t* p = pipe_producer_new(pipe);
pipe_consumer_t* c = pipe_consumer_new(pipe);
pipe_free(pipe);
int a[] = { 0, 1, 2, 3, 4 };
int b[] = { 9, 8, 7, 6, 5 };
pipe_push(p, a, countof(a));
pipe_push(p, b, countof(b));
pipe_producer_free(p);
int bufa[6];
int bufb[10];
size_t acnt = pipe_pop(c, bufa, countof(bufa)),
bcnt = pipe_pop(c, bufb, countof(bufb));
int expecteda[] = {
0, 1, 2, 3, 4, 9
};
int expectedb[] = {
8, 7, 6, 5
};
assert(array_eq_len(expecteda, bufa, acnt));
assert(array_eq_len(expectedb, bufb, bcnt));
pipe_consumer_free(c);
}
typedef struct {
int orig;
int new;
} testdata_t;
static void double_elems(const void* elems, size_t count, pipe_producer_t* out, void* aux)
{
UNUSED_PARAMETER(aux);
if(count == 0)
return;
testdata_t outbuffer[count];
memcpy(outbuffer, elems, count*sizeof(testdata_t));
for(size_t i = 0; i < count; ++i)
outbuffer[i].new *= 2;
pipe_push(out, outbuffer, count);
}
#ifdef PIPE_DEBUG
#define MAX_NUM 250000
#else
#define MAX_NUM 500000
#endif
static void generate_test_data(pipe_producer_t* p)
{
for(int i = 0; i < MAX_NUM; ++i)
{
testdata_t t = { i, i };
pipe_push(p, &t, 1);
}
}
static inline void validate_test_data(testdata_t t, int multiplier)
{
assert(t.new == t.orig*multiplier);
}
static void validate_consumer(pipe_consumer_t* c, unsigned doublings)
{
testdata_t t;
while(pipe_pop(c, &t, 1))
validate_test_data(t, 1 << doublings);
}
DEF_TEST(pipeline_multiplier)
{
pipeline_t pipeline =
pipe_pipeline(sizeof(testdata_t),
&double_elems, (void*)NULL, sizeof(testdata_t),
&double_elems, (void*)NULL, sizeof(testdata_t),
&double_elems, (void*)NULL, sizeof(testdata_t),
&double_elems, (void*)NULL, sizeof(testdata_t),
&double_elems, (void*)NULL, sizeof(testdata_t),
&double_elems, (void*)NULL, sizeof(testdata_t),
&double_elems, (void*)NULL, sizeof(testdata_t),
&double_elems, (void*)NULL, sizeof(testdata_t),
(void*)NULL
);
assert(pipeline.in);
assert(pipeline.out);
generate_test_data(pipeline.in); pipe_producer_free(pipeline.in);
validate_consumer(pipeline.out, 8); pipe_consumer_free(pipeline.out);
}
DEF_TEST(parallel_multiplier)
{
pipeline_t pipeline =
pipe_parallel(4,
sizeof(testdata_t),
&double_elems, (void*)NULL,
sizeof(testdata_t));
assert(pipeline.in);
assert(pipeline.out);
generate_test_data(pipeline.in); pipe_producer_free(pipeline.in);
validate_consumer(pipeline.out, 1); pipe_consumer_free(pipeline.out);
}
struct Foo
{
int a;
int b;
int c;
};
DEF_TEST(issue_4)
{
pipe_t* p = pipe_new(sizeof(struct Foo), 0);
pipe_producer_t* producer = pipe_producer_new(p);
pipe_consumer_t* consumer = pipe_consumer_new(p);
for(int i = 0; i < 22; ++i)
{
struct Foo f;
pipe_push(producer, &f, 1);
}
for(int i = 0; i < 22; ++i)
{
struct Foo f;
(void)pipe_pop(consumer, &f, 1);
}
for(int i = 0; i < 21; ++i)
{
struct Foo f;
pipe_push(producer, &f, 1);
(void)pipe_pop(consumer, &f, 1);
}
pipe_producer_free(producer);
pipe_consumer_free(consumer);
pipe_free(p);
}
DEF_TEST(issue_5)
{
static const int NUM = 32;
pipe_t* pipe = pipe_new(sizeof(int), 0);
pipe_producer_t* p = pipe_producer_new(pipe);
pipe_consumer_t* c = pipe_consumer_new(pipe);
pipe_free(pipe);
int data[NUM];
for(int i=0; i < NUM; ++i)
data[i] = i;
pipe_push(p, data, NUM);
pipe_producer_free(p);
int buf[NUM];
size_t ret = pipe_pop(c, buf, NUM);
assert(ret == NUM);
for(int i=0; i < NUM; ++i)
assert(buf[i] == data[i]);
pipe_consumer_free(c);
}
// set max cap (not infinite)
DEF_TEST(issue_6_a)
{
static const int NUM = 32;
pipe_t* pipe = pipe_new(sizeof(int), NUM);
pipe_producer_t* p = pipe_producer_new(pipe);
pipe_consumer_t* c = pipe_consumer_new(pipe);
pipe_free(pipe);
int data[NUM];
for(int i=0; i < NUM; ++i)
data[i] = i;
pipe_push(p, data, NUM);
pipe_producer_free(p);
int buf[NUM];
size_t ret = pipe_pop(c, buf, NUM);
assert(ret == NUM);
for(int i=0; i < NUM; ++i)
assert(buf[i] == data[i]);
pipe_consumer_free(c);
}
// set smaller min cap
DEF_TEST(issue_6_b)
{
static const int NUM = 16;
pipe_t* pipe = pipe_new(sizeof(int), NUM * 2);
pipe_reserve(PIPE_GENERIC(pipe), NUM);
pipe_producer_t* p = pipe_producer_new(pipe);
pipe_consumer_t* c = pipe_consumer_new(pipe);
pipe_free(pipe);
int data[NUM];
for(int i=0; i < NUM; ++i)
data[i] = i;
pipe_push(p, data, NUM);
pipe_producer_free(p);
int buf[NUM];
size_t ret = pipe_pop(c, buf, NUM);
assert(ret == NUM);
for(int i=0; i < NUM; ++i)
assert(buf[i] == data[i]);
pipe_consumer_free(c);
}
#ifdef _WIN32 // use the native win32 API on Windows
#include <windows.h>
#define thread_create(f, p) CloseHandle( \
CreateThread(NULL, \
0, \
(LPTHREAD_START_ROUTINE)(f), \
(p), \
0, \
NULL))
#define thread_sleep(s) Sleep((s) * 1000)
#else // fall back on pthreads
#include <pthread.h>
#include <unistd.h>
static inline void thread_create(void *(*f) (void*), void* p)
{
pthread_t t;
pthread_create(&t, NULL, f, p);
}
#define thread_sleep(s) sleep(s)
#endif
typedef struct __issue_6_t {
pipe_consumer_t* c;
int writing;
int read;
} issue_6_t;
static void* process_pipe_issue_6_c(void* param)
{
static const int NUM = 32;
issue_6_t* v = (issue_6_t *)param;
//printf("Consumer waiting for a bit ...\n");
//printf("Consumer starts to read pipe ...\n");
thread_sleep(1);
assert(v->writing); // producer still writing, blocked from finishing
int buf[NUM];
size_t ret = pipe_pop(v->c, buf, NUM);
assert(ret == NUM);
for(int i=0; i < NUM; ++i)
assert(buf[i] == i);
v->read = NUM;
pipe_consumer_free(v->c);
return NULL;
}
// producer blocking on push
// Note: pipe rounds up to power of 2 so initial
// value of 32 is rounded up to 64 so we block on
// writing 64 values, not 32
DEF_TEST(issue_6_c)
{
static const int NUM = 32;
pipe_t* pipe = pipe_new(sizeof(int), NUM);
pipe_producer_t* p = pipe_producer_new(pipe);
issue_6_t* params = malloc(sizeof(*params));
memset(params, 0, sizeof(*params));
params->c = pipe_consumer_new(pipe);
pipe_free(pipe);
thread_create(&process_pipe_issue_6_c, params);
int data[NUM];
for(int i=0; i < NUM; ++i)
data[i] = i;
//printf("Producer pushing ok ...\n");
params->writing = 1;
pipe_push(p, data, NUM);
//printf("Producer pushing should be blocked ...\n");
pipe_push(p, data, NUM);
//printf("Producer unblocked ...\n");
params->writing = 0;
thread_sleep(1);
assert(params->read == NUM);
free(params);
pipe_producer_free(p);
}
/*
// This test is only legal if DEFAULT_MINCAP is less than or equal to 8.
//
// Therefore, this test is disabled in release mode.
#ifdef PIPE_DEBUG
DEF_TEST(clobbering)
{
pipe_t* p = pipe_new(sizeof(int), 5);
pipe_producer_t* pro = pipe_producer_new(p);
pipe_consumer_t* con = pipe_consumer_new(p);
pipe_free(p);
const int x[] = { 1, 2, 3 };
int y[3];
pipe_push_clobber(pro, x, 3);
size_t popped = pipe_pop(con, y, 3);
assert(popped == 3);
assert(array_eq_len(x, y, 3));
int x2[] = { 2, 3, 1, 2, 3, 1, 2, 3 };
int y2[8];
pipe_push_clobber(pro, x, 3);
pipe_push_clobber(pro, x, 3);
pipe_push_clobber(pro, x, 3);
popped = pipe_pop(con, y2, 8);
assert(popped == 8);
assert(array_eq_len(x2, y2, 8));
pipe_producer_free(pro);
pipe_consumer_free(con);
}
#endif
*/
/*
* TEST IDEAS:
*
* - Create a fuzzer. Output random seed at program start (allow seed to be
* passed as a parameter). Put random amounts (and values) of data in one end
* of the queue, have some algorithm processing it. Do this whole bunches. If
* shit goes south, we can restart the program with the random seed and get a
* reproducable thing. Or get core dumps. Whichever is easier. Do something
* simple like randomly putting in every number from 1-10000, then ensuring
* that all the numbers are recieved on the other end, even with multiple
* consumers (and possibly multiple producers).
*/
#define RUN_TEST(name) \
do { printf("%s ->", #name); test_##name(); printf(" [ OK ]\n"); } while(0)
void pipe_run_test_suite(void)
{
RUN_TEST(basic_storage);
RUN_TEST(pipeline_multiplier);
RUN_TEST(parallel_multiplier);
RUN_TEST(issue_4);
RUN_TEST(issue_5);
RUN_TEST(issue_6_a);
RUN_TEST(issue_6_b);
RUN_TEST(issue_6_c);
/*
#ifdef PIPE_DEBUG
RUN_TEST(clobbering);
#endif
*/
}
/* vim: set et ts=4 sw=4 softtabstop=4 textwidth=80: */