Skip to content

Himel55/circular-buffer

Repository files navigation

Circular Buffer

This project implements a generic circular buffer. As part of the initialisation the size of the item the buffer will hold is given. The memory which the circular buffer will use and its size is also passed in on initilisation. This gives the client the option to use static or dynamic memory allocation. The main design goal was to try make the circular buffer flexible in the hope this will bring greater reusability. Some design decisions/restrictions were made to try keep the implementation simple, such as the input memory must be divisible by the item size.

Building

The project uses a makefile to build the code.
The makefile contains three targets of interest:

  • unit test build
  • static library generation
  • demo application.

To make all three targets simply run:

$ make all

Individual targets can also be built:

$ make test
$ make demo
$ make lib

Unit Tests

Unit tests are written in C++ using a unit testing framework called Catch2.
They are written in BDD style of language.
This should help readers, who aren't familiar with the code, to better understand what the tests are actually testing.

For example, the test below checks if UTIL_CircularBuffer_Pop is called on an empty circular buffer that the status code EMPTY is returned.

  GIVEN("an empty circular buffer is popped") {
    uint32_t popped_item = 0;
    status_code_t status =  UTIL_CircularBuffer_Pop(&bg.circular_buffer, (void*) &popped_item);

    THEN("status shall return empty") {
      CHECK(status == EMPTY);
    }
  }

To run the unit tests (remember to build first), simply run the exe from the terminal.

$ ./bin/Test_CircularBuffer.exe

Demo

There is an arbitrary demo application which show cases the use of the generated static library. The demo application initialises a circular buffer that can hold 9 items, it then attempts to adds 10 items to the buffer, finally it attempts to read out 10 items and print them to the display.The console output is given below:

Overwritten!
i: 1 j: 2 k: 3
i: 2 j: 3 k: 4
i: 3 j: 4 k: 5
i: 4 j: 5 k: 6
i: 5 j: 6 k: 7
i: 6 j: 7 k: 8
i: 7 j: 8 k: 9
i: 8 j: 9 k: 10
i: 9 j: 10 k: 11
Empty!

To run the demo (remember to build first), simply run the exe from the terminal.

$ ./bin/Demo.exe

Further Documentation

There is some further documentation generated by doxygen which outlines the API functions in more detail. Have a look at docs->doxygen->html->index.html and click on Files.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published