Statistics
| Branch: | Revision:

peerstreamer-src / Test / msg_buffer_test.c @ b87e7c7a

History | View | Annotate | Download (10.2 KB)

1
#include <malloc.h>
2
#include <assert.h>
3
#include <unistd.h>
4
#include <stdlib.h>
5
#include <string.h>
6

    
7
#include <msg_buffer.h>
8

    
9
static void msg_buffer_configure_ths_and_tos(struct msg_buffer *msgb,
10
                                             uint32_t th1_to_us,
11
                                             uint32_t th1_size,
12
                                             uint32_t th2_to_us,
13
                                             uint32_t th2_size,
14
                                             uint32_t start_to_us,
15
                                             uint32_t start_size,
16
                                             uint32_t flush_to_us)
17
{
18
        msg_buffer_set_ths_to(msgb, th1_to_us, th2_to_us);
19
        msg_buffer_set_ths_size(msgb, th1_size, th2_size);
20
        msg_buffer_set_start_buffering_to_us(msgb, start_to_us);
21
        msg_buffer_set_start_buf_size_th(msgb, start_size);
22
        msg_buffer_set_flush_to_us(msgb, flush_to_us);
23
}
24

    
25
void msg_buffer_init_test()
26
{
27

    
28
        struct msg_buffer *msgb;
29

    
30
        msgb = msg_buffer_init(0, "test");
31
        assert(msgb != NULL);
32
        msg_buffer_destroy(&msgb);
33
        assert(msgb == NULL);
34

    
35
        fprintf(stderr,"%s successfully passed!\n",__func__);
36
}
37

    
38
void msg_buffer_single_push_pop_test()
39
{
40
        struct msg_buffer *msgb;
41
        int i, res;
42
#define B1_SIZE 128
43
        uint8_t b1[B1_SIZE] = {0, };
44
        uint8_t *b1_check = NULL;
45
        int b1_size_check;
46

    
47
        for (i = 0; i < B1_SIZE; i++) {
48
                b1[i] = (uint8_t) rand();
49
        }
50

    
51
        msgb = msg_buffer_init(0, "test");
52
        assert(msgb != NULL);
53

    
54
        msg_buffer_configure_ths_and_tos(msgb,
55
                                         MSG_BUFFER_TH1_TO_US,
56
                                         0,
57
                                         MSG_BUFFER_TH2_TO_US,
58
                                         0,
59
                                         0,
60
                                         B1_SIZE >> 1,
61
                                         60000000);
62

    
63
        res = msg_buffer_push(msgb, b1, (uint32_t) B1_SIZE);
64
        assert(res == 0);
65

    
66
        res = msg_buffer_get_status(msgb, NULL);
67
        assert(res == MSG_BUFFER_DATA_READY);
68

    
69
        b1_size_check = msg_buffer_pop(msgb, &b1_check);
70
        assert(b1_size_check == B1_SIZE);
71
        res = memcmp(b1, b1_check, B1_SIZE);
72
        assert(res == 0);
73

    
74
        free(b1_check);
75
        b1_check = NULL;
76

    
77
        res = msg_buffer_get_status(msgb, NULL);
78
        assert(res == MSG_BUFFER_DATA_NOT_READY);
79

    
80
        b1_size_check = msg_buffer_pop(msgb, &b1_check);
81
        assert(b1_size_check == 0);
82
        assert(b1_check == NULL);
83

    
84
        msg_buffer_destroy(&msgb);
85
        assert(msgb == NULL);
86

    
87
#undef B1_SIZE
88

    
89
        fprintf(stderr,"%s successfully passed!\n",__func__);
90
}
91

    
92
void msg_buffer_push_pop_test()
93
{
94
        struct msg_buffer *msgb;
95
        int i, res;
96
        uint32_t current_size;
97

    
98
#define B1_SIZE 128
99
#define B2_SIZE 256
100
#define B3_SIZE 512
101

    
102
        uint8_t b1[B1_SIZE] = {0, };
103
        uint8_t b2[B2_SIZE] = {0, };
104
        uint8_t b3[B3_SIZE] = {0, };
105
        uint8_t *b_check = NULL;
106
        int b_size_check;
107

    
108
        for (i = 0; i < B1_SIZE; i++) {
109
                b1[i] = (uint8_t) rand();
110
        }
111

    
112
        for (i = 0; i < B2_SIZE; i++) {
113
                b2[i] = (uint8_t) rand();
114
        }
115

    
116
        for (i = 0; i < B3_SIZE; i++) {
117
                b3[i] = (uint8_t) rand();
118
        }
119

    
120
        msgb = msg_buffer_init(0, "test");
121
        assert(msgb != NULL);
122

    
123
        msg_buffer_configure_ths_and_tos(msgb,
124
                                         MSG_BUFFER_TH1_TO_US,
125
                                         0,
126
                                         MSG_BUFFER_TH2_TO_US,
127
                                         0,
128
                                         0,
129
                                         B1_SIZE >> 1,
130
                                         60000000);
131

    
132
        res = msg_buffer_push(msgb, b1, (uint32_t) B1_SIZE);
133
        assert(res == 0);
134

    
135
        current_size = msg_buffer_get_current_size(msgb);
136
        assert(current_size = B1_SIZE);
137

    
138
        res = msg_buffer_push(msgb, b2, (uint32_t) B2_SIZE);
139
        assert(res == 0);
140

    
141
        current_size = msg_buffer_get_current_size(msgb);
142
        assert(current_size = (B1_SIZE + B2_SIZE));
143

    
144
        res = msg_buffer_push(msgb, b3, (uint32_t) B3_SIZE);
145
        assert(res == 0);
146

    
147
        current_size = msg_buffer_get_current_size(msgb);
148
        assert(current_size = (B1_SIZE + B2_SIZE + B3_SIZE));
149

    
150
        res = msg_buffer_get_status(msgb, NULL);
151
        assert(res == MSG_BUFFER_DATA_READY);
152

    
153
        b_size_check = msg_buffer_pop(msgb, &b_check);
154
        assert(b_size_check == B1_SIZE);
155
        res = memcmp(b1, b_check, B1_SIZE);
156
        assert(res == 0);
157
        free(b_check);
158
        b_check = NULL;
159

    
160
        current_size = msg_buffer_get_current_size(msgb);
161
        assert(current_size = (B2_SIZE + B3_SIZE));
162

    
163
        res = msg_buffer_get_status(msgb, NULL);
164
        assert(res == MSG_BUFFER_DATA_READY);
165

    
166
        b_size_check = msg_buffer_pop(msgb, &b_check);
167
        assert(b_size_check == B2_SIZE);
168
        res = memcmp(b2, b_check, B2_SIZE);
169
        assert(res == 0);
170
        free(b_check);
171
        b_check = NULL;
172

    
173
        current_size = msg_buffer_get_current_size(msgb);
174
        assert(current_size = (B3_SIZE));
175

    
176
        res = msg_buffer_get_status(msgb, NULL);
177
        assert(res == MSG_BUFFER_DATA_READY);
178

    
179
        b_size_check = msg_buffer_pop(msgb, &b_check);
180
        assert(b_size_check == B3_SIZE);
181
        res = memcmp(b3, b_check, B3_SIZE);
182
        assert(res == 0);
183
        free(b_check);
184
        b_check = NULL;
185

    
186
        current_size = msg_buffer_get_current_size(msgb);
187
        assert(current_size == 0);
188

    
189
        res = msg_buffer_get_status(msgb, NULL);
190
        assert(res == MSG_BUFFER_DATA_NOT_READY);
191

    
192
        b_size_check = msg_buffer_pop(msgb, &b_check);
193
        assert(b_size_check == 0);
194
        assert(b_check == NULL);
195

    
196
        msg_buffer_destroy(&msgb);
197
        assert(msgb == NULL);
198

    
199
#undef B1_SIZE
200
#undef B2_SIZE
201
#undef B3_SIZE
202

    
203
        fprintf(stderr,"%s successfully passed!\n",__func__);
204
}
205

    
206
void msg_buffer_start_buffering_size_th_test()
207
{
208
        struct msg_buffer *msgb;
209
        int i, res;
210
#define START_SIZE (0x100000)
211
#define BN           (8)
212
#define B_SIZE           (START_SIZE / BN)
213
        uint8_t b[B_SIZE] = {0, };
214

    
215
        msgb = msg_buffer_init(0, "test");
216
        assert(msgb != NULL);
217

    
218
        msg_buffer_configure_ths_and_tos(msgb,
219
                                         MSG_BUFFER_TH1_TO_US,
220
                                         0,
221
                                         MSG_BUFFER_TH2_TO_US,
222
                                         0,
223
                                         60000000,
224
                                         START_SIZE,
225
                                         60000000);
226

    
227
        for (i = 0; i < (BN - 1); i++) {
228
                res = msg_buffer_push(msgb, b, (uint32_t) B_SIZE);
229
                assert(res == 0);
230

    
231
                res = msg_buffer_get_status(msgb, NULL);
232
                assert(res == MSG_BUFFER_DATA_START_BUF);
233
        }
234

    
235
        res = msg_buffer_push(msgb, b, (uint32_t) B_SIZE);
236
        assert(res == 0);
237

    
238
        res = msg_buffer_get_status(msgb, NULL);
239
        assert(res == MSG_BUFFER_DATA_READY);
240

    
241
        msg_buffer_destroy(&msgb);
242
        assert(msgb == NULL);
243

    
244
#undef START_SIZE
245
#undef BN
246
#undef B1_SIZE
247

    
248
        fprintf(stderr,"%s successfully passed!\n",__func__);
249

    
250
}
251

    
252
void msg_buffer_start_buffering_to_test()
253
{
254
        struct msg_buffer *msgb;
255
        int res;
256
#define START_SIZE  (0x100000)
257
#define START_TO_US (50000)
258
#define BN            (8)
259
#define B_SIZE            (START_SIZE / BN)
260
        uint8_t b[B_SIZE] = {0, };
261

    
262
        msgb = msg_buffer_init(0, "test");
263
        assert(msgb != NULL);
264

    
265
        msg_buffer_configure_ths_and_tos(msgb,
266
                                         MSG_BUFFER_TH1_TO_US,
267
                                         0,
268
                                         MSG_BUFFER_TH2_TO_US,
269
                                         0,
270
                                         START_TO_US,
271
                                         START_SIZE,
272
                                         60000000);
273

    
274
        res = msg_buffer_push(msgb, b, (uint32_t) B_SIZE);
275
        assert(res == 0);
276

    
277
        res = msg_buffer_get_status(msgb, NULL);
278
        assert(res == MSG_BUFFER_DATA_START_BUF);
279

    
280
        usleep(START_TO_US + 1000);
281

    
282
        res = msg_buffer_get_status(msgb, NULL);
283
        assert(res == MSG_BUFFER_DATA_READY);
284

    
285
        msg_buffer_destroy(&msgb);
286
        assert(msgb == NULL);
287

    
288
#undef START_SIZE
289
#undef START_TO_US
290
#undef BN
291
#undef B1_SIZE
292

    
293
        fprintf(stderr,"%s successfully passed!\n",__func__);
294

    
295
}
296

    
297
void msg_buffer_full_buffer_test()
298
{
299
        struct msg_buffer *msgb;
300
        int res;
301
        uint32_t nslots, nslots_check, i;
302
        uint8_t *b = NULL;
303
        uint8_t *b_check = NULL;
304
        uint32_t b_check_size;
305

    
306
        msgb = msg_buffer_init(0, "test");
307
        assert(msgb != NULL);
308

    
309
        nslots = msg_buffer_get_nslots(msgb);
310

    
311
        b = malloc(nslots);
312
        assert(b != NULL);
313

    
314
        for (i = 0; i < nslots; i++) {
315
                b[i] = (uint8_t) rand();
316
        }
317

    
318
        msg_buffer_configure_ths_and_tos(msgb,
319
                                         MSG_BUFFER_TH1_TO_US,
320
                                         0,
321
                                         MSG_BUFFER_TH2_TO_US,
322
                                         0,
323
                                         0,
324
                                         0,
325
                                         60000000);
326

    
327
        for (i = 0; i < nslots; i++) {
328
                res = msg_buffer_push(msgb, &b[i], 1);
329
                assert(res == 0);
330
        }
331

    
332
        nslots_check = msg_buffer_get_nslots(msgb);
333
        assert(nslots == nslots_check);
334

    
335
        res = msg_buffer_get_status(msgb, NULL);
336
        assert(res == MSG_BUFFER_DATA_READY);
337

    
338
        for (i = 0; i < nslots; i++) {
339
                b_check_size = msg_buffer_pop(msgb, &b_check);
340
                assert(b_check_size == 1);
341
                res = memcmp(&b[i], b_check, 1);
342
                assert(res == 0);
343

    
344
                free(b_check);
345
                b_check = NULL;
346
        }
347

    
348
        msg_buffer_destroy(&msgb);
349
        assert(msgb == NULL);
350

    
351
        free(b);
352

    
353
        fprintf(stderr,"%s successfully passed!\n",__func__);
354

    
355
}
356

    
357
void msg_buffer_increase_nslots_test()
358
{
359
        struct msg_buffer *msgb;
360
        int res;
361
        uint32_t nslots, new_nslots, i;
362
        uint8_t *b = NULL;
363
        uint8_t *b_check = NULL;
364
        uint32_t b_check_size;
365

    
366
        msgb = msg_buffer_init(0, "test");
367
        assert(msgb != NULL);
368

    
369
        nslots = msg_buffer_get_nslots(msgb);
370

    
371
        b = malloc(nslots * 2);
372
        assert(b != NULL);
373

    
374
        for (i = 0; i < (nslots * 2); i++) {
375
                b[i] = i;
376
        }
377

    
378
        msg_buffer_configure_ths_and_tos(msgb,
379
                                         MSG_BUFFER_TH1_TO_US,
380
                                         0,
381
                                         MSG_BUFFER_TH2_TO_US,
382
                                         0,
383
                                         0,
384
                                         0,
385
                                         60000000);
386

    
387
        for (i = 0; i < (nslots * 2); i++) {
388
                res = msg_buffer_push(msgb, &b[i], 1);
389
                assert(res == 0);
390
        }
391

    
392
        new_nslots = msg_buffer_get_nslots(msgb);
393
        assert((nslots * 2) == new_nslots);
394

    
395
        res = msg_buffer_get_status(msgb, NULL);
396
        assert(res == MSG_BUFFER_DATA_READY);
397

    
398
        for (i = 0; i < new_nslots; i++) {
399
                b_check_size = msg_buffer_pop(msgb, &b_check);
400
                assert(b_check_size == 1);
401
                res = memcmp(&b[i], b_check, 1);
402
                assert(res == 0);
403

    
404
                free(b_check);
405
                b_check = NULL;
406
        }
407

    
408
        msg_buffer_destroy(&msgb);
409
        assert(msgb == NULL);
410

    
411
        free(b);
412

    
413
        fprintf(stderr,"%s successfully passed!\n",__func__);
414

    
415
}
416

    
417
void msg_buffer_flush_test()
418
{
419
        struct msg_buffer *msgb;
420
        int res, i;
421
#define START_SIZE  (0x100000)
422
#define START_TO_US (50000)
423
#define FLUSH_TO_US (50000)
424
#define BN            (8)
425
#define B_SIZE            (START_SIZE / BN)
426
        uint8_t b[B_SIZE] = {0, };
427
        uint8_t *b_check = NULL;
428

    
429
        msgb = msg_buffer_init(0, "test");
430
        assert(msgb != NULL);
431

    
432
        msg_buffer_configure_ths_and_tos(msgb,
433
                                         MSG_BUFFER_TH1_TO_US,
434
                                         0,
435
                                         MSG_BUFFER_TH2_TO_US,
436
                                         0,
437
                                         START_TO_US,
438
                                         START_SIZE,
439
                                         FLUSH_TO_US);
440

    
441
        for (i = 0; i < BN; i++) {
442
                res = msg_buffer_push(msgb, b, (uint32_t) B_SIZE);
443
                assert(res == 0);
444
        }
445

    
446
        res = msg_buffer_get_status(msgb, NULL);
447
        assert(res == MSG_BUFFER_DATA_READY);
448

    
449
        res = msg_buffer_is_flushing(msgb);
450
        assert(res == 0);
451

    
452
        usleep(FLUSH_TO_US + 1000);
453

    
454
        res = msg_buffer_get_status(msgb, NULL);
455
        res = msg_buffer_is_flushing(msgb);
456
        assert(res == 1);
457

    
458
        for (i = 0; i < BN; i++) {
459
                msg_buffer_pop(msgb, &b_check);
460

    
461
                free(b_check);
462
                b_check = NULL;
463
        }
464

    
465
        res = msg_buffer_get_status(msgb, NULL);
466
        assert(res == MSG_BUFFER_DATA_FLUSHED);
467

    
468
        msg_buffer_destroy(&msgb);
469
        assert(msgb == NULL);
470

    
471
#undef START_SIZE
472
#undef START_TO_US
473
#undef FLUSH_TO_US
474
#undef BN
475
#undef B1_SIZE
476

    
477
        fprintf(stderr,"%s successfully passed!\n",__func__);
478

    
479
}
480

    
481
int main(int argc, char ** argv)
482
{
483
        msg_buffer_init_test();
484
        msg_buffer_single_push_pop_test();
485
        msg_buffer_push_pop_test();
486
        msg_buffer_start_buffering_size_th_test();
487
        msg_buffer_start_buffering_to_test();
488
        msg_buffer_full_buffer_test();
489
        msg_buffer_increase_nslots_test();
490
        msg_buffer_flush_test();
491
        /* TODO:
492
         * - Better tests for mixed push/pop
493
         * - th1, th2 thresholds/timeouts tests
494
         * - msg_buffer_start_buf_reinit test
495
         * - parse mechanism test
496
         */
497
        return 0;
498
}