]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-alloc-util.c
libudev: hide definition of struct udev_list from other libudev components
[thirdparty/systemd.git] / src / test / test-alloc-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
b66de1f9 2
41364475 3#include <malloc.h>
f30788ee
YW
4#include <stdint.h>
5
b66de1f9
RC
6#include "alloc-util.h"
7#include "macro.h"
0a970718 8#include "memory-util.h"
e36ddc67 9#include "tests.h"
b66de1f9
RC
10
11static void test_alloca(void) {
12 static const uint8_t zero[997] = { };
13 char *t;
14
15 t = alloca_align(17, 512);
16 assert_se(!((uintptr_t)t & 0xff));
17 memzero(t, 17);
18
19 t = alloca0_align(997, 1024);
20 assert_se(!((uintptr_t)t & 0x1ff));
21 assert_se(!memcmp(t, zero, 997));
22}
23
cc99274d
ZJS
24static void test_GREEDY_REALLOC(void) {
25 _cleanup_free_ int *a = NULL, *b = NULL;
41364475 26 size_t n_allocated = 0, i, j;
cc99274d 27
41364475 28 /* Give valgrind a chance to verify our realloc() operations */
cc99274d 29
41364475 30 for (i = 0; i < 20480; i++) {
cc99274d 31 assert_se(GREEDY_REALLOC(a, n_allocated, i + 1));
41364475
LP
32 assert_se(n_allocated >= i + 1);
33 assert_se(malloc_usable_size(a) >= (i + 1) * sizeof(int));
34 a[i] = (int) i;
cc99274d 35 assert_se(GREEDY_REALLOC(a, n_allocated, i / 2));
41364475
LP
36 assert_se(n_allocated >= i / 2);
37 assert_se(malloc_usable_size(a) >= (i / 2) * sizeof(int));
cc99274d
ZJS
38 }
39
41364475
LP
40 for (j = 0; j < i / 2; j++)
41 assert_se(a[j] == (int) j);
42
43 for (i = 30, n_allocated = 0; i < 20480; i += 7) {
cc99274d 44 assert_se(GREEDY_REALLOC(b, n_allocated, i + 1));
41364475
LP
45 assert_se(n_allocated >= i + 1);
46 assert_se(malloc_usable_size(b) >= (i + 1) * sizeof(int));
47 b[i] = (int) i;
cc99274d 48 assert_se(GREEDY_REALLOC(b, n_allocated, i / 2));
41364475
LP
49 assert_se(n_allocated >= i / 2);
50 assert_se(malloc_usable_size(b) >= (i / 2) * sizeof(int));
cc99274d 51 }
41364475
LP
52
53 for (j = 30; j < i / 2; j += 7)
54 assert_se(b[j] == (int) j);
cc99274d
ZJS
55}
56
f30788ee 57static void test_memdup_multiply_and_greedy_realloc(void) {
b66de1f9 58 int org[] = {1, 2, 3};
f30788ee
YW
59 _cleanup_free_ int *dup;
60 int *p;
61 size_t i, allocated = 3;
b66de1f9 62
f30788ee 63 dup = (int*) memdup_suffix0_multiply(org, sizeof(int), 3);
b66de1f9
RC
64 assert_se(dup);
65 assert_se(dup[0] == 1);
66 assert_se(dup[1] == 2);
67 assert_se(dup[2] == 3);
f30788ee 68 assert_se(*(uint8_t*) (dup + 3) == (uint8_t) 0);
b66de1f9 69 free(dup);
f30788ee
YW
70
71 dup = (int*) memdup_multiply(org, sizeof(int), 3);
72 assert_se(dup);
73 assert_se(dup[0] == 1);
74 assert_se(dup[1] == 2);
75 assert_se(dup[2] == 3);
76
77 p = dup;
78 assert_se(greedy_realloc0((void**) &dup, &allocated, 2, sizeof(int)) == p);
79
80 p = (int *) greedy_realloc0((void**) &dup, &allocated, 10, sizeof(int));
81 assert_se(p == dup);
82 assert_se(allocated >= 10);
83 assert_se(p[0] == 1);
84 assert_se(p[1] == 2);
85 assert_se(p[2] == 3);
86 for (i = 3; i < allocated; i++)
87 assert_se(p[i] == 0);
b66de1f9
RC
88}
89
37e744e8 90static void test_bool_assign(void) {
108ccae9 91 bool b, c, *cp = &c, d, e, f, g, h;
37e744e8
ZJS
92
93 b = 123;
94 *cp = -11;
95 d = 0xF & 0xFF;
96 e = b & d;
97 f = 0x0;
108ccae9
ZJS
98 g = cp; /* cast from pointer */
99 h = NULL; /* cast from pointer */
37e744e8
ZJS
100
101 assert(b);
102 assert(c);
103 assert(d);
104 assert(e);
105 assert(!f);
108ccae9
ZJS
106 assert(g);
107 assert(!h);
37e744e8
ZJS
108}
109
e36ddc67
ZJS
110static int cleanup_counter = 0;
111
112static void cleanup1(void *a) {
113 log_info("%s(%p)", __func__, a);
114 assert_se(++cleanup_counter == *(int*) a);
115}
116static void cleanup2(void *a) {
117 log_info("%s(%p)", __func__, a);
118 assert_se(++cleanup_counter == *(int*) a);
119}
120static void cleanup3(void *a) {
121 log_info("%s(%p)", __func__, a);
122 assert_se(++cleanup_counter == *(int*) a);
123}
124
125static void test_cleanup_order(void) {
126 _cleanup_(cleanup1) int x1 = 4, x2 = 3;
127 _cleanup_(cleanup3) int z = 2;
128 _cleanup_(cleanup2) int y = 1;
129 log_debug("x1: %p", &x1);
130 log_debug("x2: %p", &x2);
131 log_debug("y: %p", &y);
132 log_debug("z: %p", &z);
133}
134
b66de1f9 135int main(int argc, char *argv[]) {
e36ddc67
ZJS
136 test_setup_logging(LOG_DEBUG);
137
b66de1f9 138 test_alloca();
cc99274d 139 test_GREEDY_REALLOC();
f30788ee 140 test_memdup_multiply_and_greedy_realloc();
37e744e8 141 test_bool_assign();
e36ddc67 142 test_cleanup_order();
b66de1f9
RC
143
144 return 0;
145}