aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_common_allocation.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_common_allocation.c')
-rw-r--r--src/util/test_common_allocation.c177
1 files changed, 0 insertions, 177 deletions
diff --git a/src/util/test_common_allocation.c b/src/util/test_common_allocation.c
deleted file mode 100644
index 50f259257..000000000
--- a/src/util/test_common_allocation.c
+++ /dev/null
@@ -1,177 +0,0 @@
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
29static int
30check (void)
31{
32#define MAX_TESTVAL 1024
33 char *ptrs[MAX_TESTVAL];
34 unsigned int **a2;
35 char ***a3;
36 char *tmp;
37 int i;
38 int j;
39 int k;
40 unsigned int ui;
41
42 /* GNUNET_malloc/GNUNET_free test */
43 k = 352; /* random start value */
44 for (i = 1; i < MAX_TESTVAL; i++)
45 {
46 ptrs[i] = GNUNET_malloc (i);
47 for (j = 0; j < i; j++)
48 ptrs[i][j] = k++;
49 }
50
51 for (i = MAX_TESTVAL - 1; i >= 1; i--)
52 {
53 for (j = i - 1; j >= 0; j--)
54 if (ptrs[i][j] != (char) --k)
55 return 1;
56 GNUNET_free (ptrs[i]);
57 }
58
59 /* GNUNET_free test */
60 tmp = GNUNET_malloc (4);
61 GNUNET_free (tmp);
62
63 /* GNUNET_strdup tests */
64 ptrs[0] = GNUNET_strdup ("bar");
65 if (0 != strcmp (ptrs[0], "bar"))
66 return 3;
67 /* now realloc */
68 ptrs[0] = GNUNET_realloc (ptrs[0], 12);
69 strcpy (ptrs[0], "Hello World");
70
71 GNUNET_free (ptrs[0]);
72 GNUNET_asprintf (&ptrs[0], "%s %s", "Hello", "World");
73 GNUNET_assert (strlen (ptrs[0]) == 11);
74 GNUNET_free (ptrs[0]);
75
76 /* GNUNET_array_grow tests */
77 ptrs[0] = NULL;
78 ui = 0;
79 GNUNET_array_grow (ptrs[0], ui, 42);
80 if (ui != 42)
81 return 4;
82 GNUNET_array_grow (ptrs[0], ui, 22);
83 if (ui != 22)
84 return 5;
85 for (j = 0; j < 22; j++)
86 ptrs[0][j] = j;
87 GNUNET_array_grow (ptrs[0], ui, 32);
88 for (j = 0; j < 22; j++)
89 if (ptrs[0][j] != j)
90 return 6;
91 for (j = 22; j < 32; j++)
92 if (ptrs[0][j] != 0)
93 return 7;
94 GNUNET_array_grow (ptrs[0], ui, 0);
95 if (i != 0)
96 return 8;
97 if (ptrs[0] != NULL)
98 return 9;
99
100 /* GNUNET_new_array_2d tests */
101 a2 = GNUNET_new_array_2d (17, 22, unsigned int);
102 for (i = 0; i < 17; i++)
103 {
104 for (j = 0; j < 22; j++)
105 {
106 if (0 != a2[i][j])
107 {
108 GNUNET_free (a2);
109 return 10;
110 }
111 a2[i][j] = i * 100 + j;
112 }
113 }
114 GNUNET_free (a2);
115
116 /* GNUNET_new_array_3d tests */
117 a3 = GNUNET_new_array_3d (2, 3, 4, char);
118 for (i = 0; i < 2; i++)
119 {
120 for (j = 0; j < 3; j++)
121 {
122 for (k = 0; k < 4; k++)
123 {
124 if (0 != a3[i][j][k])
125 {
126 GNUNET_free (a3);
127 return 11;
128 }
129 a3[i][j][k] = i * 100 + j * 10 + k;
130 }
131 }
132 }
133 GNUNET_free (a3);
134 return 0;
135}
136
137
138static int
139check2 (void)
140{
141 char *a1 = NULL;
142 unsigned int a1_len = 0;
143 const char *a2 = "test";
144
145 GNUNET_array_append (a1,
146 a1_len,
147 'x');
148 GNUNET_array_concatenate (a1,
149 a1_len,
150 a2,
151 4);
152 GNUNET_assert (0 == strncmp ("xtest",
153 a1,
154 5));
155 GNUNET_assert (5 == a1_len);
156 return 0;
157}
158
159
160int
161main (int argc, char *argv[])
162{
163 int ret;
164
165 GNUNET_log_setup ("test-common-allocation",
166 "WARNING",
167 NULL);
168 ret = check () | check2 ();
169 if (ret != 0)
170 fprintf (stderr,
171 "ERROR %d.\n",
172 ret);
173 return ret;
174}
175
176
177/* end of test_common_allocation.c */