ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
test_circular_buffer_io_fd_poll.c
2#include "externals/unity.h"
3#include <fcntl.h>
4#include <stdio.h>
5#include <sys/stat.h>
6#include <sys/types.h>
7#include <unistd.h>
8
9#define bufsize 8
10uint8_t in_raw_buf[bufsize];
11uint8_t out_raw_buf[bufsize];
12
15
17
18void setUp(void) {
19 // set stuff up here
20 circular_buffer_init_uint8(&in_buf, bufsize, in_raw_buf);
21 circular_buffer_init_uint8(&out_buf, bufsize, out_raw_buf);
22}
23
24void tearDown(void) {
25 // clean stuff up here
26}
27
28void test_circular_buffer_io_fd_poll_init(void) {
29 const int infd = open("/dev/zero", O_RDONLY);
30 if (infd == -1) {
31 perror("Couldn't open /dev/zero");
32 TEST_FAIL_MESSAGE("Couldn't open input file /dev/zero");
33 }
34 const int outfd = open("/dev/null", O_WRONLY | O_APPEND);
35 if (outfd == -1) {
36 perror("Couldn't open /dev/null");
37 TEST_FAIL_MESSAGE("Couldn't open output file /dev/null");
38 }
39
40 circular_buffer_io_fd_poll_init(&cb_io, &in_buf, &out_buf, infd, outfd);
41
42 TEST_ASSERT_EQUAL_PTR(&in_buf, cb_io.in_buf);
43 TEST_ASSERT_EQUAL_PTR(&out_buf, cb_io.out_buf);
44 TEST_ASSERT_EQUAL(infd, cb_io.fd_in);
45 TEST_ASSERT_EQUAL(outfd, cb_io.fd_out);
46}
47
48void test_circular_buffer_io_fd_poll_do_poll(void) {
49 const int infd = open("/dev/zero", O_RDONLY);
50 if (infd == -1) {
51 perror("Couldn't open /dev/zero");
52 TEST_FAIL_MESSAGE("Couldn't open input file /dev/zero");
53 }
54 const int outfd = open("/dev/null", O_WRONLY | O_APPEND);
55 if (outfd == -1) {
56 perror("Couldn't open /dev/null");
57 TEST_FAIL_MESSAGE("Couldn't open output file /dev/null");
58 }
59
60 circular_buffer_io_fd_poll_init(&cb_io, &in_buf, &out_buf, infd, outfd);
61
62 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
63 TEST_FAIL_MESSAGE("Error while polling");
64 }
65
66 // pollin always set, pollout not set b/c out buf empty
67 // circular_buffer_io_fd_poll_print(&cb_io,stderr);
68 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].events & POLLIN) > 0); // input pollin
69 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].revents & POLLIN) > 0);
70 TEST_ASSERT_EQUAL(0, (cb_io.fds[1].events & POLLOUT) > 0); // output pollout
71 TEST_ASSERT_EQUAL(0, (cb_io.fds[1].revents & POLLOUT) > 0);
72
74
75 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
76 TEST_FAIL_MESSAGE("Error while polling");
77 }
78
79 // pollin always set, pollout set b/c out buf not empty
80 // circular_buffer_io_fd_poll_print(&cb_io,stderr);
81 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].events & POLLIN) > 0); // input pollin
82 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].revents & POLLIN) > 0);
83 TEST_ASSERT_EQUAL(1, (cb_io.fds[1].events & POLLOUT) > 0); // output pollout
84 TEST_ASSERT_EQUAL(1, (cb_io.fds[1].revents & POLLOUT) > 0);
85
87
88 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
89 TEST_FAIL_MESSAGE("Error while polling");
90 }
91
92 // pollin always set, pollout not set b/c out buf empty
93 // circular_buffer_io_fd_poll_print(&cb_io,stderr);
94 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].events & POLLIN) > 0); // input pollin
95 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].revents & POLLIN) > 0);
96 TEST_ASSERT_EQUAL(0, (cb_io.fds[1].events & POLLOUT) > 0); // output pollout
97 TEST_ASSERT_EQUAL(0, (cb_io.fds[1].revents & POLLOUT) > 0);
98}
99
100void test_circular_buffer_io_fd_poll_do_input(void) {
101 const int infd = open("/dev/zero", O_RDONLY);
102 if (infd == -1) {
103 perror("Couldn't open /dev/zero");
104 TEST_FAIL_MESSAGE("Couldn't open input file /dev/zero");
105 }
106 const int outfd = open("/dev/null", O_WRONLY | O_APPEND);
107 if (outfd == -1) {
108 perror("Couldn't open /dev/null");
109 TEST_FAIL_MESSAGE("Couldn't open output file /dev/null");
110 }
111
112 circular_buffer_io_fd_poll_init(&cb_io, &in_buf, &out_buf, infd, outfd);
113
114 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
115 TEST_FAIL_MESSAGE("Error while polling");
116 }
117
118 size_t nRead = circular_buffer_io_fd_poll_do_input(&cb_io);
119 TEST_ASSERT_EQUAL_size_t(bufsize, nRead);
120 for (size_t i = 0; i < bufsize; i++) {
121 TEST_ASSERT_EQUAL_size_t(0, circular_buffer_pop_front_uint8(&in_buf));
122 }
123
124 for (size_t i = 0; i < 5; i++) {
126 }
127
128 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
129 TEST_FAIL_MESSAGE("Error while polling");
130 }
131
133 TEST_ASSERT_GREATER_THAN_size_t(0, nRead);
134}
135
136void test_circular_buffer_io_fd_poll_do_output(void) {
137 const int infd = open("/dev/zero", O_RDONLY);
138 if (infd == -1) {
139 perror("Couldn't open /dev/zero");
140 TEST_FAIL_MESSAGE("Couldn't open input file /dev/zero");
141 }
142 const int outfd = open("/dev/null", O_WRONLY | O_APPEND);
143 if (outfd == -1) {
144 perror("Couldn't open /dev/null");
145 TEST_FAIL_MESSAGE("Couldn't open output file /dev/null");
146 }
147
148 circular_buffer_io_fd_poll_init(&cb_io, &in_buf, &out_buf, infd, outfd);
149
150 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
151 TEST_FAIL_MESSAGE("Error while polling");
152 }
153 size_t nWritten = circular_buffer_io_fd_poll_do_output(&cb_io);
154 TEST_ASSERT_EQUAL_size_t(0, nWritten);
155
156 for (size_t i = 0; i < bufsize; i++) {
158 }
159 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
160 TEST_FAIL_MESSAGE("Error while polling");
161 }
162 nWritten = circular_buffer_io_fd_poll_do_output(&cb_io);
163 TEST_ASSERT_EQUAL_size_t(bufsize, nWritten);
164 TEST_ASSERT_EQUAL_size_t(0, circular_buffer_get_size_uint8(&out_buf));
165
166 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
167 TEST_FAIL_MESSAGE("Error while polling");
168 }
169 nWritten = circular_buffer_io_fd_poll_do_output(&cb_io);
170 TEST_ASSERT_EQUAL_size_t(0, nWritten);
171
172 for (size_t i = 0; i < bufsize / 2; i++) {
174 }
175 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
176 TEST_FAIL_MESSAGE("Error while polling");
177 }
178 nWritten = circular_buffer_io_fd_poll_do_output(&cb_io);
179 TEST_ASSERT_EQUAL_size_t(bufsize / 2, nWritten);
180 TEST_ASSERT_EQUAL_size_t(0, circular_buffer_get_size_uint8(&out_buf));
181
182 for (size_t i = 0; i < 4 * bufsize + bufsize / 2; i++) {
184 }
185 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
186 TEST_FAIL_MESSAGE("Error while polling");
187 }
188 nWritten = circular_buffer_io_fd_poll_do_output(&cb_io);
189 TEST_ASSERT_EQUAL_size_t(bufsize / 2, nWritten);
190 TEST_ASSERT_EQUAL_size_t(bufsize / 2,
192 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
193 TEST_FAIL_MESSAGE("Error while polling");
194 }
195 nWritten = circular_buffer_io_fd_poll_do_output(&cb_io);
196 TEST_ASSERT_EQUAL_size_t(bufsize / 2, nWritten);
197 TEST_ASSERT_EQUAL_size_t(0, circular_buffer_get_size_uint8(&out_buf));
198}
199
200void test_circular_buffer_io_fd_poll_do_in_and_out(void) {
201 const int infd = open("/dev/zero", O_RDONLY);
202 if (infd == -1) {
203 perror("Couldn't open /dev/zero");
204 TEST_FAIL_MESSAGE("Couldn't open input file /dev/zero");
205 }
206 const int outfd = open("/dev/null", O_WRONLY | O_APPEND);
207 if (outfd == -1) {
208 perror("Couldn't open /dev/null");
209 TEST_FAIL_MESSAGE("Couldn't open output file /dev/null");
210 }
211
212 circular_buffer_io_fd_poll_init(&cb_io, &in_buf, &out_buf, infd, outfd);
213
214 for (size_t i = 0; i < 10; i++) {
215 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
216 TEST_FAIL_MESSAGE("Error while polling");
217 }
218 size_t nRead = circular_buffer_io_fd_poll_do_input(&cb_io);
219 TEST_ASSERT_EQUAL_size_t(bufsize, nRead);
220 size_t inbufsize = circular_buffer_get_size_uint8(&in_buf);
221 TEST_ASSERT_EQUAL_size_t(bufsize, inbufsize);
222 for (size_t j = 0; j < inbufsize; j++) {
225 }
226 TEST_ASSERT_EQUAL_size_t(0, circular_buffer_get_size_uint8(&in_buf));
227 TEST_ASSERT_EQUAL_size_t(inbufsize,
229 /////
230 if (circular_buffer_io_fd_poll_do_poll(&cb_io, 20) != 0) {
231 TEST_FAIL_MESSAGE("Error while polling");
232 }
233 size_t nWritten = circular_buffer_io_fd_poll_do_output(&cb_io);
234 TEST_ASSERT_EQUAL_size_t(bufsize, nWritten);
235 TEST_ASSERT_EQUAL_size_t(0, circular_buffer_get_size_uint8(&in_buf));
236 TEST_ASSERT_EQUAL_size_t(0, circular_buffer_get_size_uint8(&out_buf));
237 }
238}
239
240void test_circular_buffer_io_fd_poll_print(void) {
241 const int infd = open("/dev/zero", O_RDONLY);
242 if (infd == -1) {
243 perror("Couldn't open /dev/zero");
244 TEST_FAIL_MESSAGE("Couldn't open input file /dev/zero");
245 }
246 const int outfd = open("/dev/null", O_WRONLY | O_APPEND);
247
248 circular_buffer_io_fd_poll_init(&cb_io, &in_buf, &out_buf, outfd, outfd);
249
250 FILE *devnull = fopen("/dev/null", "w");
251 if (devnull == NULL) {
252 perror("Error opening /dev/null");
253 TEST_FAIL_MESSAGE("Error opening /dev/null");
254 }
255
256 circular_buffer_io_fd_poll_print(&cb_io, devnull);
257}
258
259int main(void) {
260 UNITY_BEGIN();
261 RUN_TEST(test_circular_buffer_io_fd_poll_init);
262 RUN_TEST(test_circular_buffer_io_fd_poll_do_poll);
263 RUN_TEST(test_circular_buffer_io_fd_poll_do_input);
264 RUN_TEST(test_circular_buffer_io_fd_poll_do_output);
265 RUN_TEST(test_circular_buffer_io_fd_poll_do_in_and_out);
266 RUN_TEST(test_circular_buffer_io_fd_poll_print);
267 return UNITY_END();
268}
uint8_t circular_buffer_pop_back_uint8(circular_buffer_uint8 *circ_buf)
circular buffer pop back
size_t circular_buffer_get_size_uint8(const circular_buffer_uint8 *circ_buf)
circular buffer get number of elements
void circular_buffer_init_uint8(circular_buffer_uint8 *circ_buf, const size_t capacity, uint8_t *buffer)
circular buffer init method
uint8_t circular_buffer_pop_front_uint8(circular_buffer_uint8 *circ_buf)
circular buffer pop front
void circular_buffer_push_back_uint8(circular_buffer_uint8 *circ_buf, const uint8_t element)
circular buffer push back
uint8_t circular_buffer_io_fd_poll_do_poll(circular_buffer_io_fd_poll *cb_io, int timeout)
poll circular buffer IO with file descriptor polling object
void circular_buffer_io_fd_poll_init(circular_buffer_io_fd_poll *cb_io, circular_buffer_uint8 *in_buf, circular_buffer_uint8 *out_buf, int fd_in, int fd_out)
Initialize circular buffer IO with file descriptor polling object.
size_t circular_buffer_io_fd_poll_do_input(circular_buffer_io_fd_poll *cb_io)
circular buffer IO with file descriptor polling object: read from input fd
size_t circular_buffer_io_fd_poll_do_output(circular_buffer_io_fd_poll *cb_io)
circular buffer IO with file descriptor polling object: write to output fd
Circular buffer IO with file descriptor polling.
Circular buffer IO with file descriptor polling struct.
circular buffer struct