From aa84b7da19009c93e30578ac1b697d800a1b2283 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 12 Jan 2019 14:01:13 -0500 Subject: [PATCH] cyclic_buffer.contains method --- firmware/util/cyclic_buffer.h | 12 ++++++++++++ unit_tests/tests/test_util.cpp | 9 ++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/firmware/util/cyclic_buffer.h b/firmware/util/cyclic_buffer.h index 8251dcf451..4c15dbc05b 100644 --- a/firmware/util/cyclic_buffer.h +++ b/firmware/util/cyclic_buffer.h @@ -14,6 +14,7 @@ #define CYCLIC_BUFFER_H #include +#include "rusefi_true.h" static const short CB_MAX_SIZE = 128; @@ -41,6 +42,7 @@ class cyclic_buffer T maxValue(int length); T minValue(int length); void setSize(int size); + bool contains(T value); int getSize(); int getCount(); void clear(); @@ -111,6 +113,16 @@ void cyclic_buffer::add(T value) { ++count; } +template +bool cyclic_buffer::contains(T value) { + for (int i = 0; i < currentIndex ; i++) { + if (elements[i] == value) { + return TRUE; + } + } + return FALSE; +} + template void cyclic_buffer::setSize(int size) { clear(); diff --git a/unit_tests/tests/test_util.cpp b/unit_tests/tests/test_util.cpp index ea303a2db3..2e9ad554bb 100644 --- a/unit_tests/tests/test_util.cpp +++ b/unit_tests/tests/test_util.cpp @@ -51,7 +51,6 @@ TEST(util, crc) { assertEqualsM("crc32 line inc", 0x4775a7b1, c); } -static cyclic_buffer sb; TEST(util, Overflow64Counter) { print("*************************************** testOverflow64Counter\r\n"); @@ -66,7 +65,15 @@ TEST(util, Overflow64Counter) { assertEquals(4294967296, o.update(0)); } +TEST(util, cyclicBufferContains) { + cyclic_buffer sb; + sb.add(10); + ASSERT_EQ(TRUE, sb.contains(10)); + ASSERT_EQ(FALSE, sb.contains(11)); +} + TEST(util, cyclicBuffer) { + cyclic_buffer sb; print("*************************************** testCyclicBuffer\r\n");