static char* test_cbuffer_transfer_data(void) { CircularBuffer* src = cbuffer_new(); CircularBuffer* dst = cbuffer_new(); for (int i = 0; i < 200; ++i) { cbuffer_push_back(src, i); } mu_assert_eq("Src buffer size", cbuffer_size(src), 200); mu_assert_eq("Dst buffer size", cbuffer_size(dst), 0); // transfer data from src -> dst cbuffer_transfer_data(src, dst); mu_assert_eq("Src buffer size post transfer", cbuffer_size(src), 0); mu_assert_eq("Dst buffer size post transfer", cbuffer_size(dst), 200); // check content for (int i = 0; i < 200; ++i) { mu_assert_eq("Dst content", cbuffer_pop_front(dst), i); } cbuffer_free(src); cbuffer_free(dst); return 0; }
int main() { xil_printf("Master SPI oRSC echo test\n"); // initialize stdout. init_platform(); tx_buffer = cbuffer_new(); rx_buffer = cbuffer_new(); vme_stream = vmestream_initialize_mem( rx_buffer, tx_buffer, (uint32_t*)ORSC_2_PC_SIZE, (uint32_t*)PC_2_ORSC_SIZE, (uint32_t*)ORSC_2_PC_DATA, (uint32_t*)PC_2_ORSC_DATA, VMERAMSIZE); //printf("Master SPI oRSC echo test\n"); while (1) { // transfer data vmestream_transfer_data(vme_stream); // now echo the data while (cbuffer_size(rx_buffer) && cbuffer_freespace(tx_buffer)) { cbuffer_push_back(tx_buffer, cbuffer_pop_front(rx_buffer)); } } return 0; }
// Test decoding + re-encoding a transaction stream static char* test_ipbus_decode_encode_transaction(void) { CircularBuffer* packet = cbuffer_new(); // a read request. uint32_t header = ipbus_transaction_header( 2, // protocol 0xACE, // transaction id 5, // number of words to read IPBUS_READ, IPBUS_INFO_REQUEST); uint32_t payload = 0xBEEFFACE; cbuffer_push_back_net(packet, header); cbuffer_push_back_net(packet, payload); ipbus_transaction_t decoded = ipbus_decode_transaction(packet, 0); CircularBuffer* encoded = cbuffer_new(); ipbus_encode_transaction(encoded, &decoded, 0); mu_assert_eq("encode-decode length", cbuffer_size(encoded), 2); mu_assert("encode-decode", memcmp(encoded->data, packet->data, 8)==0); return 0; }
static char* test_ipbus_decode_encode_write_transaction(void) { CircularBuffer* packet = cbuffer_new(); // a read request. uint32_t header = ipbus_transaction_header( 2, // protocol 0xACE, // transaction id 5, // number of words to write IPBUS_WRITE, IPBUS_INFO_REQUEST); uint32_t baseaddr = 0xBEEFFACE; cbuffer_push_back_net(packet, header); cbuffer_push_back_net(packet, baseaddr); for (size_t i = 0; i < 5; ++i) { cbuffer_push_back_net(packet, i); } ipbus_transaction_t decoded = ipbus_decode_transaction(packet, 0); CircularBuffer* encoded = cbuffer_new(); ipbus_encode_transaction(encoded, &decoded, 0); mu_assert_eq("encode-decode length", cbuffer_size(encoded), 1 + 1 + 1 * 5); mu_assert("encode-decode", memcmp(encoded->data, packet->data, 7*4)==0); return 0; }
static char * test_forwardingtransaction_handle_one_transaction(void) { // make two pipes to simulate the forwarding serial port. int txpipefd[2]; pipe(txpipefd); int rxpipefd[2]; pipe(rxpipefd); // make txpipe nonblocking, so we can check if it's empty. fcntl(txpipefd[0], F_SETFL, fcntl(txpipefd[0], F_GETFL) | O_NONBLOCK); initialize_fowarding_fds(txpipefd[1], rxpipefd[0]); CircularBuffer* input = cbuffer_new(); cbuffer_push_back_net(input, ipbus_transaction_header(2, 0xCAB, 1, IPBUS_RMW, IPBUS_INFO_REQUEST)); cbuffer_push_back_net(input, 0xBEEFCAFE); cbuffer_push_back_net(input, 0xDEAFBEEF); cbuffer_push_back_net(input, 0xFACEBEEF); // RMW expects 1+1 words back cbuffer_push_back_net(input, ipbus_transaction_header(2, 0xBAD, 5, IPBUS_READ, IPBUS_INFO_REQUEST)); cbuffer_push_back_net(input, 0xBEEFCAFE); // READ expects 1+5 words back // write some junk onto the receiving pipe so we can check // we are reading the correct amount of return bytes. uint32_t junkwords[8] = { 0xDEADBEEF, 0xBEEFCAFE, 0xFACEBEEF, 0x12345678, 0xDEADFACE, 0xBADEBEEF, 0x87654321, 0xABABABAB}; write(rxpipefd[1], junkwords, 8 * sizeof(uint32_t)); CircularBuffer* input_copy = cbuffer_copy(input); CircularBuffer* output = cbuffer_new(); int words_consumed = handle_transaction_stream(input, 0, output); // should eat both transactions mu_assert_eq("ate everything i should", words_consumed, 6); // Make sure it passed it along the TX pipe ByteBuffer outputbuf = bytebuffer_ctor(NULL, 10 * sizeof(uint32_t)); // should pass along only 6 words. read(txpipefd[0], outputbuf.buf, 10 * sizeof(uint32_t)); mu_assert("forwarded trans", memcmp(outputbuf.buf, input_copy->data, 6*sizeof(uint32_t)) == 0); // there should no more data on the pipe mu_assert_eq("output pipe empty", read(txpipefd[0], outputbuf.buf, sizeof(uint32_t)), -1); // we expect header word + 1 payload word to be read from a RMW, // 1 header + 5 data words from the READ mu_assert_eq("output size", cbuffer_size(output), 8); mu_assert_eq("output content header", cbuffer_value_at(output, 0), 0xDEADBEEF); mu_assert_eq("output content payload", cbuffer_value_at(output, 1), 0xBEEFCAFE); mu_assert_eq("output content header 2", cbuffer_value_at(output, 2), 0xFACEBEEF); mu_assert_eq("output content payload 2", cbuffer_value_at(output, 3), 0x12345678); return 0; }
static char* test_ram2() { // -------------- // Setup // -------------- CircularBuffer* pc_input = cbuffer_new(); CircularBuffer* pc_output = cbuffer_new(); CircularBuffer* orsc_input = cbuffer_new(); CircularBuffer* orsc_output = cbuffer_new(); VMEStream *pc_stream = vmestream_initialize_heap(pc_input, pc_output, 2); VMEStream *orsc_stream = malloc(sizeof(VMEStream)); orsc_stream->input = orsc_input; orsc_stream->output = orsc_output; orsc_stream->local_send_size = pc_stream->remote_send_size; orsc_stream->local_recv_size = pc_stream->remote_recv_size; orsc_stream->remote_send_size = pc_stream->local_send_size; orsc_stream->remote_recv_size = pc_stream->local_recv_size; orsc_stream->recv_data = pc_stream->send_data; orsc_stream->send_data = pc_stream->recv_data; orsc_stream->MAXRAM = pc_stream->MAXRAM; cbuffer_push_back(pc_input, 0xDEADBEEF); cbuffer_push_back(orsc_input, 0xBEEFCAFE); vmestream_transfer_data(pc_stream); vmestream_transfer_data(orsc_stream); vmestream_transfer_data(pc_stream); mu_assert("Error: pc_input not empty", cbuffer_size(pc_input) == 0); mu_assert("Error: orsc_input not empty", cbuffer_size(orsc_input) == 0); mu_assert("Error: orsc_output.pop != DEADBEEF", cbuffer_pop_front(orsc_output)); mu_assert("Error: pc_output.pop != BEEFCAFE", cbuffer_pop_front(pc_output)); mu_assert("Error: pc_output not empty", cbuffer_size(pc_output) == 0); mu_assert("Error: orsc_output not empty", cbuffer_size(orsc_output) == 0); // -------------- // Tear-Down // -------------- vmestream_destroy_heap(pc_stream); free(orsc_stream); cbuffer_free(pc_input); cbuffer_free(orsc_input); cbuffer_free(pc_output); cbuffer_free(orsc_output); return 0; }
/** * Push less data to the buffers than we have RAM available */ static char *test_ram2() { // local application buffers CircularBuffer *tx1 = cbuffer_new(); CircularBuffer *rx1 = cbuffer_new(); CircularBuffer *tx2 = cbuffer_new(); CircularBuffer *rx2 = cbuffer_new(); VMEStream *test1 = vmestream_initialize(tx1, rx1, 2); VMEStream *test2 = malloc(sizeof(VMEStream)); test2->input = tx2; test2->output = rx2; test2->rx_size = test1->tx_size; test2->tx_size = test1->rx_size; test2->rx_data = test1->tx_data; test2->tx_data = test1->rx_data; test2->MAXRAM = test1->MAXRAM; // place only one word on the buffers cbuffer_push_back(tx1, 0xDEADBEEF); // put some output data on host #2 cbuffer_push_back(tx2, 0xBEEFCAFE); vmestream_transfer_data(test1); vmestream_transfer_data(test2); vmestream_transfer_data(test1); mu_assert("Error: tx1 not empty", 0 == cbuffer_size(tx1)); mu_assert("Error: tx2 not empty", 0 == cbuffer_size(tx2)); mu_assert("Error: 0xDEADBEEF != rx2.pop", 0xDEADBEEF == cbuffer_pop_front(rx2)); mu_assert("Error: 0xBEEFCAFE != rx1.pop", 0xBEEFCAFE == cbuffer_pop_front(rx1)); mu_assert("Error: rx2 not empty", 0 == cbuffer_size(rx2)); mu_assert("Error: rx1 not empty", 0 == cbuffer_size(rx1)); // free memory vmestream_destroy(test1); free(test2); cbuffer_free(tx1); cbuffer_free(rx1); cbuffer_free(tx2); cbuffer_free(rx2); return 0; }
static char* test_cbuffer_pop(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; uint32_t test_data[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cbuffer_append(mybuf, test_data, 11); Buffer* bucky = cbuffer_pop(mybuf, 5); mu_assert_eq("size", cbuffer_size(mybuf), 6); mu_assert_eq("content", memcmp(bucky->data, test_data, 5*sizeof(uint32_t)), 0); Buffer* badger = cbuffer_pop(mybuf, 6); mu_assert_eq("size2", cbuffer_size(mybuf), 0); mu_assert_eq("content2", memcmp(badger->data, test_data + 5, 6), 0); // if we pop an empty collection, we get nothing. Buffer* empty = cbuffer_pop(mybuf, 10); mu_assert_eq("size3", empty->size, 0); cbuffer_free(mybuf); buffer_free(bucky); buffer_free(badger); buffer_free(empty); return 0; }
// Test decoding a transaction stream header-only static char* test_ipbus_decode_transaction_header(void) { CircularBuffer* packet = cbuffer_new(); // a read request. uint32_t header = ipbus_transaction_header( 2, // protocol 0xFEE, // transaction id 5, // number of words to read IPBUS_READ, IPBUS_INFO_REQUEST); cbuffer_push_back_net(packet, header); ipbus_transaction_t decoded = ipbus_decode_transaction_header(packet, 0); mu_assert("trans decode err, id", decoded.id == 0xFEE); mu_assert("trans decode err, words", decoded.words == 5); mu_assert("trans decode err, info", decoded.info == IPBUS_INFO_REQUEST); mu_assert("trans decode err, type", decoded.type == IPBUS_READ); mu_assert("trans decode err, datasize", decoded.data.size == 1); // defined by IPBUS_READ // do nothing w/ the data mu_assert("trans decode err, data", decoded.data.words == NULL); mu_assert("trans size", ipbus_transaction_endocded_size(&decoded) == 2); return 0; }
static char* test_cbuffer_append_wraps(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; uint32_t test_data[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cbuffer_append(mybuf, test_data, 11); mu_assert_eq("pos", mybuf->pos, IO_BUFFER_SIZE - 5); mu_assert_eq("size", cbuffer_size(mybuf), 11); mu_assert_eq("tail content", memcmp( &(mybuf->data[mybuf->pos]), test_data, 5 * sizeof(uint32_t)), 0); // make sure we aren't trashing the memory after the buffer. //mu_assert_eq("tail content sanity", mybuf->data[IO_BUFFER_SIZE-1], 4); mu_assert_eq("head content", memcmp( mybuf->data, test_data + 5, 6 * sizeof(uint32_t)), 0); uint32_t test_data2[3] = {11, 12, 13}; cbuffer_append(mybuf, test_data2, 3); mu_assert_eq("size", cbuffer_size(mybuf), 14); mu_assert_eq("content", memcmp(&(mybuf->data[6]), test_data2, 3), 0); cbuffer_free(mybuf); return 0; }
static char* test_ipbus_stream_state() { int swapbytes = 2; CircularBuffer* test_buffer = cbuffer_new(); cbuffer_push_back_net(test_buffer, ipbus_packet_header(0xBEEF, 2)); cbuffer_push_back_net(test_buffer, 0xBADD); // garbage, shouldn't matter mu_assert_eq("hdr in stream", ipbus_stream_state(test_buffer, &swapbytes), IPBUS_ISTREAM_PACKET); mu_assert_eq("hdr endianness detect", swapbytes, 0); cbuffer_deletefront(test_buffer, 2); // A read request cbuffer_push_back_net(test_buffer, ipbus_transaction_header(2, 0xEEF, 2, IPBUS_READ, IPBUS_INFO_REQUEST)); cbuffer_push_back_net(test_buffer, 0xDEADBEEF); // We expect one extra word (the base addr) mu_assert_eq("read length", ipbus_transaction_payload_size(2, IPBUS_READ, IPBUS_INFO_REQUEST), 1); mu_assert_eq("trns in stream", ipbus_stream_state(test_buffer, &swapbytes), IPBUS_ISTREAM_FULL_TRANS); cbuffer_deletefront(test_buffer, 2); // A write request of 8 words, that isn't fully buffered cbuffer_push_back_net(test_buffer, ipbus_transaction_header(2, 0xEEF, 8, IPBUS_WRITE, IPBUS_INFO_REQUEST)); // a base addr + 8 data words mu_assert_eq("write length", ipbus_transaction_payload_size(8, IPBUS_WRITE, IPBUS_INFO_REQUEST), 9); mu_assert_eq("trns partial", ipbus_stream_state(test_buffer, &swapbytes), IPBUS_ISTREAM_PARTIAL_TRANS); cbuffer_deletefront(test_buffer, 1); mu_assert_eq("empty", ipbus_stream_state(test_buffer, &swapbytes), IPBUS_ISTREAM_EMPTY); return 0; }
static char* test_spi_stream_construct_empty_tx_packet(void) { CircularBuffer* mybuf = cbuffer_new(); uint32_t my_pkt_expected[10] = {0xBEEF, 0, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, -1}; add_checksum(my_pkt_expected, 10); int checksum_err = -1; spi_stream_verify_packet(my_pkt_expected, 10, &checksum_err); mu_assert_eq("no cksum err", checksum_err, 0); uint32_t my_pkt[10]; spi_stream_construct_tx_packet(0xBEEF, my_pkt, 10, mybuf); // for (int i = 0; i < 10; ++i) { // printf("%i %lx %lx\n", i, my_pkt_expected[i], my_pkt[i]); // } mu_assert_eq("packet", memcmp(my_pkt_expected, my_pkt, 10 * sizeof(uint32_t)), 0); spi_stream_verify_packet(my_pkt, 10, &checksum_err); mu_assert_eq("no cksum actual", checksum_err, 0); return 0; }
static char* test_spi_stream_read_rx_packet(void) { CircularBuffer* mybuf = cbuffer_new(); uint32_t my_pkt_expected[10] = {0xBEEF, 5, 1, 2, 3, 4, 5, 0xDEADBEEF, 0xDEADBEEF, -1}; uint32_t my_pkt_expected_2[10] = {0xBEEF, 7, 1, 2, 3, 4, 5, 1, 2, -1}; add_checksum(my_pkt_expected, 10); add_checksum(my_pkt_expected_2, 10); uint32_t my_data[7] = {1, 2, 3, 4, 5, 1, 2}; // read with non-full buffer int ret = spi_stream_read_rx_packet(my_pkt_expected, mybuf); mu_assert_eq("okay1", ret, 1); mu_assert_eq("data1", memcmp(my_data, mybuf->data, 5 * sizeof(uint32_t)), 0); mybuf->tail = 0; // read with full buffer ret = spi_stream_read_rx_packet(my_pkt_expected_2, mybuf); mu_assert_eq("okay2", ret, 1); mu_assert_eq("data2", memcmp(my_data, mybuf->data, 7 * sizeof(uint32_t)), 0); // overflowing local buffer mybuf->tail = IO_BUFFER_SIZE - 5; mu_assert_eq("its too small", cbuffer_freespace(mybuf), 4); ret = spi_stream_read_rx_packet(my_pkt_expected_2, mybuf); mu_assert_eq("overflow err", ret, 0); mu_assert_eq("unmodified", cbuffer_freespace(mybuf), 4); return 0; }
// Test decoding a transaction stream static char* test_ipbus_decode_transaction(void) { CircularBuffer* packet = cbuffer_new(); // a read request. uint32_t header = ipbus_transaction_header( 2, // protocol 0xACE, // transaction id 5, // number of words to read IPBUS_READ, IPBUS_INFO_REQUEST); uint32_t payload = 0xBEEFFACE; cbuffer_push_back_net(packet, header); cbuffer_push_back_net(packet, payload); ipbus_transaction_t decoded = ipbus_decode_transaction(packet, 0); //HEX_PRINT(packet[0]); //HEX_PRINT(packet[1]); //HEX_PRINT(decoded.id); //mu_assert("trans decode err, prot", decoded.protocol == 2); mu_assert("trans decode err, id", decoded.id == 0xACE); mu_assert("trans decode err, words", decoded.words == 5); mu_assert("trans decode err, info", decoded.info == IPBUS_INFO_REQUEST); mu_assert("trans decode err, type", decoded.type == IPBUS_READ); mu_assert("trans decode err, datasize", decoded.data.size == 1); // defined by IPBUS_READ mu_assert("trans decode err, data", decoded.data.words[0] == 0xBEEFFACE); mu_assert("trans size", ipbus_transaction_endocded_size(&decoded) == 2); return 0; }
static char* test_cbuffer_new(void) { CircularBuffer* mybuf = cbuffer_new(); mu_assert_eq("size", cbuffer_size(mybuf), 0); mu_assert_eq("pos", mybuf->pos, 0); mu_assert_eq("freespace", cbuffer_freespace(mybuf), IO_BUFFER_SIZE - 1); mu_assert_eq("init is zero", (mybuf->data[0]), 0); cbuffer_free(mybuf); return 0; }
static char* test_cbuffer_freespace(void) { CircularBuffer* mybuf = cbuffer_new(); mybuf->tail = IO_BUFFER_SIZE - 5; mu_assert_eq("freespace", cbuffer_freespace(mybuf), 4); mu_assert_eq("size", cbuffer_size(mybuf), IO_BUFFER_SIZE - 5); for (int i = 1; i < 5; ++i) { cbuffer_push_back(mybuf, i); mu_assert_eq("freespace", cbuffer_freespace(mybuf), 4 - i); } cbuffer_free(mybuf); return 0; }
static char* test_cbuffer_contiguous_data_size(void) { CircularBuffer* mybuf = cbuffer_new(); mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; mu_assert_eq("size0", cbuffer_contiguous_data_size(mybuf), 0); mybuf->tail = 10; mu_assert_eq("size5", cbuffer_contiguous_data_size(mybuf), 5); mybuf->pos = 3; mu_assert_eq("size7", cbuffer_contiguous_data_size(mybuf), 7); cbuffer_free(mybuf); return 0; }
static char* test_cbuffer_size(void) { CircularBuffer* mybuf = cbuffer_new(); mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; mu_assert_eq("size0", cbuffer_size(mybuf), 0); for (int i = 0; i < 15; ++i) { cbuffer_push_back(mybuf, i); mu_assert_eq("size", cbuffer_size(mybuf), i + 1); } cbuffer_free(mybuf); return 0; }
static char* test_cbuffer_value_at_wraps(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; uint32_t test_data[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cbuffer_append(mybuf, test_data, 11); for (int i = 0; i < 11; ++i) { mu_assert_eq("read at", cbuffer_value_at(mybuf, i), test_data[i]); } cbuffer_free(mybuf); return 0; }
static char* test_cbuffer_fd_full(void) { // make sure we can stop reading if our read buffer is full // make pipes int pipefd[2]; pipe(pipefd); // make txpipe nonblocking, so we can check if it's empty. fcntl(pipefd[0], F_SETFL, fcntl(pipefd[0], F_GETFL) | O_NONBLOCK); int in = pipefd[1]; int out = pipefd[0]; CircularBuffer* frombuf = cbuffer_new(); for (int i = 0; i < 200; ++i) { cbuffer_push_back(frombuf, i); } mu_assert_eq("from size", cbuffer_size(frombuf), 200); CircularBuffer* tobuf = cbuffer_new(); tobuf->pos = IO_BUFFER_SIZE - 100; tobuf->tail = IO_BUFFER_SIZE - 100; ssize_t written = cbuffer_write_fd(frombuf, in, 200); mu_assert_eq("wrote to pipe", written, 200); mu_assert_eq("from size after", cbuffer_size(frombuf), 0); tobuf->tail += IO_BUFFER_SIZE - 100; mu_assert_eq("to freespace", cbuffer_freespace(tobuf), 99); ssize_t read = cbuffer_read_fd(tobuf, out, 200); ssize_t exp = 99; mu_assert_eq("read from pipe", (int)read, (int)exp); cbuffer_free(frombuf); cbuffer_free(tobuf); return 0; }
static char* test_cbuffer_fd_features(void) { // make pipes int pipefd[2]; pipe(pipefd); // make txpipe nonblocking, so we can check if it's empty. fcntl(pipefd[0], F_SETFL, fcntl(pipefd[0], F_GETFL) | O_NONBLOCK); int in = pipefd[1]; int out = pipefd[0]; CircularBuffer* frombuf = cbuffer_new(); for (int i = 0; i < 200; ++i) { cbuffer_push_back(frombuf, i); } mu_assert_eq("from size", cbuffer_size(frombuf), 200); CircularBuffer* tobuf = cbuffer_new(); tobuf->pos = IO_BUFFER_SIZE - 100; tobuf->tail = IO_BUFFER_SIZE - 100; ssize_t written = cbuffer_write_fd(frombuf, in, 200); mu_assert_eq("wrote to pipe", written, 200); mu_assert_eq("from size after", cbuffer_size(frombuf), 0); ssize_t read = cbuffer_read_fd(tobuf, out, 200); mu_assert_eq("read from pipe", read, 200); for (int i = 0; i < 200; ++i) { mu_assert_eq("fd closure value", cbuffer_value_at(tobuf, i), i); } cbuffer_free(frombuf); cbuffer_free(tobuf); return 0; }
static char* test_cbuffer_net_features(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer cbuffer_push_back_net(mybuf, 0xDEADBEEF); cbuffer_push_back_net(mybuf, 0xBEEFFACE); cbuffer_push_back_net(mybuf, 0xDEADFACE); mu_assert_eq("item0", cbuffer_value_at_net(mybuf, 0), 0xDEADBEEF); mu_assert_eq("item1", cbuffer_value_at_net(mybuf, 1), 0xBEEFFACE); mu_assert_eq("item2", cbuffer_value_at_net(mybuf, 2), 0xDEADFACE); cbuffer_free(mybuf); return 0; }
static char* test_cbuffer_read_wraps(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; uint32_t test_data[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cbuffer_append(mybuf, test_data, 11); Buffer* readout = buffer_new(NULL, 11); cbuffer_read(mybuf, readout->data, 11); mu_assert_eq("size", readout->size, 11); mu_assert_eq("content", memcmp(readout->data, test_data, 11 * sizeof(uint32_t)), 0); cbuffer_free(mybuf); buffer_free(readout); return 0; }
static char* test_cbuffer_push_back(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer mybuf->pos = IO_BUFFER_SIZE - 2; mybuf->tail = IO_BUFFER_SIZE - 2; cbuffer_push_back(mybuf, 0xDEADBEEF); cbuffer_push_back(mybuf, 0xBEEFFACE); cbuffer_push_back(mybuf, 0xDEADFACE); mu_assert_eq("size", cbuffer_size(mybuf), 3); mu_assert_eq("pos", mybuf->pos, IO_BUFFER_SIZE-2); mu_assert_eq("item0", mybuf->data[mybuf->pos], 0xDEADBEEF); mu_assert_eq("item1", mybuf->data[mybuf->pos + 1], 0xBEEFFACE); mu_assert_eq("item2", mybuf->data[0], 0xDEADFACE); cbuffer_free(mybuf); return 0; }
static char* test_cbuffer_read(void) { CircularBuffer* mybuf = cbuffer_new(); Buffer* readout = buffer_new(NULL, 11); uint32_t test_data[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cbuffer_append(mybuf, test_data, 11); cbuffer_read(mybuf, readout->data, 11); mu_assert_eq("size", readout->size, 11); mu_assert_eq("content", memcmp(readout->data, test_data, 11 * sizeof(uint32_t)), 0); // if we ask for too much it cbuffer gives us what it has. Buffer* readout2 = buffer_new(NULL, 30); int actually_read = cbuffer_read(mybuf, readout2->data, 30); mu_assert_eq("size2", actually_read, 11); mu_assert_eq("content2", memcmp(readout2->data, test_data, 11 * sizeof(uint32_t)), 0); cbuffer_free(mybuf); buffer_free(readout); buffer_free(readout2); return 0; }
static char* test_cbuffer_copy(void) { CircularBuffer* mybuf = cbuffer_new(); mu_assert_eq("size", cbuffer_size(mybuf), 0); mu_assert_eq("pos", mybuf->pos, 0); mu_assert_eq("tail", mybuf->tail, 0); for (uint32_t i = 0; i < IO_BUFFER_SIZE - 2; ++i) { cbuffer_push_back(mybuf, i); } mu_assert_eq("tail", mybuf->tail, IO_BUFFER_SIZE - 2); CircularBuffer* copy = cbuffer_copy(mybuf); mu_assert_eq("content", memcmp(mybuf->data, copy->data, IO_BUFFER_SIZE * sizeof(uint32_t)), 0); mu_assert_eq("pos copy", mybuf->pos, copy->pos); mu_assert_eq("tail copy", mybuf->tail, copy->tail); cbuffer_free(mybuf); cbuffer_free(copy); return 0; }
static char* test_cbuffer_append(void) { CircularBuffer* mybuf = cbuffer_new(); uint32_t test_data[5] = {0, 1, 2, 3, 4}; cbuffer_append(mybuf, test_data, 5); mu_assert_eq("pos", mybuf->pos, 0); mu_assert_eq("size", cbuffer_size(mybuf), 5); mu_assert_eq("content", memcmp(mybuf->data, test_data, 5 * sizeof(uint32_t)), 0); uint32_t test_data2[3] = {6, 7, 8}; cbuffer_append(mybuf, test_data2, 3); mu_assert_eq("pos", mybuf->pos, 0); mu_assert_eq("size", cbuffer_size(mybuf), 8); mu_assert_eq("content2", memcmp(mybuf->data, test_data, 5 * sizeof(uint32_t)), 0); mu_assert_eq("content3", memcmp(&(mybuf->data[5]), test_data2, 3 * sizeof(uint32_t)), 0); cbuffer_free(mybuf); return 0; }
static char* test_cbuffer_delete_front_wraps(void) { CircularBuffer* mybuf = cbuffer_new(); uint32_t test_data[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // put us at the end of the buffer mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; cbuffer_append(mybuf, test_data, 11); mu_assert_eq("content", memcmp(&(mybuf->data[mybuf->pos]), test_data, 5 * sizeof(uint32_t)), 0); int deleted = cbuffer_deletefront(mybuf, 5); mu_assert_eq("deleted", deleted, 5); mu_assert_eq("pos", mybuf->pos, 0); mu_assert_eq("size", cbuffer_size(mybuf), 6); mu_assert_eq("item0", mybuf->data[mybuf->pos], 5); mu_assert_eq("item1", mybuf->data[mybuf->pos+1], 6); mu_assert_eq("item2", mybuf->data[mybuf->pos+2], 7); mu_assert_eq("remaining content in cbuffer", memcmp( &(mybuf->data[0]), test_data + 5, 6 * sizeof(uint32_t)), 0); Buffer* readout = buffer_new(NULL, 6); cbuffer_read(mybuf, readout->data, 6); mu_assert_eq("remaining content", memcmp( readout->data, (test_data + 5), 6 * sizeof(uint32_t)), 0); // if we ask to delete everything, just return what was actually deleted. int deleted_just_to_end = cbuffer_deletefront(mybuf, 100); mu_assert_eq("deleted just to end", deleted_just_to_end, 6); mu_assert_eq("pos2", mybuf->pos, 6); mu_assert_eq("size2", cbuffer_size(mybuf), 0); cbuffer_free(mybuf); buffer_free(readout); return 0; }
static char* test_spi_stream_construct_tx_packet(void) { uint32_t my_data[5] = {1, 2, 3, 4, 5}; CircularBuffer* mybuf = cbuffer_new(); cbuffer_append(mybuf, my_data, 5); uint32_t my_pkt_expected[10] = {0xBEEF, 5, 1, 2, 3, 4, 5, 0xDEADBEEF, 0xDEADBEEF, -1}; add_checksum(my_pkt_expected, 10); int checksum_err = -1; spi_stream_verify_packet(my_pkt_expected, 10, &checksum_err); mu_assert_eq("no cksum err", checksum_err, 0); uint32_t my_pkt[10]; spi_stream_construct_tx_packet(0xBEEF, my_pkt, 10, mybuf); // for (int i = 0; i < 10; ++i) { // printf("%i %lx %lx\n", i, my_pkt_expected[i], my_pkt[i]); // } mu_assert_eq("packet", memcmp(my_pkt_expected, my_pkt, 10 * sizeof(uint32_t)), 0); mu_assert_eq("consumed", cbuffer_size(mybuf), 0); // put more in the buffer than we can consume at once cbuffer_append(mybuf, my_data, 5); cbuffer_append(mybuf, my_data, 5); cbuffer_append(mybuf, my_data, 5); uint32_t my_pkt_expected_2[10] = {0xBEEF, 7, 1, 2, 3, 4, 5, 1, 2, -1}; add_checksum(my_pkt_expected_2, 10); spi_stream_verify_packet(my_pkt_expected_2, 10, &checksum_err); mu_assert_eq("no cksum err 2", checksum_err, 0); spi_stream_construct_tx_packet(0xBEEF, my_pkt, 10, mybuf); mu_assert_eq("packet", memcmp(my_pkt_expected_2, my_pkt, 10 * sizeof(uint32_t)), 0); mu_assert_eq("consumed", cbuffer_size(mybuf), 8); spi_stream_verify_packet(my_pkt, 10, &checksum_err); mu_assert_eq("no cksum actual", checksum_err, 0); return 0; }
static char* test_cbuffer_pop_front(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; uint32_t test_data[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cbuffer_append(mybuf, test_data, 11); for (int i = 0; i < 11; i++) { mu_assert_eq("pre-pop size", cbuffer_size(mybuf), 11 - i); uint32_t value = cbuffer_pop_front(mybuf); mu_assert_eq("popped_content", (int)value, i); } // if we pop an empty collection, we get dead beef. uint32_t empty = cbuffer_pop_front(mybuf); mu_assert_eq("empty", empty, 0xDEADBEEF); cbuffer_free(mybuf); return 0; }