[PATCH 2/3] Add initial unit tests and unit test framework.

U. Artie Eoff ullysses.a.eoff at intel.com
Wed Feb 29 11:36:29 PST 2012


From: "U. Artie Eoff" <ullysses.a.eoff at intel.com>

Add tests directory and makefiles to the toolchain configuration.
Created first unit tests for wayland-util.

Unit tests can be executed with the command 'make check'.

Signed-off-by: U. Artie Eoff <ullysses.a.eoff at intel.com>
---
 Makefile.am                |    2 +-
 configure.ac               |    3 +-
 tests/Makefile.am          |    8 +++
 tests/check-wayland-util.c |  107 ++++++++++++++++++++++++++++++++++++++++++++
 tests/wayland-check.h      |   39 ++++++++++++++++
 5 files changed, 157 insertions(+), 2 deletions(-)
 create mode 100644 tests/Makefile.am
 create mode 100644 tests/check-wayland-util.c
 create mode 100644 tests/wayland-check.h

diff --git a/Makefile.am b/Makefile.am
index 016bb76..6c4b7e8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = src protocol
+SUBDIRS = src protocol tests
 
 ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
 
diff --git a/configure.ac b/configure.ac
index 50ab47b..19e48ae 100644
--- a/configure.ac
+++ b/configure.ac
@@ -60,5 +60,6 @@ AC_CONFIG_FILES([Makefile
 		 src/Makefile
 		 src/wayland-server.pc
 		 src/wayland-client.pc
-		 protocol/Makefile])
+		 protocol/Makefile
+         tests/Makefile])
 AC_OUTPUT
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..3b305c2
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,8 @@
+
+TESTS = check-wayland-util
+
+check_PROGRAMS = check-wayland-util
+
+check_wayland_util_SOURCES = check-wayland-util.c
+check_wayland_util_CFLAGS = @CHECK_CFLAGS@ -I$(top_builddir)/src/ -std=c99
+check_wayland_util_LDADD = $(top_builddir)/src/libwayland-util.la @CHECK_LIBS@
diff --git a/tests/check-wayland-util.c b/tests/check-wayland-util.c
new file mode 100644
index 0000000..dda2f50
--- /dev/null
+++ b/tests/check-wayland-util.c
@@ -0,0 +1,107 @@
+#include <wayland-util.h>
+#include "wayland-check.h"
+
+WLD_START_TEST(test_wl_array_init)
+{
+    const int iterations = 4122; // this is arbitrary
+    // init array an arbitray amount of times
+    // and verify the defaults are sensible
+    for (int i = 0; i < iterations; ++i)
+    {
+        struct wl_array array;
+        wl_array_init(&array);
+
+        fail_unless(0 == array.size);
+        fail_unless(0 == array.alloc);
+        fail_unless(0 == array.data);
+    }
+}
+WLD_END_TEST
+
+WLD_START_TEST(test_wl_array_add)
+{
+    struct mydata {
+        int         a;
+        unsigned    b;
+        double      c;
+        double      d;
+    };
+
+    const int iterations = 1321; // this is arbitrary
+    const int datasize = sizeof(struct mydata);
+
+    struct wl_array array;
+    wl_array_init(&array);
+
+    // add some data
+    for (int i = 0; i < iterations; ++i)
+    {
+        struct mydata* ptr = wl_array_add(&array, datasize);
+        fail_unless( (i + 1) * datasize == array.size);
+
+        ptr->a = i * 3;
+        ptr->b = -i;
+        ptr->c = (double)(i);
+        ptr->d = (double)(i / 2.);
+    }
+
+    // verify the data
+    for (int i = 0; i < iterations; ++i)
+    {
+        const int index = datasize * i;
+        struct mydata* check = (struct mydata*)(array.data + index);
+        fail_unless(i*3 == check->a);
+        fail_unless(-i == check->b);
+        fail_unless((double)(i) == check->c);
+        fail_unless((double)(i/2.) == check->d);
+    }
+
+    wl_array_release(&array);
+}
+WLD_END_TEST
+
+WLD_START_TEST(test_wl_array_copy)
+{
+    const int iterations = 1529; // this is arbitrary
+
+    struct wl_array source;
+    wl_array_init(&source);
+
+    // add some data
+    for (int i = 0; i < iterations; ++i)
+    {
+        int *p = wl_array_add(&source, sizeof(int));
+        *p = i * 2 + i;
+    }
+
+    // copy the array
+    struct wl_array copy;
+    wl_array_copy(&copy, &source);
+
+    // check the copy
+    for (int i = 0; i < iterations; ++i)
+    {
+        const int index = sizeof(int) * i;
+        int *s = (int *)(source.data + index);
+        int *c = (int *)(copy.data + index);
+        fail_unless(*s == *c); // verify the values are the same
+        fail_if(s == c); // ensure the addresses aren't the same
+        fail_unless(i * 2 + i == *s); // sanity check
+    }
+
+    wl_array_release(&source);
+    wl_array_release(&copy);
+}
+WLD_END_TEST
+
+// Add all the tests to a test suite
+WLD_START_SUITE(util)
+{
+    WLD_SUITE_ADD_TEST(test_wl_array_init)
+    WLD_SUITE_ADD_TEST(test_wl_array_add)
+    WLD_SUITE_ADD_TEST(test_wl_array_copy)
+}
+WLD_END_SUITE
+
+// Generate test main
+WLD_CHECK_MAIN(util)
diff --git a/tests/wayland-check.h b/tests/wayland-check.h
new file mode 100644
index 0000000..754afa2
--- /dev/null
+++ b/tests/wayland-check.h
@@ -0,0 +1,39 @@
+#ifndef WLD_CHECK_H
+#define WLD_CHECK_H
+
+#include <check.h>
+
+#define WLD_START_TEST(fname)               \
+    START_TEST (fname)
+
+#define WLD_END_TEST                        \
+    END_TEST
+
+#define WLD_START_SUITE(name)               \
+static Suite * wld_ ## name ## _suite (void)\
+{                                           \
+    Suite *suite = suite_create(# name);    \
+    TCase *tc_chain = tcase_create("core"); \
+    suite_add_tcase (suite, tc_chain);      \
+
+#define WLD_SUITE_ADD_TEST(name)            \
+    tcase_add_test(tc_chain, name);
+
+#define WLD_END_SUITE                       \
+    return suite;                           \
+}
+
+#define WLD_CHECK_MAIN(name)                \
+int main (int argc, char **argv)            \
+{                                           \
+    Suite *suite = wld_ ## name ## _suite();\
+    SRunner *sr;                            \
+    int num_failed;                         \
+    sr = srunner_create(suite);             \
+    srunner_run_all(sr, CK_VERBOSE);        \
+    num_failed = srunner_ntests_failed(sr); \
+    srunner_free(sr);                       \
+    return num_failed;                      \
+}
+
+#endif
-- 
1.7.7.6



More information about the wayland-devel mailing list