Testing
This commit is contained in:
parent
7833bf73a7
commit
d6a3c95951
3 changed files with 72 additions and 20 deletions
61
driver.c
61
driver.c
|
@ -5,19 +5,44 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define rb_size_t size_t
|
#define rb_size_t size_t
|
||||||
|
// #define rb_size_t int
|
||||||
#include "ringbuf.h"
|
#include "ringbuf.h"
|
||||||
|
|
||||||
|
typedef int DATATYPE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Debug print and empty the ringbuf
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
rb_debug_empty(struct RingBuf *rb)
|
||||||
|
{
|
||||||
|
int d;
|
||||||
|
if(rb->count == 0)
|
||||||
|
return;
|
||||||
|
printf("Debug Data: [");
|
||||||
|
while(rb_pop_front(rb, &d, memcpy) == ReadOk)
|
||||||
|
printf("%d,", d);
|
||||||
|
printf("\b]\n");
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(void)
|
main(void)
|
||||||
{
|
{
|
||||||
struct RingBuf rb;
|
struct RingBuf rb;
|
||||||
int d;
|
|
||||||
rb_init(&rb, 10, malloc, sizeof(int));
|
DATATYPE d;
|
||||||
const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
rb_init(&rb, 10, malloc, sizeof(DATATYPE));
|
||||||
|
|
||||||
|
rb_debug_print(&rb);
|
||||||
|
|
||||||
|
const DATATYPE arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||||
|
int arrlen = (rb_size_t)(sizeof(arr) / sizeof(DATATYPE));
|
||||||
|
|
||||||
// Single writes
|
// Single writes
|
||||||
printf("\n=== Single writes ===\n\n");
|
printf("\n=== Single writes ===\n\n");
|
||||||
|
|
||||||
|
rb_debug_print(&rb);
|
||||||
|
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
while(idx < 5) {
|
while(idx < 5) {
|
||||||
if(rb_push_back(&rb, &arr[idx], memcpy) != WriteOk) {
|
if(rb_push_back(&rb, &arr[idx], memcpy) != WriteOk) {
|
||||||
|
@ -26,22 +51,29 @@ main(void)
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rb_debug_print(&rb);
|
||||||
|
|
||||||
// Pop the last n elements
|
// Pop the last n elements
|
||||||
for(int a = 0; a < 1; a++) {
|
for(int a = 0; a < 10; a++) {
|
||||||
if(rb_pop_front(&rb, &d, memcpy) != ReadOk) {
|
if(rb_pop_front(&rb, &d, memcpy) != ReadOk) {
|
||||||
printf("Failed to read data from buffer...\n");
|
printf("Failed to read data from buffer...\n");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
printf("Data: %d\n", d);
|
printf("Data: %d\n", d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rb_debug_print(&rb);
|
||||||
|
if(rb.read_head == rb.write_head)
|
||||||
|
printf("OK\n");
|
||||||
printf("idx: %d\n", idx);
|
printf("idx: %d\n", idx);
|
||||||
|
|
||||||
// Push the rest
|
// Push the rest
|
||||||
while(rb_push_back(&rb, &arr[idx], memcpy) == WriteOk) {
|
while(idx < arrlen && rb_push_back(&rb, &arr[idx], memcpy) == WriteOk) {
|
||||||
printf("Wrote: %d\n", arr[idx]);
|
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rb_debug_print(&rb);
|
||||||
|
|
||||||
printf("Data: [");
|
printf("Data: [");
|
||||||
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
|
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
|
||||||
printf("%d,", d);
|
printf("%d,", d);
|
||||||
|
@ -50,25 +82,20 @@ main(void)
|
||||||
// Multiple writes
|
// Multiple writes
|
||||||
printf("\n=== Multiple writes ===\n\n");
|
printf("\n=== Multiple writes ===\n\n");
|
||||||
|
|
||||||
rb_clear(&rb);
|
rb_clear(&rb); // Make sure
|
||||||
|
rb_debug_print(&rb);
|
||||||
|
|
||||||
int ok = WriteOk; // Assume we can write
|
int ok = WriteOk; // Assume we can write
|
||||||
if(rb_push_many(&rb, arr, memcpy, 8) != WriteOk) {
|
if(rb_push_many(&rb, arr, memcpy, 8) != WriteOk) {
|
||||||
printf("Failed to write data to buffer...\n");
|
printf("Failed to write data to buffer...\n");
|
||||||
}
|
}
|
||||||
|
rb_debug_print(&rb);
|
||||||
printf("Data: [");
|
rb_debug_empty(&rb);
|
||||||
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
|
|
||||||
printf("%d,", d);
|
|
||||||
printf("\b]\n");
|
|
||||||
|
|
||||||
// Test wrap around
|
// Test wrap around
|
||||||
rb_push_many(&rb, arr, memcpy, 10);
|
rb_push_many(&rb, arr, memcpy, 10);
|
||||||
|
rb_debug_print(&rb);
|
||||||
printf("Data: [");
|
rb_debug_empty(&rb);
|
||||||
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
|
|
||||||
printf("%d,", d);
|
|
||||||
printf("\b]\n");
|
|
||||||
|
|
||||||
// Test clear
|
// Test clear
|
||||||
rb_clear(&rb);
|
rb_clear(&rb);
|
||||||
|
|
22
ringbuf.c
22
ringbuf.c
|
@ -6,6 +6,7 @@
|
||||||
#include "ringbuf.h"
|
#include "ringbuf.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define DEBUG_PRINT(fmt, ...) printf(fmt, __VA_ARGS__)
|
#define DEBUG_PRINT(fmt, ...) printf(fmt, __VA_ARGS__)
|
||||||
|
@ -74,7 +75,7 @@ rb_push_many(struct RingBuf *rb, const void *items, MEMCPY_T memcpy_fn,
|
||||||
// Calculate the number of items that can be written in the first chunk
|
// Calculate the number of items that can be written in the first chunk
|
||||||
rb_size_t first_chunk = (char *)rb->buffer_end - (char *)rb->write_head;
|
rb_size_t first_chunk = (char *)rb->buffer_end - (char *)rb->write_head;
|
||||||
|
|
||||||
DEBUG_PRINT("Multi-chunk write. First chunk: %d\n", first_chunk);
|
DEBUG_PRINT("Multi-chunk write. First chunk: %ld\n", first_chunk);
|
||||||
|
|
||||||
// Write the first chunk
|
// Write the first chunk
|
||||||
memcpy_fn(rb->write_head, items, rb->struct_size * first_chunk);
|
memcpy_fn(rb->write_head, items, rb->struct_size * first_chunk);
|
||||||
|
@ -87,7 +88,7 @@ rb_push_many(struct RingBuf *rb, const void *items, MEMCPY_T memcpy_fn,
|
||||||
DEBUG_PRINT("Single-chunk write. No need to wrap around.%s\n", "");
|
DEBUG_PRINT("Single-chunk write. No need to wrap around.%s\n", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_PRINT("Writing %d items\n", n);
|
DEBUG_PRINT("Writing %ld items\n", n);
|
||||||
memcpy_fn(rb->write_head, items, rb->struct_size * n);
|
memcpy_fn(rb->write_head, items, rb->struct_size * n);
|
||||||
if(rb->write_head == rb->buffer_end)
|
if(rb->write_head == rb->buffer_end)
|
||||||
rb->write_head = rb->buffer;
|
rb->write_head = rb->buffer;
|
||||||
|
@ -116,3 +117,20 @@ rb_pop_front(struct RingBuf *rb, void *item, MEMCPY_T memcpy_fn)
|
||||||
rb->count--;
|
rb->count--;
|
||||||
return ReadOk;
|
return ReadOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
rb_debug_print(struct RingBuf *rb)
|
||||||
|
{
|
||||||
|
printf("============\n");
|
||||||
|
printf("Count %lu\n", rb->count);
|
||||||
|
printf("Capacity: %ld\n", rb->capacity);
|
||||||
|
printf("Left: %ld\n", rb->capacity - rb->count);
|
||||||
|
printf("Base addr:\t%p\n", rb->buffer);
|
||||||
|
|
||||||
|
printf("Read Head:\t%p (%ld:th position)\n", rb->read_head,
|
||||||
|
((rb->read_head) - (rb->buffer)) / rb->struct_size);
|
||||||
|
printf("Write Head:\t%p (%ld:th position)\n", rb->write_head,
|
||||||
|
((rb->write_head) - (rb->buffer)) / rb->struct_size);
|
||||||
|
|
||||||
|
printf("============\n");
|
||||||
|
}
|
||||||
|
|
|
@ -2,8 +2,10 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#ifndef rb_size_t
|
#ifndef rb_size_t
|
||||||
#define rb_size_t int
|
#define rb_size_t size_t
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** Signatures of allocators */
|
/** Signatures of allocators */
|
||||||
|
@ -113,3 +115,8 @@ enum ReadResult rb_pop_front(struct RingBuf *rb, void *item,
|
||||||
* @param free The free function
|
* @param free The free function
|
||||||
*/
|
*/
|
||||||
void rb_destroy(struct RingBuf *rb, void(free)());
|
void rb_destroy(struct RingBuf *rb, void(free)());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Debug print
|
||||||
|
*/
|
||||||
|
void rb_debug_print(struct RingBuf *rb);
|
||||||
|
|
Loading…
Reference in a new issue