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