forked from pineforge-4pass/pineforge-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ringbuffer.cpp
More file actions
79 lines (67 loc) · 2.58 KB
/
Copy pathtest_ringbuffer.cpp
File metadata and controls
79 lines (67 loc) · 2.58 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
#include <cstdio>
#include <pineforge/series.hpp>
#include <pineforge/na.hpp>
static int tests_passed = 0;
static int tests_failed = 0;
#define CHECK(expr) \
do { \
if (!(expr)) { \
std::printf(" FAIL %s:%d %s\n", __FILE__, __LINE__, #expr); \
++tests_failed; \
} else { \
++tests_passed; \
} \
} while (0)
#define CHECK_DOUBLE_EQ(a, b) CHECK(std::abs((a) - (b)) < 1e-9)
void test_basic_operations() {
using namespace pineforge;
std::printf("test_basic_operations\n");
DynamicRingBuffer<double> rb(3);
CHECK(rb.size() == 0);
rb.push_front(1.0);
rb.push_front(2.0);
rb.push_front(3.0);
CHECK(rb.size() == 3);
CHECK_DOUBLE_EQ(rb[0], 3.0);
CHECK_DOUBLE_EQ(rb[1], 2.0);
CHECK_DOUBLE_EQ(rb[2], 1.0);
// Verify wrapping
rb.push_front(4.0);
CHECK(rb.size() == 3);
CHECK_DOUBLE_EQ(rb[0], 4.0);
CHECK_DOUBLE_EQ(rb[1], 3.0);
CHECK_DOUBLE_EQ(rb[2], 2.0);
CHECK(is_na(rb[3]));
}
void test_wraparound_equivalence() {
using namespace pineforge;
std::printf("test_wraparound_equivalence\n");
// After capacity is exceeded, reads must walk newest -> oldest
// across the wrap seam.
DynamicRingBuffer<double> rb(5);
for (int i = 0; i < 13; ++i) rb.push_front((double)i); // newest = 12
for (std::size_t k = 0; k < 5; ++k) CHECK(rb[k] == (double)(12 - (int)k));
CHECK(is_na(rb[5])); // out of range -> na
CHECK(is_na(rb[99]));
}
void test_lazy_alloc_characterization() {
using namespace pineforge;
std::printf("test_lazy_alloc_characterization\n");
// Reads before any push return na; first push materializes normally.
Series<double> s; // default max_len 500
CHECK(is_na(s[0]));
CHECK(is_na(s[7]));
CHECK(s.size() == 0);
s.push(3.5);
CHECK(s[0] == 3.5);
CHECK(is_na(s[1]));
}
int main() {
std::printf("=== DynamicRingBuffer Tests ===\n\n");
test_basic_operations();
test_wraparound_equivalence();
test_lazy_alloc_characterization();
std::printf("\n=== Results: %d passed, %d failed ===\n",
tests_passed, tests_failed);
return tests_failed > 0 ? 1 : 0;
}