fix
[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 #define CHECK(c) do { if (! (c)) { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); GNUNET_CONTAINER_slist_destroy (l); return 1; } } while (0)
32
33 int
34 main (int argc, char *argv[])
35 {
36   struct GNUNET_CONTAINER_SList *l;
37   struct GNUNET_CONTAINER_SList_Iterator *it;
38   unsigned int i;
39   int *ip;
40   unsigned int j;
41   size_t s;
42   const void *p;
43
44   GNUNET_log_setup ("test-container-slist", "WARNING", NULL);
45
46   l = GNUNET_CONTAINER_slist_create ();
47   CHECK (l != NULL);
48   CHECK (GNUNET_CONTAINER_slist_count (l) == 0);
49
50   for (i = 0; i < 100; i++)
51     GNUNET_CONTAINER_slist_add (l,
52                                 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
53                                 &i, sizeof (i));
54   CHECK (GNUNET_CONTAINER_slist_count (l) == 100);
55
56   for (it = GNUNET_CONTAINER_slist_begin (l), i = 99;
57        GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
58        GNUNET_CONTAINER_slist_next (it), i--)
59     {
60       p = GNUNET_CONTAINER_slist_get (it, &s);
61
62       if ( (p == NULL) ||
63            (i != (j = *(int *) p)) ||
64            (s != sizeof (i)) )
65         {
66           GNUNET_CONTAINER_slist_iter_destroy (it);
67           CHECK (0);
68         }
69       j *= 2;
70       GNUNET_CONTAINER_slist_insert (it,
71                                      GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
72                                      &j, sizeof (j));
73     }
74   GNUNET_CONTAINER_slist_iter_destroy (it);
75   CHECK (GNUNET_CONTAINER_slist_count (l) == 200);
76   i = 198;
77   CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)));
78
79   for (it = GNUNET_CONTAINER_slist_begin (l);
80        GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;)
81     {
82       p = GNUNET_CONTAINER_slist_get (it, &s);
83       CHECK (p != NULL);
84       CHECK (s == sizeof (i));
85       i = *(int *) p;
86
87       CHECK (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES);
88       CHECK (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES);
89
90       p = GNUNET_CONTAINER_slist_get (it, &s);
91       CHECK (p != NULL);
92       CHECK (s == sizeof (j));
93       j = *(int *) p;
94
95       CHECK (j * 2 == i);
96
97       GNUNET_CONTAINER_slist_erase (it);
98     }
99   GNUNET_CONTAINER_slist_iter_destroy (it);
100   CHECK (GNUNET_CONTAINER_slist_count (l) == 100);
101   i = 99;
102   CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == GNUNET_NO);
103   i = 198;
104   CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == GNUNET_YES);
105
106   GNUNET_CONTAINER_slist_clear (l);
107   CHECK (GNUNET_CONTAINER_slist_count (l) == 0);
108
109   for (i = 0; i < 100; i++)
110     GNUNET_CONTAINER_slist_add (l,
111                                 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
112                                 &i, sizeof (i));
113   /*check slist_append*/
114   GNUNET_CONTAINER_slist_append(l,l);
115   CHECK (GNUNET_CONTAINER_slist_count (l) == 200);
116
117   GNUNET_CONTAINER_slist_destroy (l);
118
119   /*check slist_add_end*/
120   l = GNUNET_CONTAINER_slist_create ();
121   for (i = 0; i < 100; i++)
122     GNUNET_CONTAINER_slist_add_end (l,
123                                 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
124                                 &i, sizeof (i));
125
126   CHECK (GNUNET_CONTAINER_slist_count (l) == 100);
127
128   for (it = GNUNET_CONTAINER_slist_begin (l), i = 0;
129        GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
130        GNUNET_CONTAINER_slist_next (it), i++)
131   {
132     p = GNUNET_CONTAINER_slist_get (it, &s);
133
134     if ((p == NULL) ||
135         (i != *(int *) p) ||
136         (s != sizeof (i)))
137     {
138       GNUNET_CONTAINER_slist_iter_destroy (it);
139       CHECK (0);
140     }
141   }
142
143   GNUNET_CONTAINER_slist_destroy (l);
144
145   /*check if disp = GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC*/
146   l = GNUNET_CONTAINER_slist_create ();
147   
148   for (i = 0; i < 100; i++)
149     {
150       ip = GNUNET_malloc (sizeof (int));
151       *ip = i;
152       GNUNET_CONTAINER_slist_add (l,
153                                   GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC,
154                                   ip, sizeof (int));
155     }
156   //creat_add
157   it = GNUNET_CONTAINER_slist_begin (l);
158   p = GNUNET_CONTAINER_slist_get (it, &s);
159   CHECK (p != NULL);
160   //slist_erase
161   CHECK (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES); 
162   GNUNET_CONTAINER_slist_erase (it);
163   GNUNET_CONTAINER_slist_iter_destroy (it);
164   CHECK (GNUNET_CONTAINER_slist_count (l) == 99);
165   //slist_clear
166   GNUNET_CONTAINER_slist_clear(l);
167   CHECK (GNUNET_CONTAINER_slist_count (l) == 0);
168   GNUNET_CONTAINER_slist_destroy (l);
169
170   return 0;
171 }