summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: d1abe95)
raw | patch | inline | side by side (parent: d1abe95)
author | Florian Forster <octo@collectd.org> | |
Tue, 28 May 2013 19:21:29 +0000 (21:21 +0200) | ||
committer | Florian Forster <octo@collectd.org> | |
Mon, 28 Jul 2014 13:06:35 +0000 (15:06 +0200) |
src/tests/common_test.c | patch | blob | history | |
src/tests/macros.h | [new file with mode: 0644] | patch | blob |
src/tests/utils_avltree_test.c | patch | blob | history | |
src/tests/utils_vl_lookup_test.c | patch | blob | history |
index a0d282cd58a81227a17eec4b31079f090e49efcc..eabba11df019de47ddbd142b8ed1430888cd7461 100644 (file)
--- a/src/tests/common_test.c
+++ b/src/tests/common_test.c
* Florian octo Forster <octo at collectd.org>
*/
+#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];
return (0);
}
-static int test_ssnprintf (void)
+DEF_TEST(ssnprintf)
{
char buffer[16] = "";
char *ptr = &buffer[4];
return (0);
}
-static int test_sstrdup (void)
+DEF_TEST(sstrdup)
{
char *ptr;
return (0);
}
-static int test_strsplit (void)
+DEF_TEST(strsplit)
{
char buffer[32];
char *fields[8];
return (0);
}
-int test_strjoin (void)
+DEF_TEST(strjoin)
{
char buffer[16];
char *fields[4];
return (0);
}
-static int test_strunescape ()
+DEF_TEST(strunescape)
{
char buffer[16];
int status;
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
--- /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 <octo at collectd.org>
+ */
+
+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)
index 260aa32832b2cf190ab719c56a452ca2656e98bc..0e0b988a71d8042ddaa1e3402a485cfbbbf95836 100644 (file)
* Florian octo Forster <octo at collectd.org>
*/
+#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)
return (strcmp (v0, v1));
}
-static int test_success ()
+DEF_TEST(success)
{
c_avl_tree_t *t;
char key_orig[] = "foo";
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 : */
index bbb3a67fbe24d640029bef0f02cd7823f7bc2eac..f958601f711423d503dbfa01ab02f10a88203218 100644 (file)
* Florian Forster <octo at collectd.org>
**/
+#include "tests/macros.h"
#include "collectd.h"
#include "utils_vl_lookup.h"
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));
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));
{
identifier_t ident;
void *user_class;
- int status;
memset (&ident, 0, sizeof (ident));
strncpy (ident.host, host, sizeof (ident.host));
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,
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 ();
/* expect new = */ 0);
lookup_destroy (obj);
+ return (0);
}
-static void testcase1 (void)
+DEF_TEST(group_by_any_host)
{
lookup_t *obj = checked_lookup_create ();
/* expect new = */ 0);
lookup_destroy (obj);
+ return (0);
}
-static void testcase2 (void)
+DEF_TEST(multiple_lookups)
{
lookup_t *obj = checked_lookup_create ();
int status;
assert (status == 2);
lookup_destroy (obj);
+ return (0);
}
-static void testcase3 (void)
+DEF_TEST(regex)
{
lookup_t *obj = checked_lookup_create ();
/* 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 */