{GPL, other}: Relicense to MIT license.
[collectd.git] / src / tests / test_utils_avltree.c
1 /**
2  * collectd - src/tests/test_utils_avltree.c
3  * Copyright (C) 2013       Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  */
26
27 #include "tests/macros.h"
28 #include "collectd.h"
29 #include "utils_avltree.h"
30
31 static int compare_total_count = 0;
32 #define RESET_COUNTS() do { compare_total_count = 0; } while (0)
33
34 static int compare_callback (void const *v0, void const *v1)
35 {
36   assert (v0 != NULL);
37   assert (v1 != NULL);
38
39   compare_total_count++;
40   return (strcmp (v0, v1));
41 }
42
43 DEF_TEST(success)
44 {
45   c_avl_tree_t *t;
46   char key_orig[] = "foo";
47   char value_orig[] = "bar";
48   char *key_ret = NULL;
49   char *value_ret = NULL;
50
51   RESET_COUNTS ();
52   t = c_avl_create (compare_callback);
53   OK (t != NULL);
54
55   OK (c_avl_insert (t, key_orig, value_orig) == 0);
56   OK (c_avl_size (t) == 1);
57
58   /* Key already exists. */
59   OK (c_avl_insert (t, "foo", "qux") > 0);
60
61   OK (c_avl_get (t, "foo", (void *) &value_ret) == 0);
62   OK (value_ret == &value_orig[0]);
63
64   key_ret = value_ret = NULL;
65   OK (c_avl_remove (t, "foo", (void *) &key_ret, (void *) &value_ret) == 0);
66   OK (key_ret == &key_orig[0]);
67   OK (value_ret == &value_orig[0]);
68   OK (c_avl_size (t) == 0);
69
70   c_avl_destroy (t);
71
72   return (0);
73 }
74
75 int main (void)
76 {
77   RUN_TEST(success);
78
79   END_TEST;
80 }
81
82 /* vim: set sw=2 sts=2 et : */