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