From: Leandro Lucarella Date: Fri, 12 Dec 2008 12:43:10 +0000 (-0200) Subject: Unify C and Python implementations header file X-Git-Tag: 1.0~8 X-Git-Url: https://git.llucax.com/software/mutest.git/commitdiff_plain/a894239e9a62676c861bdd817714b5f8b3c05265?ds=sidebyside;hp=d5be024b1cc1a456cd9726d85a7e108e1818ae7f Unify C and Python implementations header file Make both implementations use the same mutest.h. When using the Python implementation the macro MUTEST_PY should be set. --- diff --git a/py/mutest b/mutest similarity index 97% rename from py/mutest rename to mutest index b9a4042..68d6acc 100755 --- a/py/mutest +++ b/mutest @@ -80,19 +80,19 @@ class TestCase(object): 'mutest_set_verbose_level', argtype=[c_int]) @property - def passed_count(self): - return get_val(self.so, 'mutest_passed_count') + def passed_checks(self): + return get_val(self.so, 'mutest_passed_checks') @property - def failed_count(self): - return get_val(self.so, 'mutest_failed_count') + def failed_checks(self): + return get_val(self.so, 'mutest_failed_checks') def run(self): global verbose_level self.set_verbose_level(verbose_level) self.reset_counters() self.testcase() - return (self.passed_count, self.failed_count) + return (self.passed_checks, self.failed_checks) class TestSuiteInfo (object): diff --git a/mutest.h b/mutest.h index 159625b..c84eb15 100644 --- a/mutest.h +++ b/mutest.h @@ -6,8 +6,10 @@ * http://blitiri.com.ar/p/bola/ * * This header file should be included in the source files that will make up - * a test suite. If you implement your mu_run_suites() function yourself, you - * probably will need to include this header too (see mkmutest). + * a test suite. It's used for both C and Python implementation, but when + * using the Python implementation you should define the MUTEST_PY macro. + * If you implement your mu_run_suites() function yourself, you probably will + * need to include this header too (see mkmutest). * * Please, read the README file for more details. */ @@ -18,6 +20,27 @@ extern "C" { #endif +/* verbosity level (each level shows all the previous levels too) */ +enum { + MU_QUIET = 0, /* be completely quiet */ + MU_ERROR, /* shows errors only */ + MU_SUMMARY, /* shows a summary */ + MU_SUITE, /* shows test suites progress */ + MU_CASE, /* shows test cases progress */ + MU_CHECK /* shows the current running check */ +}; + +/* print a message according to the verbosity level */ +#define mu_print(level, ...) \ + do { \ + if (mutest_verbose_level >= level) { \ + if (mutest_verbose_level == MU_ERROR) \ + fprintf(stderr, __VA_ARGS__); \ + else \ + fprintf(stdout, __VA_ARGS__); \ + } \ + } while (0) + /* check that an expression evaluates to true, continue if the check fails */ #define mu_check(exp) \ do { \ @@ -50,32 +73,7 @@ extern "C" { } \ } while (0) -/* - * you don't need to pay any attention to what's next, unless you want to do - * some customization, of course, in which case, you're encouraged to take - * a look an play =) - */ - -/* verbosity level (each level shows all the previous levels too) */ -enum { - MU_QUIET = 0, /* be completely quiet */ - MU_ERROR, /* shows errors only */ - MU_SUMMARY, /* shows a summary */ - MU_SUITE, /* shows test suites progress */ - MU_CASE, /* shows test cases progress */ - MU_CHECK /* shows the current running check */ -}; - -/* print a message according to the verbosity level */ -#define mu_print(level, ...) \ - do { \ - if (mutest_verbose_level >= level) { \ - if (mutest_verbose_level == MU_ERROR) \ - fprintf(stderr, __VA_ARGS__); \ - else \ - fprintf(stdout, __VA_ARGS__); \ - } \ - } while (0) +#ifndef MUTEST_PY /* we are using the C implementation */ /* * this function implements the test suites execution, you should generate @@ -149,6 +147,27 @@ extern int mutest_passed_checks; /* verbosity */ extern int mutest_verbose_level; +#else /* MUTEST_PY is defined, using the Python implementation */ + +/* this increments when the "API" changes, it's just for sanity check */ +int mutest_api_version = 1; + +int mutest_case_failed; /* unused, for C implementation compatibility */ + +int mutest_passed_checks; +int mutest_failed_checks; +void mutest_reset_counters() { + mutest_passed_checks = 0; + mutest_failed_checks = 0; +} + +int mutest_verbose_level = MU_ERROR; +void mutest_set_verbose_level(int val) { + mutest_verbose_level = val; +} + +#endif /* MUTEST_PY */ + #ifdef __cplusplus } #endif diff --git a/py/mutest.h b/py/mutest.h deleted file mode 100644 index 8c42ca8..0000000 --- a/py/mutest.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * This file is part of mutest, a simple micro unit testing framework for C. - * - * mutest was written by Leandro Lucarella and is released - * under the BOLA license, please see the LICENSE file or visit: - * http://blitiri.com.ar/p/bola/ - * - * This is a Python implementation of the mutest main idea. This header file - * should be included in the source files that will make up a test suite, as - * a dynamic shared object (.so file). - * - * Please, read the README file for more details. - */ - -#include /* printf(), fprintf() */ - -#ifdef __cplusplus -extern "C" { -#endif - -/* this increments when the "API" changes */ -int mutest_api_version = 1; - -int mutest_passed_count; -int mutest_failed_count; -void mutest_reset_counters() { - mutest_passed_count = 0; - mutest_failed_count = 0; -} - -/* - * Verbose level: - * 0: quiet - * 1: errors - * 2: summary - * 3: suites - * 4: cases - * 5: checks - */ -int mutest_verbose_level = 1; -void mutest_set_verbose_level(int val) { - mutest_verbose_level = val; -} - -#define mu_check(exp) \ - do { \ - if (exp) { \ - ++mutest_passed_count; \ - if (mutest_verbose_level >= 5) \ - printf("%s:%d: mu_check(%s) passed\n", \ - __FILE__, __LINE__, #exp); \ - } else { \ - ++mutest_failed_count; \ - if (mutest_verbose_level) \ - fprintf(stderr, "%s:%d: mu_check(%s) " \ - "failed, resuming test case\n", \ - __FILE__, __LINE__, #exp); \ - } \ - } while (0) - -#define mu_ensure(exp) \ - do { \ - if (exp) { \ - ++mutest_passed_count; \ - if (mutest_verbose_level >=5) \ - printf("%s:%d: mu_ensure(%s) passed\n", \ - __FILE__, __LINE__, #exp); \ - } else { \ - ++mutest_failed_count; \ - if (mutest_verbose_level) \ - fprintf(stderr, "%s:%d: mu_ensure(%s) " \ - "failed, aborting test case\n", \ - __FILE__, __LINE__, #exp); \ - return; \ - } \ - } while (0) - -#ifdef __cplusplus -} -#endif - diff --git a/sample/Makefile b/sample/Makefile index 3c1588c..19bf4c3 100644 --- a/sample/Makefile +++ b/sample/Makefile @@ -18,7 +18,7 @@ CFLAGS = -Wall -std=c89 TARGET = tester RUNNER_SRC = test_suite_runner.c MKMUTEST = ../mkmutest -MUTEST = ../py/mutest +MUTEST = ../mutest MUTEST_H = ../mutest.h MUTEST_C = ../mutest.c diff --git a/sample/factorial_test.c b/sample/factorial_test.c index 8dc56a8..5e8c7f2 100644 --- a/sample/factorial_test.c +++ b/sample/factorial_test.c @@ -13,11 +13,7 @@ #include "factorial.h" -#ifdef MUTEST_PY -#include "../py/mutest.h" -#else #include "../mutest.h" -#endif void mu_test_factorial_zero() { unsigned x = factorial(0); diff --git a/sample/init_fail_test.c b/sample/init_fail_test.c index b3c7475..8fe74e3 100644 --- a/sample/init_fail_test.c +++ b/sample/init_fail_test.c @@ -14,11 +14,7 @@ * Please, read the README file for more details. */ -#ifdef MUTEST_PY -#include "../py/mutest.h" -#else #include "../mutest.h" -#endif static int ret = 0; diff --git a/sample/sum_test.c b/sample/sum_test.c index 505f814..e4fd532 100644 --- a/sample/sum_test.c +++ b/sample/sum_test.c @@ -19,11 +19,7 @@ #include "sum.h" #include /* malloc(), free() */ -#ifdef MUTEST_PY -#include "../py/mutest.h" -#else #include "../mutest.h" -#endif /* unused, just for ilustrate the test suite initialization/termination */ static char* global;