diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-01-25 19:59:29 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-01-25 19:59:29 +0100 |
commit | a6a84661da2dc124560dfdfdb92304f3e9bdf92e (patch) | |
tree | 5a1f66e4992006b96d288c4b733639e1e61c1629 /src | |
parent | 64d8b62366d5c9d63ded495a31cb63b643a67f3f (diff) | |
download | gnunet-a6a84661da2dc124560dfdfdb92304f3e9bdf92e.tar.gz gnunet-a6a84661da2dc124560dfdfdb92304f3e9bdf92e.zip |
fix insert_sorted macro
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_container_lib.h | 12 | ||||
-rw-r--r-- | src/util/.gitignore | 1 | ||||
-rw-r--r-- | src/util/test_container_dll.c | 115 |
3 files changed, 124 insertions, 4 deletions
diff --git a/src/include/gnunet_container_lib.h b/src/include/gnunet_container_lib.h index e6f4ba43b..c77d82fd3 100644 --- a/src/include/gnunet_container_lib.h +++ b/src/include/gnunet_container_lib.h | |||
@@ -2093,7 +2093,7 @@ GNUNET_CONTAINER_multihashmap32_iterator_destroy (struct GNUNET_CONTAINER_MultiH | |||
2093 | head)) ) \ | 2093 | head)) ) \ |
2094 | { \ | 2094 | { \ |
2095 | /* insert at head, element < head */ \ | 2095 | /* insert at head, element < head */ \ |
2096 | GNUNET_CONTAINER_DLL_insert (head, \ | 2096 | GNUNET_CONTAINER_DLL_insert (head, \ |
2097 | tail, \ | 2097 | tail, \ |
2098 | element); \ | 2098 | element); \ |
2099 | } \ | 2099 | } \ |
@@ -2110,14 +2110,18 @@ GNUNET_CONTAINER_multihashmap32_iterator_destroy (struct GNUNET_CONTAINER_MultiH | |||
2110 | pos)) \ | 2110 | pos)) \ |
2111 | break; /* element < pos */ \ | 2111 | break; /* element < pos */ \ |
2112 | if (NULL == pos) /* => element > tail */ \ | 2112 | if (NULL == pos) /* => element > tail */ \ |
2113 | GNUNET_CONTAINER_DLL_insert_tail (head, \ | 2113 | { \ |
2114 | GNUNET_CONTAINER_DLL_insert_tail (head, \ | ||
2114 | tail, \ | 2115 | tail, \ |
2115 | element); \ | 2116 | element); \ |
2117 | } \ | ||
2116 | else /* prev < element < pos */ \ | 2118 | else /* prev < element < pos */ \ |
2119 | { \ | ||
2117 | GNUNET_CONTAINER_DLL_insert_after (head, \ | 2120 | GNUNET_CONTAINER_DLL_insert_after (head, \ |
2118 | tail, \ | 2121 | tail, \ |
2119 | element, \ | 2122 | pos->prev, \ |
2120 | pos->prev); \ | 2123 | element); \ |
2124 | } \ | ||
2121 | } \ | 2125 | } \ |
2122 | } while (0) | 2126 | } while (0) |
2123 | 2127 | ||
diff --git a/src/util/.gitignore b/src/util/.gitignore index f207e07bf..a0cb43874 100644 --- a/src/util/.gitignore +++ b/src/util/.gitignore | |||
@@ -22,6 +22,7 @@ test_connection_timeout.nc | |||
22 | test_connection_timeout_no_connect.nc | 22 | test_connection_timeout_no_connect.nc |
23 | test_connection_transmit_cancel.nc | 23 | test_connection_transmit_cancel.nc |
24 | test_container_bloomfilter | 24 | test_container_bloomfilter |
25 | test_container_dll | ||
25 | test_container_heap | 26 | test_container_heap |
26 | test_container_meta_data | 27 | test_container_meta_data |
27 | test_container_multihashmap | 28 | test_container_multihashmap |
diff --git a/src/util/test_container_dll.c b/src/util/test_container_dll.c new file mode 100644 index 000000000..8ad08423a --- /dev/null +++ b/src/util/test_container_dll.c | |||
@@ -0,0 +1,115 @@ | |||
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 | */ | ||
33 | struct 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 | */ | ||
60 | static int | ||
61 | cmp_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 | |||
71 | int | ||
72 | main (int argc, char **argv) | ||
73 | { | ||
74 | unsigned int values2[] = { | ||
75 | 4, 5, 8, 6, 9, 3, 7, 2, 6, 1, 0 | ||
76 | }; | ||
77 | unsigned int values[] = { | ||
78 | 1, 3, 2, 0 | ||
79 | }; | ||
80 | struct Element *head = NULL; | ||
81 | struct Element *tail = NULL; | ||
82 | struct Element *e; | ||
83 | unsigned int want; | ||
84 | |||
85 | GNUNET_log_setup ("test-container-dll", | ||
86 | "WARNING", | ||
87 | NULL); | ||
88 | for (unsigned int off=0; | ||
89 | 0 != values[off]; | ||
90 | off++) | ||
91 | { | ||
92 | e = GNUNET_new (struct Element); | ||
93 | e->value = values[off]; | ||
94 | GNUNET_CONTAINER_DLL_insert_sorted (struct Element, | ||
95 | cmp_elem, | ||
96 | NULL, | ||
97 | head, | ||
98 | tail, | ||
99 | e); | ||
100 | } | ||
101 | |||
102 | want = 1; | ||
103 | while (NULL != (e = head)) | ||
104 | { | ||
105 | GNUNET_assert (e->value == want); | ||
106 | GNUNET_CONTAINER_DLL_remove (head, | ||
107 | tail, | ||
108 | e); | ||
109 | GNUNET_free (e); | ||
110 | want++; | ||
111 | } | ||
112 | return 0; | ||
113 | } | ||
114 | |||
115 | /* end of test_container_heap.c */ | ||