-
Notifications
You must be signed in to change notification settings - Fork 291
/
circular_buffer.cpp
81 lines (68 loc) · 1.45 KB
/
circular_buffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2021 Embedded Artistry LLC
#include <cstdio>
#include <catch2/catch_test_macros.hpp>
#include "circular_buffer/circular_buffer.hpp"
TEST_CASE("Create circular buffer")
{
circular_buffer<uint32_t, 10> cbuf;
CHECK(cbuf.size() == 0);
CHECK(cbuf.capacity() == 10);
CHECK(cbuf.full() == false);
CHECK(cbuf.empty() == true);
}
TEST_CASE("Circular buffer operations")
{
circular_buffer<uint32_t, 10> cbuf;
SECTION("Check empty")
{
CHECK(cbuf.empty() == true);
for(uint32_t i = 0; i < cbuf.capacity(); i++)
{
cbuf.put(i);
CHECK(cbuf.empty() == false);
}
}
SECTION("Check Full")
{
for(uint32_t i = 0; i < cbuf.capacity(); i++)
{
CHECK(cbuf.full() == false);
cbuf.put(i);
}
CHECK(cbuf.full() == true);
CHECK(cbuf.size() == cbuf.capacity());
}
SECTION("Fill and empty buffer")
{
for(uint32_t i = 0; i < cbuf.capacity(); i++)
{
cbuf.put(i);
}
for(uint32_t i = 0; i < cbuf.capacity(); i++)
{
auto value = cbuf.get();
CHECK(value.has_value() == true);
CHECK(i == value.value());
}
}
SECTION("Get empty value")
{
auto value = cbuf.get();
CHECK(value.has_value() == false);
}
SECTION("Check overflow behavior")
{
for(uint32_t i = 0; i < cbuf.capacity(); i++)
{
cbuf.put(i);
}
// Force an overflow
cbuf.put(cbuf.capacity());
for(uint32_t i = 0; i < cbuf.capacity(); i++)
{
auto value = cbuf.get();
CHECK(value.has_value() == true);
CHECK(i + 1 == value.value());
}
}
}