int write_some(RingBuffer *buffer, int fd, int is_socket) { int rc = 0; bstring data = RingBuffer_get_all(buffer); check(data != NULL, "Failed to get data from the buffer."); check(bfindreplace(data, &NL, &CRLF, 0) == BSTR_OK, "Failed to replace NL."); if(is_socket) { rc = send(fd, bdata(data), blength(data), 0); } else { rc = write(fd, bdata(data), blength(data)); } check(rc == blength(data), "failed to write everything to fd: %d", fd); bdestroy(data); return rc; error: return -1; }
char *test_read_write() { RingBuffer_puts(rbuf, &test_data_0); mu_assert(rbuf->end == 6, "ends at not reset correctly"); mu_assert(RingBuffer_empty(rbuf) == 0, "not actually empty"); RingBuffer_puts(rbuf, &test_data_1); mu_assert(rbuf->end == 34, "ends at not reset correctly"); char *data_1 = malloc(sizeof(char) * 6); RingBuffer_read(rbuf, data_1, 6); mu_assert(bstrcmp(bfromcstr(data_1), &test_data_0) == 0, "read did not return proper string" ); mu_assert(rbuf->start == 6, "did not reset start correctly"); char *data_2 = malloc(sizeof(char) * 28); RingBuffer_read(rbuf, data_2, 28); mu_assert(bstrcmp(bfromcstr(data_2), &test_data_1) == 0, "read did not return proper string" ); mu_assert(rbuf->start == 0, "did not reset start correctly"); mu_assert(rbuf->end == 0, "did not reset end correctly"); RingBuffer_puts(rbuf, &test_data_2); RingBuffer_puts(rbuf, &test_data_3); mu_assert(rbuf->start == 0, "start not maintained"); mu_assert(rbuf->end == 64, "end not set correctly"); mu_assert(RingBuffer_full(rbuf) == 1, "RingBuffer actually full"); mu_assert(RingBuffer_available_space(rbuf) == 0, "RingBuffer has no space"); mu_assert(RingBuffer_available_data(rbuf) == 64, "RingBuffer has wrong available data"); bstring data_3 = RingBuffer_get_all(rbuf); bstring expected = bformat("%s%s", bdata(&test_data_2), bdata(&test_data_3)); mu_assert(bstrcmp(data_3, expected) == 0, "data not returned correctly"); mu_assert(RingBuffer_empty(rbuf) == 1, "RingBuffer should be empty"); return NULL; }