indentation
[oweals/gnunet.git] / src / util / test_container_slist.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/test_container_slist.c
23  * @brief Testcases for singly linked lists
24  * @author Nils Durner
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
30
31 int
32 main (int argc, char *argv[])
33 {
34   struct GNUNET_CONTAINER_SList *l;
35   struct GNUNET_CONTAINER_SList_Iterator *it;
36   unsigned int i;
37   int *ip;
38   unsigned int j;
39   size_t s;
40   const void *p;
41
42   GNUNET_log_setup ("test-container-slist", "WARNING", NULL);
43
44   l = GNUNET_CONTAINER_slist_create ();
45   GNUNET_assert (l != NULL);
46   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
47
48   for (i = 0; i < 100; i++)
49     GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
50                                 &i, sizeof (i));
51   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
52
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--)
56   {
57     p = GNUNET_CONTAINER_slist_get (it, &s);
58
59     if ((p == NULL) || (i != (j = *(int *) p)) || (s != sizeof (i)))
60     {
61       GNUNET_CONTAINER_slist_iter_destroy (it);
62       GNUNET_assert (0);
63     }
64     j *= 2;
65     GNUNET_CONTAINER_slist_insert (it,
66                                    GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
67                                    &j, sizeof (j));
68   }
69   GNUNET_CONTAINER_slist_iter_destroy (it);
70   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200);
71   i = 198;
72   GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)));
73
74   for (it = GNUNET_CONTAINER_slist_begin (l);
75        GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;)
76   {
77     p = GNUNET_CONTAINER_slist_get (it, &s);
78     GNUNET_assert (p != NULL);
79     GNUNET_assert (s == sizeof (i));
80     i = *(int *) p;
81
82     GNUNET_assert (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES);
83     GNUNET_assert (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES);
84
85     p = GNUNET_CONTAINER_slist_get (it, &s);
86     GNUNET_assert (p != NULL);
87     GNUNET_assert (s == sizeof (j));
88     j = *(int *) p;
89
90     GNUNET_assert (j * 2 == i);
91
92     GNUNET_CONTAINER_slist_erase (it);
93   }
94   GNUNET_CONTAINER_slist_iter_destroy (it);
95   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
96   i = 99;
97   GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) ==
98                  GNUNET_NO);
99   i = 198;
100   GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) ==
101                  GNUNET_YES);
102
103   GNUNET_CONTAINER_slist_clear (l);
104   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
105
106   for (i = 0; i < 100; i++)
107     GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
108                                 &i, sizeof (i));
109   /*check slist_append */
110   GNUNET_CONTAINER_slist_append (l, l);
111   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200);
112
113   GNUNET_CONTAINER_slist_destroy (l);
114
115   /*check slist_add_end */
116   l = GNUNET_CONTAINER_slist_create ();
117   for (i = 0; i < 100; i++)
118     GNUNET_CONTAINER_slist_add_end (l,
119                                     GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
120                                     &i, sizeof (i));
121
122   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
123
124   for (it = GNUNET_CONTAINER_slist_begin (l), i = 0;
125        GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
126        GNUNET_CONTAINER_slist_next (it), i++)
127   {
128     p = GNUNET_CONTAINER_slist_get (it, &s);
129
130     if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i)))
131     {
132       GNUNET_CONTAINER_slist_iter_destroy (it);
133       GNUNET_assert (0);
134     }
135   }
136   GNUNET_CONTAINER_slist_iter_destroy (it);
137   GNUNET_CONTAINER_slist_destroy (l);
138
139   /*check if disp = GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC */
140   l = GNUNET_CONTAINER_slist_create ();
141
142   for (i = 0; i < 100; i++)
143   {
144     ip = GNUNET_malloc (sizeof (int));
145     *ip = i;
146     GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC,
147                                 ip, sizeof (int));
148   }
149   //creat_add
150   it = GNUNET_CONTAINER_slist_begin (l);
151   p = GNUNET_CONTAINER_slist_get (it, &s);
152   GNUNET_assert (p != NULL);
153   //slist_erase
154   GNUNET_assert (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES);
155   GNUNET_CONTAINER_slist_erase (it);
156   GNUNET_CONTAINER_slist_iter_destroy (it);
157   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 99);
158   //slist_clear
159   GNUNET_CONTAINER_slist_clear (l);
160   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
161   GNUNET_CONTAINER_slist_destroy (l);
162
163   return 0;
164 }