- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathringbuffer_host_test.cpp
More file actions
Latest commit
143 lines (118 loc) · 4.88 KB
/
Copy pathringbuffer_host_test.cpp
File metadata and controls
143 lines (118 loc) · 4.88 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include"RingBuffer.h"
#include<cstdint>
#include<cstdlib>
#include<iostream>
namespace {
structSample {
uint16_t id;
float reading;
};
boolcheck(bool condition, constchar* message) {
if (!condition) {
std::cerr << "FAIL: " << message << '\n';
returnfalse;
}
returntrue;
}
booltestUint8Basic() {
RingBuffer<uint8_t, 8> buffer;
constuint8_t values[] = {1, 2, 3, 4};
uint8_t out[4] = {};
returncheck(buffer.push(values, 4) == 4, "uint8_t push count") &&
check(buffer.available() == 4, "uint8_t available after push") &&
check(buffer.peek(out, 4) == 4, "uint8_t peek count") &&
check(out[0] == 1 && out[1] == 2 && out[2] == 3 && out[3] == 4, "uint8_t peek contents") &&
check(buffer.pop(out, 4) == 4, "uint8_t pop count") &&
check(out[0] == 1 && out[1] == 2 && out[2] == 3 && out[3] == 4, "uint8_t pop contents") &&
check(buffer.available() == 0, "uint8_t available after pop");
}
booltestOverwriteKeepsNewest() {
RingBuffer<uint8_t, 8> buffer;
constuint8_t values[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
uint8_t out[8] = {};
returncheck(buffer.push(values, 12, true) == 8, "overwrite push count") &&
check(buffer.available() == 8, "overwrite available") &&
check(buffer.pop(out, 8) == 8, "overwrite pop count") &&
check(out[0] == 4 && out[1] == 5 && out[2] == 6 && out[3] == 7 &&
out[4] == 8 && out[5] == 9 && out[6] == 10 && out[7] == 11,
"overwrite keeps newest values");
}
booltestInt16Wrap() {
RingBuffer<int16_t, 8> buffer;
constint16_t first[] = {10, 20, 30, 40, 50, 60};
int16_t out[4] = {};
if (!check(buffer.push(first, 6) == 6, "int16_t initial push")) returnfalse;
if (!check(buffer.consume(4) == 4, "int16_t consume")) returnfalse;
constint16_t second[] = {70, 80, 90, 100};
returncheck(buffer.push(second, 4) == 4, "int16_t wrapped push") &&
check(buffer.pop(out, 4) == 4, "int16_t wrapped pop") &&
check(out[0] == 50 && out[1] == 60 && out[2] == 70 && out[3] == 80, "int16_t wrapped contents");
}
booltestFloatPeekConsume() {
RingBuffer<float, 8> buffer;
constfloat values[] = {1.25f, 2.5f, 3.75f};
float out[3] = {};
returncheck(buffer.push(values, 3) == 3, "float push") &&
check(buffer.peek(out, 2) == 2, "float peek count") &&
check(out[0] == 1.25f && out[1] == 2.5f, "float peek contents") &&
check(buffer.consume() == 1, "float consume one") &&
check(buffer.pop(out, 2) == 2, "float pop after consume") &&
check(out[0] == 2.5f && out[1] == 3.75f, "float pop contents");
}
booltestStructType() {
RingBuffer<Sample, 4> buffer;
const Sample values[] = {
{1, 0.5f},
{2, 1.5f},
{3, 2.5f},
};
Sample out[3] = {};
returncheck(buffer.push(values, 3) == 3, "struct push") &&
check(buffer.pop(out, 3) == 3, "struct pop") &&
check(out[0].id == 1 && out[0].reading == 0.5f, "struct first value") &&
check(out[1].id == 2 && out[1].reading == 1.5f, "struct second value") &&
check(out[2].id == 3 && out[2].reading == 2.5f, "struct third value");
}
booltestSingleStructPushPop() {
RingBuffer<Sample, 4> buffer;
const Sample input = {42, 12.5f};
Sample output = {};
returncheck(buffer.push(input) == 1, "single struct push") &&
check(buffer.available() == 1, "single struct available") &&
check(buffer.pop(output) == 1, "single struct pop") &&
check(output.id == 42 && output.reading == 12.5f, "single struct contents") &&
check(buffer.available() == 0, "single struct empty after pop");
}
booltestRetainedStorage() {
using RetainedBuffer = RingBuffer<uint8_t, 8>;
RetainedBuffer::Storage storage = {};
{
RetainedBuffer writer(storage, false);
constuint8_t values[] = {21, 22, 23};
if (!check(writer.push(values, 3) == 3, "retained push")) returnfalse;
if (!check(writer.available() == 3, "retained available after write")) returnfalse;
}
{
RetainedBuffer reader(storage, true);
uint8_t out[3] = {};
returncheck(reader.available() == 3, "retained available after reconstruct") &&
check(reader.pop(out, 3) == 3, "retained pop") &&
check(out[0] == 21 && out[1] == 22 && out[2] == 23, "retained contents");
}
}
} // namespace
intmain() {
bool ok = true;
ok = testUint8Basic() && ok;
ok = testOverwriteKeepsNewest() && ok;
ok = testInt16Wrap() && ok;
ok = testFloatPeekConsume() && ok;
ok = testStructType() && ok;
ok = testSingleStructPushPop() && ok;
ok = testRetainedStorage() && ok;
if (!ok) {
returnEXIT_FAILURE;
}
std::cout << "PASS: ring buffer host tests" << '\n';
returnEXIT_SUCCESS;
}