source: test/test-datastruct-deque.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@…>, 7 years ago

Add tests for deque, ring_buffer and vector.

  • Property mode set to 100644
File size: 2.2 KB
Line 
1#include "data-struct/deque.h"
2#include <pthread.h>
3#include <assert.h>
4
5#define TEST_SIZE 1000000
6
7static void * producer(void * a) {
8        libtrace_queue_t * deque = (libtrace_queue_t *) a;
9        int i;
10        for (i = 0; i < TEST_SIZE; i++)
11                libtrace_deque_push_front(deque, &i);
12        return 0;
13}
14
15static void * consumer(void * a) {
16        libtrace_queue_t * deque = (libtrace_queue_t *) a;
17        int i, value;
18        for (i = 0; i < TEST_SIZE; i++) {
19                /* We are going to be running quite slow */
20                while (libtrace_deque_pop_tail(deque, &value) == 0);
21                assert(value == i);
22        }
23        return 0;
24}
25
26/**
27 * Tests the deque data structure, first this establishes that single
28 * threaded operations work correctly, then does a basic consumer producer
29 * thread-safety test.
30 */
31int main() {
32        int i, value;
33        pthread_t t[2];
34        libtrace_queue_t deque;
35
36        libtrace_deque_init(&deque, sizeof(int));
37        assert(libtrace_deque_get_size(&deque) == 0);
38
39        /* Fill the deqeue like so (TEST_SIZE-1) ... 2 1 0 0 1 2... (TEST_SIZE-1) */
40        for (i = 0; i < TEST_SIZE; i++)
41                libtrace_deque_push_back(&deque, &i);
42        for (i = 0; i < TEST_SIZE; i++)
43                libtrace_deque_push_front(&deque, &i);
44
45        assert(libtrace_deque_get_size(&deque) == TEST_SIZE * 2);
46
47        /* Now verify and remove */
48        for (i = TEST_SIZE-1; i >= 0; i--) {
49                value = -1;
50                assert(libtrace_deque_peek_front(&deque, &value));
51                assert(value == i);
52                value = -1;
53                assert(libtrace_deque_pop_front(&deque, &value));
54                assert(value == i);
55                value = -1;
56                assert(libtrace_deque_peek_tail(&deque, &value));
57                assert(value == i);
58                value = -1;
59                assert(libtrace_deque_pop_tail(&deque, &value));
60                assert(value == i);
61        }
62        // It's empty make sure nothing works
63        value = -1;
64        assert(!libtrace_deque_peek_front(&deque, &value));
65        assert(!libtrace_deque_pop_front(&deque, &value));
66        assert(!libtrace_deque_peek_tail(&deque, &value));
67        assert(!libtrace_deque_pop_tail(&deque, &value));
68        assert(value == -1);
69
70        // Test thread safety - We only really care about the single producer single
71        // consumer case
72        pthread_create(&t[0], NULL, &producer, (void *) &deque);
73        pthread_create(&t[1], NULL, &consumer, (void *) &deque);
74
75        pthread_join(t[0], NULL);
76        pthread_join(t[1], NULL);
77        assert(libtrace_deque_get_size(&deque) == 0);
78
79        return 0;
80}
Note: See TracBrowser for help on using the repository browser.