Rename foo_test to test_foo.
[collectd.git] / src / tests / test_utils_heap.c
1 /**
2  * collectd - src/tests/utils_heap_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 "tests/macros.h"
30 #include "utils_heap.h"
31
32 static int compare (void const *v0, void const *v1)
33 {
34   int const *i0 = v0;
35   int const *i1 = v1;
36
37   if ((*i0) < (*i1))
38     return -1;
39   else if ((*i0) > (*i1))
40     return 1;
41   else
42     return 0;
43 }
44
45 DEF_TEST(simple)
46 {
47   int values[] = { 9, 5, 6, 1, 3, 4, 0, 8, 2, 7 };
48   int i;
49   c_heap_t *h;
50
51   CHECK_NOT_NULL(h = c_heap_create (compare));
52   for (i = 0; i < 10; i++)
53     CHECK_ZERO(c_heap_insert (h, &values[i]));
54
55   for (i = 0; i < 5; i++)
56   {
57     int *ret = NULL;
58     CHECK_NOT_NULL(ret = c_heap_get_root(h));
59     OK(*ret == i);
60   }
61
62   CHECK_ZERO(c_heap_insert (h, &values[6] /* = 0 */));
63   CHECK_ZERO(c_heap_insert (h, &values[3] /* = 1 */));
64   CHECK_ZERO(c_heap_insert (h, &values[8] /* = 2 */));
65   CHECK_ZERO(c_heap_insert (h, &values[4] /* = 3 */));
66   CHECK_ZERO(c_heap_insert (h, &values[5] /* = 4 */));
67
68   for (i = 0; i < 10; i++)
69   {
70     int *ret = NULL;
71     CHECK_NOT_NULL(ret = c_heap_get_root(h));
72     OK(*ret == i);
73   }
74
75   c_heap_destroy(h);
76   return (0);
77 }
78
79 int main (void)
80 {
81   RUN_TEST(simple);
82
83   END_TEST;
84 }
85
86 /* vim: set sw=2 sts=2 et : */