cleaning argz mess
[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 2, 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   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       CHECK (p != NULL);
62       j = *(int *) p;
63       CHECK (i == j);
64       CHECK (s == sizeof (i));
65
66       j *= 2;
67       GNUNET_CONTAINER_slist_insert (it,
68                                      GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
69                                      &j, sizeof (j));
70     }
71   GNUNET_CONTAINER_slist_iter_destroy (it);
72   CHECK (GNUNET_CONTAINER_slist_count (l) == 200);
73   i = 198;
74   CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)));
75
76   for (it = GNUNET_CONTAINER_slist_begin (l);
77        GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;)
78     {
79       p = GNUNET_CONTAINER_slist_get (it, &s);
80       CHECK (p != NULL);
81       CHECK (s == sizeof (i));
82       i = *(int *) p;
83
84       CHECK (GNUNET_CONTAINER_slist_next (it) == GNUNET_YES);
85       CHECK (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES);
86
87       p = GNUNET_CONTAINER_slist_get (it, &s);
88       CHECK (p != NULL);
89       CHECK (s == sizeof (j));
90       j = *(int *) p;
91
92       CHECK (j * 2 == i);
93
94       GNUNET_CONTAINER_slist_erase (it);
95     }
96   GNUNET_CONTAINER_slist_iter_destroy (it);
97   CHECK (GNUNET_CONTAINER_slist_count (l) == 100);
98   i = 99;
99   CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == GNUNET_NO);
100   i = 198;
101   CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)));
102
103   GNUNET_CONTAINER_slist_clear (l);
104   CHECK (GNUNET_CONTAINER_slist_count (l) == 0);
105
106   for (i = 0; i < 100; i++)
107     GNUNET_CONTAINER_slist_add (l,
108                                 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
109                                 &i, sizeof (i));
110
111   GNUNET_CONTAINER_slist_destroy (l);
112
113   return 0;
114 }