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