Created
November 16, 2009 14:04
-
-
Save yukioc/236001 to your computer and use it in GitHub Desktop.
C unit testing framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __macrounit_h__ | |
#define __macrounit_h__ | |
#define mu_prn_failed(file,line,expr) printf( "%s:%u: failed assertion `%s'\n",file,line,expr) | |
#define mu_prn_test(test,passed) printf( " Test: %s ... %s\n",test,(passed)?"passed":"FAILED") | |
#define mu_prn_suite(suite) printf( "Suite: %s\n",suite) | |
#define mu_assert(file,line,expr) do { ++mu_asserts; if (!(expr)) { ++mu_failures; mu_prn_failed(file,line,#expr); }} while (0) | |
#define MU_ASSERT(expr) mu_assert(__FILE__,__LINE__,expr) | |
#define mu_run_test(test) do { int f=mu_failures; ++mu_tests; test(); mu_prn_test(#test,(f==mu_failures)); } while (0) | |
#define mu_run_suite(suite) do { ++mu_suites; mu_prn_suite(#suite); suite(); } while(0) | |
#define mu_show_failures() do { printf("### Failed %d of %d asserts (%d suites, %d tests).\n",mu_failures,mu_asserts,mu_suites,mu_tests); } while(0) | |
extern int mu_suites; | |
extern int mu_tests; | |
extern int mu_failures; | |
extern int mu_asserts; | |
#endif /* __macrounit_h__ */ | |
int mu_suites = 0; | |
int mu_tests = 0; | |
int mu_asserts = 0; | |
int mu_failures = 0; | |
static void test_foo() { | |
int foo = 7; | |
MU_ASSERT(foo == 7); | |
} | |
static void test_bar() { | |
int bar = 4; | |
MU_ASSERT(bar == 4); | |
MU_ASSERT(bar == 5); | |
} | |
static void suite_one() { | |
mu_run_test(test_foo); | |
mu_run_test(test_bar); | |
} | |
static void test_hoge() { | |
int hoge = 7; | |
MU_ASSERT(hoge == 7); | |
} | |
static void test_fuga() { | |
int fuga = 7; | |
MU_ASSERT(fuga == 7); | |
} | |
static void suite_two() { | |
mu_run_test(test_hoge); | |
mu_run_test(test_fuga); | |
} | |
int main(int argc, char **argv) { | |
mu_run_suite(suite_one); | |
mu_run_suite(suite_two); | |
mu_show_failures(); | |
return mu_failures != 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __munit_h__ | |
#define __munit_h__ | |
#include <stdio.h> | |
#define mu_failed(file,line,expr) printf( "%s:%u: failed assertion `%s'\n",file,line,expr) | |
#define mu_tested(test,passed) printf( "Test: %-10s ... %s\n",test,(passed)?"passed":"FAILED") | |
#define mu_assert(expr) do{mu_nassert++;if(!(expr)){++mu_nfail;mu_failed(__FILE__,__LINE__,#expr);}}while(0) | |
#define mu_run_test(test) do{int f=mu_nfail;++mu_ntest;test();mu_tested(#test,(f==mu_nfail));}while(0) | |
#define mu_show_failures() do{printf("### Failed %d of %d asserts (%d tests).\n",mu_nfail,mu_nassert,mu_ntest);}while(0) | |
extern int mu_nfail; | |
extern int mu_ntest; | |
extern int mu_nassert; | |
#endif /* __munit_h__ */ | |
int mu_nfail=0; | |
int mu_ntest=0; | |
int mu_nassert=0; | |
static void test_foo(){ | |
int foo=7; | |
mu_assert(foo==7); | |
mu_assert(foo==0||foo==7); | |
} | |
static void test_bar() { | |
int bar=4,fail=4; | |
mu_assert(bar==4); | |
mu_assert(fail==5); | |
} | |
static void test_hoge() { | |
int hoge=7; | |
mu_assert(hoge==7); | |
} | |
static void test_fuga() { | |
int fuga=7; | |
mu_assert(fuga==7); | |
} | |
int main(int argc, char **argv) { | |
mu_run_test(test_foo); | |
mu_run_test(test_bar); | |
mu_run_test(test_hoge); | |
mu_run_test(test_fuga); | |
mu_show_failures(); | |
return mu_nfail!=0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment