2024-06-23 16:27:51 +02:00
|
|
|
/* SPDX-License-Identifier: MIT */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-06-27 01:21:25 +02:00
|
|
|
#include <stdio.h>
|
2024-06-23 16:27:51 +02:00
|
|
|
|
|
|
|
#define rb_size_t size_t
|
2024-07-14 21:32:40 +02:00
|
|
|
// #define rb_size_t int
|
2024-06-23 16:27:51 +02:00
|
|
|
#include "ringbuf.h"
|
|
|
|
|
2024-07-14 21:32:40 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2024-06-27 00:05:49 +02:00
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2024-06-23 16:27:51 +02:00
|
|
|
struct RingBuf rb;
|
2024-07-14 21:32:40 +02:00
|
|
|
|
|
|
|
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));
|
2024-06-23 16:27:51 +02:00
|
|
|
|
2024-07-02 08:39:31 +02:00
|
|
|
// Single writes
|
|
|
|
printf("\n=== Single writes ===\n\n");
|
2024-07-03 13:31:57 +02:00
|
|
|
|
2024-07-14 21:32:40 +02:00
|
|
|
rb_debug_print(&rb);
|
|
|
|
|
2024-07-02 08:39:31 +02:00
|
|
|
int idx = 0;
|
2024-07-03 13:31:57 +02:00
|
|
|
while(idx < 5) {
|
2024-07-02 08:39:31 +02:00
|
|
|
if(rb_push_back(&rb, &arr[idx], memcpy) != WriteOk) {
|
|
|
|
printf("Failed to write data to buffer...\n");
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2024-07-14 21:32:40 +02:00
|
|
|
rb_debug_print(&rb);
|
|
|
|
|
2024-07-02 08:39:31 +02:00
|
|
|
// Pop the last n elements
|
2024-07-14 21:32:40 +02:00
|
|
|
for(int a = 0; a < 10; a++) {
|
2024-07-02 08:39:31 +02:00
|
|
|
if(rb_pop_front(&rb, &d, memcpy) != ReadOk) {
|
|
|
|
printf("Failed to read data from buffer...\n");
|
2024-07-14 21:32:40 +02:00
|
|
|
break;
|
2024-07-02 08:39:31 +02:00
|
|
|
}
|
|
|
|
printf("Data: %d\n", d);
|
|
|
|
}
|
|
|
|
|
2024-07-14 21:32:40 +02:00
|
|
|
rb_debug_print(&rb);
|
|
|
|
if(rb.read_head == rb.write_head)
|
|
|
|
printf("OK\n");
|
2024-07-02 08:39:31 +02:00
|
|
|
printf("idx: %d\n", idx);
|
|
|
|
|
|
|
|
// Push the rest
|
2024-07-14 21:32:40 +02:00
|
|
|
while(idx < arrlen && rb_push_back(&rb, &arr[idx], memcpy) == WriteOk) {
|
2024-07-02 08:39:31 +02:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2024-07-14 21:32:40 +02:00
|
|
|
rb_debug_print(&rb);
|
|
|
|
|
2024-07-02 08:39:31 +02:00
|
|
|
printf("Data: [");
|
|
|
|
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
|
|
|
|
printf("%d,", d);
|
|
|
|
printf("\b]\n");
|
|
|
|
|
|
|
|
// Multiple writes
|
|
|
|
printf("\n=== Multiple writes ===\n\n");
|
|
|
|
|
2024-07-14 21:32:40 +02:00
|
|
|
rb_clear(&rb); // Make sure
|
|
|
|
rb_debug_print(&rb);
|
2024-06-30 05:08:42 +02:00
|
|
|
|
2024-06-30 23:57:08 +02:00
|
|
|
int ok = WriteOk; // Assume we can write
|
2024-07-02 06:37:58 +02:00
|
|
|
if(rb_push_many(&rb, arr, memcpy, 8) != WriteOk) {
|
|
|
|
printf("Failed to write data to buffer...\n");
|
|
|
|
}
|
2024-07-14 21:32:40 +02:00
|
|
|
rb_debug_print(&rb);
|
|
|
|
rb_debug_empty(&rb);
|
2024-07-02 06:27:39 +02:00
|
|
|
|
|
|
|
// Test wrap around
|
|
|
|
rb_push_many(&rb, arr, memcpy, 10);
|
2024-07-14 21:32:40 +02:00
|
|
|
rb_debug_print(&rb);
|
|
|
|
rb_debug_empty(&rb);
|
2024-06-27 01:21:25 +02:00
|
|
|
|
2024-07-02 06:27:39 +02:00
|
|
|
// Test clear
|
|
|
|
rb_clear(&rb);
|
|
|
|
if(rb_pop_front(&rb, &d, memcpy) != Empty) {
|
|
|
|
printf("Buffer is not empty after clear...\n");
|
|
|
|
}
|
|
|
|
|
2024-06-27 01:21:25 +02:00
|
|
|
rb_destroy(&rb, free);
|
2024-06-23 16:27:51 +02:00
|
|
|
|
2024-07-02 06:27:39 +02:00
|
|
|
enum WriteResult wr = WriteOk;
|
|
|
|
enum ReadResult rr = ReadOk;
|
|
|
|
printf("Size of wr: %lu bytes.\n", sizeof(wr));
|
|
|
|
printf("Size of rr: %lu bytes.\n", sizeof(rr));
|
|
|
|
|
2024-06-23 16:27:51 +02:00
|
|
|
return 0;
|
2024-06-27 00:05:49 +02:00
|
|
|
}
|