150dd1b5b8a8476c98a6fefdc01e9c5761ca02b7
[collectd.git] / src / daemon / utils_avltree_test.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 "common.h" /* STATIC_ARRAY_SIZE */
28 #include "collectd.h"
29
30 #include "testing.h"
31 #include "utils_avltree.h"
32
33 static int compare_total_count = 0;
34 #define RESET_COUNTS() do { compare_total_count = 0; } while (0)
35
36 static int compare_callback (void const *v0, void const *v1)
37 {
38   assert (v0 != NULL);
39   assert (v1 != NULL);
40
41   compare_total_count++;
42   return (strcmp (v0, v1));
43 }
44
45 DEF_TEST(success)
46 {
47   struct {
48     char *key;
49     char *value;
50   } cases[] = {
51     {"Eeph7chu", "vai1reiV"},
52     {"igh3Paiz", "teegh1Ee"},
53     {"caip6Uu8", "ooteQu8n"},
54     {"Aech6vah", "AijeeT0l"},
55     {"Xah0et2L", "gah8Taep"},
56     {"BocaeB8n", "oGaig8io"},
57     {"thai8AhM", "ohjeFo3f"},
58     {"ohth6ieC", "hoo8ieWo"},
59     {"aej7Woow", "phahuC2s"},
60     {"Hai8ier2", "Yie6eimi"},
61     {"phuXi3Li", "JaiF7ieb"},
62     {"Shaig5ef", "aihi5Zai"},
63     {"voh6Aith", "Oozaeto0"},
64     {"zaiP5kie", "seep5veM"},
65     {"pae7ba7D", "chie8Ojo"},
66     {"Gou2ril3", "ouVoo0ha"},
67     {"lo3Thee3", "ahDu4Zuj"},
68     {"Rah8kohv", "ieShoc7E"},
69     {"ieN5engi", "Aevou1ah"},
70     {"ooTe4OhP", "aingai5Y"},
71   };
72
73   c_avl_tree_t *t;
74
75   RESET_COUNTS ();
76   CHECK_NOT_NULL (t = c_avl_create (compare_callback));
77
78   /* insert */
79   for (size_t i = 0; i < STATIC_ARRAY_SIZE (cases); i++)
80   {
81     char *key;
82     char *value;
83
84     CHECK_NOT_NULL (key = strdup (cases[i].key));
85     CHECK_NOT_NULL (value = strdup (cases[i].value));
86
87     CHECK_ZERO (c_avl_insert (t, key, value));
88     EXPECT_EQ_INT ((int) (i + 1), c_avl_size (t));
89   }
90
91   /* Key already exists. */
92   for (size_t i = 0; i < STATIC_ARRAY_SIZE (cases); i++)
93     EXPECT_EQ_INT (1, c_avl_insert (t, cases[i].key, cases[i].value));
94
95   /* get */
96   for (size_t i = 0; i < STATIC_ARRAY_SIZE (cases); i++)
97   {
98     char *value_ret = NULL;
99
100     CHECK_ZERO (c_avl_get (t, cases[i].key, (void *) &value_ret));
101     EXPECT_EQ_STR (cases[i].value, value_ret);
102   }
103
104   /* remove half */
105   for (size_t i = 0; i < STATIC_ARRAY_SIZE (cases) / 2; i++)
106   {
107     char *key = NULL;
108     char *value = NULL;
109
110     int expected_size = (int) (STATIC_ARRAY_SIZE (cases) - (i + 1));
111
112     CHECK_ZERO (c_avl_remove (t, cases[i].key, (void *) &key, (void *) &value));
113
114     EXPECT_EQ_STR (cases[i].key, key);
115     EXPECT_EQ_STR (cases[i].value, value);
116
117     free (key);
118     free (value);
119
120     EXPECT_EQ_INT (expected_size, c_avl_size (t));
121   }
122
123   /* pick the other half */
124   for (size_t i = STATIC_ARRAY_SIZE (cases) / 2; i < STATIC_ARRAY_SIZE (cases); i++)
125   {
126     char *key = NULL;
127     char *value = NULL;
128
129     int expected_size = (int) (STATIC_ARRAY_SIZE (cases) - (i + 1));
130
131     EXPECT_EQ_INT (expected_size + 1, c_avl_size (t));
132     EXPECT_EQ_INT (0, c_avl_pick (t, (void *) &key, (void *) &value));
133
134     free (key);
135     free (value);
136
137     EXPECT_EQ_INT (expected_size, c_avl_size (t));
138   }
139
140   c_avl_destroy (t);
141
142   return (0);
143 }
144
145 int main (void)
146 {
147   RUN_TEST(success);
148
149   END_TEST;
150 }
151
152 /* vim: set sw=2 sts=2 et : */