2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2005, 2006, 2017 GNUnet e.V.
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.
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.
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/>.
20 * @file util/test_common_allocation.c
21 * @brief testcase for common_allocation.c
24 #include "gnunet_util_lib.h"
30 #define MAX_TESTVAL 1024
31 char *ptrs[MAX_TESTVAL];
39 /* GNUNET_malloc/GNUNET_free test */
40 k = 352; /* random start value */
41 for (i = 1; i < MAX_TESTVAL; i++)
43 ptrs[i] = GNUNET_malloc (i);
44 for (j = 0; j < i; j++)
48 for (i = MAX_TESTVAL - 1; i >= 1; i--)
50 for (j = i - 1; j >= 0; j--)
51 if (ptrs[i][j] != (char) --k)
53 GNUNET_free (ptrs[i]);
56 /* GNUNET_free_non_null test */
57 GNUNET_free_non_null (NULL);
58 GNUNET_free_non_null (GNUNET_malloc (4));
60 /* GNUNET_strdup tests */
61 ptrs[0] = GNUNET_strdup ("bar");
62 if (0 != strcmp (ptrs[0], "bar"))
65 ptrs[0] = GNUNET_realloc (ptrs[0], 12);
66 strcpy (ptrs[0], "Hello World");
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]);
73 /* GNUNET_array_grow tests */
76 GNUNET_array_grow (ptrs[0], ui, 42);
79 GNUNET_array_grow (ptrs[0], ui, 22);
82 for (j = 0; j < 22; j++)
84 GNUNET_array_grow (ptrs[0], ui, 32);
85 for (j = 0; j < 22; j++)
88 for (j = 22; j < 32; j++)
91 GNUNET_array_grow (ptrs[0], ui, 0);
97 /* GNUNET_new_array_2d tests */
98 a2 = GNUNET_new_array_2d (17, 22, unsigned int);
99 for (i = 0; i < 17; i++)
101 for (j = 0; j < 22; j++)
108 a2[i][j] = i * 100 + j;
113 /* GNUNET_new_array_3d tests */
114 a3 = GNUNET_new_array_3d (2, 3, 4, char);
115 for (i = 0; i < 2; i++)
117 for (j = 0; j < 3; j++)
119 for (k = 0; k < 4; k++)
121 if (0 != a3[i][j][k])
126 a3[i][j][k] = i * 100 + j * 10 + k;
136 main (int argc, char *argv[])
140 GNUNET_log_setup ("test-common-allocation",
151 /* end of test_common_allocation.c */