2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2005, 2006 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"
31 #define MAX_TESTVAL 1024
32 char *ptrs[MAX_TESTVAL];
40 /* GNUNET_malloc/GNUNET_free test */
41 k = 352; /* random start value */
42 for (i = 1; i < MAX_TESTVAL; i++)
44 ptrs[i] = GNUNET_malloc (i);
45 for (j = 0; j < i; j++)
49 for (i = MAX_TESTVAL - 1; i >= 1; i--)
51 for (j = i - 1; j >= 0; j--)
52 if (ptrs[i][j] != (char) --k)
54 GNUNET_free (ptrs[i]);
57 /* GNUNET_free_non_null test */
58 GNUNET_free_non_null (NULL);
59 GNUNET_free_non_null (GNUNET_malloc (4));
61 /* GNUNET_strdup tests */
62 ptrs[0] = GNUNET_strdup ("bar");
63 if (0 != strcmp (ptrs[0], "bar"))
66 ptrs[0] = GNUNET_realloc (ptrs[0], 12);
67 strcpy (ptrs[0], "Hello World");
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]);
74 /* GNUNET_array_grow tests */
77 GNUNET_array_grow (ptrs[0], ui, 42);
80 GNUNET_array_grow (ptrs[0], ui, 22);
83 for (j = 0; j < 22; j++)
85 GNUNET_array_grow (ptrs[0], ui, 32);
86 for (j = 0; j < 22; j++)
89 for (j = 22; j < 32; j++)
92 GNUNET_array_grow (ptrs[0], ui, 0);
98 /* GNUNET_new_array_2d tests */
99 a2 = GNUNET_new_array_2d (17, 22, unsigned int);
100 for (i = 0; i < 17; i++)
102 for (j = 0; j < 22; j++)
106 a2[i][j] = i * 100 + j;
111 /* GNUNET_new_array_3d tests */
112 a3 = GNUNET_new_array_3d (2, 3, 4, char);
113 for (i = 0; i < 2; i++)
115 for (j = 0; j < 3; j++)
117 for (k = 0; k < 4; k++)
119 if (0 != a3[i][j][k])
121 a3[i][j][k] = i * 100 + j * 10 + k;
131 main (int argc, char *argv[])
135 GNUNET_log_setup ("test-common-allocation", "WARNING", NULL);
138 FPRINTF (stderr, "ERROR %d.\n", ret);
142 /* end of test_common_allocation.c */