Add Glk unit test framework
authorPhilip Chimento <philip.chimento@gmail.com>
Wed, 25 Sep 2013 06:14:46 +0000 (23:14 -0700)
committerPhilip Chimento <philip.chimento@gmail.com>
Wed, 25 Sep 2013 06:25:46 +0000 (23:25 -0700)
This is the beginning of a unit test framework for Glk. Currently it has
only one test. But the potential is infinite! It could be used to test
how well other Glk implementations conform too.

Makefile.am
configure.ac
tests/Makefile.am
tests/unit/Makefile.am [new file with mode: 0644]
tests/unit/datetime.c [new file with mode: 0644]
tests/unit/glkunit.c [new file with mode: 0644]
tests/unit/glkunit.h [new file with mode: 0644]

index d0d290d371a83d72597f41f791e54a8703ba5ace..b7200b63eacc69fa4e8919358c10e1bd2e4690e3 100644 (file)
@@ -31,7 +31,7 @@ macros_ignore = codeset gettext glibc2 glibc21 gtk-doc iconv intdiv0 intl \
 MAINTAINERCLEANFILES = ABOUT-NLS INSTALL aclocal.m4 compile config.guess \
        config.h.in config.rpath config.sub depcomp gtk-doc.make install-sh \
        intltool-extract.in intltool-merge.in intltool-update.in ltmain.sh missing \
-       ylwrap autom4te.cache \
+       ylwrap autom4te.cache test-driver \
        $(addprefix 'm4/',$(addsuffix '.m4', $(macros_ignore)))
 
 ACLOCAL_AMFLAGS = -I m4
index 7e72a84315a01784abcfeba28a018b33beff04ef..2d89075424622fb67ef739b8e6877b026322a9c4 100644 (file)
@@ -171,6 +171,7 @@ interpreters/glulxe/Makefile
 interpreters/git/Makefile
 interpreters/bocfel/Makefile
 tests/Makefile
+tests/unit/Makefile
 player/Makefile
 player/config.py
 docs/Makefile
index aacfc6e4a5fb694e97fdfa53b60c9da451e2d531..f8909a5d8063f17bc728687080b4f32a3539a5a9 100644 (file)
@@ -1,3 +1,5 @@
+SUBDIRS = unit
+
 AM_CFLAGS = -Wall
 AM_CPPFLAGS = -I$(top_srcdir)
 
diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am
new file mode 100644 (file)
index 0000000..fc51028
--- /dev/null
@@ -0,0 +1,28 @@
+# The rpath is necessary for check_ too (see rant in ../Makefile.am)
+TEST_PLUGIN_LIBTOOL_FLAGS = \
+       -module \
+       -shared \
+       -avoid-version \
+       -export-symbols-regex "^glk_main$$" \
+       -rpath $(abs_builddir) \
+       $(NULL)
+
+# Set up include dirs so that #include "glk.h" works
+AM_CPPFLAGS = -I$(top_srcdir)/libchimara $(CPPFLAGS)
+
+check_LTLIBRARIES = datetime.la
+datetime_la_SOURCES = datetime.c glkunit.c glkunit.h
+datetime_la_LDFLAGS = $(TEST_PLUGIN_LIBTOOL_FLAGS)
+
+TESTS = datetime.la
+TEST_EXTENSIONS = .la
+LA_LOG_COMPILER = $(builddir)/../plugin-loader
+
+CLEANFILES = \
+       test-suite.log \
+       $(TESTS) \
+       $(TESTS:.la=.log) \
+       $(TESTS:.la=.trs) \
+       $(NULL)
+
+-include $(top_srcdir)/git.mk
diff --git a/tests/unit/datetime.c b/tests/unit/datetime.c
new file mode 100644 (file)
index 0000000..591dc9f
--- /dev/null
@@ -0,0 +1,26 @@
+#include "glk.h"
+#include "glkunit.h"
+
+static int
+test_current_time_equals_current_simple_time(void)
+{
+       glktimeval_t timeval;
+
+       /* This test will fail if the following two operations do not occur within
+       a second of each other. That is not a robust test, but you could argue that
+       there is a serious bug if either operation takes more than one second. */
+       glk_current_time(&timeval);
+       glsi32 simple_time = glk_current_simple_time(1);
+       ASSERT_EQUAL(simple_time, timeval.low_sec);
+
+       glk_current_time(&timeval);
+       glsi32 simple_time_high_bits = glk_current_simple_time(0xFFFFFFFF);
+       ASSERT_EQUAL(simple_time_high_bits, timeval.high_sec);
+
+       SUCCEED;
+}
+
+struct TestDescription tests[] = {
+       { "current time equals simple time", test_current_time_equals_current_simple_time },
+       { NULL, NULL }
+};
diff --git a/tests/unit/glkunit.c b/tests/unit/glkunit.c
new file mode 100644 (file)
index 0000000..63d8134
--- /dev/null
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "glk.h"
+#include "glkunit.h"
+
+extern struct TestDescription tests[];
+
+void
+glk_main(void)
+{
+    struct TestDescription *test = tests;
+    int tested = 0, succeeded = 0, failed = 0;
+    while(test->name != NULL) {
+        tested++;
+        /* Use stdio.h to print to stdout, Glk can't do that */
+        printf("  Testing %s... ", test->name);
+        if( test->testfunc() ) {
+            succeeded++;
+            printf("PASS\n");
+        } else {
+            failed++;
+            printf("FAIL\n");
+        }
+        test++;
+    }
+    printf("%d tests run, %d failures\n", tested, failed);
+    exit(failed > 0);
+}
diff --git a/tests/unit/glkunit.h b/tests/unit/glkunit.h
new file mode 100644 (file)
index 0000000..3828987
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef GLKUNIT_H
+#define GLKUNIT_H
+
+#include <stdio.h>
+
+#define _BEGIN do {
+#define _END } while(0);
+
+/* msg must be a string literal */
+#define _ASSERT(expr, msg, ...) _BEGIN \
+    if( !(expr) ) { \
+        fprintf(stderr, "Assertion failed: " msg "\n", __VA_ARGS__); \
+        return 0; \
+    } _END
+
+#define SUCCEED _BEGIN return 1; _END
+#define ASSERT(expr) _ASSERT(expr, "%s", #expr)
+#define ASSERT_EQUAL(expected, actual) _ASSERT((expected) == (actual), "%s == %s", #expected, #actual);
+
+struct TestDescription {
+    char *name;
+    int (*testfunc)(void);
+};
+
+#endif /* GLKUNIT_H */