summaryrefslogtreecommitdiffstats
path: root/test/ringbuffer_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/ringbuffer_test.cpp')
-rw-r--r--test/ringbuffer_test.cpp79
1 files changed, 53 insertions, 26 deletions
diff --git a/test/ringbuffer_test.cpp b/test/ringbuffer_test.cpp
index 3a36bb8..ea1088c 100644
--- a/test/ringbuffer_test.cpp
+++ b/test/ringbuffer_test.cpp
@@ -1,47 +1,74 @@
#include <iostream>
#include <cstring>
+#include <cstdlib>
+
+#include "raul/log.hpp"
#include "raul/RingBuffer.hpp"
using namespace std;
using namespace Raul;
-void
-print_buf(size_t size, char* buf)
-{
- cout << "{ ";
- for (size_t i=0; i < size; ++i) {
- cout << buf[i];
- if (i < size-1)
- cout << ", ";
- }
-
- cout << " }" << endl;
-}
-
-
int
main()
{
- RingBuffer rb(5);
+ static const int n_tests = 32;
+ for (int i = 0; i < n_tests; ++i) {
+ const int size = (rand() % 2000) + 8;
+ RingBuffer rb(size);
- char ev[] = { 'a', 'b', 'c' };
+ for (int j = 0; j < size * 32; ++j) {
+ char ev1[] = { 'a', 'b', 'c' };
+ rb.write(3, ev1);
- rb.write(3, ev);
+ char buf[3];
+ uint32_t read = rb.read(3, buf);
+ if (read != 3 || strncmp(buf, "abc", 3)) {
+ error << "Corrupt event " << i << ".1: "
+ << buf[0] << buf[1] << buf[2] << endl;
+ return 1;
+ }
+
+ char ev2[] = { 'd', 'e', 'f' };
+ if (!rb.write(3, ev2)) {
+ error << "Failed write " << i << ".2" << endl;
+ return 1;
+ }
- char buf[3];
- rb.read(3, buf);
- print_buf(3, buf);
+ char ev3[] = { 'g', 'h' };
+ if (!rb.write(2, ev3)) {
+ error << "Failed write " << i << ".3" << endl;
+ return 1;
+ }
- char ev2[] = { 'd', 'e', 'f' };
- rb.write(3, ev2);
+ if (rb.skip(1) != 1) {
+ error << "Failed skip " << i << endl;
+ return 1;
+ }
+ uint32_t n_read = rb.read(2, buf);
+ if (n_read != 2 || strncmp(buf, "ef", 2)) {
+ error << "Corrupt event " << i << ".4: "
+ << buf[0] << buf[1] << buf[2] << endl;
+ return 1;
+ }
- size_t read = rb.read(3, buf);
- if (read < 3)
- rb.read(3 - read, buf + read);
+ n_read = rb.peek(2, buf);
+ if (n_read != 2 || strncmp(buf, "gh", 2)) {
+ error << "Corrupt peek event " << i << ".5: "
+ << buf[0] << buf[1] << endl;
+ return 1;
+ }
- print_buf(3, buf);
+ n_read = rb.read(2, buf);
+ if (n_read != 2 || strncmp(buf, "gh", 2)) {
+ error << "Corrupt event " << i << ".6: "
+ << buf[0] << buf[1] << endl;
+ return 1;
+ }
+ }
+ }
+ info << "Successfully ran " << n_tests << " tests." << endl;
return 0;
}