Simple XUnit test library / objects in plain C Snapshot
vtest.h
Go to the documentation of this file.
00001 /* Copyright (c) Michael Moser (2011) . 3-clause BSD License applies */
00002 
00003 #ifndef _VTEST_H_
00004 #define _VTEST_H_
00005 
00006 #ifdef  __cplusplus
00007 extern "C" {
00008 #endif
00009 
00010 typedef void (*VTEST_ACTION) (void);
00011 
00012 typedef struct tagVTEST_TEST
00013 {
00014   const char    *name;
00015   VTEST_ACTION  function;
00016   int                   repeat; 
00017 } VTEST_TEST;
00018 
00019 typedef struct tagVTEST_TEST_SUITE
00020 {
00021   const char            *name;
00022   VTEST_ACTION          setUp,tearDown;
00023 
00024   struct tagVTEST_TEST_SUITE 
00025                                         *next_suite;
00026   VTEST_TEST            *test_cases; 
00027   
00028 } VTEST_TEST_SUITE;
00029 
00030 
00031 /**
00032  * Start declaration of a test suite that is the last one out of a chain of tests.
00033  * \param name name of test (not string)
00034  * \param setUp function is called before start of suite
00035  * \param tearDown function is called after completion of suite
00036  */
00037 #define VTEST_DEFINE_LAST_SUITE(name, setUp,tearDown) \
00038 extern VTEST_TEST_SUITE *get_vtest_##name () \
00039 { \
00040   VTEST_ACTION argSetUp = (VTEST_ACTION) (setUp), argTearDown = (VTEST_ACTION) (tearDown);\
00041   \
00042   VTEST_TEST_SUITE *next_suite = 0;\
00043   \
00044   const char *suitename = #name;\
00045   \
00046   static VTEST_TEST_SUITE test; \
00047   \
00048   static VTEST_TEST test_cases[] = { \
00049 
00050 
00051 /**
00052  * Start declaration of a test suite
00053  * \param name name of test (not string)
00054  * \param setUp function is called before start of suite
00055  * \param tearDown function is called after completion of suite
00056  * \param nextsuite the next test suite to follow after this one (not string)
00057  */
00058 #define VTEST_DEFINE_SUITE(name, setUp, tearDown, nextsuite) \
00059 \
00060 extern VTEST_TEST_SUITE *get_vtest_##nextsuite (); \
00061 \
00062 extern VTEST_TEST_SUITE *get_vtest_##name () \
00063 { \
00064   VTEST_ACTION argSetUp = (VTEST_ACTION) (setUp), argTearDown = (VTEST_ACTION) (tearDown);\
00065   \
00066   VTEST_TEST_SUITE *next_suite = get_vtest_##nextsuite ();\
00067   \
00068   const char *suitename = #name ;\
00069   \
00070   static VTEST_TEST_SUITE test; \
00071   \
00072   static VTEST_TEST test_cases[] = { \
00073 
00074 
00075 /**
00076  * declarations ends definition of a test suite
00077  */
00078 #define VTEST_END_SUITE \
00079 { 0, 0, 0 } \
00080 };\
00081   test.test_cases = (VTEST_TEST *) test_cases;\
00082   test.next_suite = next_suite;\
00083   test.setUp = argSetUp;\
00084   test.tearDown = argTearDown;\
00085   test.name = suitename;\
00086   \
00087   return &test;\
00088 }
00089 
00090 /**
00091  * define a test that is repeated for a number of times
00092  * \param tname  test name
00093  * \param test_fun test function of type VTEST_ACTION
00094  * \param repeat_count number of times that test is repeated
00095  */
00096 #define VTEST_TEST_REPEATED( tname, test_fun, repeat_count) \
00097 { (tname), (VTEST_ACTION) (test_fun), (int) (repeat_count) }, 
00098 
00099 /**
00100  * define a new test 
00101  * \param tname  test name
00102  * \param test_fun test function of type VTEST_ACTION
00103  */
00104 #define VTEST_TEST( tname, test_fun) \
00105 { (tname), (VTEST_ACTION) (test_fun), 1 }, 
00106 
00107 
00108 /**
00109  * define a new test 
00110  * \param test_fun - the function is also the name of the test
00111  */
00112 #define VTEST_TESTN( test_fun) \
00113 { #test_fun, (VTEST_ACTION) (test_fun), 1 }, 
00114 
00115 /**
00116  *  call to this function will signal test failure.
00117  */
00118 void VFAIL(const char *cond, const char *file,int line);
00119 
00120 /**
00121  *  Check the condition, failure of condtion will signal test failure, but test execution will continue.
00122  */
00123 #define VCHECK(cond) { if (!(cond)) { VFAIL( #cond, __FILE__,__LINE__); } while(0);
00124 
00125 
00126 /**
00127  *  Assert the condition, failure of condtion will signal test failure,and return from function (this assumes that curernt function is void).
00128  */
00129 #define VASSERT(cond) { if (!(cond)) { VFAIL( #cond, __FILE__,__LINE__); return; } } while(0);
00130 
00131 
00132 /**
00133  *  Assert the condition, failure of condtion will signal test failure, 
00134  *  and return with will be called (this assumes that curernt function is void).
00135  */
00136 #define VASSERT_RET(cond,ret) { if (!(cond)) { VFAIL( #cond, __FILE__,__LINE__); return (ret); } } while(0);
00137 
00138 
00139 /**
00140  * calls function that creates forward declaration for the test suite structure accessor.
00141  */
00142 #define VTEST_SUITE_DECLARE_GET(name) \
00143 extern VTEST_TEST_SUITE *get_vtest_##name ();
00144 
00145 
00146 /**
00147  * calls function that returns the test suite structure for a given test.
00148  */
00149 #define VTEST_SUITE_GET(name) \
00150   (VTEST_TEST_SUITE *)get_vtest_##name ()
00151 
00152 #ifdef  __cplusplus
00153 }
00154 #endif
00155 
00156 #endif
00157