summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2012-01-31 21:12:48 +0000
committerDavid Robillard <d@drobilla.net>2012-01-31 21:12:48 +0000
commit86826ae6733119d462be9f3642161db895756643 (patch)
tree27edf411aeecde4d96c01f1305e8764da963d84b /test
parent790aa117d1bd1c012c6982d1c6bd0832eb9d6118 (diff)
downloadzix-86826ae6733119d462be9f3642161db895756643.tar.gz
zix-86826ae6733119d462be9f3642161db895756643.tar.bz2
zix-86826ae6733119d462be9f3642161db895756643.zip
Full test coverage for ZixRing.
Update waf. git-svn-id: http://svn.drobilla.net/zix/trunk@50 df6676b4-ccc9-40e5-b5d6-7c4628a128e3
Diffstat (limited to 'test')
-rw-r--r--test/ring_test.c96
1 files changed, 91 insertions, 5 deletions
diff --git a/test/ring_test.c b/test/ring_test.c
index 7cde650..658f6af 100644
--- a/test/ring_test.c
+++ b/test/ring_test.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include <pthread.h>
+#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -30,6 +31,17 @@ size_t n_writes = 0;
bool read_error = false;
static int
+failure(const char* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stderr, "error: ");
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ return 1;
+}
+
+static int
gen_msg(int* msg, int start)
{
for (int i = 0; i < MSG_SIZE; ++i) {
@@ -120,26 +132,100 @@ main(int argc, char** argv)
n_writes, MSG_SIZE, size);
ring = zix_ring_new(size);
+ if (zix_ring_read_space(ring) != 0) {
+ return failure("New ring is not empty\n");
+ }
+ if (zix_ring_write_space(ring) != zix_ring_capacity(ring)) {
+ return failure("New ring write space != capacity\n");
+ }
+
+ zix_ring_mlock(ring);
pthread_t reader_thread;
if (pthread_create(&reader_thread, NULL, reader, NULL)) {
- fprintf(stderr, "Failed to create reader thread\n");
- return 1;
+ return failure("Failed to create reader thread\n");
}
pthread_t writer_thread;
if (pthread_create(&writer_thread, NULL, writer, NULL)) {
- fprintf(stderr, "Failed to create writer thread\n");
- return 1;
+ return failure("Failed to create writer thread\n");
}
pthread_join(reader_thread, NULL);
pthread_join(writer_thread, NULL);
if (read_error) {
- fprintf(stderr, "FAIL: Read error\n");
+ return failure("Read error\n");
+ }
+
+ zix_ring_reset(ring);
+ if (zix_ring_read_space(ring) > 0) {
+ fprintf(stderr, "Reset did not empty ring.\n");
return 1;
}
+ if (zix_ring_write_space(ring) != zix_ring_capacity(ring)) {
+ fprintf(stderr, "Empty write space != capacity\n");
+ return 1;
+ }
+
+ zix_ring_write(ring, "a", 1);
+ zix_ring_write(ring, "b", 1);
+
+ char buf;
+ uint32_t n = zix_ring_peek(ring, &buf, 1);
+ if (n != 1) {
+ return failure("Peek n (%d) != 1\n", n);
+ }
+ if (buf != 'a') {
+ return failure("Peek error: '%c' != 'a'\n", buf);
+ }
+
+ n = zix_ring_skip(ring, 1);
+ if (n != 1) {
+ return failure("Skip n (%d) != 1\n", n);
+ }
+
+ if (zix_ring_read_space(ring) != 1) {
+ return failure("Read space %d != 1\n", zix_ring_read_space(ring));
+ }
+
+ n = zix_ring_read(ring, &buf, 1);
+ if (n != 1) {
+ return failure("Peek n (%d) != 1\n", n);
+ }
+ if (buf != 'b') {
+ return failure("Peek error: '%c' != 'b'\n", buf);
+ }
+
+ if (zix_ring_read_space(ring) != 0) {
+ return failure("Read space %d != 0\n", zix_ring_read_space(ring));
+ }
+
+ n = zix_ring_peek(ring, &buf, 1);
+ if (n > 0) {
+ return failure("Successful underrun peak\n");
+ }
+
+ n = zix_ring_read(ring, &buf, 1);
+ if (n > 0) {
+ return failure("Successful underrun read\n");
+ }
+
+ n = zix_ring_skip(ring, 1);
+ if (n > 0) {
+ return failure("Successful underrun read\n");
+ }
+
+ char* big_buf = calloc(size, 1);
+ n = zix_ring_write(ring, big_buf, size - 1);
+ if (n != (uint32_t)size - 1) {
+ return failure("Maximum size write failed (wrote %u)\n", n);
+ }
+
+ n = zix_ring_write(ring, big_buf, size);
+ if (n != 0) {
+ return failure("Successful overrun write (size %u)\n", n);
+ }
zix_ring_free(ring);
return 0;