Skip to content

Instantly share code, notes, and snippets.

@jsimmons
Created October 4, 2010 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsimmons/609649 to your computer and use it in GitHub Desktop.
Save jsimmons/609649 to your computer and use it in GitHub Desktop.
Test cases for my ringbuffer codes
josh@josh-laptop:~/Projects/rirc$ ./build/tests/rirc-tests
/ringbuffer/new free: OK
/ringbuffer/read write: OK
/ringbuffer/stream: OK
/ringbuffer/wrap around: OK
#include "tests.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <ring-buffer.h>
static const size_t BUFFER_SIZE = 512;
typedef struct
{
RingBuffer *buffer;
} RingBufferFixture;
static void setup(RingBufferFixture *fixture, gconstpointer static_data)
{
fixture->buffer = rb_new(BUFFER_SIZE);
}
static void teardown(RingBufferFixture *fixture, gconstpointer static_data)
{
rb_free(fixture->buffer);
}
static void test_new_free()
{
RingBuffer *buf = rb_new(BUFFER_SIZE);
g_assert(buf != NULL);
g_assert(buf->data != NULL);
g_assert_cmpuint(buf->size, ==, BUFFER_SIZE);
g_assert_cmpuint(buf->head, ==, 0);
g_assert_cmpuint(buf->fill, ==, 0);
g_assert(rb_is_empty(buf));
g_assert(!rb_is_full(buf));
g_assert_cmpuint(rb_size(buf), ==, BUFFER_SIZE);
g_assert_cmpuint(rb_used(buf), ==, 0);
g_assert_cmpuint(rb_remain(buf), ==, BUFFER_SIZE);
rb_free(buf);
}
static void test_rw(RingBufferFixture *fixture, gconstpointer static_data)
{
RingBuffer *buf = fixture->buffer;
/// WRITING
const char *data = "Hello, World!";
size_t data_length = strlen(data) + 1;
// Simple write.
g_assert_cmpuint(rb_remain(buf), >=, data_length);
rb_write(buf, data, data_length);
g_assert_cmpuint(rb_used(buf), ==, data_length);
// Block write.
size_t writable;
char *writer = rb_write_pointer(buf, &writable);
g_assert(writer != NULL);
g_assert_cmpuint(writable, >=, data_length);
memcpy(writer, data, data_length);
rb_write_commit(buf, data_length);
g_assert_cmpuint(rb_used(buf), ==, data_length * 2);
/// READING
char *data_out = malloc(sizeof(data_length));
// Simple read.
rb_read(buf, data_out, data_length);
g_assert_cmpstr(data_out, ==, data);
// Buffer should have advanced after read.
g_assert_cmpuint(rb_used(buf), ==, data_length);
memset(data_out, 0, data_length);
// Block read.
size_t readable;
size_t read = 0;
do
{
g_assert(!rb_is_empty(buf));
const char *out = rb_read_pointer(buf, 0, &readable);
g_assert(out != NULL);
g_assert_cmpuint(readable, >, 0);
memcpy(data_out + read, out, readable);
read += readable;
rb_read_commit(buf, readable);
} while(read < data_length);
g_assert(rb_is_empty(buf));
g_assert_cmpstr(data_out, ==, data);
}
static void test_stream(RingBufferFixture *fixture, gconstpointer static_data)
{
RingBuffer *buf = fixture->buffer;
const char *data = "Hello, World!";
size_t data_length = strlen(data) + 1;
for(int i = 0; i < 5; i++)
{
rb_write(buf, data, data_length);
}
RingBuffer *buf2 = rb_new(data_length * 5);
rb_stream(buf, buf2, rb_used(buf));
g_assert(rb_is_full(buf2));
rb_empty(buf);
g_assert(rb_is_empty(buf));
char *out = malloc(data_length);
g_assert(out != NULL);
for(int i = 0; i < 5; i++)
{
memset(out, 0, data_length);
rb_read(buf2, out, data_length);
g_assert_cmpstr(data, ==, out);
}
g_assert(rb_is_empty(buf));
}
void test_wrap_around(RingBufferFixture *fixture, gconstpointer static_data)
{
RingBuffer *buf = fixture->buffer;
const char *data = "Hello, World!";
size_t data_length = strlen(data) + 1;
rb_write(buf, data, data_length);
for(int i = 0; i < 500; i++)
{
rb_write(buf, data, data_length);
rb_read_commit(buf, data_length);
}
char *out = malloc(data_length);
g_assert(out != NULL);
rb_read(buf, out, data_length);
g_assert_cmpstr(data, ==, out);
g_assert(rb_is_empty(buf));
}
void ringbuffer_setup_tests()
{
g_test_add_func("/ringbuffer/new free", test_new_free);
g_test_add("/ringbuffer/read write", RingBufferFixture, NULL, setup, test_rw, teardown);
g_test_add("/ringbuffer/stream", RingBufferFixture, NULL, setup, test_stream, teardown);
g_test_add("/ringbuffer/wrap around", RingBufferFixture, NULL, setup, test_wrap_around, teardown);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment