From c9e19f0f303db3d15f2dcb1185417a4200a29573 Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 15 Jan 2010 09:21:11 +0000 Subject: Added more tests. --- server/src/entitylist.cc | 4 ++ tools/test.h | 167 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 153 insertions(+), 18 deletions(-) diff --git a/server/src/entitylist.cc b/server/src/entitylist.cc index 141345d..6964df1 100644 --- a/server/src/entitylist.cc +++ b/server/src/entitylist.cc @@ -294,8 +294,12 @@ TEST_EQUAL_STR(lst.getLatestVersion("test"), _DIR"/file1.xml", "Test"); unlink(_DIR"/file1.xml"); +TEST_EXCEPTION(lst.getLatestVersion("test"), Exception, "Test lookup of missing macro."); + rmdir(_DIR); +TEST_EXCEPTION(lst.getLatestVersion("test"), Exception, "Test lookup in missing folder."); + TEST_END; #endif/*TEST_ENTITYLIST*/ 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 -#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__*/ -- cgit v1.2.3