1 /* Shared definitions for dynarray tests.
2    Copyright (C) 2017-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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    <https://www.gnu.org/licenses/>.  */
18 
19 #include <stddef.h>
20 
21 struct int_array
22 {
23   int *array;
24   size_t length;
25 };
26 
27 #define DYNARRAY_STRUCT dynarray_int
28 #define DYNARRAY_ELEMENT int
29 #define DYNARRAY_PREFIX dynarray_int_
30 #define DYNARRAY_FINAL_TYPE struct int_array
31 #include <malloc/dynarray-skeleton.c>
32 
33 struct str_array
34 {
35   char **array;
36   size_t length;
37 };
38 
39 #define DYNARRAY_STRUCT dynarray_str
40 #define DYNARRAY_ELEMENT char *
41 #define DYNARRAY_ELEMENT_FREE(ptr) free (*ptr)
42 #define DYNARRAY_PREFIX dynarray_str_
43 #define DYNARRAY_FINAL_TYPE struct str_array
44 #include <malloc/dynarray-skeleton.c>
45 
46 /* Check that *DYN is equivalent to its initial state.  */
47 #define CHECK_INIT_STATE(type, dyn)                             \
48   ({                                                            \
49     TEST_VERIFY_EXIT (!dynarray_##type##_has_failed (dyn));     \
50     TEST_VERIFY_EXIT (dynarray_##type##_size (dyn) == 0);       \
51     TEST_VERIFY_EXIT ((dyn)->u.dynarray_header.array            \
52                       == (dyn)->scratch);                       \
53     TEST_VERIFY_EXIT ((dyn)->u.dynarray_header.allocated > 0);  \
54     (void) 0;                                                   \
55   })
56 
57 /* Check that *DYN behaves as if it is in its initial state.  */
58 #define CHECK_EMPTY(type, dyn)                                       \
59   ({                                                                 \
60     CHECK_INIT_STATE (type, (dyn));                                  \
61     dynarray_##type##_free (dyn);                                    \
62     CHECK_INIT_STATE (type, (dyn));                                  \
63     dynarray_##type##_clear (dyn);                                   \
64     CHECK_INIT_STATE (type, (dyn));                                  \
65     dynarray_##type##_remove_last (dyn);                             \
66     CHECK_INIT_STATE (type, (dyn));                                  \
67     dynarray_##type##_mark_failed (dyn);                             \
68     TEST_VERIFY_EXIT (dynarray_##type##_has_failed (dyn));           \
69     dynarray_##type##_clear (dyn);                                   \
70     TEST_VERIFY_EXIT (dynarray_##type##_has_failed (dyn));           \
71     dynarray_##type##_remove_last (dyn);                             \
72     TEST_VERIFY_EXIT (dynarray_##type##_has_failed (dyn));           \
73     TEST_VERIFY_EXIT (dynarray_##type##_emplace (dyn) == NULL);      \
74     dynarray_##type##_free (dyn);                                    \
75     CHECK_INIT_STATE (type, (dyn));                                  \
76     /* These functions should not assert.  */                        \
77     dynarray_##type##_begin (dyn);                                   \
78     dynarray_##type##_end (dyn);                                     \
79     (void) 0;                                                        \
80   })
81