1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
9 static void test_alloca(void) {
10 static const uint8_t zero[997] = { };
13 t = alloca_align(17, 512);
14 assert_se(!((uintptr_t)t & 0xff));
17 t = alloca0_align(997, 1024);
18 assert_se(!((uintptr_t)t & 0x1ff));
19 assert_se(!memcmp(t, zero, 997));
22 static void test_memdup_multiply_and_greedy_realloc(void) {
23 int org[] = {1, 2, 3};
24 _cleanup_free_ int *dup;
26 size_t i, allocated = 3;
28 dup = (int*) memdup_suffix0_multiply(org, sizeof(int), 3);
30 assert_se(dup[0] == 1);
31 assert_se(dup[1] == 2);
32 assert_se(dup[2] == 3);
33 assert_se(*(uint8_t*) (dup + 3) == (uint8_t) 0);
36 dup = (int*) memdup_multiply(org, sizeof(int), 3);
38 assert_se(dup[0] == 1);
39 assert_se(dup[1] == 2);
40 assert_se(dup[2] == 3);
43 assert_se(greedy_realloc0((void**) &dup, &allocated, 2, sizeof(int)) == p);
45 p = (int *) greedy_realloc0((void**) &dup, &allocated, 10, sizeof(int));
47 assert_se(allocated >= 10);
51 for (i = 3; i < allocated; i++)
55 static void test_bool_assign(void) {
56 bool b, c, *cp = &c, d, e, f, g, h;
63 g = cp; /* cast from pointer */
64 h = NULL; /* cast from pointer */
75 int main(int argc, char *argv[]) {
77 test_memdup_multiply_and_greedy_realloc();