87 lines
1.8 KiB
C
87 lines
1.8 KiB
C
/* SPDX-License-Identifier: MIT */
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#define rb_size_t size_t
|
|
#include "ringbuf.h"
|
|
|
|
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 };
|
|
|
|
// Single writes
|
|
printf("\n=== Single writes ===\n\n");
|
|
|
|
int idx = 0;
|
|
while(idx < 5) {
|
|
if(rb_push_back(&rb, &arr[idx], memcpy) != WriteOk) {
|
|
printf("Failed to write data to buffer...\n");
|
|
}
|
|
idx++;
|
|
}
|
|
|
|
// Pop the last n elements
|
|
for(int a = 0; a < 1; a++) {
|
|
if(rb_pop_front(&rb, &d, memcpy) != ReadOk) {
|
|
printf("Failed to read data from buffer...\n");
|
|
}
|
|
printf("Data: %d\n", d);
|
|
}
|
|
|
|
printf("idx: %d\n", idx);
|
|
|
|
// Push the rest
|
|
while(rb_push_back(&rb, &arr[idx], memcpy) == WriteOk) {
|
|
printf("Wrote: %d\n", arr[idx]);
|
|
idx++;
|
|
}
|
|
|
|
printf("Data: [");
|
|
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
|
|
printf("%d,", d);
|
|
printf("\b]\n");
|
|
|
|
// Multiple writes
|
|
printf("\n=== Multiple writes ===\n\n");
|
|
|
|
rb_clear(&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");
|
|
|
|
// 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");
|
|
|
|
// Test clear
|
|
rb_clear(&rb);
|
|
if(rb_pop_front(&rb, &d, memcpy) != Empty) {
|
|
printf("Buffer is not empty after clear...\n");
|
|
}
|
|
|
|
rb_destroy(&rb, free);
|
|
|
|
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));
|
|
|
|
return 0;
|
|
}
|