ringbuf/driver.c

88 lines
1.8 KiB
C
Raw Normal View History

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
#include "ringbuf.h"
2024-06-27 00:05:49 +02:00
int
main(void)
{
2024-06-23 16:27:51 +02:00
struct RingBuf rb;
2024-07-02 08:39:31 +02:00
int d;
2024-06-23 16:27:51 +02:00
rb_init(&rb, 10, malloc, sizeof(int));
2024-07-02 08:39:31 +02:00
const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
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-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++;
}
// 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
2024-07-03 13:31:57 +02:00
while(rb_push_back(&rb, &arr[idx], memcpy) == WriteOk) {
2024-07-02 08:39:31 +02:00
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);
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-06-30 05:08:42 +02:00
2024-07-02 06:49:49 +02:00
printf("Data: [");
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
printf("%d,", d);
printf("\b]\n");
2024-07-02 06:27:39 +02:00
// Test wrap around
rb_push_many(&rb, arr, memcpy, 10);
2024-07-02 06:49:49 +02:00
printf("Data: [");
while(rb_pop_front(&rb, &d, memcpy) == ReadOk)
printf("%d,", d);
printf("\b]\n");
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
}