2#include "externals/unity.h"
10uint8_t in_raw_buf[bufsize];
11uint8_t out_raw_buf[bufsize];
28void test_circular_buffer_io_fd_poll_init(
void) {
29 const int infd = open(
"/dev/zero", O_RDONLY);
31 perror(
"Couldn't open /dev/zero");
32 TEST_FAIL_MESSAGE(
"Couldn't open input file /dev/zero");
34 const int outfd = open(
"/dev/null", O_WRONLY | O_APPEND);
36 perror(
"Couldn't open /dev/null");
37 TEST_FAIL_MESSAGE(
"Couldn't open output file /dev/null");
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);
48void test_circular_buffer_io_fd_poll_do_poll(
void) {
49 const int infd = open(
"/dev/zero", O_RDONLY);
51 perror(
"Couldn't open /dev/zero");
52 TEST_FAIL_MESSAGE(
"Couldn't open input file /dev/zero");
54 const int outfd = open(
"/dev/null", O_WRONLY | O_APPEND);
56 perror(
"Couldn't open /dev/null");
57 TEST_FAIL_MESSAGE(
"Couldn't open output file /dev/null");
63 TEST_FAIL_MESSAGE(
"Error while polling");
68 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].events & POLLIN) > 0);
69 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].revents & POLLIN) > 0);
70 TEST_ASSERT_EQUAL(0, (cb_io.fds[1].events & POLLOUT) > 0);
71 TEST_ASSERT_EQUAL(0, (cb_io.fds[1].revents & POLLOUT) > 0);
76 TEST_FAIL_MESSAGE(
"Error while polling");
81 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].events & POLLIN) > 0);
82 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].revents & POLLIN) > 0);
83 TEST_ASSERT_EQUAL(1, (cb_io.fds[1].events & POLLOUT) > 0);
84 TEST_ASSERT_EQUAL(1, (cb_io.fds[1].revents & POLLOUT) > 0);
89 TEST_FAIL_MESSAGE(
"Error while polling");
94 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].events & POLLIN) > 0);
95 TEST_ASSERT_EQUAL(1, (cb_io.fds[0].revents & POLLIN) > 0);
96 TEST_ASSERT_EQUAL(0, (cb_io.fds[1].events & POLLOUT) > 0);
97 TEST_ASSERT_EQUAL(0, (cb_io.fds[1].revents & POLLOUT) > 0);
100void test_circular_buffer_io_fd_poll_do_input(
void) {
101 const int infd = open(
"/dev/zero", O_RDONLY);
103 perror(
"Couldn't open /dev/zero");
104 TEST_FAIL_MESSAGE(
"Couldn't open input file /dev/zero");
106 const int outfd = open(
"/dev/null", O_WRONLY | O_APPEND);
108 perror(
"Couldn't open /dev/null");
109 TEST_FAIL_MESSAGE(
"Couldn't open output file /dev/null");
115 TEST_FAIL_MESSAGE(
"Error while polling");
119 TEST_ASSERT_EQUAL_size_t(bufsize, nRead);
120 for (
size_t i = 0; i < bufsize; i++) {
124 for (
size_t i = 0; i < 5; i++) {
129 TEST_FAIL_MESSAGE(
"Error while polling");
133 TEST_ASSERT_GREATER_THAN_size_t(0, nRead);
136void test_circular_buffer_io_fd_poll_do_output(
void) {
137 const int infd = open(
"/dev/zero", O_RDONLY);
139 perror(
"Couldn't open /dev/zero");
140 TEST_FAIL_MESSAGE(
"Couldn't open input file /dev/zero");
142 const int outfd = open(
"/dev/null", O_WRONLY | O_APPEND);
144 perror(
"Couldn't open /dev/null");
145 TEST_FAIL_MESSAGE(
"Couldn't open output file /dev/null");
151 TEST_FAIL_MESSAGE(
"Error while polling");
154 TEST_ASSERT_EQUAL_size_t(0, nWritten);
156 for (
size_t i = 0; i < bufsize; i++) {
160 TEST_FAIL_MESSAGE(
"Error while polling");
163 TEST_ASSERT_EQUAL_size_t(bufsize, nWritten);
167 TEST_FAIL_MESSAGE(
"Error while polling");
170 TEST_ASSERT_EQUAL_size_t(0, nWritten);
172 for (
size_t i = 0; i < bufsize / 2; i++) {
176 TEST_FAIL_MESSAGE(
"Error while polling");
179 TEST_ASSERT_EQUAL_size_t(bufsize / 2, nWritten);
182 for (
size_t i = 0; i < 4 * bufsize + bufsize / 2; i++) {
186 TEST_FAIL_MESSAGE(
"Error while polling");
189 TEST_ASSERT_EQUAL_size_t(bufsize / 2, nWritten);
190 TEST_ASSERT_EQUAL_size_t(bufsize / 2,
193 TEST_FAIL_MESSAGE(
"Error while polling");
196 TEST_ASSERT_EQUAL_size_t(bufsize / 2, nWritten);
200void test_circular_buffer_io_fd_poll_do_in_and_out(
void) {
201 const int infd = open(
"/dev/zero", O_RDONLY);
203 perror(
"Couldn't open /dev/zero");
204 TEST_FAIL_MESSAGE(
"Couldn't open input file /dev/zero");
206 const int outfd = open(
"/dev/null", O_WRONLY | O_APPEND);
208 perror(
"Couldn't open /dev/null");
209 TEST_FAIL_MESSAGE(
"Couldn't open output file /dev/null");
214 for (
size_t i = 0; i < 10; i++) {
216 TEST_FAIL_MESSAGE(
"Error while polling");
219 TEST_ASSERT_EQUAL_size_t(bufsize, nRead);
221 TEST_ASSERT_EQUAL_size_t(bufsize, inbufsize);
222 for (
size_t j = 0; j < inbufsize; j++) {
227 TEST_ASSERT_EQUAL_size_t(inbufsize,
231 TEST_FAIL_MESSAGE(
"Error while polling");
234 TEST_ASSERT_EQUAL_size_t(bufsize, nWritten);
240void test_circular_buffer_io_fd_poll_print(
void) {
241 const int infd = open(
"/dev/zero", O_RDONLY);
243 perror(
"Couldn't open /dev/zero");
244 TEST_FAIL_MESSAGE(
"Couldn't open input file /dev/zero");
246 const int outfd = open(
"/dev/null", O_WRONLY | O_APPEND);
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");
256 circular_buffer_io_fd_poll_print(&cb_io, devnull);
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);
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.