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.
17 * @file util/test_common_allocation.c
18 * @brief testcase for common_allocation.c
21 #include "gnunet_util_lib.h"
27 #define MAX_TESTVAL 1024
28 char *ptrs[MAX_TESTVAL];
36 /* GNUNET_malloc/GNUNET_free test */
37 k = 352; /* random start value */
38 for (i = 1; i < MAX_TESTVAL; i++)
40 ptrs[i] = GNUNET_malloc (i);
41 for (j = 0; j < i; j++)
45 for (i = MAX_TESTVAL - 1; i >= 1; i--)
47 for (j = i - 1; j >= 0; j--)
48 if (ptrs[i][j] != (char) --k)
50 GNUNET_free (ptrs[i]);
53 /* GNUNET_free_non_null test */
54 GNUNET_free_non_null (NULL);
55 GNUNET_free_non_null (GNUNET_malloc (4));
57 /* GNUNET_strdup tests */
58 ptrs[0] = GNUNET_strdup ("bar");
59 if (0 != strcmp (ptrs[0], "bar"))
62 ptrs[0] = GNUNET_realloc (ptrs[0], 12);
63 strcpy (ptrs[0], "Hello World");
65 GNUNET_free (ptrs[0]);
66 GNUNET_asprintf (&ptrs[0], "%s %s", "Hello", "World");
67 GNUNET_assert (strlen (ptrs[0]) == 11);
68 GNUNET_free (ptrs[0]);
70 /* GNUNET_array_grow tests */
73 GNUNET_array_grow (ptrs[0], ui, 42);
76 GNUNET_array_grow (ptrs[0], ui, 22);
79 for (j = 0; j < 22; j++)
81 GNUNET_array_grow (ptrs[0], ui, 32);
82 for (j = 0; j < 22; j++)
85 for (j = 22; j < 32; j++)
88 GNUNET_array_grow (ptrs[0], ui, 0);
94 /* GNUNET_new_array_2d tests */
95 a2 = GNUNET_new_array_2d (17, 22, unsigned int);
96 for (i = 0; i < 17; i++)
98 for (j = 0; j < 22; j++)
105 a2[i][j] = i * 100 + j;
110 /* GNUNET_new_array_3d tests */
111 a3 = GNUNET_new_array_3d (2, 3, 4, char);
112 for (i = 0; i < 2; i++)
114 for (j = 0; j < 3; j++)
116 for (k = 0; k < 4; k++)
118 if (0 != a3[i][j][k])
123 a3[i][j][k] = i * 100 + j * 10 + k;
133 main (int argc, char *argv[])
137 GNUNET_log_setup ("test-common-allocation",
148 /* end of test_common_allocation.c */