From: Florian Forster Date: Tue, 28 May 2013 19:21:29 +0000 (+0200) Subject: Tests: Make the self-rolled test framework a bit more useful and consistent. X-Git-Tag: collectd-5.5.0~273 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=797727e080fa8d932864b6e677fd0c56e9cd73b2;p=collectd.git Tests: Make the self-rolled test framework a bit more useful and consistent. --- diff --git a/src/tests/common_test.c b/src/tests/common_test.c index a0d282cd..eabba11d 100644 --- a/src/tests/common_test.c +++ b/src/tests/common_test.c @@ -25,37 +25,10 @@ * Florian octo Forster */ +#include "tests/macros.h" #include "common.h" -static int fail_count = 0; -static int check_count = 0; - -#define TEST(func) do { \ - int status; \ - printf ("Testing %s ...\n", #func); \ - status = test_ ## func (); \ - printf ("%s.\n", (status == 0) ? "Success" : "FAILURE"); \ - if (status != 0) { fail_count++; } \ -} while (0); - -#define OK1(cond, text) do { \ - _Bool result = (cond); \ - printf ("%s %i - %s\n", result ? "ok" : "not ok", ++check_count, text); \ - if (!result) { return (-1); } \ -} while (0); - -#define STREQ(expect, actual) do { \ - if (strcmp (expect, actual) != 0) { \ - printf ("not ok %i - %s incorrect: expected \"%s\", got \"%s\"\n", \ - ++check_count, #actual, expect, actual); \ - return (-1); \ - } \ - printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count, #actual, expect); \ -} while (0) - -#define OK(cond) OK1(cond, #cond) - -static int test_sstrncpy (void) +DEF_TEST(sstrncpy) { char buffer[16] = ""; char *ptr = &buffer[4]; @@ -83,7 +56,7 @@ static int test_sstrncpy (void) return (0); } -static int test_ssnprintf (void) +DEF_TEST(ssnprintf) { char buffer[16] = ""; char *ptr = &buffer[4]; @@ -105,7 +78,7 @@ static int test_ssnprintf (void) return (0); } -static int test_sstrdup (void) +DEF_TEST(sstrdup) { char *ptr; @@ -122,7 +95,7 @@ static int test_sstrdup (void) return (0); } -static int test_strsplit (void) +DEF_TEST(strsplit) { char buffer[32]; char *fields[8]; @@ -173,7 +146,7 @@ static int test_strsplit (void) return (0); } -int test_strjoin (void) +DEF_TEST(strjoin) { char buffer[16]; char *fields[4]; @@ -217,7 +190,7 @@ int test_strjoin (void) return (0); } -static int test_strunescape () +DEF_TEST(strunescape) { char buffer[16]; int status; @@ -261,16 +234,14 @@ static int test_strunescape () int main (void) { - TEST(sstrncpy); - TEST(ssnprintf); - TEST(sstrdup); - TEST(strsplit); - TEST(strjoin); - TEST(strunescape); - - if (fail_count != 0) - exit (EXIT_FAILURE); - exit (EXIT_SUCCESS); + RUN_TEST(sstrncpy); + RUN_TEST(ssnprintf); + RUN_TEST(sstrdup); + RUN_TEST(strsplit); + RUN_TEST(strjoin); + RUN_TEST(strunescape); + + END_TEST; } /* vim: set sw=2 sts=2 et : */ diff --git a/src/tests/macros.h b/src/tests/macros.h new file mode 100644 index 00000000..e454b693 --- /dev/null +++ b/src/tests/macros.h @@ -0,0 +1,68 @@ +/** + * collectd - src/tests/macros.h + * + * Copyright (C) 2013 Florian octo Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + */ + +static int fail_count__ = 0; +static int check_count__ = 0; + +#define DEF_TEST(func) static int test_##func () + +#define RUN_TEST(func) do { \ + int status; \ + printf ("Testing %s ...\n", #func); \ + status = test_ ## func (); \ + printf ("%s.\n", (status == 0) ? "Success" : "FAILURE"); \ + if (status != 0) { fail_count__++; } \ +} while (0) + +#define END_TEST exit ((fail_count__ == 0) ? 0 : 1); + +#define OK1(cond, text) do { \ + _Bool result = (cond); \ + printf ("%s %i - %s\n", result ? "ok" : "not ok", ++check_count__, text); \ +} while (0) +#define OK(cond) OK1(cond, #cond) + +#define STREQ(expect, actual) do { \ + if (strcmp (expect, actual) != 0) { \ + printf ("not ok %i - %s incorrect: expected \"%s\", got \"%s\"\n", \ + ++check_count__, #actual, expect, actual); \ + return (-1); \ + } \ + printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count__, #actual, expect); \ +} while (0) + +#define CHECK_NOT_NULL(expr) do { \ + void *ptr_; \ + ptr_ = (expr); \ + OK1(ptr_ != NULL, #expr); \ +} while (0) + +#define CHECK_ZERO(expr) do { \ + long status_; \ + status_ = (long) (expr); \ + OK1(status_ == 0L, #expr); \ +} while (0) diff --git a/src/tests/utils_avltree_test.c b/src/tests/utils_avltree_test.c index 260aa328..0e0b988a 100644 --- a/src/tests/utils_avltree_test.c +++ b/src/tests/utils_avltree_test.c @@ -25,37 +25,10 @@ * Florian octo Forster */ +#include "tests/macros.h" #include "collectd.h" #include "utils_avltree.h" -static int fail_count = 0; -static int check_count = 0; - -#define TEST(func) do { \ - int status; \ - printf ("Testing %s ...\n", #func); \ - status = test_ ## func (); \ - printf ("%s.\n", (status == 0) ? "Success" : "FAILURE"); \ - if (status != 0) { fail_count++; } \ -} while (0); - -#define OK1(cond, text) do { \ - _Bool result = (cond); \ - printf ("%s %i - %s\n", result ? "ok" : "not ok", ++check_count, text); \ - if (!result) { return (-1); } \ -} while (0); - -#define STREQ(expect, actual) do { \ - if (strcmp (expect, actual) != 0) { \ - printf ("not ok %i - %s incorrect: expected \"%s\", got \"%s\"\n", \ - ++check_count, #actual, expect, actual); \ - return (-1); \ - } \ - printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count, #actual, expect); \ -} while (0) - -#define OK(cond) OK1(cond, #cond) - static int compare_total_count = 0; #define RESET_COUNTS() do { compare_total_count = 0; } while (0) @@ -68,7 +41,7 @@ static int compare_callback (void const *v0, void const *v1) return (strcmp (v0, v1)); } -static int test_success () +DEF_TEST(success) { c_avl_tree_t *t; char key_orig[] = "foo"; @@ -102,11 +75,9 @@ static int test_success () int main (void) { - TEST(success); + RUN_TEST(success); - if (fail_count != 0) - exit (EXIT_FAILURE); - exit (EXIT_SUCCESS); + END_TEST; } /* vim: set sw=2 sts=2 et : */ diff --git a/src/tests/utils_vl_lookup_test.c b/src/tests/utils_vl_lookup_test.c index bbb3a67f..f958601f 100644 --- a/src/tests/utils_vl_lookup_test.c +++ b/src/tests/utils_vl_lookup_test.c @@ -24,6 +24,7 @@ * Florian Forster **/ +#include "tests/macros.h" #include "collectd.h" #include "utils_vl_lookup.h" @@ -46,7 +47,8 @@ static int lookup_obj_callback (data_set_t const *ds, identifier_t *class = user_class; identifier_t *obj = user_obj; - assert (expect_new_obj == have_new_obj); + OK1(expect_new_obj == have_new_obj, + (expect_new_obj ? "New obj is created." : "Updating existing obj.")); memcpy (&last_class_ident, class, sizeof (last_class_ident)); memcpy (&last_obj_ident, obj, sizeof (last_obj_ident)); @@ -63,7 +65,7 @@ static void *lookup_class_callback (data_set_t const *ds, identifier_t *class = user_class; identifier_t *obj; - assert (expect_new_obj); + OK(expect_new_obj); memcpy (&last_class_ident, class, sizeof (last_class_ident)); @@ -87,7 +89,6 @@ static void checked_lookup_add (lookup_t *obj, /* {{{ */ { identifier_t ident; void *user_class; - int status; memset (&ident, 0, sizeof (ident)); strncpy (ident.host, host, sizeof (ident.host)); @@ -99,8 +100,7 @@ static void checked_lookup_add (lookup_t *obj, /* {{{ */ user_class = malloc (sizeof (ident)); memmove (user_class, &ident, sizeof (ident)); - status = lookup_add (obj, &ident, group_by, user_class); - assert (status == 0); + OK(lookup_add (obj, &ident, group_by, user_class) == 0); } /* }}} void test_add */ static int checked_lookup_search (lookup_t *obj, @@ -136,11 +136,11 @@ static lookup_t *checked_lookup_create (void) lookup_obj_callback, (void *) free, (void *) free); - assert (obj != NULL); + OK(obj != NULL); return (obj); } -static void testcase0 (void) +DEF_TEST(group_by_specific_host) { lookup_t *obj = checked_lookup_create (); @@ -155,9 +155,10 @@ static void testcase0 (void) /* expect new = */ 0); lookup_destroy (obj); + return (0); } -static void testcase1 (void) +DEF_TEST(group_by_any_host) { lookup_t *obj = checked_lookup_create (); @@ -180,9 +181,10 @@ static void testcase1 (void) /* expect new = */ 0); lookup_destroy (obj); + return (0); } -static void testcase2 (void) +DEF_TEST(multiple_lookups) { lookup_t *obj = checked_lookup_create (); int status; @@ -204,9 +206,10 @@ static void testcase2 (void) assert (status == 2); lookup_destroy (obj); + return (0); } -static void testcase3 (void) +DEF_TEST(regex) { lookup_t *obj = checked_lookup_create (); @@ -232,13 +235,15 @@ static void testcase3 (void) /* expect new = */ 1); lookup_destroy (obj); + return (0); } int main (int argc, char **argv) /* {{{ */ { - testcase0 (); - testcase1 (); - testcase2 (); - testcase3 (); - return (EXIT_SUCCESS); + RUN_TEST(group_by_specific_host); + RUN_TEST(group_by_any_host); + RUN_TEST(multiple_lookups); + RUN_TEST(regex); + + END_TEST; } /* }}} int main */