4ef98b6298e850210535105e1c2ccc8d72c202c0
[oweals/gnunet.git] / src / util / test_common_allocation.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2003, 2005, 2006 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file util/test_common_allocation.c
23  * @brief testcase for common_allocation.c
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27
28 static int
29 check ()
30 {
31 #define MAX_TESTVAL 1024
32   char *ptrs[MAX_TESTVAL];
33   unsigned int **a2;
34   char ***a3;
35   int i;
36   int j;
37   int k;
38   unsigned int ui;
39
40   /* GNUNET_malloc/GNUNET_free test */
41   k = 352;                      /* random start value */
42   for (i = 1; i < MAX_TESTVAL; i++)
43   {
44     ptrs[i] = GNUNET_malloc (i);
45     for (j = 0; j < i; j++)
46       ptrs[i][j] = k++;
47   }
48
49   for (i = MAX_TESTVAL - 1; i >= 1; i--)
50   {
51     for (j = i - 1; j >= 0; j--)
52       if (ptrs[i][j] != (char) --k)
53         return 1;
54     GNUNET_free (ptrs[i]);
55   }
56
57   /* GNUNET_free_non_null test */
58   GNUNET_free_non_null (NULL);
59   GNUNET_free_non_null (GNUNET_malloc (4));
60
61   /* GNUNET_strdup tests */
62   ptrs[0] = GNUNET_strdup ("bar");
63   if (0 != strcmp (ptrs[0], "bar"))
64     return 3;
65   /* now realloc */
66   ptrs[0] = GNUNET_realloc (ptrs[0], 12);
67   strcpy (ptrs[0], "Hello World");
68
69   GNUNET_free (ptrs[0]);
70   GNUNET_asprintf (&ptrs[0], "%s %s", "Hello", "World");
71   GNUNET_assert (strlen (ptrs[0]) == 11);
72   GNUNET_free (ptrs[0]);
73
74   /* GNUNET_array_grow tests */
75   ptrs[0] = NULL;
76   ui = 0;
77   GNUNET_array_grow (ptrs[0], ui, 42);
78   if (ui != 42)
79     return 4;
80   GNUNET_array_grow (ptrs[0], ui, 22);
81   if (ui != 22)
82     return 5;
83   for (j = 0; j < 22; j++)
84     ptrs[0][j] = j;
85   GNUNET_array_grow (ptrs[0], ui, 32);
86   for (j = 0; j < 22; j++)
87     if (ptrs[0][j] != j)
88       return 6;
89   for (j = 22; j < 32; j++)
90     if (ptrs[0][j] != 0)
91       return 7;
92   GNUNET_array_grow (ptrs[0], ui, 0);
93   if (i != 0)
94     return 8;
95   if (ptrs[0] != NULL)
96     return 9;
97
98         /* GNUNET_new_array_2d tests */
99         a2 = GNUNET_new_array_2d (17, 22, unsigned int);
100         for (i = 0; i < 17; i++)
101         {
102                 for (j = 0; j < 22; j++)
103                 {
104                         if (0 != a2[i][j])
105                                 return 10;
106                         a2[i][j] = i * 100 + j;
107                 }
108         }
109         free (a2);
110
111         /* GNUNET_new_array_3d tests */
112         a3 = GNUNET_new_array_3d (2, 3, 4, char);
113         for (i = 0; i < 2; i++)
114         {
115                 for (j = 0; j < 3; j++)
116                 {
117                         for (k = 0; k < 4; k++)
118                         {
119                                 if (0 != a3[i][j][k])
120                                         return 11;
121                                 a3[i][j][k] = i * 100 + j * 10 + k;
122                         }
123                 }
124         }
125         free (a3);
126
127   return 0;
128 }
129
130 int
131 main (int argc, char *argv[])
132 {
133   int ret;
134
135   GNUNET_log_setup ("test-common-allocation", "WARNING", NULL);
136   ret = check ();
137   if (ret != 0)
138     FPRINTF (stderr, "ERROR %d.\n", ret);
139   return ret;
140 }
141
142 /* end of test_common_allocation.c */