e67360044a3c0a4c38ca78191d0c9bb8d9b423cb
[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      SPDX-License-Identifier: AGPL3.0-or-later
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
29 static int
30 check()
31 {
32 #define MAX_TESTVAL 1024
33   char *ptrs[MAX_TESTVAL];
34   unsigned int **a2;
35   char ***a3;
36   int i;
37   int j;
38   int k;
39   unsigned int ui;
40
41   /* GNUNET_malloc/GNUNET_free test */
42   k = 352;                      /* random start value */
43   for (i = 1; i < MAX_TESTVAL; i++)
44     {
45       ptrs[i] = GNUNET_malloc(i);
46       for (j = 0; j < i; j++)
47         ptrs[i][j] = k++;
48     }
49
50   for (i = MAX_TESTVAL - 1; i >= 1; i--)
51     {
52       for (j = i - 1; j >= 0; j--)
53         if (ptrs[i][j] != (char)--k)
54           return 1;
55       GNUNET_free(ptrs[i]);
56     }
57
58   /* GNUNET_free_non_null test */
59   GNUNET_free_non_null(NULL);
60   GNUNET_free_non_null(GNUNET_malloc(4));
61
62   /* GNUNET_strdup tests */
63   ptrs[0] = GNUNET_strdup("bar");
64   if (0 != strcmp(ptrs[0], "bar"))
65     return 3;
66   /* now realloc */
67   ptrs[0] = GNUNET_realloc(ptrs[0], 12);
68   strcpy(ptrs[0], "Hello World");
69
70   GNUNET_free(ptrs[0]);
71   GNUNET_asprintf(&ptrs[0], "%s %s", "Hello", "World");
72   GNUNET_assert(strlen(ptrs[0]) == 11);
73   GNUNET_free(ptrs[0]);
74
75   /* GNUNET_array_grow tests */
76   ptrs[0] = NULL;
77   ui = 0;
78   GNUNET_array_grow(ptrs[0], ui, 42);
79   if (ui != 42)
80     return 4;
81   GNUNET_array_grow(ptrs[0], ui, 22);
82   if (ui != 22)
83     return 5;
84   for (j = 0; j < 22; j++)
85     ptrs[0][j] = j;
86   GNUNET_array_grow(ptrs[0], ui, 32);
87   for (j = 0; j < 22; j++)
88     if (ptrs[0][j] != j)
89       return 6;
90   for (j = 22; j < 32; j++)
91     if (ptrs[0][j] != 0)
92       return 7;
93   GNUNET_array_grow(ptrs[0], ui, 0);
94   if (i != 0)
95     return 8;
96   if (ptrs[0] != NULL)
97     return 9;
98
99   /* GNUNET_new_array_2d tests */
100   a2 = GNUNET_new_array_2d(17, 22, unsigned int);
101   for (i = 0; i < 17; i++)
102     {
103       for (j = 0; j < 22; j++)
104         {
105           if (0 != a2[i][j])
106             {
107               GNUNET_free(a2);
108               return 10;
109             }
110           a2[i][j] = i * 100 + j;
111         }
112     }
113   GNUNET_free(a2);
114
115   /* GNUNET_new_array_3d tests */
116   a3 = GNUNET_new_array_3d(2, 3, 4, char);
117   for (i = 0; i < 2; i++)
118     {
119       for (j = 0; j < 3; j++)
120         {
121           for (k = 0; k < 4; k++)
122             {
123               if (0 != a3[i][j][k])
124                 {
125                   GNUNET_free(a3);
126                   return 11;
127                 }
128               a3[i][j][k] = i * 100 + j * 10 + k;
129             }
130         }
131     }
132   GNUNET_free(a3);
133   return 0;
134 }
135
136
137 int
138 main(int argc, char *argv[])
139 {
140   int ret;
141
142   GNUNET_log_setup("test-common-allocation",
143                    "WARNING",
144                    NULL);
145   ret = check();
146   if (ret != 0)
147     fprintf(stderr,
148             "ERROR %d.\n",
149             ret);
150   return ret;
151 }
152
153 /* end of test_common_allocation.c */