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/midi_ringbuffer_test.cpp | 48 -------------------------- test/ringbuffer_test.cpp | 79 +++++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 74 deletions(-) delete mode 100644 test/midi_ringbuffer_test.cpp (limited to 'test') diff --git a/test/midi_ringbuffer_test.cpp b/test/midi_ringbuffer_test.cpp deleted file mode 100644 index fed3ca0..0000000 --- a/test/midi_ringbuffer_test.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include -#include -#include "raul/TimeStamp.hpp" -#include "raul/EventRingBuffer.hpp" -#include "raul/midi_names.h" - -using namespace std; -using namespace Raul; - -int -read_write_test(EventRingBuffer& rb, unsigned offset) -{ - TimeStamp t(TimeUnit(TimeUnit::FRAMES, 48000), 0, 0); - size_t size; - unsigned char write_buf[5]; - unsigned char read_buf[5]; - - snprintf(reinterpret_cast(write_buf), 5, "%d", offset); - size = strlen(reinterpret_cast(write_buf)); - -#ifndef NDEBUG - const size_t written = rb.write(t, size, write_buf); - assert(written == size); -#endif - - rb.read(&t, &size, read_buf); - - return strncmp( - reinterpret_cast(write_buf), - reinterpret_cast(read_buf), - size); -} - - -int -main() -{ - EventRingBuffer rb(32); - - for (size_t i = 0; i < 100000; ++i) - if (read_write_test(rb, i)) - return 1; - - return 0; -} - 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