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>
|
||||
|
||||
#define rb_size_t size_t
|
||||
// #define rb_size_t int
|
||||
#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
|
||||
main(void)
|
||||
{
|
||||
struct RingBuf rb;
|
||||
int d;
|
||||
rb_init(&rb, 10, malloc, sizeof(int));
|
||||
const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
|
||||
DATATYPE d;
|
||||
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
|
||||
printf("\n=== Single writes ===\n\n");
|
||||
|
||||
rb_debug_print(&rb);
|
||||
|
||||
int idx = 0;
|
||||
while(idx < 5) {
|
||||
if(rb_push_back(&rb, &arr[idx], memcpy) != WriteOk) {
|
||||
|
@ -26,22 +51,29 @@ main(void)
|
|||
idx++;
|
||||
}
|
||||
|
||||
rb_debug_print(&rb);
|
||||
|
||||
// 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) {
|
||||
printf("Failed to read data from buffer...\n");
|
||||
break;
|
||||
}
|
||||
printf("Data: %d\n", d);
|
||||
}
|
||||
|
||||
rb_debug_print(&rb);
|
||||
if(rb.read_head == rb.write_head)
|
||||
printf("OK\n");
|
||||
printf("idx: %d\n", idx);
|
||||
|
||||
// Push the rest
|
||||
while(rb_push_back(&rb, &arr[idx], memcpy) == WriteOk) {
|
||||
printf("Wrote: %d\n", arr[idx]);
|
||||
while(idx < arrlen && rb_push_back(&rb, &arr[idx], memcpy) == WriteOk) {
|
||||
idx++;
|
||||
}
|
||||
|
||||
rb_debug_print(&rb);
|
||||
|
||||
printf("Data: [");
|
||||
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
|
||||
printf("%d,", d);
|
||||
|
@ -50,25 +82,20 @@ main(void)
|
|||
// Multiple writes
|
||||
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
|
||||
if(rb_push_many(&rb, arr, memcpy, 8) != WriteOk) {
|
||||
printf("Failed to write data to buffer...\n");
|
||||
}
|
||||
|
||||
printf("Data: [");
|
||||
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
|
||||
printf("%d,", d);
|
||||
printf("\b]\n");
|
||||
rb_debug_print(&rb);
|
||||
rb_debug_empty(&rb);
|
||||
|
||||
// Test wrap around
|
||||
rb_push_many(&rb, arr, memcpy, 10);
|
||||
|
||||
printf("Data: [");
|
||||
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
|
||||
printf("%d,", d);
|
||||
printf("\b]\n");
|
||||
rb_debug_print(&rb);
|
||||
rb_debug_empty(&rb);
|
||||
|
||||
// Test clear
|
||||
rb_clear(&rb);
|
||||
|
|
22
ringbuf.c
22
ringbuf.c
|
@ -6,6 +6,7 @@
|
|||
#include "ringbuf.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#ifdef DEBUG
|
||||
#include <stdio.h>
|
||||
#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
|
||||
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
|
||||
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("Writing %d items\n", n);
|
||||
DEBUG_PRINT("Writing %ld items\n", n);
|
||||
memcpy_fn(rb->write_head, items, rb->struct_size * n);
|
||||
if(rb->write_head == rb->buffer_end)
|
||||
rb->write_head = rb->buffer;
|
||||
|
@ -116,3 +117,20 @@ rb_pop_front(struct RingBuf *rb, void *item, MEMCPY_T memcpy_fn)
|
|||
rb->count--;
|
||||
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
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#ifndef rb_size_t
|
||||
#define rb_size_t int
|
||||
#define rb_size_t size_t
|
||||
#endif
|
||||
|
||||
/** Signatures of allocators */
|
||||
|
@ -113,3 +115,8 @@ enum ReadResult rb_pop_front(struct RingBuf *rb, void *item,
|
|||
* @param free The free function
|
||||
*/
|
||||
void rb_destroy(struct RingBuf *rb, void(free)());
|
||||
|
||||
/**
|
||||
* @brief Debug print
|
||||
*/
|
||||
void rb_debug_print(struct RingBuf *rb);
|
||||
|
|
Loading…
Reference in a new issue