From de4b32ef543191ea5f598de4300372f2af0e0514 Mon Sep 17 00:00:00 2001 From: Hendrik van Essen Date: Mon, 29 Nov 2021 13:37:19 +0100 Subject: [PATCH] sys/tsrb: add tsrb_clear --- sys/include/tsrb.h | 11 +++++++++++ tests/unittests/tests-tsrb/tests-tsrb.c | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/sys/include/tsrb.h b/sys/include/tsrb.h index 04d8b26345..4d9c610dcb 100644 --- a/sys/include/tsrb.h +++ b/sys/include/tsrb.h @@ -67,6 +67,17 @@ static inline void tsrb_init(tsrb_t *rb, uint8_t *buffer, unsigned bufsize) rb->writes = 0; } +/** + * @brief Clear a tsrb. + * @param[out] rb Ringbuffer to operate on + */ +static inline void tsrb_clear(tsrb_t *rb) +{ + unsigned irq_state = irq_disable(); + rb->reads = rb->writes; + irq_restore(irq_state); +} + /** * @brief Test if the tsrb is empty. * @param[in] rb Ringbuffer to operate on diff --git a/tests/unittests/tests-tsrb/tests-tsrb.c b/tests/unittests/tests-tsrb/tests-tsrb.c index 94d7a8745b..028d75c7f7 100644 --- a/tests/unittests/tests-tsrb/tests-tsrb.c +++ b/tests/unittests/tests-tsrb/tests-tsrb.c @@ -37,6 +37,20 @@ static void tear_down(void) tsrb_init(&_tsrb, _tsrb_buffer, BUFFER_SIZE); } +static void test_clear(void) +{ + TEST_ASSERT_EQUAL_INT(0, tsrb_avail(&_tsrb)); + + for (int i = 0; i < BUFFER_SIZE; i++) { + TEST_ASSERT_EQUAL_INT(0, tsrb_add_one(&_tsrb, TEST_INPUT)); + TEST_ASSERT_EQUAL_INT(i + 1, tsrb_avail(&_tsrb)); + } + + tsrb_clear(&_tsrb); + + TEST_ASSERT_EQUAL_INT(0, tsrb_avail(&_tsrb)); +} + static void test_empty(void) { TEST_ASSERT_EQUAL_INT(1, tsrb_empty(&_tsrb)); @@ -202,6 +216,7 @@ static void test_add(void) static Test *tests_tsrb_tests(void) { EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_clear), new_TestFixture(test_empty), new_TestFixture(test_avail), new_TestFixture(test_full),