ringbuf/driver.c

47 lines
941 B
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;
rb_init(&rb, 10, malloc, sizeof(int));
2024-06-30 23:57:08 +02:00
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
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:27:39 +02:00
ok = rb_push_many(&rb, arr, memcpy, 8);
2024-06-30 05:08:42 +02:00
int d;
2024-07-02 06:27:39 +02:00
while(rb_pop_front(&rb, &d, memcpy) == ReadOk) {
printf("Data: %d\n", d);
}
// Test wrap around
rb_push_many(&rb, arr, memcpy, 10);
while(rb_pop_front(&rb, &d, memcpy) == ReadOk) {
2024-06-30 23:57:08 +02:00
printf("Data: %d\n", d);
}
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
}