mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
circularBuffer_test.cpp
1/** \file psdFilter_test.cpp
2 */
3#include "../../catch2/catch.hpp"
4
5#include <vector>
6#include <Eigen/Dense>
7
8#define MX_NO_ERROR_REPORTS
9
10#include "../../../include/sigproc/circularBuffer.hpp"
11
12/** Scenario: using a circular buffer with branch-wrapping
13 *
14 * Verify circular buffer operation with branch-wrapping
15 *
16 * \anchor tests_sigproc_circularBuffer_branch
17 */
18SCENARIO( "creating a circular buffer with branching", "[sigproc::circularBuffer::circularBufferBranch]" )
19{
20 GIVEN( "a circular buffer" )
21 {
22 WHEN( "adding exactly max entries worth" )
23 {
25 cb.maxEntries(5);
26 cb.nextEntry(0);
27 cb.nextEntry(1);
28 cb.nextEntry(2);
29 cb.nextEntry(3);
30 cb.nextEntry(4);
31
32 REQUIRE( cb[0] == 0 );
33 REQUIRE( cb[1] == 1 );
34 REQUIRE( cb[2] == 2 );
35 REQUIRE( cb[3] == 3 );
36 REQUIRE( cb[4] == 4 );
37
38 //reverse
39 REQUIRE( cb[-1] == 4 );
40 REQUIRE( cb[-2] == 3 );
41 REQUIRE( cb[-3] == 2 );
42 REQUIRE( cb[-4] == 1 );
43
44 REQUIRE( cb.at(cb.earliest(),0) == 0 );
45 REQUIRE( cb.at(cb.latest(),0) == 4 );
46 }
47
48 WHEN( "adding new values past the end" )
49 {
51 cb.maxEntries(5);
52 cb.nextEntry(0);
53 cb.nextEntry(1);
54 cb.nextEntry(2);
55 cb.nextEntry(3);
56 cb.nextEntry(4);
57 cb.nextEntry(5);
58 cb.nextEntry(6);
59
60 REQUIRE( cb[0] == 2 );
61 REQUIRE( cb[1] == 3 );
62 REQUIRE( cb[2] == 4 );
63 REQUIRE( cb[3] == 5 );
64 REQUIRE( cb[4] == 6 );
65
66 //reverse
67 REQUIRE( cb[-1] == 6 );
68 REQUIRE( cb[-2] == 5 );
69 REQUIRE( cb[-3] == 4 );
70 REQUIRE( cb[-4] == 3 );
71
72 REQUIRE( cb.at(cb.earliest(),0) == 2 );
73 REQUIRE( cb.at(cb.latest(),0) == 6 );
74 }
75
76 WHEN( "wrapping when not full" )
77 {
79 cb.maxEntries(5);
80 cb.nextEntry(0);
81 cb.nextEntry(1);
82 cb.nextEntry(2);
83 cb.nextEntry(3);
84
85 REQUIRE( cb[0] == 0 );
86 REQUIRE( cb[1] == 1 );
87 REQUIRE( cb[2] == 2 );
88 REQUIRE( cb[3] == 3 );
89 REQUIRE( cb[4] == 0 );
90
91 //reverse
92 REQUIRE( cb[-1] == 3 );
93 REQUIRE( cb[-2] == 2 );
94 REQUIRE( cb[-3] == 1 );
95 REQUIRE( cb[-4] == 0 );
96 }
97 }
98}
99
100/** Scenario: using a circular buffer with index-wrapping
101 *
102 * Verify circular buffer operation with index-wrapping
103 *
104 * \anchor tests_sigproc_circularBuffer_branch
105 */
106SCENARIO( "creating a circular buffer with indexing", "[sigproc::circularBuffer::circularBufferIndex]" )
107{
108 GIVEN( "a circular buffer" )
109 {
110 WHEN( "adding exactly max entries worth" )
111 {
113 cb.maxEntries(5);
114 cb.nextEntry(0);
115 cb.nextEntry(1);
116 cb.nextEntry(2);
117 cb.nextEntry(3);
118 cb.nextEntry(4);
119
120 REQUIRE( cb[0] == 0 );
121 REQUIRE( cb[1] == 1 );
122 REQUIRE( cb[2] == 2 );
123 REQUIRE( cb[3] == 3 );
124 REQUIRE( cb[4] == 4 );
125
126 //reverse
127 REQUIRE( cb[-1] == 4 );
128 REQUIRE( cb[-2] == 3 );
129 REQUIRE( cb[-3] == 2 );
130 REQUIRE( cb[-4] == 1 );
131
132 REQUIRE( cb.at(cb.earliest(),0) == 0 );
133 REQUIRE( cb.at(cb.latest(),0) == 4 );
134 }
135
136 WHEN( "adding new values past the end" )
137 {
139 cb.maxEntries(5);
140 cb.nextEntry(0);
141 cb.nextEntry(1);
142 cb.nextEntry(2);
143 cb.nextEntry(3);
144 cb.nextEntry(4);
145 cb.nextEntry(5);
146 cb.nextEntry(6);
147
148 REQUIRE( cb[0] == 2 );
149 REQUIRE( cb[1] == 3 );
150 REQUIRE( cb[2] == 4 );
151 REQUIRE( cb[3] == 5 );
152 REQUIRE( cb[4] == 6 );
153
154 //reverse
155 REQUIRE( cb[-1] == 6 );
156 REQUIRE( cb[-2] == 5 );
157 REQUIRE( cb[-3] == 4 );
158 REQUIRE( cb[-4] == 3 );
159
160 REQUIRE( cb.at(cb.earliest(),0) == 2 );
161 REQUIRE( cb.at(cb.latest(),0) == 6 );
162 }
163 }
164}
165
SCENARIO("Loading aoAtmosphere config settings", "[ao::analysis::aoAtmosphere]")
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].
storedT & at(indexT refEntry, indexT idx)
Interface implementation for entry access.