mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
circularBuffer_test.cpp
Go to the documentation of this file.
1/** \file circularBuffer_test.cpp
2 * \brief Tests circular-buffer wrapping and indexing.
3 */
4#include "../../catch2/catch.hpp"
5
6#include <vector>
7#include <Eigen/Dense>
8
9#define MX_NO_ERROR_REPORTS
10
12
13/** using a circular buffer with branch-wrapping
14 *
15 * Verify circular buffer operation with branch-wrapping
16 *
17 */
18/**
19 * \ingroup circularBuffer_unit_tests
20 */
21TEST_CASE( "creating a circular buffer with branching", "[sigproc::circularBuffer::circularBufferBranch]" )
22{
23 GIVEN( "a circular buffer" )
24 {
25 WHEN( "adding exactly max entries worth" )
26 {
28 cb.maxEntries(5);
29 cb.nextEntry(0);
30 cb.nextEntry(1);
31 cb.nextEntry(2);
32 cb.nextEntry(3);
33 cb.nextEntry(4);
34
35 REQUIRE( cb[0] == 0 );
36 REQUIRE( cb[1] == 1 );
37 REQUIRE( cb[2] == 2 );
38 REQUIRE( cb[3] == 3 );
39 REQUIRE( cb[4] == 4 );
40
41 //reverse
42 REQUIRE( cb[-1] == 4 );
43 REQUIRE( cb[-2] == 3 );
44 REQUIRE( cb[-3] == 2 );
45 REQUIRE( cb[-4] == 1 );
46
47 REQUIRE( cb.at(cb.earliest(),0) == 0 );
48 REQUIRE( cb.at(cb.latest(),0) == 4 );
49 }
50
51 WHEN( "adding new values past the end" )
52 {
54 cb.maxEntries(5);
55 cb.nextEntry(0);
56 cb.nextEntry(1);
57 cb.nextEntry(2);
58 cb.nextEntry(3);
59 cb.nextEntry(4);
60 cb.nextEntry(5);
61 cb.nextEntry(6);
62
63 REQUIRE( cb[0] == 2 );
64 REQUIRE( cb[1] == 3 );
65 REQUIRE( cb[2] == 4 );
66 REQUIRE( cb[3] == 5 );
67 REQUIRE( cb[4] == 6 );
68
69 //reverse
70 REQUIRE( cb[-1] == 6 );
71 REQUIRE( cb[-2] == 5 );
72 REQUIRE( cb[-3] == 4 );
73 REQUIRE( cb[-4] == 3 );
74
75 REQUIRE( cb.at(cb.earliest(),0) == 2 );
76 REQUIRE( cb.at(cb.latest(),0) == 6 );
77 }
78
79 WHEN( "wrapping when not full" )
80 {
82 cb.maxEntries(5);
83 cb.nextEntry(0);
84 cb.nextEntry(1);
85 cb.nextEntry(2);
86 cb.nextEntry(3);
87
88 REQUIRE( cb[0] == 0 );
89 REQUIRE( cb[1] == 1 );
90 REQUIRE( cb[2] == 2 );
91 REQUIRE( cb[3] == 3 );
92 REQUIRE( cb[4] == 0 );
93
94 //reverse
95 REQUIRE( cb[-1] == 3 );
96 REQUIRE( cb[-2] == 2 );
97 REQUIRE( cb[-3] == 1 );
98 REQUIRE( cb[-4] == 0 );
99 }
100 }
101}
102
103/** using a circular buffer with index-wrapping
104 *
105 * Verify circular buffer operation with index-wrapping
106 *
107 */
108/**
109 * \ingroup circularBuffer_unit_tests
110 */
111TEST_CASE( "creating a circular buffer with indexing", "[sigproc::circularBuffer::circularBufferIndex]" )
112{
113 GIVEN( "a circular buffer" )
114 {
115 WHEN( "adding exactly max entries worth" )
116 {
118 cb.maxEntries(5);
119 cb.nextEntry(0);
120 cb.nextEntry(1);
121 cb.nextEntry(2);
122 cb.nextEntry(3);
123 cb.nextEntry(4);
124
125 REQUIRE( cb[0] == 0 );
126 REQUIRE( cb[1] == 1 );
127 REQUIRE( cb[2] == 2 );
128 REQUIRE( cb[3] == 3 );
129 REQUIRE( cb[4] == 4 );
130
131 //reverse
132 REQUIRE( cb[-1] == 4 );
133 REQUIRE( cb[-2] == 3 );
134 REQUIRE( cb[-3] == 2 );
135 REQUIRE( cb[-4] == 1 );
136
137 REQUIRE( cb.at(cb.earliest(),0) == 0 );
138 REQUIRE( cb.at(cb.latest(),0) == 4 );
139 }
140
141 WHEN( "adding new values past the end" )
142 {
144 cb.maxEntries(5);
145 cb.nextEntry(0);
146 cb.nextEntry(1);
147 cb.nextEntry(2);
148 cb.nextEntry(3);
149 cb.nextEntry(4);
150 cb.nextEntry(5);
151 cb.nextEntry(6);
152
153 REQUIRE( cb[0] == 2 );
154 REQUIRE( cb[1] == 3 );
155 REQUIRE( cb[2] == 4 );
156 REQUIRE( cb[3] == 5 );
157 REQUIRE( cb[4] == 6 );
158
159 //reverse
160 REQUIRE( cb[-1] == 6 );
161 REQUIRE( cb[-2] == 5 );
162 REQUIRE( cb[-3] == 4 );
163 REQUIRE( cb[-4] == 3 );
164
165 REQUIRE( cb.at(cb.earliest(),0) == 2 );
166 REQUIRE( cb.at(cb.latest(),0) == 6 );
167 }
168 }
169}
A circular buffer class.
void maxEntries(indexT maxEnt)
Set the maximum size of the buffer.
indexT earliest()
Returns the index of the earliest entry.
indexT latest()
Returns the index of the latest entry.
void nextEntry(const storedT &newEnt)
Add the next entry to the circular buffer.
Circular buffer which wraps with an if statement (branching) [faster than mod, less memory than index...
storedT & at(indexT refEntry, indexT idx)
Interface implementation for entry access.
Circular buffer which wraps with a pre-populated indices array [generally fastest].
TEST_CASE("creating a circular buffer with branching", "[sigproc::circularBuffer::circularBufferBranch]")