paragraph for gnunet devs that don't know how to use the web
[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
19 /**
20  * @author Christian Grothoff
21  * @file util/test_container_dll.c
22  * @brief Test of DLL operations
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27
28 /**
29  * Element in the DLL.
30  */
31 struct Element
32 {
33   /**
34    * Required pointer to previous element.
35    */
36   struct Element *prev;
37
38   /**
39    * Required pointer to next element.
40    */
41   struct Element *next;
42
43   /**
44    * Used to sort.
45    */
46   unsigned int value;
47 };
48
49
50 /**
51  * Compare two elements.
52  *
53  * @param cls closure, NULL
54  * @param e1 an element of to sort
55  * @param e2 another element to sort
56  * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
57  */
58 static int
59 cmp_elem (void *cls,
60           struct Element *e1,
61           struct Element *e2)
62 {
63   if (e1->value == e2->value)
64     return 0;
65   return (e1->value < e2->value) ? 1 : -1;
66 }
67
68
69 int
70 main (int argc, char **argv)
71 {
72   unsigned int values[] = {
73     4, 5, 8, 6, 9, 3, 7, 2, 1, 0
74   };
75   struct Element *head = NULL;
76   struct Element *tail = NULL;
77   struct Element *e;
78   unsigned int want;
79
80   GNUNET_log_setup ("test-container-dll",
81                     "WARNING",
82                     NULL);
83   for (unsigned int off=0;
84        0 != values[off];
85        off++)
86   {
87     e = GNUNET_new (struct Element);
88     e->value = values[off];
89     GNUNET_CONTAINER_DLL_insert_sorted (struct Element,
90                                         cmp_elem,
91                                         NULL,
92                                         head,
93                                         tail,
94                                         e);
95   }
96
97   want = 1;
98   while (NULL != (e = head))
99   {
100     GNUNET_assert (e->value == want);
101     GNUNET_CONTAINER_DLL_remove (head,
102                                  tail,
103                                  e);
104     GNUNET_free (e);
105     want++;
106   }
107   return 0;
108 }
109
110 /* end of test_container_heap.c */