[PATCH 3/3] tests: add wl_buffer tests

Marek Ch mchqwerty at gmail.com
Fri Nov 29 05:52:33 PST 2013


Using scanner to have current code of wl_buffer_* static function,
test their behaviour
---
 src/connection.c              |  3 ++
 tests/Makefile.am             |  5 +++-
 tests/test-runner/Makefile.am |  1 +
 tests/wl_buffer-test.c        | 64 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+), 1 deletion(-)
 create mode 100644 tests/wl_buffer-test.c

diff --git a/src/connection.c b/src/connection.c
index 1d8b61b..644fb1f 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -44,6 +44,7 @@
 
 #define DIV_ROUNDUP(n, a) ( ((n) + ((a) - 1)) / (a) )
 
+WL_EXPORT_TEST
 struct wl_buffer {
 	char data[4096];
 	uint32_t head, tail;
@@ -61,6 +62,7 @@ struct wl_connection {
 	int want_flush;
 };
 
+WL_EXPORT_TEST
 static void
 wl_buffer_put(struct wl_buffer *b, const void *data, size_t count)
 {
@@ -141,6 +143,7 @@ wl_buffer_copy(struct wl_buffer *b, void *data, size_t count)
 	}
 }
 
+WL_EXPORT_TEST
 static uint32_t
 wl_buffer_size(struct wl_buffer *b)
 {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4a999b9..6e2001c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,5 +1,6 @@
 SUBDIRS = test-runner
 test_runner_dir = $(top_builddir)/tests/test-runner
+tests_private = $(test_runner_dir)/tests-private.c
 
 TESTS =						\
 	array-test				\
@@ -15,7 +16,8 @@ TESTS =						\
 	socket-test				\
 	queue-test				\
 	signal-test				\
-	resources-test
+	resources-test				\
+	wl_buffer-test
 
 check_PROGRAMS =				\
 	$(TESTS)				\
@@ -37,6 +39,7 @@ socket_test_SOURCES = socket-test.c
 queue_test_SOURCES = queue-test.c
 signal_test_SOURCES = signal-test.c
 resources_test_SOURCES = resources-test.c
+wl_buffer_test_SOURCES = wl_buffer-test.c $(tests_private)
 
 fixed_benchmark_SOURCES = fixed-benchmark.c
 
diff --git a/tests/test-runner/Makefile.am b/tests/test-runner/Makefile.am
index bd7c292..8333bb2 100644
--- a/tests/test-runner/Makefile.am
+++ b/tests/test-runner/Makefile.am
@@ -10,6 +10,7 @@ libtest_helpers_la_SOURCES =	\
 	test-helpers.c
 
 scanner_input_files =	\
+	$(top_srcdir)/src/connection.c	\
 	scanner-tests.c
 
 noinst_PROGRAMS = scanner-tests
diff --git a/tests/wl_buffer-test.c b/tests/wl_buffer-test.c
new file mode 100644
index 0000000..39c42dd
--- /dev/null
+++ b/tests/wl_buffer-test.c
@@ -0,0 +1,64 @@
+#include "test-runner.h"
+#include "tests-private.h"
+
+/* Assert with formated output */
+#define assertf(cond, ...) 							\
+	do {									\
+		if (!(cond)) {							\
+			fprintf(stderr, "%s (%s: %d): Assertion %s failed!",	\
+					__FUNCTION__, __FILE__, __LINE__, #cond);\
+			fprintf(stderr, " " __VA_ARGS__);			\
+			putc('\n', stderr);					\
+			abort();						\
+		}								\
+	} while (0)
+
+static const char data[] = "abcdefghijklmnopqrstuvwxyz";
+
+TEST(wl_buffer_put_tst)
+{
+	struct wl_buffer b = {.head = 0, .tail = 0};
+	size_t index;
+
+	assert(wl_buffer_size(&b) == 0);
+
+	int i;
+	for (i = 1; i * (sizeof data) <= sizeof b.data; i++) {
+		wl_buffer_put(&b, data, sizeof data);
+
+		assert(b.tail == 0);
+		assertf(b.head == i * (sizeof data),
+			"head is %u instead of %lu", b.head, i * (sizeof data));
+		assert(wl_buffer_size(&b) == i * sizeof data);
+	}
+
+	/* do overflow */
+	wl_buffer_put(&b, data, sizeof data);
+
+	/* ensure */
+	assert(i * sizeof data > sizeof b.data);
+
+	assertf(MASK(b.head) < sizeof b.data);
+	assert(b.tail == 0);
+	index = sizeof b.data % sizeof data;
+	assertf(strncmp((char *) b.data + sizeof b.data - index, data, index - 1) == 0,
+		"Should have '%*s', but have '%*s'\n", (int) (index - 1), data,
+		(int) (index - 1), (char *) b.data + sizeof b.data - index);
+	assertf(strncmp(b.data, (char *) data + index, sizeof data - index) == 0,
+		"Should have '%*s', but have '%*s'\n",
+		(int) (sizeof data - index), data + index,
+		(int) (sizeof data - index), (char *) b.data);
+
+	struct wl_buffer bb = {.head = 0, .tail = 0};
+	wl_buffer_put(&bb, data, sizeof data);
+	assert(strcmp(data, bb.data) == 0);
+}
+
+TEST(wl_buffer_fill_alot)
+{
+	struct wl_buffer b = {.head = 0, .tail = 0};
+	int i;
+
+	for (i = 0; i *sizeof data < 100 * sizeof b.data; i++)
+		wl_buffer_put(&b, data, sizeof data);
+}
-- 
1.8.4.2



More information about the wayland-devel mailing list