From f16b82f28f1d2115de766375f4ef2e668fcd82ec Mon Sep 17 00:00:00 2001 From: mquinson Date: Tue, 10 Jun 2008 15:40:58 +0000 Subject: [PATCH 1/1] Make cunit memory clean git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5601 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- include/xbt/cunit.h | 2 ++ src/xbt/cunit.c | 38 ++++++++++++++++++++++++++------------ tools/sg_unit_extractor.pl | 1 + 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/include/xbt/cunit.h b/include/xbt/cunit.h index 6a6e8e6229..ec4c43ab94 100644 --- a/include/xbt/cunit.h +++ b/include/xbt/cunit.h @@ -52,6 +52,8 @@ XBT_PUBLIC(void) xbt_test_suite_push (xbt_test_suite_t suite, const XBT_PUBLIC(int) xbt_test_run(char *selection); /* Show information about the selection of tests */ XBT_PUBLIC(void) xbt_test_dump(char *selection); +/* Cleanup the mess */ +XBT_PUBLIC(void) xbt_test_exit(void); /* test operations */ XBT_PUBLIC(void) _xbt_test_add(const char*file,int line, const char *fmt, ...)_XBT_GNUC_PRINTF(3,4); diff --git a/src/xbt/cunit.c b/src/xbt/cunit.c index ab53a53787..ac0ca354a8 100644 --- a/src/xbt/cunit.c +++ b/src/xbt/cunit.c @@ -50,13 +50,6 @@ static void xbt_test_log_dump(xbt_test_log_t log) { else fprintf(stderr," log=NULL\n"); } -static void xbt_test_log_free(xbt_test_log_t log) { - if (!log) - return; - if (log->text) - free(log->text); - free(log); -} /* test suite test check */ typedef struct s_xbt_test_test { @@ -135,6 +128,25 @@ static void xbt_test_suite_free(void *s) { free(suite); } +static void xbt_test_unit_free(void *unit) { + xbt_test_unit_t u = *(xbt_test_unit_t*)unit; + /* name is static */ + free(u->title); + xbt_dynar_free(&u->tests); + free(u); +} +static void xbt_test_test_free(void *test) { + xbt_test_test_t t = *(xbt_test_test_t*)test; + free(t->title); + xbt_dynar_free(&(t->logs)); + free(t); +} +static void xbt_test_log_free(void *log) { + xbt_test_log_t l= *(xbt_test_log_t*) log; + free(l->text); + free(l); +} + /** @brief create test suite */ xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...) { xbt_test_suite_t suite = xbt_new0(struct s_xbt_test_suite,1); @@ -145,7 +157,7 @@ xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...) { va_start(ap, fmt); vasprintf(&suite->title,fmt, ap); - suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), NULL); + suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free); va_end(ap); suite->name = name; suite->enabled = 1; @@ -210,7 +222,7 @@ void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t unit->file = NULL; unit->line = 0; unit->enabled = 1; - unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), NULL); + unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free); xbt_dynar_push(suite->units, &unit); return; @@ -623,7 +635,9 @@ int xbt_test_run(char *selection) { } return _xbt_test_unit_failed; } - +void xbt_test_exit(void) { + xbt_dynar_free(&_xbt_test_suites); +} /* annotate test case with test */ void _xbt_test_add(const char*file,int line, const char *fmt, ...) { @@ -634,7 +648,7 @@ void _xbt_test_add(const char*file,int line, const char *fmt, ...) { xbt_assert(unit); xbt_assert(fmt); - test = xbt_new(struct s_xbt_test_test,1); + test = xbt_new0(struct s_xbt_test_test,1); va_start(ap, fmt); vasprintf(&test->title, fmt, ap); va_end(ap); @@ -643,7 +657,7 @@ void _xbt_test_add(const char*file,int line, const char *fmt, ...) { test->ignored = 0; test->file = file; test->line = line; - test->logs = xbt_dynar_new(sizeof(xbt_test_log_t),NULL); + test->logs = xbt_dynar_new(sizeof(xbt_test_log_t),xbt_test_log_free); xbt_dynar_push(unit->tests,&test); return; } diff --git a/tools/sg_unit_extractor.pl b/tools/sg_unit_extractor.pl index 30686a70d1..9ef69f986c 100755 --- a/tools/sg_unit_extractor.pl +++ b/tools/sg_unit_extractor.pl @@ -148,6 +148,7 @@ int main(int argc, char *argv[]) { /* Got all my tests to do */ res = xbt_test_run(selection); + xbt_test_exit(); xbt_exit(); return res; } -- 2.20.1