ucx/test.h

changeset 1
1bcaac272cdf
child 5
88625853ae74
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ucx/test.h	Fri Nov 30 21:18:13 2012 +0100
@@ -0,0 +1,90 @@
+/* 
+ * File:   test.h
+ * Author: Mike
+ *
+ * Created on 18. Februar 2012, 14:15
+ *
+ *
+ *
+ * Usage of this test framework:
+ *
+ * **** IN HEADER FILE: ****
+ *
+ * UCX_TEST_DECLARE(function_name)
+ *
+ * **** IN SOURCE FILE: ****
+ *
+ * UCX_TEST_IMPLEMENT(function_name) {
+ *  <memory allocation and other stuff here>
+ *  UCX_TEST_BEGIN
+ *  <tests with UCX_TEST_ASSERT here>
+ *  UCX_TEST_END
+ *  <cleanup of memory here>
+ * }
+ *
+ * PLEASE NOTE: if a test fails, a longjump is performed
+ * back to the UCX_TEST_BEGIN macro!
+ *
+ * You may use multiple BEGIN-END blocks if you are aware of the
+ * longjmp behaviour.
+ *
+ */
+
+#ifndef TEST_H
+#define	TEST_H
+
+#include "ucx.h"
+#include <stdio.h>
+#include <string.h>
+#include <setjmp.h>
+#include "list.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+#ifndef __FUNCTION__
+#define __FUNCTION__ __func__
+#endif
+
+typedef struct {
+    unsigned int success;
+    unsigned int failure;
+    UcxList *tests;
+} UcxTestSuite;
+
+typedef void(*UcxTest)(UcxTestSuite*,FILE*);
+
+UcxTestSuite* ucx_test_suite_new();
+void ucx_test_suite_free(UcxTestSuite*);
+
+void ucx_test_register(UcxTestSuite*, UcxTest);
+void ucx_test_run(UcxTestSuite*, FILE*);
+
+#define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *)
+#define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_)
+
+#define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
+        fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
+        fwrite("... ", 1, 4, _output_);\
+        jmp_buf _env_; \
+        if (!setjmp(_env_)) {
+
+#define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
+        fwrite(message".\n", 1, 2+strlen(message), _output_); \
+        _suite_->failure++; \
+        longjmp(_env_, 1);\
+    }
+
+#define UCX_TEST_SUBROUTINE(name,data) void name(UcxTestSuite* _suite_,\
+        FILE *_output_, jmp_buf _env_, void* data)
+#define UCX_TEST_CALL_SUBROUTINE(name,data) name(_suite_,_output_,_env_,data);
+
+#define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* TEST_H */
+

mercurial