1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
8 #include "alloc-util.h"
12 static void test_alloca(void) {
13 static const uint8_t zero[997] = { };
16 t = alloca_align(17, 512);
17 assert_se(!((uintptr_t)t & 0xff));
20 t = alloca0_align(997, 1024);
21 assert_se(!((uintptr_t)t & 0x1ff));
22 assert_se(!memcmp(t, zero, 997));
25 static void test_memdup_multiply_and_greedy_realloc(void) {
26 int org[] = {1, 2, 3};
27 _cleanup_free_ int *dup;
29 size_t i, allocated = 3;
31 dup = (int*) memdup_suffix0_multiply(org, sizeof(int), 3);
33 assert_se(dup[0] == 1);
34 assert_se(dup[1] == 2);
35 assert_se(dup[2] == 3);
36 assert_se(*(uint8_t*) (dup + 3) == (uint8_t) 0);
39 dup = (int*) memdup_multiply(org, sizeof(int), 3);
41 assert_se(dup[0] == 1);
42 assert_se(dup[1] == 2);
43 assert_se(dup[2] == 3);
46 assert_se(greedy_realloc0((void**) &dup, &allocated, 2, sizeof(int)) == p);
48 p = (int *) greedy_realloc0((void**) &dup, &allocated, 10, sizeof(int));
50 assert_se(allocated >= 10);
54 for (i = 3; i < allocated; i++)
58 static void test_bool_assign(void) {
59 bool b, c, *cp = &c, d, e, f, g, h;
66 g = cp; /* cast from pointer */
67 h = NULL; /* cast from pointer */
78 int main(int argc, char *argv[]) {
80 test_memdup_multiply_and_greedy_realloc();