source: test/test-datastruct-ringbuffer.c @ df668eb

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since df668eb was df668eb, checked in by Richard Sanger <rsangerarj@…>, 8 years ago

Add tests for deque, ring_buffer and vector.

  • Property mode set to 100644
File size: 3.5 KB
Line 
1#include "data-struct/ring_buffer.h"
2#include <pthread.h>
3#include <assert.h>
4
5#define TEST_SIZE 1000000
6#define RINGBUFFER_SIZE 10000
7
8static void * producer(void * a) {
9        libtrace_ringbuffer_t * rb = (libtrace_ringbuffer_t *) a;
10        int i;
11        void * value;
12        for (i = 0; i < TEST_SIZE; i++) {
13                value = (void *) i;
14                libtrace_ringbuffer_write(rb, value);
15        }
16        return 0;
17}
18
19static void * consumer(void * a) {
20        libtrace_ringbuffer_t * rb = (libtrace_ringbuffer_t *) a;
21        int i;
22        void * value;
23        for (i = 0; i < TEST_SIZE; i++) {
24                value = libtrace_ringbuffer_read(rb);
25                assert(value == (void *) i);
26        }
27        return 0;
28}
29
30/**
31 * Tests the ringbuffer data structure, first this establishes that single
32 * threaded operations work correctly, then does a basic consumer producer
33 * thread-safety test.
34 */
35int main() {
36        int i;
37        void *value;
38        pthread_t t[4];
39        libtrace_ringbuffer_t rb_block;
40        libtrace_ringbuffer_t rb_polling;
41
42        libtrace_ringbuffer_init(&rb_block, RINGBUFFER_SIZE, LIBTRACE_RINGBUFFER_BLOCKING);
43        libtrace_ringbuffer_init(&rb_polling, RINGBUFFER_SIZE, LIBTRACE_RINGBUFFER_BLOCKING);
44        assert(libtrace_ringbuffer_is_empty(&rb_block));
45        assert(libtrace_ringbuffer_is_empty(&rb_polling));
46
47        for (i = 0; i < RINGBUFFER_SIZE; i++) {
48                value = (void *) i;
49                libtrace_ringbuffer_write(&rb_block, value);
50                libtrace_ringbuffer_write(&rb_polling, value);
51        }
52
53        assert(libtrace_ringbuffer_is_full(&rb_block));
54        assert(libtrace_ringbuffer_is_full(&rb_polling));
55
56        // Full so trying to write should fail
57        assert(!libtrace_ringbuffer_try_write(&rb_block, value));
58        assert(!libtrace_ringbuffer_try_write(&rb_polling, value));
59        assert(!libtrace_ringbuffer_try_swrite(&rb_block, value));
60        assert(!libtrace_ringbuffer_try_swrite(&rb_polling, value));
61        assert(!libtrace_ringbuffer_try_swrite_bl(&rb_block, value));
62        assert(!libtrace_ringbuffer_try_swrite_bl(&rb_polling, value));
63
64        // Cycle the buffer a few times
65        for (i = 0; i < TEST_SIZE; i++) {
66                value = (void *) -1;
67                value = libtrace_ringbuffer_read(&rb_block);
68                assert(value == (void *) i);
69                value = (void *) -1;
70                value = libtrace_ringbuffer_read(&rb_polling);
71                assert(value == (void *) i);
72                value = (void *) (i + RINGBUFFER_SIZE);
73                libtrace_ringbuffer_write(&rb_block, value);
74                libtrace_ringbuffer_write(&rb_polling, value);
75        }
76
77        // Empty it completely
78        for (i = TEST_SIZE; i < TEST_SIZE + RINGBUFFER_SIZE; i++) {
79                value = libtrace_ringbuffer_read(&rb_block);
80                assert(value == (void *) i);
81                value = libtrace_ringbuffer_read(&rb_polling);
82                assert(value == (void *) i);
83        }
84        assert(libtrace_ringbuffer_is_empty(&rb_block));
85        assert(libtrace_ringbuffer_is_empty(&rb_polling));
86
87        // Empty so trying to read should fail
88        assert(!libtrace_ringbuffer_try_read(&rb_block, &value));
89        assert(!libtrace_ringbuffer_try_read(&rb_polling, &value));
90        assert(!libtrace_ringbuffer_try_sread(&rb_block, &value));
91        assert(!libtrace_ringbuffer_try_sread(&rb_polling, &value));
92        assert(!libtrace_ringbuffer_try_sread_bl(&rb_block, &value));
93        assert(!libtrace_ringbuffer_try_sread_bl(&rb_polling, &value));
94
95        // Test thread safety - We only really care about the single producer single
96        // consumer case
97        pthread_create(&t[0], NULL, &producer, (void *) &rb_block);
98        pthread_create(&t[1], NULL, &consumer, (void *) &rb_block);
99        pthread_join(t[0], NULL);
100        pthread_join(t[1], NULL);
101        assert(libtrace_ringbuffer_is_empty(&rb_block));
102
103        pthread_create(&t[0], NULL, &producer, (void *) &rb_polling);
104        pthread_create(&t[1], NULL, &consumer, (void *) &rb_polling);
105        pthread_join(t[0], NULL);
106        pthread_join(t[1], NULL);
107        assert(libtrace_ringbuffer_is_empty(&rb_polling));
108
109        return 0;
110}
Note: See TracBrowser for help on using the repository browser.