1 /* Test for dynamic arrays.
2 Copyright (C) 2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include "tst-dynarray-shared.h"
21 #define DYNARRAY_STRUCT dynarray_long
22 #define DYNARRAY_ELEMENT long
23 #define DYNARRAY_PREFIX dynarray_long_
24 #define DYNARRAY_ELEMENT_INIT(e) (*(e) = 17)
25 #include <malloc/dynarray-skeleton.c>
33 #define DYNARRAY_STRUCT dynarray_long_noscratch
34 #define DYNARRAY_ELEMENT long
35 #define DYNARRAY_PREFIX dynarray_long_noscratch_
36 #define DYNARRAY_ELEMENT_INIT(e) (*(e) = 23)
37 #define DYNARRAY_FINAL_TYPE struct long_array
38 #define DYNARRAY_INITIAL_SIZE 0
39 #include <malloc/dynarray-skeleton.c>
41 #define DYNARRAY_STRUCT zstr
42 #define DYNARRAY_ELEMENT char
43 #define DYNARRAY_PREFIX zstr_
44 #define DYNARRAY_INITIAL_SIZE 128
45 #include <malloc/dynarray-skeleton.c>
50 #include <support/check.h>
51 #include <support/support.h>
53 enum { max_count
= 20 };
55 /* Test dynamic arrays with int elements (no automatic deallocation
62 struct dynarray_int dyn
;
63 dynarray_int_init (&dyn
);
64 CHECK_EMPTY (int, &dyn
);
67 /* Empty array with finalization. */
69 struct dynarray_int dyn
;
70 dynarray_int_init (&dyn
);
71 CHECK_INIT_STATE (int, &dyn
);
72 struct int_array result
= { (int *) (uintptr_t) -1, -1 };
73 TEST_VERIFY_EXIT (dynarray_int_finalize (&dyn
, &result
));
74 CHECK_INIT_STATE (int, &dyn
);
75 TEST_VERIFY_EXIT (result
.array
== NULL
);
76 TEST_VERIFY_EXIT (result
.length
== 0);
79 /* Non-empty array tests.
81 do_add: Switch between emplace (false) and add (true).
82 do_finalize: Perform finalize call at the end.
83 do_clear: Perform clear call at the end.
84 do_remove_last: Perform remove_last call after adding elements.
85 count: Number of elements added to the array. */
86 for (int do_add
= 0; do_add
< 2; ++do_add
)
87 for (int do_finalize
= 0; do_finalize
< 2; ++do_finalize
)
88 for (int do_clear
= 0; do_clear
< 2; ++do_clear
)
89 for (int do_remove_last
= 0; do_remove_last
< 2; ++do_remove_last
)
90 for (unsigned int count
= 0; count
< max_count
; ++count
)
92 if (do_remove_last
&& count
== 0)
94 unsigned int base
= count
* count
;
95 struct dynarray_int dyn
;
96 dynarray_int_init (&dyn
);
97 for (unsigned int i
= 0; i
< count
; ++i
)
100 dynarray_int_add (&dyn
, base
+ i
);
103 int *place
= dynarray_int_emplace (&dyn
);
104 TEST_VERIFY_EXIT (place
!= NULL
);
107 TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn
));
108 TEST_VERIFY_EXIT (dynarray_int_size (&dyn
) == i
+ 1);
109 TEST_VERIFY_EXIT (dynarray_int_size (&dyn
)
110 <= dyn
.dynarray_header
.allocated
);
112 TEST_VERIFY_EXIT (dynarray_int_size (&dyn
) == count
);
113 TEST_VERIFY_EXIT (count
<= dyn
.dynarray_header
.allocated
);
116 TEST_VERIFY (dynarray_int_begin (&dyn
)
117 == dynarray_int_at (&dyn
, 0));
118 TEST_VERIFY (dynarray_int_end (&dyn
)
119 == dynarray_int_at (&dyn
, count
- 1) + 1);
121 unsigned final_count
;
122 bool heap_array
= dyn
.dynarray_header
.array
!= dyn
.scratch
;
125 dynarray_int_remove_last (&dyn
);
129 final_count
= count
- 1;
135 TEST_VERIFY (dynarray_int_begin (&dyn
)
136 == dynarray_int_at (&dyn
, 0));
137 TEST_VERIFY (dynarray_int_end (&dyn
)
138 == dynarray_int_at (&dyn
, final_count
- 1) + 1);
142 dynarray_int_clear (&dyn
);
145 TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn
));
146 TEST_VERIFY_EXIT ((dyn
.dynarray_header
.array
!= dyn
.scratch
)
148 TEST_VERIFY_EXIT (dynarray_int_size (&dyn
) == final_count
);
149 TEST_VERIFY_EXIT (dyn
.dynarray_header
.allocated
>= final_count
);
151 for (unsigned int i
= 0; i
< final_count
; ++i
)
152 TEST_VERIFY_EXIT (*dynarray_int_at (&dyn
, i
) == base
+ i
);
155 struct int_array result
= { (int *) (uintptr_t) -1, -1 };
156 TEST_VERIFY_EXIT (dynarray_int_finalize (&dyn
, &result
));
157 CHECK_INIT_STATE (int, &dyn
);
158 TEST_VERIFY_EXIT (result
.length
== final_count
);
159 if (final_count
== 0)
160 TEST_VERIFY_EXIT (result
.array
== NULL
);
163 TEST_VERIFY_EXIT (result
.array
!= NULL
);
164 TEST_VERIFY_EXIT (result
.array
!= (int *) (uintptr_t) -1);
166 (malloc_usable_size (result
.array
)
167 >= final_count
* sizeof (result
.array
[0]));
168 for (unsigned int i
= 0; i
< final_count
; ++i
)
169 TEST_VERIFY_EXIT (result
.array
[i
] == base
+ i
);
173 else /* !do_finalize */
175 dynarray_int_free (&dyn
);
176 CHECK_INIT_STATE (int, &dyn
);
181 /* Test dynamic arrays with char * elements (with automatic
182 deallocation of the pointed-to strings). */
188 struct dynarray_str dyn
;
189 dynarray_str_init (&dyn
);
190 CHECK_EMPTY (str
, &dyn
);
193 /* Empty array with finalization. */
195 struct dynarray_str dyn
;
196 dynarray_str_init (&dyn
);
197 TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn
));
198 struct str_array result
= { (char **) (uintptr_t) -1, -1 };
199 TEST_VERIFY_EXIT (dynarray_str_finalize (&dyn
, &result
));
200 CHECK_INIT_STATE (str
, &dyn
);
201 TEST_VERIFY_EXIT (result
.array
== NULL
);
202 TEST_VERIFY_EXIT (result
.length
== 0);
205 /* Non-empty array tests.
207 do_add: Switch between emplace (false) and add (true).
208 do_finalize: Perform finalize call at the end.
209 do_clear: Perform clear call at the end.
210 do_remove_last: Perform remove_last call after adding elements.
211 count: Number of elements added to the array. */
212 for (int do_add
= 0; do_add
< 2; ++do_add
)
213 for (int do_finalize
= 0; do_finalize
< 2; ++do_finalize
)
214 for (int do_clear
= 0; do_clear
< 2; ++do_clear
)
215 for (int do_remove_last
= 0; do_remove_last
< 2; ++do_remove_last
)
216 for (unsigned int count
= 0; count
< max_count
; ++count
)
218 if (do_remove_last
&& count
== 0)
220 unsigned int base
= count
* count
;
221 struct dynarray_str dyn
;
222 dynarray_str_init (&dyn
);
223 for (unsigned int i
= 0; i
< count
; ++i
)
225 char *item
= xasprintf ("%d", base
+ i
);
227 dynarray_str_add (&dyn
, item
);
230 char **place
= dynarray_str_emplace (&dyn
);
231 TEST_VERIFY_EXIT (place
!= NULL
);
232 TEST_VERIFY_EXIT (*place
== NULL
);
235 TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn
));
236 TEST_VERIFY_EXIT (dynarray_str_size (&dyn
) == i
+ 1);
237 TEST_VERIFY_EXIT (dynarray_str_size (&dyn
)
238 <= dyn
.dynarray_header
.allocated
);
240 TEST_VERIFY_EXIT (dynarray_str_size (&dyn
) == count
);
241 TEST_VERIFY_EXIT (count
<= dyn
.dynarray_header
.allocated
);
244 TEST_VERIFY (dynarray_str_begin (&dyn
)
245 == dynarray_str_at (&dyn
, 0));
246 TEST_VERIFY (dynarray_str_end (&dyn
)
247 == dynarray_str_at (&dyn
, count
- 1) + 1);
249 unsigned final_count
;
250 bool heap_array
= dyn
.dynarray_header
.array
!= dyn
.scratch
;
253 dynarray_str_remove_last (&dyn
);
257 final_count
= count
- 1;
263 TEST_VERIFY (dynarray_str_begin (&dyn
)
264 == dynarray_str_at (&dyn
, 0));
265 TEST_VERIFY (dynarray_str_end (&dyn
)
266 == dynarray_str_at (&dyn
, final_count
- 1) + 1);
270 dynarray_str_clear (&dyn
);
273 TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn
));
274 TEST_VERIFY_EXIT ((dyn
.dynarray_header
.array
!= dyn
.scratch
)
276 TEST_VERIFY_EXIT (dynarray_str_size (&dyn
) == final_count
);
277 TEST_VERIFY_EXIT (dyn
.dynarray_header
.allocated
>= final_count
);
279 for (unsigned int i
= 0; i
< count
- do_remove_last
; ++i
)
281 char *expected
= xasprintf ("%d", base
+ i
);
282 const char *actual
= *dynarray_str_at (&dyn
, i
);
283 TEST_VERIFY_EXIT (strcmp (actual
, expected
) == 0);
288 struct str_array result
= { (char **) (uintptr_t) -1, -1 };
289 TEST_VERIFY_EXIT (dynarray_str_finalize (&dyn
, &result
));
290 CHECK_INIT_STATE (str
, &dyn
);
291 TEST_VERIFY_EXIT (result
.length
== final_count
);
292 if (final_count
== 0)
293 TEST_VERIFY_EXIT (result
.array
== NULL
);
296 TEST_VERIFY_EXIT (result
.array
!= NULL
);
297 TEST_VERIFY_EXIT (result
.array
298 != (char **) (uintptr_t) -1);
299 TEST_VERIFY_EXIT (result
.length
300 == count
- do_remove_last
);
302 (malloc_usable_size (result
.array
)
303 >= final_count
* sizeof (result
.array
[0]));
304 for (unsigned int i
= 0; i
< count
- do_remove_last
; ++i
)
306 char *expected
= xasprintf ("%d", base
+ i
);
307 char *actual
= result
.array
[i
];
308 TEST_VERIFY_EXIT (strcmp (actual
, expected
) == 0);
315 else /* !do_finalize */
317 dynarray_str_free (&dyn
);
318 CHECK_INIT_STATE (str
, &dyn
);
324 enum { count
= 2131 };
325 struct dynarray_str dyn
;
326 dynarray_str_init (&dyn
);
328 /* From length 0 to length 1. */
329 TEST_VERIFY (dynarray_str_resize (&dyn
, 1));
330 TEST_VERIFY (dynarray_str_size (&dyn
) == 1);
331 TEST_VERIFY (*dynarray_str_at (&dyn
, 0) == NULL
);
332 *dynarray_str_at (&dyn
, 0) = xstrdup ("allocated");
333 dynarray_str_free (&dyn
);
335 /* From length 0 to length 1 and 2. */
336 TEST_VERIFY (dynarray_str_resize (&dyn
, 1));
337 TEST_VERIFY (dynarray_str_size (&dyn
) == 1);
338 TEST_VERIFY (*dynarray_str_at (&dyn
, 0) == NULL
);
339 *dynarray_str_at (&dyn
, 0) = xstrdup ("allocated0");
340 TEST_VERIFY (dynarray_str_resize (&dyn
, 2));
341 TEST_VERIFY (dynarray_str_size (&dyn
) == 2);
342 TEST_VERIFY (strcmp (*dynarray_str_at (&dyn
, 0), "allocated0") == 0);
343 TEST_VERIFY (*dynarray_str_at (&dyn
, 1) == NULL
);
344 *dynarray_str_at (&dyn
, 1) = xstrdup ("allocated1");
345 TEST_VERIFY (dynarray_str_resize (&dyn
, count
));
346 TEST_VERIFY (dynarray_str_size (&dyn
) == count
);
347 TEST_VERIFY (strcmp (*dynarray_str_at (&dyn
, 0), "allocated0") == 0);
348 TEST_VERIFY (strcmp (*dynarray_str_at (&dyn
, 1), "allocated1") == 0);
349 for (int i
= 2; i
< count
; ++i
)
350 TEST_VERIFY (*dynarray_str_at (&dyn
, i
) == NULL
);
351 *dynarray_str_at (&dyn
, count
- 1) = xstrdup ("allocated2");
352 TEST_VERIFY (dynarray_str_resize (&dyn
, 3));
353 TEST_VERIFY (strcmp (*dynarray_str_at (&dyn
, 0), "allocated0") == 0);
354 TEST_VERIFY (strcmp (*dynarray_str_at (&dyn
, 1), "allocated1") == 0);
355 TEST_VERIFY (*dynarray_str_at (&dyn
, 2) == NULL
);
356 dynarray_str_free (&dyn
);
360 /* Verify that DYNARRAY_ELEMENT_INIT has an effect. */
362 test_long_init (void)
364 enum { count
= 2131 };
366 struct dynarray_long dyn
;
367 dynarray_long_init (&dyn
);
368 for (int i
= 0; i
< count
; ++i
)
370 long *place
= dynarray_long_emplace (&dyn
);
371 TEST_VERIFY_EXIT (place
!= NULL
);
372 TEST_VERIFY (*place
== 17);
374 TEST_VERIFY (dynarray_long_size (&dyn
) == count
);
375 for (int i
= 0; i
< count
; ++i
)
376 TEST_VERIFY (*dynarray_long_at (&dyn
, i
) == 17);
377 dynarray_long_free (&dyn
);
379 TEST_VERIFY (dynarray_long_resize (&dyn
, 1));
380 TEST_VERIFY (dynarray_long_size (&dyn
) == 1);
381 TEST_VERIFY (*dynarray_long_at (&dyn
, 0) == 17);
382 *dynarray_long_at (&dyn
, 0) = 18;
383 dynarray_long_free (&dyn
);
384 TEST_VERIFY (dynarray_long_resize (&dyn
, 1));
385 TEST_VERIFY (dynarray_long_size (&dyn
) == 1);
386 TEST_VERIFY (*dynarray_long_at (&dyn
, 0) == 17);
387 TEST_VERIFY (dynarray_long_resize (&dyn
, 2));
388 TEST_VERIFY (dynarray_long_size (&dyn
) == 2);
389 TEST_VERIFY (*dynarray_long_at (&dyn
, 0) == 17);
390 TEST_VERIFY (*dynarray_long_at (&dyn
, 1) == 17);
391 *dynarray_long_at (&dyn
, 0) = 18;
392 TEST_VERIFY (dynarray_long_resize (&dyn
, count
));
393 TEST_VERIFY (dynarray_long_size (&dyn
) == count
);
394 TEST_VERIFY (*dynarray_long_at (&dyn
, 0) == 18);
395 for (int i
= 1; i
< count
; ++i
)
396 TEST_VERIFY (*dynarray_long_at (&dyn
, i
) == 17);
397 dynarray_long_free (&dyn
);
400 /* Similar, but without an on-stack scratch region
401 (DYNARRAY_INITIAL_SIZE is 0). */
403 struct dynarray_long_noscratch dyn
;
404 dynarray_long_noscratch_init (&dyn
);
405 struct long_array result
;
406 TEST_VERIFY_EXIT (dynarray_long_noscratch_finalize (&dyn
, &result
));
407 TEST_VERIFY (result
.array
== NULL
);
408 TEST_VERIFY (result
.length
== 0);
410 /* Test with one element. */
412 long *place
= dynarray_long_noscratch_emplace (&dyn
);
413 TEST_VERIFY_EXIT (place
!= NULL
);
414 TEST_VERIFY (*place
== 23);
416 TEST_VERIFY (dynarray_long_noscratch_size (&dyn
) == 1);
417 TEST_VERIFY (*dynarray_long_noscratch_at (&dyn
, 0) == 23);
418 TEST_VERIFY_EXIT (dynarray_long_noscratch_finalize (&dyn
, &result
));
419 TEST_VERIFY_EXIT (result
.array
!= NULL
);
420 TEST_VERIFY (result
.length
== 1);
421 TEST_VERIFY (result
.array
[0] == 23);
424 for (int i
= 0; i
< count
; ++i
)
426 long *place
= dynarray_long_noscratch_emplace (&dyn
);
427 TEST_VERIFY_EXIT (place
!= NULL
);
428 TEST_VERIFY (*place
== 23);
432 TEST_VERIFY (dynarray_long_noscratch_size (&dyn
) == count
);
433 TEST_VERIFY (*dynarray_long_noscratch_at (&dyn
, 0) == 29);
434 for (int i
= 1; i
< count
; ++i
)
435 TEST_VERIFY (*dynarray_long_noscratch_at (&dyn
, i
) == 23);
436 TEST_VERIFY_EXIT (dynarray_long_noscratch_finalize (&dyn
, &result
));
437 TEST_VERIFY_EXIT (result
.array
!= NULL
);
438 TEST_VERIFY (result
.length
== count
);
439 TEST_VERIFY (result
.array
[0] == 29);
440 for (int i
= 1; i
< count
; ++i
)
441 TEST_VERIFY (result
.array
[i
] == 23);
444 TEST_VERIFY (dynarray_long_noscratch_resize (&dyn
, 1));
445 TEST_VERIFY (dynarray_long_noscratch_size (&dyn
) == 1);
446 TEST_VERIFY (*dynarray_long_noscratch_at (&dyn
, 0) == 23);
447 *dynarray_long_noscratch_at (&dyn
, 0) = 24;
448 dynarray_long_noscratch_free (&dyn
);
449 TEST_VERIFY (dynarray_long_noscratch_resize (&dyn
, 1));
450 TEST_VERIFY (dynarray_long_noscratch_size (&dyn
) == 1);
451 TEST_VERIFY (*dynarray_long_noscratch_at (&dyn
, 0) == 23);
452 TEST_VERIFY (dynarray_long_noscratch_resize (&dyn
, 2));
453 TEST_VERIFY (dynarray_long_noscratch_size (&dyn
) == 2);
454 TEST_VERIFY (*dynarray_long_noscratch_at (&dyn
, 0) == 23);
455 TEST_VERIFY (*dynarray_long_noscratch_at (&dyn
, 1) == 23);
456 *dynarray_long_noscratch_at (&dyn
, 0) = 24;
457 TEST_VERIFY (dynarray_long_noscratch_resize (&dyn
, count
));
458 TEST_VERIFY (dynarray_long_noscratch_size (&dyn
) == count
);
459 TEST_VERIFY (*dynarray_long_noscratch_at (&dyn
, 0) == 24);
460 for (int i
= 1; i
< count
; ++i
)
461 TEST_VERIFY (*dynarray_long_noscratch_at (&dyn
, i
) == 23);
462 dynarray_long_noscratch_free (&dyn
);
466 /* Test NUL-terminated string construction with the add function and
467 the simple finalize function. */
471 /* Totally empty string (no NUL termination). */
475 char *result
= zstr_finalize (&s
, NULL
);
476 TEST_VERIFY (result
== NULL
);
477 TEST_VERIFY (zstr_size (&s
) == 0);
479 result
= zstr_finalize (&s
, &length
);
480 TEST_VERIFY (result
== NULL
);
481 TEST_VERIFY (length
== 0);
482 TEST_VERIFY (zstr_size (&s
) == 0);
490 char *result
= zstr_finalize (&s
, NULL
);
491 TEST_VERIFY_EXIT (result
!= NULL
);
492 TEST_VERIFY (*result
== '\0');
493 TEST_VERIFY (zstr_size (&s
) == 0);
498 result
= zstr_finalize (&s
, &length
);
499 TEST_VERIFY_EXIT (result
!= NULL
);
500 TEST_VERIFY (*result
== '\0');
501 TEST_VERIFY (length
== 1);
502 TEST_VERIFY (zstr_size (&s
) == 0);
506 /* A few characters. */
514 char *result
= zstr_finalize (&s
, NULL
);
515 TEST_VERIFY_EXIT (result
!= NULL
);
516 TEST_VERIFY (strcmp (result
, "Abc") == 0);
517 TEST_VERIFY (zstr_size (&s
) == 0);
525 result
= zstr_finalize (&s
, &length
);
526 TEST_VERIFY_EXIT (result
!= NULL
);
527 TEST_VERIFY (strcmp (result
, "Xyz") == 0);
528 TEST_VERIFY (length
== 4);
529 TEST_VERIFY (zstr_size (&s
) == 0);
545 #include <support/test-driver.c>