summaryrefslogtreecommitdiff
path: root/tools/test.h
diff options
context:
space:
mode:
Diffstat (limited to 'tools/test.h')
-rw-r--r--tools/test.h167
1 files changed, 149 insertions, 18 deletions
diff --git a/tools/test.h b/tools/test.h
index 6fb3e51..4a12d85 100644
--- a/tools/test.h
+++ b/tools/test.h
@@ -30,30 +30,161 @@
#include <stdio.h>
-#define TEST_REPORT { printf("\nTest report:\n%d tests\n%d test failed.\n", TEST_num_tests, TEST_num_fails); }
+#define TEST_REPORT { \
+ fprintf(stderr, "\nTest report:\n%d tests\n%d test failed.\n", \
+ TEST_num_tests, TEST_num_fails); \
+ }
-#define TEST_BEGIN int main() { int TEST_num_fails = 0; int TEST_num_tests = 0; {}
-#define TEST_END { TEST_REPORT; return TEST_num_fails != 0; } }
-#define TEST_FATAL(m) { printf("\n!!!! Test fatal error !!!!\n"m"\n"); { TEST_END; }
+#define TEST_BEGIN \
+ int main() { \
+ int TEST_num_fails = 0; \
+ int TEST_num_tests = 0; \
+ {}
-#define TEST_OK(m) { printf(" OK: "m"\n"); }
-#define TEST_FAIL(m) { printf(" FAIL: "m"\n"); TEST_num_fails++; }
-#define TEST_MSG(fmt...) { TEST_num_tests++; printf("\n"); printf(fmt); printf(" (line %d)\n", __LINE__); }
+#define TEST_END { \
+ TEST_REPORT; \
+ return TEST_num_fails != 0; \
+ } }
-#define TEST_TRUE(x, fmt...) { TEST_MSG(fmt); \
- if(x) { TEST_OK(#x" is true.") } \
- else { TEST_FAIL(#x" is not true.") } }
+#define TEST_OK(m) { \
+ fprintf(stderr, " OK: "m"\n"); \
+ }
-#define TEST_FALSE(x, fmt...) { TEST_MSG(fmt); \
- if(!x) { TEST_OK(#x" is false.") } \
- else { TEST_FAIL(#x" is not false.") } }
+#define TEST_FAIL(m) { \
+ fprintf(stderr, " FAIL: "m"\t\t\t<------------\n"); \
+ TEST_num_fails++; \
+ }
-#define TEST_EQUAL(x, y, fmt...) { TEST_MSG(fmt); \
+#define TEST_FATAL(m) { \
+ fprintf(stderr, "FATAL: "m"\t\t\t<============\n"); \
+ TEST_num_fails++; \
+ { TEST_END; }
+
+#define TEST_MSG(fmt...) { \
+ fprintf(stderr, "\n"); \
+ fprintf(stderr, fmt); \
+ fprintf(stderr, " (line %d)\n", __LINE__); \
+ }
+
+#define TEST_BASE(fmt...) { \
+ TEST_num_tests++; \
+ TEST_MSG(fmt); \
+ }
+
+#define TEST_TRUE(x, fmt...) { \
+ TEST_BASE(fmt); \
+ if(x) { TEST_OK(#x" is true.") } \
+ else { TEST_FAIL(#x" is not true.") } \
+ }
+
+#define TEST_FALSE(x, fmt...) { \
+ TEST_BASE(fmt); \
+ if(!x) { TEST_OK(#x" is false.") } \
+ else { TEST_FAIL(#x" is not false.") } \
+ }
+
+#define TEST_EQUAL(x, y, fmt...) { \
+ TEST_BASE(fmt); \
if(x == y) { TEST_OK(#x" and "#y" are equal.") } \
- else { TEST_FAIL(#x" and "#y" are not equal.") } }
+ else { TEST_FAIL(#x" and "#y" are not equal.") } \
+ }
+
+#define TEST_NOTEQUAL(x, y, fmt...) { \
+ TEST_BASE(fmt); \
+ if(x != y) { TEST_OK(#x" and "#y" are not equal.") } \
+ else { TEST_FAIL(#x" and "#y" are equal.") } \
+ }
+
+#define TEST_EQUAL_STR(x, y, fmt...) { \
+ TEST_BASE(fmt); \
+ fprintf(stderr, "Comparing: \"%s\" == \"%s\"\n", \
+ std::string(x).c_str(), std::string(y).c_str()); \
+ if(x == y) { \
+ TEST_OK(#x" and "#y" are equal."); \
+ } else { \
+ TEST_FAIL(#x" and "#y" are not equal."); \
+ } \
+ }
+
+#define TEST_NOTEQUAL_STR(x, y, fmt...) { \
+ TEST_BASE(fmt); \
+ fprintf(stderr, "Comparing: \"%s\" != \"%s\"\n", \
+ std::string(x).c_str(), std::string(y).c_str()); \
+ if(x != y) { \
+ TEST_OK(#x" and "#y" are equal."); \
+ } else { \
+ TEST_FAIL(#x" and "#y" are not equal."); \
+ } \
+ }
+
+#define TEST_EQUAL_INT(x, y, fmt...) { \
+ TEST_BASE(fmt); \
+ fprintf(stderr, "Comparing: \"%d\" == \"%d\"\n", x, y); \
+ if(x == y) { \
+ TEST_OK(#x" and "#y" are equal."); \
+ } else { \
+ TEST_FAIL(#x" and "#y" are not equal."); \
+ } \
+ }
+
+#define TEST_NOTEQUAL_INT(x, y, fmt...) { \
+ TEST_BASE(fmt); \
+ fprintf(stderr, "Comparing: \"%d\" != \"%d\"\n", x, y); \
+ if(x != y) { \
+ TEST_OK(#x" and "#y" are equal."); \
+ } else { \
+ TEST_FAIL(#x" and "#y" are not equal."); \
+ } \
+ }
+
+#define TEST_EQUAL_FLOAT(x, y, fmt...) { \
+ TEST_BASE(fmt); \
+ fprintf(stderr, "Comparing: \"%f\" == \"%f\"\n", x, y); \
+ if(x == y) { \
+ TEST_OK(#x" and "#y" are equal."); \
+ } else { \
+ TEST_FAIL(#x" and "#y" are not equal."); \
+ } \
+ }
+
+#define TEST_NOTEQUAL_FLOAT(x, y, fmt...) { \
+ TEST_BASE(fmt); \
+ fprintf(stderr, "Comparing: \"%f\" != \"%f\"\n", x, y); \
+ if(x != y) { \
+ TEST_OK(#x" and "#y" are equal."); \
+ } else { \
+ TEST_FAIL(#x" and "#y" are not equal."); \
+ } \
+ }
+
+#define TEST_EXCEPTION(x, y, fmt...) { \
+ TEST_BASE(fmt); \
+ try { \
+ x; \
+ TEST_FAIL("Exception "#y" was not trown."); \
+ } catch( y &e ) { \
+ TEST_OK("Exception "#y" was thrown as expected."); \
+ } \
+ }
+
+#define TEST_NOTEXCEPTION(x, y, fmt...) { \
+ TEST_BASE(fmt); \
+ try { \
+ x; \
+ TEST_OK("Exception "#y" was not trown as expected"); \
+ } catch( y &e ) { \
+ TEST_FAIL("Exception "#y" was thrown."); \
+ } \
+ }
-#define TEST_NOTEQUAL(x, y, fmt...) { TEST_MSG(fmt); \
- if(x != y) { TEST_OK(#x" and "#y" are not equal.") } \
- else { TEST_FAIL(#x" and "#y" are equal.") } }
+#define TEST_NOEXCEPTION(x, fmt...) { \
+ TEST_BASE(fmt); \
+ try { \
+ x; \
+ TEST_OK("Exception was not trown as expected"); \
+ } catch( ... ) { \
+ TEST_FAIL("Exception was thrown."); \
+ } \
+ }
#endif/*__PRACRO_TEST_H__*/