converting to GNUNET_LOG_from*
[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,
50                                 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
51                                 &i, sizeof (i));
52   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
53
54   for (it = GNUNET_CONTAINER_slist_begin (l), i = 99;
55        GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
56        GNUNET_CONTAINER_slist_next (it), i--)
57     {
58       p = GNUNET_CONTAINER_slist_get (it, &s);
59
60       if ((p == NULL) || (i != (j = *(int *) p)) || (s != sizeof (i)))
61         {
62           GNUNET_CONTAINER_slist_iter_destroy (it);
63           GNUNET_assert (0);
64         }
65       j *= 2;
66       GNUNET_CONTAINER_slist_insert (it,
67                                      GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
68                                      &j, sizeof (j));
69     }
70   GNUNET_CONTAINER_slist_iter_destroy (it);
71   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200);
72   i = 198;
73   GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)));
74
75   for (it = GNUNET_CONTAINER_slist_begin (l);
76        GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;)
77     {
78       p = GNUNET_CONTAINER_slist_get (it, &s);
79       GNUNET_assert (p != NULL);
80       GNUNET_assert (s == sizeof (i));
81       i = *(int *) p;
82
83       GNUNET_assert (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES);
84       GNUNET_assert (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES);
85
86       p = GNUNET_CONTAINER_slist_get (it, &s);
87       GNUNET_assert (p != NULL);
88       GNUNET_assert (s == sizeof (j));
89       j = *(int *) p;
90
91       GNUNET_assert (j * 2 == i);
92
93       GNUNET_CONTAINER_slist_erase (it);
94     }
95   GNUNET_CONTAINER_slist_iter_destroy (it);
96   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
97   i = 99;
98   GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) ==
99                  GNUNET_NO);
100   i = 198;
101   GNUNET_assert (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) ==
102                  GNUNET_YES);
103
104   GNUNET_CONTAINER_slist_clear (l);
105   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
106
107   for (i = 0; i < 100; i++)
108     GNUNET_CONTAINER_slist_add (l,
109                                 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
110                                 &i, sizeof (i));
111   /*check slist_append */
112   GNUNET_CONTAINER_slist_append (l, l);
113   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 200);
114
115   GNUNET_CONTAINER_slist_destroy (l);
116
117   /*check slist_add_end */
118   l = GNUNET_CONTAINER_slist_create ();
119   for (i = 0; i < 100; i++)
120     GNUNET_CONTAINER_slist_add_end (l,
121                                     GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
122                                     &i, sizeof (i));
123
124   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 100);
125
126   for (it = GNUNET_CONTAINER_slist_begin (l), i = 0;
127        GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
128        GNUNET_CONTAINER_slist_next (it), i++)
129     {
130       p = GNUNET_CONTAINER_slist_get (it, &s);
131
132       if ((p == NULL) || (i != *(int *) p) || (s != sizeof (i)))
133         {
134           GNUNET_CONTAINER_slist_iter_destroy (it);
135           GNUNET_assert (0);
136         }
137     }
138   GNUNET_CONTAINER_slist_iter_destroy (it);
139   GNUNET_CONTAINER_slist_destroy (l);
140
141   /*check if disp = GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC */
142   l = GNUNET_CONTAINER_slist_create ();
143
144   for (i = 0; i < 100; i++)
145     {
146       ip = GNUNET_malloc (sizeof (int));
147       *ip = i;
148       GNUNET_CONTAINER_slist_add (l,
149                                   GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC,
150                                   ip, sizeof (int));
151     }
152   //creat_add
153   it = GNUNET_CONTAINER_slist_begin (l);
154   p = GNUNET_CONTAINER_slist_get (it, &s);
155   GNUNET_assert (p != NULL);
156   //slist_erase
157   GNUNET_assert (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES);
158   GNUNET_CONTAINER_slist_erase (it);
159   GNUNET_CONTAINER_slist_iter_destroy (it);
160   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 99);
161   //slist_clear
162   GNUNET_CONTAINER_slist_clear (l);
163   GNUNET_assert (GNUNET_CONTAINER_slist_count (l) == 0);
164   GNUNET_CONTAINER_slist_destroy (l);
165
166   return 0;
167 }