paragraph for gnunet devs that don't know how to use the web
[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, 2017 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file util/test_common_allocation.c
21  * @brief testcase for common_allocation.c
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25
26
27 static int
28 check ()
29 {
30 #define MAX_TESTVAL 1024
31   char *ptrs[MAX_TESTVAL];
32   unsigned int **a2;
33   char ***a3;
34   int i;
35   int j;
36   int k;
37   unsigned int ui;
38
39   /* GNUNET_malloc/GNUNET_free test */
40   k = 352;                      /* random start value */
41   for (i = 1; i < MAX_TESTVAL; i++)
42   {
43     ptrs[i] = GNUNET_malloc (i);
44     for (j = 0; j < i; j++)
45       ptrs[i][j] = k++;
46   }
47
48   for (i = MAX_TESTVAL - 1; i >= 1; i--)
49   {
50     for (j = i - 1; j >= 0; j--)
51       if (ptrs[i][j] != (char) --k)
52         return 1;
53     GNUNET_free (ptrs[i]);
54   }
55
56   /* GNUNET_free_non_null test */
57   GNUNET_free_non_null (NULL);
58   GNUNET_free_non_null (GNUNET_malloc (4));
59
60   /* GNUNET_strdup tests */
61   ptrs[0] = GNUNET_strdup ("bar");
62   if (0 != strcmp (ptrs[0], "bar"))
63     return 3;
64   /* now realloc */
65   ptrs[0] = GNUNET_realloc (ptrs[0], 12);
66   strcpy (ptrs[0], "Hello World");
67
68   GNUNET_free (ptrs[0]);
69   GNUNET_asprintf (&ptrs[0], "%s %s", "Hello", "World");
70   GNUNET_assert (strlen (ptrs[0]) == 11);
71   GNUNET_free (ptrs[0]);
72
73   /* GNUNET_array_grow tests */
74   ptrs[0] = NULL;
75   ui = 0;
76   GNUNET_array_grow (ptrs[0], ui, 42);
77   if (ui != 42)
78     return 4;
79   GNUNET_array_grow (ptrs[0], ui, 22);
80   if (ui != 22)
81     return 5;
82   for (j = 0; j < 22; j++)
83     ptrs[0][j] = j;
84   GNUNET_array_grow (ptrs[0], ui, 32);
85   for (j = 0; j < 22; j++)
86     if (ptrs[0][j] != j)
87       return 6;
88   for (j = 22; j < 32; j++)
89     if (ptrs[0][j] != 0)
90       return 7;
91   GNUNET_array_grow (ptrs[0], ui, 0);
92   if (i != 0)
93     return 8;
94   if (ptrs[0] != NULL)
95     return 9;
96
97   /* GNUNET_new_array_2d tests */
98   a2 = GNUNET_new_array_2d (17, 22, unsigned int);
99   for (i = 0; i < 17; i++)
100   {
101     for (j = 0; j < 22; j++)
102     {
103       if (0 != a2[i][j])
104       {
105         GNUNET_free (a2);
106         return 10;
107       }
108       a2[i][j] = i * 100 + j;
109     }
110   }
111   GNUNET_free (a2);
112
113   /* GNUNET_new_array_3d tests */
114   a3 = GNUNET_new_array_3d (2, 3, 4, char);
115   for (i = 0; i < 2; i++)
116   {
117     for (j = 0; j < 3; j++)
118     {
119       for (k = 0; k < 4; k++)
120       {
121         if (0 != a3[i][j][k])
122         {
123           GNUNET_free (a3);
124           return 11;
125         }
126         a3[i][j][k] = i * 100 + j * 10 + k;
127       }
128     }
129   }
130   GNUNET_free (a3);
131   return 0;
132 }
133
134
135 int
136 main (int argc, char *argv[])
137 {
138   int ret;
139
140   GNUNET_log_setup ("test-common-allocation",
141                     "WARNING",
142                     NULL);
143   ret = check ();
144   if (ret != 0)
145     FPRINTF (stderr,
146              "ERROR %d.\n",
147              ret);
148   return ret;
149 }
150
151 /* end of test_common_allocation.c */