260aa32832b2cf190ab719c56a452ca2656e98bc
[collectd.git] / src / tests / utils_avltree_test.c
1 /**
2  * collectd - src/utils_avltree_test.c
3  *
4  * Copyright (C) 2013       Florian octo Forster
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  *
24  * Authors:
25  *   Florian octo Forster <octo at collectd.org>
26  */
27
28 #include "collectd.h"
29 #include "utils_avltree.h"
30
31 static int fail_count = 0;
32 static int check_count = 0;
33
34 #define TEST(func) do { \
35   int status; \
36   printf ("Testing %s ...\n", #func); \
37   status = test_ ## func (); \
38   printf ("%s.\n", (status == 0) ? "Success" : "FAILURE"); \
39   if (status != 0) { fail_count++; } \
40 } while (0);
41
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); } \
46 } while (0);
47
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); \
52     return (-1); \
53   } \
54   printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count, #actual, expect); \
55 } while (0)
56
57 #define OK(cond) OK1(cond, #cond)
58
59 static int compare_total_count = 0;
60 #define RESET_COUNTS() do { compare_total_count = 0; } while (0)
61
62 static int compare_callback (void const *v0, void const *v1)
63 {
64   assert (v0 != NULL);
65   assert (v1 != NULL);
66
67   compare_total_count++;
68   return (strcmp (v0, v1));
69 }
70
71 static int test_success ()
72 {
73   c_avl_tree_t *t;
74   char key_orig[] = "foo";
75   char value_orig[] = "bar";
76   char *key_ret = NULL;
77   char *value_ret = NULL;
78
79   RESET_COUNTS ();
80   t = c_avl_create (compare_callback);
81   OK (t != NULL);
82
83   OK (c_avl_insert (t, key_orig, value_orig) == 0);
84   OK (c_avl_size (t) == 1);
85
86   /* Key already exists. */
87   OK (c_avl_insert (t, "foo", "qux") > 0);
88
89   OK (c_avl_get (t, "foo", (void *) &value_ret) == 0);
90   OK (value_ret == &value_orig[0]);
91
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);
97
98   c_avl_destroy (t);
99
100   return (0);
101 }
102
103 int main (void)
104 {
105   TEST(success);
106
107   if (fail_count != 0)
108     exit (EXIT_FAILURE);
109   exit (EXIT_SUCCESS);
110 }
111
112 /* vim: set sw=2 sts=2 et : */