Skip to content

Instantly share code, notes, and snippets.

@larshei
Last active March 2, 2023 09:30
Show Gist options
  • Select an option

  • Save larshei/444ecee2a90174757b49ac1fe85f604a to your computer and use it in GitHub Desktop.

Select an option

Save larshei/444ecee2a90174757b49ac1fe85f604a to your computer and use it in GitHub Desktop.
"test" and usage demonstration for fifo_buffer.c gist: https://gist.github.com/larshei/cec020e037b0ebd103ef6252977f8b12
/* ===========================================================================
* Lars Heinrichs, 2020
* Feel free to use this piece of code as desired. Provided as is, with no
* guarantees.
*
* "Tests" (in quotes!) for
* https://gist.github.com/larshei/cec020e037b0ebd103ef6252977f8b12
* Verifies basic behaviour.
*
* Use the gist above if you quickly need a ring buffer and do not care about
* being dirty.
* This file demonstrates how to use the gist (what you need is to be found in
* the block limited by +++++++++++++++++++++++++++++++++++).
*
* ========================================================================= */
// FIFO_... are undef'd in fifo_buffer.c
// We need these values in the test cases, so we use a 2 step approach here.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define ELEMENT_COUNT 10
#define OVERWRITE 0 // set this to 0: FIFO or 1: RING buffer behaviour
#define FIFO_DATA_TYPE int
#define FIFO_BUFFER_SIZE ELEMENT_COUNT
FIFO_DATA_TYPE _buffer[FIFO_BUFFER_SIZE];
#define FIFO_MEMORY _buffer
#define FIFO_OVERWRITE_WHEN_FULL OVERWRITE
#include "fifo_buffer.c"
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
// ---- PROTOTYPES FOR TESTS --------------------------------------------------
void test_single_add_then_read_many_times();
void test_fill_then_read_all();
void test_fill_twice_no_overwrite();
void test_fill_twice_overwrite();
// ----------------------------------------------------------------------------
int main() {
test_single_add_then_read_many_times();
test_fill_then_read_all();
#if (!defined(OVERWRITE) || (OVERWRITE == 0))
test_fill_twice_no_overwrite();
#else
test_fill_twice_overwrite();
#endif
return 0;
}
// ---- HELPERS ---------------------------------------------------------------
void print_buffer() {
printf("{ ");
for (int i = 0 ; i < ELEMENT_COUNT ; i++) {
printf(" %2u,", _buffer[i]);
}
printf(" }\r\n");
}
int fill(int start, int increment) {
for (int i = 0 ; i < ELEMENT_COUNT ; i++) {
if (fifo_add_element(increment*i+start)){
printf("ERROR: Couldnt add value\r\n");
return 1;
}
}
return 0;
}
int fill_no_overwrite(int start, int increment) {
for (int i = 0 ; i < ELEMENT_COUNT ; i++) {
if (fifo_add_element(increment*i+start) == 0){
printf("ERROR: Overwritting value even tho not allowed\r\n");
return 1;
}
}
return 0;
}
int read_compare(int start, int increment) {
for (int i = 0 ; i < ELEMENT_COUNT ; i++) {
if (fifo_read_element() != increment*i+start){
printf("ERROR: read incorrect value\r\n");
return 1;
}
}
return 0;
}
// ---- "Tests" ---------------------------------------------------------------
void test_single_add_then_read_many_times() {
fifo_reset();
for (int i = 0 ; i < 3 * ELEMENT_COUNT ; i++) {
if (fifo_add_element(2*(i+1))) printf("ERROR: Couldnt add value\r\n");
if (fifo_read_element() != 2*(i+1)) printf("ERROR: read incorrect value\r\n");
if (!fifo_is_empty()) {
printf("ERROR: expected empty fifo!\r\n");
return;
}
}
printf("SUCCESS: %s\r\n", __FUNCTION__);
}
void test_fill_then_read_all() {
int start = 2;
int increment = 2;
int error = 0;
fifo_reset();
error |= fill(start, increment);
error |= read_compare(start, increment);
if (error) return;
printf("SUCCESS: %s\r\n", __FUNCTION__);
}
void test_fill_twice_no_overwrite() {
int start = 2;
int increment = 2;
int error = 0;
fifo_reset();
error |= fill(start, increment);
error |= fill_no_overwrite(start + 500, increment);
error |= read_compare(start, increment);
if (error) return;
printf("SUCCESS: %s\r\n", __FUNCTION__);
}
void test_fill_twice_overwrite() {
int start = 2;
int increment = 2;
int error = 0;
fifo_reset();
error |= fill(start, increment);
error |= fill(start + 500, increment);
error |= read_compare(start + 500, increment);
if (error) return;
printf("SUCCESS: %s\r\n", __FUNCTION__);
}
@larshei
Copy link
Author

larshei commented Sep 15, 2020

Implementation of the actual buffer functions can be found here:
https://gist.github.com/larshei/cec020e037b0ebd103ef6252977f8b12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment