aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_container_dll.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_container_dll.c')
-rw-r--r--src/util/test_container_dll.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/util/test_container_dll.c b/src/util/test_container_dll.c
new file mode 100644
index 000000000..4d2830807
--- /dev/null
+++ b/src/util/test_container_dll.c
@@ -0,0 +1,112 @@
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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 */
33struct Element
34{
35 /**
36 * Required pointer to previous element.
37 */
38 struct Element *prev;
39
40 /**
41 * Required pointer to next element.
42 */
43 struct Element *next;
44
45 /**
46 * Used to sort.
47 */
48 unsigned int value;
49};
50
51
52/**
53 * Compare two elements.
54 *
55 * @param cls closure, NULL
56 * @param e1 an element of to sort
57 * @param e2 another element to sort
58 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
59 */
60static int
61cmp_elem (void *cls,
62 struct Element *e1,
63 struct Element *e2)
64{
65 if (e1->value == e2->value)
66 return 0;
67 return (e1->value < e2->value) ? 1 : -1;
68}
69
70
71int
72main (int argc, char **argv)
73{
74 unsigned int values[] = {
75 4, 5, 8, 6, 9, 3, 7, 2, 1, 0
76 };
77 struct Element *head = NULL;
78 struct Element *tail = NULL;
79 struct Element *e;
80 unsigned int want;
81
82 GNUNET_log_setup ("test-container-dll",
83 "WARNING",
84 NULL);
85 for (unsigned int off=0;
86 0 != values[off];
87 off++)
88 {
89 e = GNUNET_new (struct Element);
90 e->value = values[off];
91 GNUNET_CONTAINER_DLL_insert_sorted (struct Element,
92 cmp_elem,
93 NULL,
94 head,
95 tail,
96 e);
97 }
98
99 want = 1;
100 while (NULL != (e = head))
101 {
102 GNUNET_assert (e->value == want);
103 GNUNET_CONTAINER_DLL_remove (head,
104 tail,
105 e);
106 GNUNET_free (e);
107 want++;
108 }
109 return 0;
110}
111
112/* end of test_container_heap.c */