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
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.
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.
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.
22 * @file util/test_common_allocation.c
23 * @brief testcase for common_allocation.c
26 #include "gnunet_util_lib.h"
32 #define MAX_TESTVAL 1024
33 char *ptrs[MAX_TESTVAL];
41 /* GNUNET_malloc/GNUNET_free test */
42 k = 352; /* random start value */
43 for (i = 1; i < MAX_TESTVAL; i++)
45 ptrs[i] = GNUNET_malloc (i);
46 for (j = 0; j < i; j++)
50 for (i = MAX_TESTVAL - 1; i >= 1; i--)
52 for (j = i - 1; j >= 0; j--)
53 if (ptrs[i][j] != (char) --k)
55 GNUNET_free (ptrs[i]);
58 /* GNUNET_free_non_null test */
59 GNUNET_free_non_null (NULL);
60 GNUNET_free_non_null (GNUNET_malloc (4));
62 /* GNUNET_strdup tests */
63 ptrs[0] = GNUNET_strdup ("bar");
64 if (0 != strcmp (ptrs[0], "bar"))
67 ptrs[0] = GNUNET_realloc (ptrs[0], 12);
68 strcpy (ptrs[0], "Hello World");
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]);
75 /* GNUNET_array_grow tests */
78 GNUNET_array_grow (ptrs[0], ui, 42);
81 GNUNET_array_grow (ptrs[0], ui, 22);
84 for (j = 0; j < 22; j++)
86 GNUNET_array_grow (ptrs[0], ui, 32);
87 for (j = 0; j < 22; j++)
90 for (j = 22; j < 32; j++)
93 GNUNET_array_grow (ptrs[0], ui, 0);
99 /* GNUNET_new_array_2d tests */
100 a2 = GNUNET_new_array_2d (17, 22, unsigned int);
101 for (i = 0; i < 17; i++)
103 for (j = 0; j < 22; j++)
110 a2[i][j] = i * 100 + j;
115 /* GNUNET_new_array_3d tests */
116 a3 = GNUNET_new_array_3d (2, 3, 4, char);
117 for (i = 0; i < 2; i++)
119 for (j = 0; j < 3; j++)
121 for (k = 0; k < 4; k++)
123 if (0 != a3[i][j][k])
128 a3[i][j][k] = i * 100 + j * 10 + k;
138 main (int argc, char *argv[])
142 GNUNET_log_setup ("test-common-allocation",
153 /* end of test_common_allocation.c */