This repository has been archived by the owner on Nov 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.cpp
95 lines (81 loc) · 2.64 KB
/
test.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <cstdio>
#define RINGBUFFER_DEBUG
#include "ringbuffer.hpp"
#include <algorithm>
#include <string>
enum { RINGBUFFER_COUNT = 14 };
typedef stack_ringbuffer<uint8_t, RINGBUFFER_COUNT, ringbuffer_no_overwrite> ringbuffer_t;
// typedef heap_ringbuffer<uint8_t, ringbuffer_no_overwrite> ringbuffer_t;
void print_rb(ringbuffer_t& rb) {
printf("--- (print) ---\n%lu elements: ", rb.size());
for(auto c : rb) {
printf("%c ", c);
}
printf("\n");
rb.print();
printf("{begin_|end_}contig1: from %c to %c (excluded)\n", *rb.begin_contig1(), *rb.end_contig1());
printf("contig1 as a buffer: ");
auto c = rb.contig1();
// printf(" (%lu) %s\n", c.second, rb.contiguous()? "(contiguous)":"(non-contiguous)");
for(size_t i=0; i<c.second; ++i) {
printf("%c", c.first[i]);
}
printf("\n");
printf("---\n");
}
void pop_rb(ringbuffer_t& rb) {
if(rb.empty()) {
printf("Cannot pop 1 element\n");
} else {
auto e = rb.front();
rb.pop_front();
printf("Popping 1 element: %c\n", e);
}
}
template <class RingBufferIterator, class OverwritePolicy = typename RingBufferIterator::container_type::overwrite_policy>
struct ringbuffer_iterator_update {
static void update(RingBufferIterator&, RingBufferIterator) {}
};
template <class RingBufferIterator>
struct ringbuffer_iterator_update<RingBufferIterator, ringbuffer_overwrite> {
static void update(RingBufferIterator& it, RingBufferIterator it2) {
it = it2;
printf("coucou\n");
}
};
void test_ringbuffer_iterator_update(ringbuffer_t& rb) {
// printf("starting test\n");
auto it = rb.offset(1);
ringbuffer_iterator_update<decltype(it)>::update(it, rb.begin());
// printf("ending test\n");
}
int main() {
ringbuffer_t rb(RINGBUFFER_COUNT);
std::fill(rb.data_begin(), rb.data_end(), ' ');
print_rb(rb);
std::string elts = "ABCDE";
printf("Inserting '%s'\n", elts.c_str());
std::copy(begin(elts), end(elts), std::back_inserter(rb));
print_rb(rb);
printf("Clearing '%s'\n", elts.c_str());
rb.pop_front(std::end(rb));
print_rb(rb);
printf("Inserting '%s'\n", elts.c_str());
std::copy(begin(elts), end(elts), std::back_inserter(rb));
print_rb(rb);
printf("Removing 3 elements\n");
// for(size_t i=0; i<3; ++i) pop_rb(rb);
rb.pop_front(rb.offset(3));
print_rb(rb);
elts = "abcde";
printf("Inserting '%s'\n", elts.c_str());
std::copy(begin(elts), end(elts), std::back_inserter(rb));
print_rb(rb);
printf("%ld is the distance\n", std::distance(std::begin(rb), std::end(rb)));
rb.clear();
rb.push_back('a');
rb.push_back('b');
rb.push_back('c');
test_ringbuffer_iterator_update(rb); // this function is empty if ringbuffer_no_overwrite is chosen, but not if ringbuffer_overwrite is chosen
return 0;
}