0e0b988a71d8042ddaa1e3402a485cfbbbf95836
[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 "tests/macros.h"
29 #include "collectd.h"
30 #include "utils_avltree.h"
31
32 static int compare_total_count = 0;
33 #define RESET_COUNTS() do { compare_total_count = 0; } while (0)
34
35 static int compare_callback (void const *v0, void const *v1)
36 {
37   assert (v0 != NULL);
38   assert (v1 != NULL);
39
40   compare_total_count++;
41   return (strcmp (v0, v1));
42 }
43
44 DEF_TEST(success)
45 {
46   c_avl_tree_t *t;
47   char key_orig[] = "foo";
48   char value_orig[] = "bar";
49   char *key_ret = NULL;
50   char *value_ret = NULL;
51
52   RESET_COUNTS ();
53   t = c_avl_create (compare_callback);
54   OK (t != NULL);
55
56   OK (c_avl_insert (t, key_orig, value_orig) == 0);
57   OK (c_avl_size (t) == 1);
58
59   /* Key already exists. */
60   OK (c_avl_insert (t, "foo", "qux") > 0);
61
62   OK (c_avl_get (t, "foo", (void *) &value_ret) == 0);
63   OK (value_ret == &value_orig[0]);
64
65   key_ret = value_ret = NULL;
66   OK (c_avl_remove (t, "foo", (void *) &key_ret, (void *) &value_ret) == 0);
67   OK (key_ret == &key_orig[0]);
68   OK (value_ret == &value_orig[0]);
69   OK (c_avl_size (t) == 0);
70
71   c_avl_destroy (t);
72
73   return (0);
74 }
75
76 int main (void)
77 {
78   RUN_TEST(success);
79
80   END_TEST;
81 }
82
83 /* vim: set sw=2 sts=2 et : */