summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-04-28 18:19:28 +0000
committerDavid Robillard <d@drobilla.net>2011-04-28 18:19:28 +0000
commit0d229deb0d6059e997d69207e152343ef811da80 (patch)
tree54c2163fa1eb0f848204e7e9841a58b934dad5ef /test
parent4378875d3c74b4fa501e92d6e11837c7fe67c525 (diff)
downloadraul-0d229deb0d6059e997d69207e152343ef811da80.tar.gz
raul-0d229deb0d6059e997d69207e152343ef811da80.tar.bz2
raul-0d229deb0d6059e997d69207e152343ef811da80.zip
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
Diffstat (limited to 'test')
-rw-r--r--test/midi_ringbuffer_test.cpp48
-rw-r--r--test/ringbuffer_test.cpp79
2 files changed, 53 insertions, 74 deletions
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 <iostream>
-#include <cstring>
-#include <cstdio>
-#include <stdio.h>
-#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<char*>(write_buf), 5, "%d", offset);
- size = strlen(reinterpret_cast<const char*>(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<const char*>(write_buf),
- reinterpret_cast<const char*>(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 <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;
}