-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ring_buffer.c
138 lines (104 loc) · 2.94 KB
/
test_ring_buffer.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "ring_buffer.h"
static void test_ring_buffer_create(void)
{
ring_buffer_t * buf = NULL;
uint16_t head_index = 0U;
uint16_t size = 0U;
double sum = 0.0;
int ret = 0;
ret = ring_buffer_create(&buf, 10);
assert(ret == 0);
ret = ring_buffer_get_size(buf, &size);
assert(ret == 0);
assert(size == 10U);
ret = ring_buffer_get_head_index(buf, &head_index);
assert(ret == 0);
assert(head_index == 0U);
ret = ring_buffer_get_sum(buf, &sum);
assert(ret == 0);
assert(sum == 0U);
(void)ring_buffer_destroy(buf);
printf("\r%s passed\n", __func__);
}
static void test_ring_buffer_get_head_index(void)
{
ring_buffer_t * buf = NULL;
uint16_t head_index = 0U;
int ret = 0;
ret = ring_buffer_create(&buf, 10);
assert(ret == 0);
ret = ring_buffer_get_head_index(buf, &head_index);
assert(ret == 0);
assert(head_index == 0U);
(void)ring_buffer_add(buf, 1.0);
(void)ring_buffer_add(buf, 2.0);
ret = ring_buffer_get_head_index(buf, &head_index);
assert(ret == 0);
assert(head_index == 2U);
(void)ring_buffer_destroy(buf);
printf("\r%s passed\n", __func__);
}
static void test_ring_buffer_add(void)
{
ring_buffer_t * buf = NULL;
int ret = ring_buffer_create(&buf, 3);
assert(ret == 0);
ret = ring_buffer_add(buf, 1.0);
assert(ret == 0);
ret = ring_buffer_add(buf, 2.0);
assert(ret == 0);
ret = ring_buffer_add(buf, 3.0);
assert(ret == 0);
ret = ring_buffer_add(buf, 4.0);
assert(ret == 0);
double val;
ret = ring_buffer_get_element(buf, 0, &val);
assert(ret == 0);
assert(val == 2.0);
ret = ring_buffer_get_element(buf, 1, &val);
assert(ret == 0);
assert(val == 3.0);
ret = ring_buffer_get_element(buf, 2, &val);
assert(ret == 0);
assert(val == 4.0);
ret = ring_buffer_destroy(buf);
assert(ret == 0);
printf("\r%s passed\n", __func__);
}
static void test_ring_buffer_get(void)
{
ring_buffer_t * buf;
int ret = ring_buffer_create(&buf, 3);
assert(ret == 0);
ret = ring_buffer_add(buf, 1.0);
assert(ret == 0);
ret = ring_buffer_add(buf, 2.0);
assert(ret == 0);
ret = ring_buffer_add(buf, 3.0);
assert(ret == 0);
double val;
ret = ring_buffer_get_element(buf, 0, &val);
assert(ret == 0);
assert(val == 1.0);
ret = ring_buffer_get_element(buf, 1, &val);
assert(ret == 0);
assert(val == 2.0);
ret = ring_buffer_get_element(buf, 2, &val);
assert(ret == 0);
assert(val == 3.0);
ret = ring_buffer_get_element(buf, 3, &val);
assert(ret != 0);
(void)ring_buffer_destroy(buf);
printf("\r%s passed\n", __func__);
}
int main(void)
{
test_ring_buffer_create();
test_ring_buffer_get_head_index();
test_ring_buffer_add();
test_ring_buffer_get();
}