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