2 * collectd - src/utils_avltree_test.c
4 * Copyright (C) 2013 Florian octo Forster
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Florian octo Forster <octo at collectd.org>
29 #include "utils_avltree.h"
31 static int fail_count = 0;
32 static int check_count = 0;
34 #define TEST(func) do { \
36 printf ("Testing %s ...\n", #func); \
37 status = test_ ## func (); \
38 printf ("%s.\n", (status == 0) ? "Success" : "FAILURE"); \
39 if (status != 0) { fail_count++; } \
42 #define OK1(cond, text) do { \
43 _Bool result = (cond); \
44 printf ("%s %i - %s\n", result ? "ok" : "not ok", ++check_count, text); \
45 if (!result) { return (-1); } \
48 #define STREQ(expect, actual) do { \
49 if (strcmp (expect, actual) != 0) { \
50 printf ("not ok %i - %s incorrect: expected \"%s\", got \"%s\"\n", \
51 ++check_count, #actual, expect, actual); \
54 printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count, #actual, expect); \
57 #define OK(cond) OK1(cond, #cond)
59 static int compare_total_count = 0;
60 #define RESET_COUNTS() do { compare_total_count = 0; } while (0)
62 static int compare_callback (void const *v0, void const *v1)
67 compare_total_count++;
68 return (strcmp (v0, v1));
71 static int test_success ()
74 char key_orig[] = "foo";
75 char value_orig[] = "bar";
77 char *value_ret = NULL;
80 t = c_avl_create (compare_callback);
83 OK (c_avl_insert (t, key_orig, value_orig) == 0);
84 OK (c_avl_size (t) == 1);
86 /* Key already exists. */
87 OK (c_avl_insert (t, "foo", "qux") > 0);
89 OK (c_avl_get (t, "foo", (void *) &value_ret) == 0);
90 OK (value_ret == &value_orig[0]);
92 key_ret = value_ret = NULL;
93 OK (c_avl_remove (t, "foo", (void *) &key_ret, (void *) &value_ret) == 0);
94 OK (key_ret == &key_orig[0]);
95 OK (value_ret == &value_orig[0]);
96 OK (c_avl_size (t) == 0);
112 /* vim: set sw=2 sts=2 et : */