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