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