From 0d229deb0d6059e997d69207e152343ef811da80 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 28 Apr 2011 18:19:28 +0000 Subject: Improve RingBuffer implementation. Previous implementation was broken when written to full capacity, and this version is significantly faster as well. git-svn-id: http://svn.drobilla.net/lad/trunk/raul@3213 a436a847-0d15-0410-975c-d299462d15a1 --- test/ringbuffer_test.cpp | 79 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 26 deletions(-) (limited to 'test/ringbuffer_test.cpp') 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 #include +#include + +#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; } -- cgit v1.2.1