chiark / gitweb /
d8258007925e76a14bee8fd5d38f5e2f82a87f96
[elogind.git] / src / test / test-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2010 Lennart Poettering
6   Copyright 2013 Thomas H.P. Andersen
7 ***/
8
9 #include <errno.h>
10 #include <string.h>
11 #include <sys/wait.h>
12 #include <unistd.h>
13
14 #include "def.h"
15 #include "fileio.h"
16 #include "fs-util.h"
17 #include "parse-util.h"
18 #include "process-util.h"
19 #include "raw-clone.h"
20 #include "rm-rf.h"
21 #include "string-util.h"
22 #include "util.h"
23
24 static void test_align_power2(void) {
25         unsigned long i, p2;
26
27         assert_se(ALIGN_POWER2(0) == 0);
28         assert_se(ALIGN_POWER2(1) == 1);
29         assert_se(ALIGN_POWER2(2) == 2);
30         assert_se(ALIGN_POWER2(3) == 4);
31         assert_se(ALIGN_POWER2(12) == 16);
32
33         assert_se(ALIGN_POWER2(ULONG_MAX) == 0);
34         assert_se(ALIGN_POWER2(ULONG_MAX - 1) == 0);
35         assert_se(ALIGN_POWER2(ULONG_MAX - 1024) == 0);
36         assert_se(ALIGN_POWER2(ULONG_MAX / 2) == ULONG_MAX / 2 + 1);
37         assert_se(ALIGN_POWER2(ULONG_MAX + 1) == 0);
38
39         for (i = 1; i < 131071; ++i) {
40                 for (p2 = 1; p2 < i; p2 <<= 1)
41                         /* empty */ ;
42
43                 assert_se(ALIGN_POWER2(i) == p2);
44         }
45
46         for (i = ULONG_MAX - 1024; i < ULONG_MAX; ++i) {
47                 for (p2 = 1; p2 && p2 < i; p2 <<= 1)
48                         /* empty */ ;
49
50                 assert_se(ALIGN_POWER2(i) == p2);
51         }
52 }
53
54 static void test_max(void) {
55         static const struct {
56                 int a;
57                 int b[CONST_MAX(10, 100)];
58         } val1 = {
59                 .a = CONST_MAX(10, 100),
60         };
61         int d = 0;
62
63         assert_cc(sizeof(val1.b) == sizeof(int) * 100);
64
65         /* CONST_MAX returns (void) instead of a value if the passed arguments
66          * are not of the same type or not constant expressions. */
67         assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int));
68         assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void));
69
70         assert_se(val1.a == 100);
71         assert_se(MAX(++d, 0) == 1);
72         assert_se(d == 1);
73
74         assert_cc(MAXSIZE(char[3], uint16_t) == 3);
75         assert_cc(MAXSIZE(char[3], uint32_t) == 4);
76         assert_cc(MAXSIZE(char, long) == sizeof(long));
77
78         assert_se(MAX(-5, 5) == 5);
79         assert_se(MAX(5, 5) == 5);
80         assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5);
81         assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3);
82         assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5);
83         assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2);
84         assert_se(LESS_BY(8, 4) == 4);
85         assert_se(LESS_BY(8, 8) == 0);
86         assert_se(LESS_BY(4, 8) == 0);
87         assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12);
88         assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0);
89         assert_se(CLAMP(-5, 0, 1) == 0);
90         assert_se(CLAMP(5, 0, 1) == 1);
91         assert_se(CLAMP(5, -10, 1) == 1);
92         assert_se(CLAMP(5, -10, 10) == 5);
93         assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10);
94 }
95
96 #pragma GCC diagnostic push
97 #ifdef __clang__
98 #  pragma GCC diagnostic ignored "-Waddress-of-packed-member"
99 #endif
100
101 static void test_container_of(void) {
102         struct mytype {
103                 uint8_t pad1[3];
104                 uint64_t v1;
105                 uint8_t pad2[2];
106                 uint32_t v2;
107         } _packed_ myval = { };
108
109         assert_cc(sizeof(myval) == 17);
110         assert_se(container_of(&myval.v1, struct mytype, v1) == &myval);
111         assert_se(container_of(&myval.v2, struct mytype, v2) == &myval);
112         assert_se(container_of(&container_of(&myval.v2,
113                                              struct mytype,
114                                              v2)->v1,
115                                struct mytype,
116                                v1) == &myval);
117 }
118
119 #pragma GCC diagnostic pop
120
121 static void test_div_round_up(void) {
122         int div;
123
124         /* basic tests */
125         assert_se(DIV_ROUND_UP(0, 8) == 0);
126         assert_se(DIV_ROUND_UP(1, 8) == 1);
127         assert_se(DIV_ROUND_UP(8, 8) == 1);
128         assert_se(DIV_ROUND_UP(12, 8) == 2);
129         assert_se(DIV_ROUND_UP(16, 8) == 2);
130
131         /* test multiple evaluation */
132         div = 0;
133         assert_se(DIV_ROUND_UP(div++, 8) == 0 && div == 1);
134         assert_se(DIV_ROUND_UP(++div, 8) == 1 && div == 2);
135         assert_se(DIV_ROUND_UP(8, div++) == 4 && div == 3);
136         assert_se(DIV_ROUND_UP(8, ++div) == 2 && div == 4);
137
138         /* overflow test with exact division */
139         assert_se(sizeof(0U) == 4);
140         assert_se(0xfffffffaU % 10U == 0U);
141         assert_se(0xfffffffaU / 10U == 429496729U);
142         assert_se(DIV_ROUND_UP(0xfffffffaU, 10U) == 429496729U);
143         assert_se((0xfffffffaU + 10U - 1U) / 10U == 0U);
144         assert_se(0xfffffffaU / 10U + !!(0xfffffffaU % 10U) == 429496729U);
145
146         /* overflow test with rounded division */
147         assert_se(0xfffffffdU % 10U == 3U);
148         assert_se(0xfffffffdU / 10U == 429496729U);
149         assert_se(DIV_ROUND_UP(0xfffffffdU, 10U) == 429496730U);
150         assert_se((0xfffffffdU + 10U - 1U) / 10U == 0U);
151         assert_se(0xfffffffdU / 10U + !!(0xfffffffdU % 10U) == 429496730U);
152 }
153
154 static void test_u64log2(void) {
155         assert_se(u64log2(0) == 0);
156         assert_se(u64log2(8) == 3);
157         assert_se(u64log2(9) == 3);
158         assert_se(u64log2(15) == 3);
159         assert_se(u64log2(16) == 4);
160         assert_se(u64log2(1024*1024) == 20);
161         assert_se(u64log2(1024*1024+5) == 20);
162 }
163
164 static void test_protect_errno(void) {
165         errno = 12;
166         {
167                 PROTECT_ERRNO;
168                 errno = 11;
169         }
170         assert_se(errno == 12);
171 }
172
173 static void test_in_set(void) {
174         assert_se(IN_SET(1, 1));
175         assert_se(IN_SET(1, 1, 2, 3, 4));
176         assert_se(IN_SET(2, 1, 2, 3, 4));
177         assert_se(IN_SET(3, 1, 2, 3, 4));
178         assert_se(IN_SET(4, 1, 2, 3, 4));
179         assert_se(!IN_SET(0, 1));
180         assert_se(!IN_SET(0, 1, 2, 3, 4));
181 }
182
183 static void test_log2i(void) {
184         assert_se(log2i(1) == 0);
185         assert_se(log2i(2) == 1);
186         assert_se(log2i(3) == 1);
187         assert_se(log2i(4) == 2);
188         assert_se(log2i(32) == 5);
189         assert_se(log2i(33) == 5);
190         assert_se(log2i(63) == 5);
191         assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
192 }
193
194 #if 0 /// UNNEEDED by elogind
195 static void test_raw_clone(void) {
196         pid_t parent, pid, pid2;
197
198         parent = getpid();
199         log_info("before clone: getpid()→"PID_FMT, parent);
200         assert_se(raw_getpid() == parent);
201
202         pid = raw_clone(0);
203         assert_se(pid >= 0);
204
205         pid2 = raw_getpid();
206         log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT,
207                  pid, getpid(), pid2);
208         if (pid == 0) {
209                 assert_se(pid2 != parent);
210                 _exit(EXIT_SUCCESS);
211         } else {
212                 int status;
213
214                 assert_se(pid2 == parent);
215                 waitpid(pid, &status, __WCLONE);
216                 assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
217         }
218
219         errno = 0;
220         assert_se(raw_clone(CLONE_FS|CLONE_NEWNS) == -1);
221         assert_se(errno == EINVAL);
222 }
223 #endif // 0
224
225 static void test_physical_memory(void) {
226         uint64_t p;
227         char buf[FORMAT_BYTES_MAX];
228
229         p = physical_memory();
230         assert_se(p > 0);
231         assert_se(p < UINT64_MAX);
232         assert_se(p % page_size() == 0);
233
234         log_info("Memory: %s (%" PRIu64 ")", format_bytes(buf, sizeof(buf), p), p);
235 }
236
237 static void test_physical_memory_scale(void) {
238         uint64_t p;
239
240         p = physical_memory();
241
242         assert_se(physical_memory_scale(0, 100) == 0);
243         assert_se(physical_memory_scale(100, 100) == p);
244
245         log_info("Memory original: %" PRIu64, physical_memory());
246         log_info("Memory scaled by 50%%: %" PRIu64, physical_memory_scale(50, 100));
247         log_info("Memory divided by 2: %" PRIu64, physical_memory() / 2);
248         log_info("Page size: %zu", page_size());
249
250         /* There might be an uneven number of pages, hence permit these calculations to be half a page off... */
251         assert_se(page_size()/2 + physical_memory_scale(50, 100) - p/2 <= page_size());
252         assert_se(physical_memory_scale(200, 100) == p*2);
253
254         assert_se(physical_memory_scale(0, 1) == 0);
255         assert_se(physical_memory_scale(1, 1) == p);
256         assert_se(physical_memory_scale(2, 1) == p*2);
257
258         assert_se(physical_memory_scale(0, 2) == 0);
259
260         assert_se(page_size()/2 + physical_memory_scale(1, 2) - p/2 <= page_size());
261         assert_se(physical_memory_scale(2, 2) == p);
262         assert_se(physical_memory_scale(4, 2) == p*2);
263
264         assert_se(physical_memory_scale(0, UINT32_MAX) == 0);
265         assert_se(physical_memory_scale(UINT32_MAX, UINT32_MAX) == p);
266
267         /* overflow */
268         assert_se(physical_memory_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX);
269 }
270
271 static void test_system_tasks_max(void) {
272         uint64_t t;
273
274         t = system_tasks_max();
275         assert_se(t > 0);
276         assert_se(t < UINT64_MAX);
277
278         log_info("Max tasks: %" PRIu64, t);
279 }
280
281 static void test_system_tasks_max_scale(void) {
282         uint64_t t;
283
284         t = system_tasks_max();
285
286         assert_se(system_tasks_max_scale(0, 100) == 0);
287         assert_se(system_tasks_max_scale(100, 100) == t);
288
289         assert_se(system_tasks_max_scale(0, 1) == 0);
290         assert_se(system_tasks_max_scale(1, 1) == t);
291         assert_se(system_tasks_max_scale(2, 1) == 2*t);
292
293         assert_se(system_tasks_max_scale(0, 2) == 0);
294         assert_se(system_tasks_max_scale(1, 2) == t/2);
295         assert_se(system_tasks_max_scale(2, 2) == t);
296         assert_se(system_tasks_max_scale(3, 2) == (3*t)/2);
297         assert_se(system_tasks_max_scale(4, 2) == t*2);
298
299         assert_se(system_tasks_max_scale(0, UINT32_MAX) == 0);
300         assert_se(system_tasks_max_scale((UINT32_MAX-1)/2, UINT32_MAX-1) == t/2);
301         assert_se(system_tasks_max_scale(UINT32_MAX, UINT32_MAX) == t);
302
303         /* overflow */
304
305         assert_se(system_tasks_max_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX);
306 }
307
308 int main(int argc, char *argv[]) {
309         log_parse_environment();
310         log_open();
311
312         test_align_power2();
313         test_max();
314         test_container_of();
315         test_div_round_up();
316         test_u64log2();
317         test_protect_errno();
318         test_in_set();
319         test_log2i();
320 #if 0 /// UNNEEDED by elogind
321         test_raw_clone();
322 #endif // 0
323         test_physical_memory();
324         test_physical_memory_scale();
325         test_system_tasks_max();
326         test_system_tasks_max_scale();
327
328         return 0;
329 }