aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_container_dll.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_container_dll.c')
-rw-r--r--src/lib/util/test_container_dll.c114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/lib/util/test_container_dll.c b/src/lib/util/test_container_dll.c
new file mode 100644
index 000000000..fcbef4e8b
--- /dev/null
+++ b/src/lib/util/test_container_dll.c
@@ -0,0 +1,114 @@
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
28#include "platform.h"
29#include "gnunet_util_lib.h"
30
31/**
32 * Element in the DLL.
33 */
34struct Element
35{
36 /**
37 * Required pointer to previous element.
38 */
39 struct Element *prev;
40
41 /**
42 * Required pointer to next element.
43 */
44 struct Element *next;
45
46 /**
47 * Used to sort.
48 */
49 unsigned int value;
50};
51
52
53/**
54 * Compare two elements.
55 *
56 * @param cls closure, NULL
57 * @param e1 an element of to sort
58 * @param e2 another element to sort
59 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
60 */
61static int
62cmp_elem (void *cls,
63 struct Element *e1,
64 struct Element *e2)
65{
66 if (e1->value == e2->value)
67 return 0;
68 return (e1->value < e2->value) ? 1 : -1;
69}
70
71
72int
73main (int argc, char **argv)
74{
75 unsigned int values[] = {
76 4, 5, 8, 6, 9, 3, 7, 2, 1, 0
77 };
78 struct Element *head = NULL;
79 struct Element *tail = NULL;
80 struct Element *e;
81 unsigned int want;
82
83 GNUNET_log_setup ("test-container-dll",
84 "WARNING",
85 NULL);
86 for (unsigned int off = 0;
87 0 != values[off];
88 off++)
89 {
90 e = GNUNET_new (struct Element);
91 e->value = values[off];
92 GNUNET_CONTAINER_DLL_insert_sorted (struct Element,
93 cmp_elem,
94 NULL,
95 head,
96 tail,
97 e);
98 }
99
100 want = 1;
101 while (NULL != (e = head))
102 {
103 GNUNET_assert (e->value == want);
104 GNUNET_CONTAINER_DLL_remove (head,
105 tail,
106 e);
107 GNUNET_free (e);
108 want++;
109 }
110 return 0;
111}
112
113
114/* end of test_container_heap.c */