Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular buffer library improvements + Testing #43

Merged
merged 8 commits into from
Oct 27, 2021
2 changes: 1 addition & 1 deletion examples/c/circular_buffer/circular_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void circular_buf_put(cbuf_handle_t cbuf, uint8_t data)
advance_pointer(cbuf);
}

int circular_buf_put2(cbuf_handle_t cbuf, uint8_t data)
int circular_buf_try_put(cbuf_handle_t cbuf, uint8_t data)
{
int r = -1;

Expand Down
7 changes: 4 additions & 3 deletions examples/c/circular_buffer/circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CIRCULAR_BUFFER_H_

#include <stdbool.h>
#include <stdint.h>

/// Opaque circular buffer structure
typedef struct circular_buf_t circular_buf_t;
Expand All @@ -24,15 +25,15 @@ void circular_buf_free(cbuf_handle_t cbuf);
/// Requires: cbuf is valid and created by circular_buf_init
void circular_buf_reset(cbuf_handle_t cbuf);

/// Put version 1 continues to add data if the buffer is full
/// Put that continues to add data if the buffer is full
/// Old data is overwritten
/// Requires: cbuf is valid and created by circular_buf_init
void circular_buf_put(cbuf_handle_t cbuf, uint8_t data);

/// Put Version 2 rejects new data if the buffer is full
/// Put that rejects new data if the buffer is full
/// Requires: cbuf is valid and created by circular_buf_init
/// Returns 0 on success, -1 if buffer is full
int circular_buf_put2(cbuf_handle_t cbuf, uint8_t data);
int circular_buf_try_put(cbuf_handle_t cbuf, uint8_t data);

/// Retrieve a value from the buffer
/// Requires: cbuf is valid and created by circular_buf_init
Expand Down
2 changes: 1 addition & 1 deletion examples/c/circular_buffer/circular_buffer_no_modulo.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void circular_buf_put(cbuf_handle_t cbuf, uint8_t data)
advance_pointer(cbuf);
}

int circular_buf_put2(cbuf_handle_t cbuf, uint8_t data)
int circular_buf_try_put(cbuf_handle_t cbuf, uint8_t data)
{
int r = -1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ size_t circular_buf_size(cbuf_handle_t cbuf)
{
assert(cbuf);

size_t size = cbuf->max;
// We account for the space we can't use for thread safety
size_t size = cbuf->max - 1;

if(!circular_buf_full(cbuf))
{
Expand All @@ -101,7 +102,8 @@ size_t circular_buf_capacity(cbuf_handle_t cbuf)
{
assert(cbuf);

return cbuf->max;
// We account for the space we can't use for thread safety
return cbuf->max - 1;
}

void circular_buf_put(cbuf_handle_t cbuf, uint8_t data)
Expand All @@ -113,7 +115,7 @@ void circular_buf_put(cbuf_handle_t cbuf, uint8_t data)
advance_pointer(cbuf);
}

int circular_buf_put2(cbuf_handle_t cbuf, uint8_t data)
int circular_buf_try_put(cbuf_handle_t cbuf, uint8_t data)
{
int r = -1;

Expand Down
99 changes: 0 additions & 99 deletions examples/c/circular_buffer_test.c

This file was deleted.

159 changes: 159 additions & 0 deletions examples/c/circular_buffer_tests/circular_buffer_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright © 2021 Embedded Artistry LLC.
* See LICENSE file for licensing information.
*/

// Cmocka needs these
// clang-format off
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <cmocka.h>
// clang-format on

#include "circular_buffer/circular_buffer.h"

#define CIRCULAR_BUFFER_SIZE 10

static uint8_t circular_buffer_storage_[CIRCULAR_BUFFER_SIZE] = {0};
static cbuf_handle_t handle_ = NULL;

static int circular_buffer_setup(void** __unused state)
{
handle_ = circular_buf_init(circular_buffer_storage_, CIRCULAR_BUFFER_SIZE);

return 0;
}

static int circular_buffer_teardown(void** __unused state)
{
circular_buf_free(handle_);

return 0;
}

void circular_buffer_init_test(void** __unused state)
{
assert_non_null(handle_);
assert_true(circular_buf_empty(handle_));
assert_false(circular_buf_full(handle_));
#ifdef TEST_WITH_REDUCED_CAPACITY
// We are using a strategy that reserves an empty slot for thread safety
assert_int_equal(circular_buf_capacity(handle_), CIRCULAR_BUFFER_SIZE - 1);
#else
assert_int_equal(circular_buf_capacity(handle_), CIRCULAR_BUFFER_SIZE);
#endif
}

void circular_buf_put_get_test(void** __unused state)
{
const int capacity = circular_buf_capacity(handle_);
for(int i = 0; i < capacity; i++)
{
circular_buf_put(handle_, i);

assert_int_equal(i + 1, circular_buf_size(handle_));
}

// Check overflow condition
circular_buf_put(handle_, capacity);
assert_int_equal(capacity, circular_buf_size(handle_));

// Check get - we are expecting that one byte has been overwritten
// so we should see that the data is [1..10] instead of [0..9]
for(int i = 0; i < capacity; i++)
{
uint8_t data;
circular_buf_get(handle_, &data);
assert_int_equal(data, i + 1);
}
}

void circular_buf_try_put_get_test(void** __unused state)
{
int success;
const int capacity = circular_buf_capacity(handle_);

for(int i = 0; i < capacity; i++)
{
success = circular_buf_try_put(handle_, i);
assert_int_equal(success, 0);
assert_int_equal(i + 1, circular_buf_size(handle_));
}

// Check overflow condition
success = circular_buf_try_put(handle_, capacity);
assert_int_equal(success, -1);

// Check get - we are expecting that the previous put failed,
// so we should see that the data is [0..9]
for(int i = 0; i < capacity; i++)
{
uint8_t data;
circular_buf_get(handle_, &data);
assert_int_equal(data, i);
}
}

void circular_buffer_full_test(void** __unused state)
{
const int capacity = circular_buf_capacity(handle_);

for(int i = 0; i < capacity; i++)
{
assert_false(circular_buf_full(handle_));
circular_buf_put(handle_, i);
}

assert_true(circular_buf_full(handle_));
}

void circular_buffer_empty_test(void** __unused state)
{
const int capacity = circular_buf_capacity(handle_);

assert_true(circular_buf_empty(handle_));

for(int i = 0; i < capacity; i++)
{
circular_buf_put(handle_, i);
assert_false(circular_buf_empty(handle_));
}
}

void circular_buffer_get_more_than_stored_test(void** __unused state)
{
uint8_t data;

// We will put one and read two

circular_buf_put(handle_, 1);

assert_int_equal(0, circular_buf_get(handle_, &data));
assert_int_equal(data, 1);
data = 0;
assert_int_equal(-1, circular_buf_get(handle_, &data));
assert_int_equal(data, 0);
}

#pragma mark - Public Functions -

int circular_buffer_test_suite(void)
{
const struct CMUnitTest circular_buffer_tests[] = {
cmocka_unit_test_setup_teardown(circular_buffer_init_test, circular_buffer_setup,
circular_buffer_teardown),
cmocka_unit_test_setup_teardown(circular_buf_put_get_test, circular_buffer_setup,
circular_buffer_teardown),
cmocka_unit_test_setup_teardown(circular_buf_try_put_get_test, circular_buffer_setup,
circular_buffer_teardown),
cmocka_unit_test_setup_teardown(circular_buffer_full_test, circular_buffer_setup,
circular_buffer_teardown),
cmocka_unit_test_setup_teardown(circular_buffer_empty_test, circular_buffer_setup,
circular_buffer_teardown),
cmocka_unit_test_setup_teardown(circular_buffer_get_more_than_stored_test,
circular_buffer_setup, circular_buffer_teardown),
};

return cmocka_run_group_tests(circular_buffer_tests, NULL, NULL);
}
11 changes: 11 additions & 0 deletions examples/c/circular_buffer_tests/circular_buffer_tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright © 2021 Embedded Artistry LLC.
* See LICENSE file for licensing information.
*/

#ifndef CIRCULAR_BUFFER_TESTS_H_
#define CIRCULAR_BUFFER_TESTS_H_

int circular_buffer_test_suite(void);

#endif // CIRCULAR_BUFFER_TESTS_H_
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions examples/c/malloc/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
memory_inc = include_directories('.')

libmalloc_threadx = static_library('malloc_threadx',
'malloc_threadx.c',
c_args: '-fno-builtin',
dependencies: threadx_dep
)

executable('malloc_aligned',
'malloc_aligned.c',
c_args: '-DCOMPILE_AS_EXAMPLE',
)

executable('malloc_freelist',
[
'malloc_freelist.c',
'malloc_test.c'
],
dependencies: linked_list_dep,
c_args: '-DCOMPILE_AS_EXAMPLE',
)
Loading