2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/test_container_slist.c
23 * @brief Testcases for singly linked lists
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
32 main (int argc, char *argv[])
34 struct GNUNET_CONTAINER_SList *l;
35 struct GNUNET_CONTAINER_SList_Iterator it;
42 GNUNET_log_setup ("test-container-slist", "WARNING", NULL);
44 l = GNUNET_CONTAINER_slist_create ();
45 GNUNET_assert (l != NULL);
46 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
48 for (i = 0; i < 100; i++)
49 GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
51 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
53 for (it = GNUNET_CONTAINER_slist_begin (l), i = 99;
54 GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES;
55 GNUNET_CONTAINER_slist_next (&it), i--)
57 p = GNUNET_CONTAINER_slist_get (&it, &s);
59 if ((p == NULL) || (i != (j = *(int *) p)) || (s != sizeof (i)))
61 GNUNET_CONTAINER_slist_iter_destroy (&it);
65 GNUNET_CONTAINER_slist_insert (&it,
66 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
69 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200);
71 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)));
73 for (it = GNUNET_CONTAINER_slist_begin (l);
74 GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES;)
76 p = GNUNET_CONTAINER_slist_get (&it, &s);
77 GNUNET_assert (p != NULL);
78 GNUNET_assert (s == sizeof (i));
81 GNUNET_assert (GNUNET_CONTAINER_slist_next (&it) == GNUNET_YES);
82 GNUNET_assert (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES);
84 p = GNUNET_CONTAINER_slist_get (&it, &s);
85 GNUNET_assert (p != NULL);
86 GNUNET_assert (s == sizeof (j));
89 GNUNET_assert (j * 2 == i);
91 GNUNET_CONTAINER_slist_erase (&it);
93 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
95 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) ==
98 GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) ==
101 GNUNET_CONTAINER_slist_clear (l);
102 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
104 for (i = 0; i < 100; i++)
105 GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
107 /*check slist_append */
108 GNUNET_CONTAINER_slist_append (l, l);
109 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200);
111 GNUNET_CONTAINER_slist_destroy (l);
113 /*check slist_add_end */
114 l = GNUNET_CONTAINER_slist_create ();
115 for (i = 0; i < 100; i++)
116 GNUNET_CONTAINER_slist_add_end (l,
117 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
120 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
122 for (it = GNUNET_CONTAINER_slist_begin (l), i = 0;
123 GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES;
124 GNUNET_CONTAINER_slist_next (&it), i++)
126 p = GNUNET_CONTAINER_slist_get (&it, &s);
128 if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i)))
133 GNUNET_CONTAINER_slist_destroy (l);
135 /*check if disp = GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC */
136 l = GNUNET_CONTAINER_slist_create ();
138 for (i = 0; i < 100; i++)
140 ip = GNUNET_malloc (sizeof (int));
142 GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC,
146 it = GNUNET_CONTAINER_slist_begin (l);
147 p = GNUNET_CONTAINER_slist_get (&it, &s);
148 GNUNET_assert (p != NULL);
150 GNUNET_assert (GNUNET_CONTAINER_slist_next (&it) == GNUNET_YES);
151 GNUNET_CONTAINER_slist_erase (&it);
152 GNUNET_CONTAINER_slist_iter_destroy (&it);
153 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 99);
155 GNUNET_CONTAINER_slist_clear (l);
156 GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
157 GNUNET_CONTAINER_slist_destroy (l);