]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-alloc-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
7 #include "memory-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_GREEDY_REALLOC(void) {
23 _cleanup_free_
int *a
= NULL
, *b
= NULL
;
24 size_t n_allocated
= 0, i
;
26 /* Give valgrind a chance to verify our realloc operations */
28 for (i
= 0; i
< 2048; i
++) {
29 assert_se(GREEDY_REALLOC(a
, n_allocated
, i
+ 1));
31 assert_se(GREEDY_REALLOC(a
, n_allocated
, i
/ 2));
34 for (i
= 30, n_allocated
= 0; i
< 2048; i
+=7) {
35 assert_se(GREEDY_REALLOC(b
, n_allocated
, i
+ 1));
37 assert_se(GREEDY_REALLOC(b
, n_allocated
, i
/ 2));
41 static void test_memdup_multiply_and_greedy_realloc(void) {
42 int org
[] = {1, 2, 3};
43 _cleanup_free_
int *dup
;
45 size_t i
, allocated
= 3;
47 dup
= (int*) memdup_suffix0_multiply(org
, sizeof(int), 3);
49 assert_se(dup
[0] == 1);
50 assert_se(dup
[1] == 2);
51 assert_se(dup
[2] == 3);
52 assert_se(*(uint8_t*) (dup
+ 3) == (uint8_t) 0);
55 dup
= (int*) memdup_multiply(org
, sizeof(int), 3);
57 assert_se(dup
[0] == 1);
58 assert_se(dup
[1] == 2);
59 assert_se(dup
[2] == 3);
62 assert_se(greedy_realloc0((void**) &dup
, &allocated
, 2, sizeof(int)) == p
);
64 p
= (int *) greedy_realloc0((void**) &dup
, &allocated
, 10, sizeof(int));
66 assert_se(allocated
>= 10);
70 for (i
= 3; i
< allocated
; i
++)
74 static void test_bool_assign(void) {
75 bool b
, c
, *cp
= &c
, d
, e
, f
, g
, h
;
82 g
= cp
; /* cast from pointer */
83 h
= NULL
; /* cast from pointer */
94 int main(int argc
, char *argv
[]) {
96 test_GREEDY_REALLOC();
97 test_memdup_multiply_and_greedy_realloc();