uncrustify as demanded.
[oweals/gnunet.git] / src / util / test_container_dll.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2017 GNUnet e.V.
4
5    GNUnet is free software: you can redistribute it and/or modify it
6    under the terms of the GNU Affero General Public License as published
7    by the Free Software Foundation, either version 3 of the License,
8    or (at your option) any later version.
9
10    GNUnet is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Affero General Public License for more details.
14
15    You should have received a copy of the GNU Affero General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @author Christian Grothoff
23  * @file util/test_container_dll.c
24  * @brief Test of DLL operations
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29
30 /**
31  * Element in the DLL.
32  */
33 struct Element {
34   /**
35    * Required pointer to previous element.
36    */
37   struct Element *prev;
38
39   /**
40    * Required pointer to next element.
41    */
42   struct Element *next;
43
44   /**
45    * Used to sort.
46    */
47   unsigned int value;
48 };
49
50
51 /**
52  * Compare two elements.
53  *
54  * @param cls closure, NULL
55  * @param e1 an element of to sort
56  * @param e2 another element to sort
57  * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
58  */
59 static int
60 cmp_elem(void *cls,
61          struct Element *e1,
62          struct Element *e2)
63 {
64   if (e1->value == e2->value)
65     return 0;
66   return (e1->value < e2->value) ? 1 : -1;
67 }
68
69
70 int
71 main(int argc, char **argv)
72 {
73   unsigned int values[] = {
74     4, 5, 8, 6, 9, 3, 7, 2, 1, 0
75   };
76   struct Element *head = NULL;
77   struct Element *tail = NULL;
78   struct Element *e;
79   unsigned int want;
80
81   GNUNET_log_setup("test-container-dll",
82                    "WARNING",
83                    NULL);
84   for (unsigned int off = 0;
85        0 != values[off];
86        off++)
87     {
88       e = GNUNET_new(struct Element);
89       e->value = values[off];
90       GNUNET_CONTAINER_DLL_insert_sorted(struct Element,
91                                          cmp_elem,
92                                          NULL,
93                                          head,
94                                          tail,
95                                          e);
96     }
97
98   want = 1;
99   while (NULL != (e = head))
100     {
101       GNUNET_assert(e->value == want);
102       GNUNET_CONTAINER_DLL_remove(head,
103                                   tail,
104                                   e);
105       GNUNET_free(e);
106       want++;
107     }
108   return 0;
109 }
110
111 /* end of test_container_heap.c */