mirror of https://github.com/meshcore-dev/MeshCore
4 changed files with 10 additions and 113 deletions
@ -1,39 +0,0 @@ |
|||
#pragma once |
|||
|
|||
#include <stddef.h> |
|||
|
|||
namespace mesh { |
|||
|
|||
template <typename T, size_t Capacity> |
|||
class BoundedQueue { |
|||
static_assert(Capacity > 0, "BoundedQueue capacity must be greater than zero"); |
|||
|
|||
T _items[Capacity]; |
|||
size_t _head = 0; |
|||
size_t _size = 0; |
|||
|
|||
public: |
|||
bool push(const T& item) { |
|||
if (_size == Capacity) return false; |
|||
_items[(_head + _size) % Capacity] = item; |
|||
_size++; |
|||
return true; |
|||
} |
|||
|
|||
bool pop(T& item) { |
|||
if (_size == 0) return false; |
|||
item = _items[_head]; |
|||
_head = (_head + 1) % Capacity; |
|||
_size--; |
|||
return true; |
|||
} |
|||
|
|||
void clear() { |
|||
_head = 0; |
|||
_size = 0; |
|||
} |
|||
|
|||
size_t size() const { return _size; } |
|||
}; |
|||
|
|||
} // namespace mesh
|
|||
@ -1,58 +0,0 @@ |
|||
#include <gtest/gtest.h> |
|||
|
|||
#include <helpers/BoundedQueue.h> |
|||
|
|||
TEST(BoundedQueue, PreservesOrderAcrossWraparound) { |
|||
mesh::BoundedQueue<int, 4> queue; |
|||
int value = 0; |
|||
|
|||
for (int i = 0; i < 4; i++) EXPECT_TRUE(queue.push(i)); |
|||
EXPECT_FALSE(queue.push(4)); |
|||
|
|||
EXPECT_TRUE(queue.pop(value)); |
|||
EXPECT_EQ(0, value); |
|||
EXPECT_TRUE(queue.pop(value)); |
|||
EXPECT_EQ(1, value); |
|||
|
|||
EXPECT_TRUE(queue.push(4)); |
|||
EXPECT_TRUE(queue.push(5)); |
|||
|
|||
for (int expected = 2; expected < 6; expected++) { |
|||
EXPECT_TRUE(queue.pop(value)); |
|||
EXPECT_EQ(expected, value); |
|||
} |
|||
EXPECT_FALSE(queue.pop(value)); |
|||
} |
|||
|
|||
TEST(BoundedQueue, SurvivesRepeatedFillAndDrainCycles) { |
|||
mesh::BoundedQueue<int, 4> queue; |
|||
int value = 0; |
|||
|
|||
for (int cycle = 0; cycle < 1000; cycle++) { |
|||
for (int i = 0; i < 4; i++) EXPECT_TRUE(queue.push(cycle * 4 + i)); |
|||
for (int i = 0; i < 4; i++) { |
|||
EXPECT_TRUE(queue.pop(value)); |
|||
EXPECT_EQ(cycle * 4 + i, value); |
|||
} |
|||
} |
|||
} |
|||
|
|||
TEST(BoundedQueue, ClearDropsPendingItems) { |
|||
mesh::BoundedQueue<int, 4> queue; |
|||
int value = 0; |
|||
|
|||
EXPECT_TRUE(queue.push(1)); |
|||
EXPECT_TRUE(queue.push(2)); |
|||
queue.clear(); |
|||
|
|||
EXPECT_EQ(0u, queue.size()); |
|||
EXPECT_FALSE(queue.pop(value)); |
|||
EXPECT_TRUE(queue.push(3)); |
|||
EXPECT_TRUE(queue.pop(value)); |
|||
EXPECT_EQ(3, value); |
|||
} |
|||
|
|||
int main(int argc, char **argv) { |
|||
::testing::InitGoogleTest(&argc, argv); |
|||
return RUN_ALL_TESTS(); |
|||
} |
|||
Loading…
Reference in new issue