]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
539ad707 | 2 | |
618234a5 | 3 | #include <errno.h> |
8852362b | 4 | #include <sys/wait.h> |
618234a5 | 5 | #include <unistd.h> |
539ad707 | 6 | |
65b3903f | 7 | #include "fileio.h" |
f4f15635 | 8 | #include "fs-util.h" |
eefc66aa | 9 | #include "limits-util.h" |
cfb4a849 | 10 | #include "memory-util.h" |
36dd5ffd | 11 | #include "missing_syscall.h" |
d9ab2bcf | 12 | #include "parse-util.h" |
dccca82b | 13 | #include "process-util.h" |
8869a0b4 | 14 | #include "raw-clone.h" |
618234a5 | 15 | #include "rm-rf.h" |
07630cea | 16 | #include "string-util.h" |
09c984c6 | 17 | #include "tests.h" |
cfb4a849 | 18 | #include "util.h" |
539ad707 | 19 | |
625e870b DH |
20 | static void test_align_power2(void) { |
21 | unsigned long i, p2; | |
22 | ||
09c984c6 ZJS |
23 | log_info("/* %s */", __func__); |
24 | ||
625e870b DH |
25 | assert_se(ALIGN_POWER2(0) == 0); |
26 | assert_se(ALIGN_POWER2(1) == 1); | |
27 | assert_se(ALIGN_POWER2(2) == 2); | |
28 | assert_se(ALIGN_POWER2(3) == 4); | |
47ac31f7 LP |
29 | assert_se(ALIGN_POWER2(4) == 4); |
30 | assert_se(ALIGN_POWER2(5) == 8); | |
31 | assert_se(ALIGN_POWER2(6) == 8); | |
32 | assert_se(ALIGN_POWER2(7) == 8); | |
33 | assert_se(ALIGN_POWER2(9) == 16); | |
34 | assert_se(ALIGN_POWER2(10) == 16); | |
35 | assert_se(ALIGN_POWER2(11) == 16); | |
625e870b | 36 | assert_se(ALIGN_POWER2(12) == 16); |
47ac31f7 LP |
37 | assert_se(ALIGN_POWER2(13) == 16); |
38 | assert_se(ALIGN_POWER2(14) == 16); | |
39 | assert_se(ALIGN_POWER2(15) == 16); | |
40 | assert_se(ALIGN_POWER2(16) == 16); | |
41 | assert_se(ALIGN_POWER2(17) == 32); | |
625e870b DH |
42 | |
43 | assert_se(ALIGN_POWER2(ULONG_MAX) == 0); | |
44 | assert_se(ALIGN_POWER2(ULONG_MAX - 1) == 0); | |
45 | assert_se(ALIGN_POWER2(ULONG_MAX - 1024) == 0); | |
46 | assert_se(ALIGN_POWER2(ULONG_MAX / 2) == ULONG_MAX / 2 + 1); | |
47 | assert_se(ALIGN_POWER2(ULONG_MAX + 1) == 0); | |
48 | ||
49 | for (i = 1; i < 131071; ++i) { | |
50 | for (p2 = 1; p2 < i; p2 <<= 1) | |
51 | /* empty */ ; | |
52 | ||
53 | assert_se(ALIGN_POWER2(i) == p2); | |
54 | } | |
55 | ||
56 | for (i = ULONG_MAX - 1024; i < ULONG_MAX; ++i) { | |
57 | for (p2 = 1; p2 && p2 < i; p2 <<= 1) | |
58 | /* empty */ ; | |
59 | ||
60 | assert_se(ALIGN_POWER2(i) == p2); | |
61 | } | |
62 | } | |
63 | ||
7242d742 DH |
64 | static void test_max(void) { |
65 | static const struct { | |
66 | int a; | |
67 | int b[CONST_MAX(10, 100)]; | |
68 | } val1 = { | |
69 | .a = CONST_MAX(10, 100), | |
70 | }; | |
71 | int d = 0; | |
84fb2131 FB |
72 | unsigned long x = 12345; |
73 | unsigned long y = 54321; | |
74 | const char str[] = "a_string_constant"; | |
75 | const unsigned long long arr[] = {9999ULL, 10ULL, 0ULL, 3000ULL, 2000ULL, 1000ULL, 100ULL, 9999999ULL}; | |
76 | void *p = (void *)str; | |
77 | void *q = (void *)&str[16]; | |
7242d742 | 78 | |
09c984c6 ZJS |
79 | log_info("/* %s */", __func__); |
80 | ||
7242d742 DH |
81 | assert_cc(sizeof(val1.b) == sizeof(int) * 100); |
82 | ||
83 | /* CONST_MAX returns (void) instead of a value if the passed arguments | |
84 | * are not of the same type or not constant expressions. */ | |
85 | assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int)); | |
7242d742 DH |
86 | assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void)); |
87 | ||
88 | assert_se(val1.a == 100); | |
89 | assert_se(MAX(++d, 0) == 1); | |
90 | assert_se(d == 1); | |
40a1eebd DH |
91 | |
92 | assert_cc(MAXSIZE(char[3], uint16_t) == 3); | |
93 | assert_cc(MAXSIZE(char[3], uint32_t) == 4); | |
94 | assert_cc(MAXSIZE(char, long) == sizeof(long)); | |
667a0377 DH |
95 | |
96 | assert_se(MAX(-5, 5) == 5); | |
97 | assert_se(MAX(5, 5) == 5); | |
98 | assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5); | |
99 | assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3); | |
100 | assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5); | |
101 | assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2); | |
102 | assert_se(LESS_BY(8, 4) == 4); | |
103 | assert_se(LESS_BY(8, 8) == 0); | |
104 | assert_se(LESS_BY(4, 8) == 0); | |
105 | assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12); | |
106 | assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0); | |
ef6e83f0 YW |
107 | assert_se(CMP(3, 5) == -1); |
108 | assert_se(CMP(5, 3) == 1); | |
84fb2131 FB |
109 | assert_se(CMP(5, 5) == 0); |
110 | assert_se(CMP(x, y) == -1); | |
111 | assert_se(CMP(y, x) == 1); | |
112 | assert_se(CMP(x, x) == 0); | |
113 | assert_se(CMP(y, y) == 0); | |
ef6e83f0 YW |
114 | assert_se(CMP(UINT64_MAX, (uint64_t) 0) == 1); |
115 | assert_se(CMP((uint64_t) 0, UINT64_MAX) == -1); | |
84fb2131 FB |
116 | assert_se(CMP(UINT64_MAX, UINT64_MAX) == 0); |
117 | assert_se(CMP(INT64_MIN, INT64_MAX) == -1); | |
118 | assert_se(CMP(INT64_MAX, INT64_MIN) == 1); | |
119 | assert_se(CMP(INT64_MAX, INT64_MAX) == 0); | |
120 | assert_se(CMP(INT64_MIN, INT64_MIN) == 0); | |
ef6e83f0 YW |
121 | assert_se(CMP(INT64_MAX, (int64_t) 0) == 1); |
122 | assert_se(CMP((int64_t) 0, INT64_MIN) == 1); | |
123 | assert_se(CMP(INT64_MIN, (int64_t) 0) == -1); | |
124 | assert_se(CMP((int64_t) 0, INT64_MAX) == -1); | |
84fb2131 FB |
125 | assert_se(CMP(&str[2], &str[7]) == -1); |
126 | assert_se(CMP(&str[2], &str[2]) == 0); | |
127 | assert_se(CMP(&str[7], (const char *)str) == 1); | |
128 | assert_se(CMP(str[2], str[7]) == 1); | |
129 | assert_se(CMP(str[7], *str) == 1); | |
130 | assert_se(CMP((const unsigned long long *)arr, &arr[3]) == -1); | |
131 | assert_se(CMP(*arr, arr[3]) == 1); | |
132 | assert_se(CMP(p, q) == -1); | |
133 | assert_se(CMP(q, p) == 1); | |
134 | assert_se(CMP(p, p) == 0); | |
135 | assert_se(CMP(q, q) == 0); | |
667a0377 DH |
136 | assert_se(CLAMP(-5, 0, 1) == 0); |
137 | assert_se(CLAMP(5, 0, 1) == 1); | |
138 | assert_se(CLAMP(5, -10, 1) == 1); | |
139 | assert_se(CLAMP(5, -10, 10) == 5); | |
140 | assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10); | |
7242d742 DH |
141 | } |
142 | ||
09571e3f ZJS |
143 | #pragma GCC diagnostic push |
144 | #ifdef __clang__ | |
145 | # pragma GCC diagnostic ignored "-Waddress-of-packed-member" | |
146 | #endif | |
147 | ||
fb835651 DH |
148 | static void test_container_of(void) { |
149 | struct mytype { | |
150 | uint8_t pad1[3]; | |
151 | uint64_t v1; | |
152 | uint8_t pad2[2]; | |
153 | uint32_t v2; | |
6b68c268 | 154 | } myval = { }; |
fb835651 | 155 | |
09c984c6 ZJS |
156 | log_info("/* %s */", __func__); |
157 | ||
6b68c268 | 158 | assert_cc(sizeof(myval) >= 17); |
fb835651 DH |
159 | assert_se(container_of(&myval.v1, struct mytype, v1) == &myval); |
160 | assert_se(container_of(&myval.v2, struct mytype, v2) == &myval); | |
161 | assert_se(container_of(&container_of(&myval.v2, | |
162 | struct mytype, | |
163 | v2)->v1, | |
164 | struct mytype, | |
165 | v1) == &myval); | |
166 | } | |
167 | ||
09571e3f ZJS |
168 | #pragma GCC diagnostic pop |
169 | ||
180a60bc DH |
170 | static void test_div_round_up(void) { |
171 | int div; | |
172 | ||
09c984c6 ZJS |
173 | log_info("/* %s */", __func__); |
174 | ||
180a60bc DH |
175 | /* basic tests */ |
176 | assert_se(DIV_ROUND_UP(0, 8) == 0); | |
177 | assert_se(DIV_ROUND_UP(1, 8) == 1); | |
178 | assert_se(DIV_ROUND_UP(8, 8) == 1); | |
179 | assert_se(DIV_ROUND_UP(12, 8) == 2); | |
180 | assert_se(DIV_ROUND_UP(16, 8) == 2); | |
181 | ||
182 | /* test multiple evaluation */ | |
183 | div = 0; | |
184 | assert_se(DIV_ROUND_UP(div++, 8) == 0 && div == 1); | |
185 | assert_se(DIV_ROUND_UP(++div, 8) == 1 && div == 2); | |
186 | assert_se(DIV_ROUND_UP(8, div++) == 4 && div == 3); | |
187 | assert_se(DIV_ROUND_UP(8, ++div) == 2 && div == 4); | |
188 | ||
189 | /* overflow test with exact division */ | |
190 | assert_se(sizeof(0U) == 4); | |
191 | assert_se(0xfffffffaU % 10U == 0U); | |
192 | assert_se(0xfffffffaU / 10U == 429496729U); | |
193 | assert_se(DIV_ROUND_UP(0xfffffffaU, 10U) == 429496729U); | |
194 | assert_se((0xfffffffaU + 10U - 1U) / 10U == 0U); | |
195 | assert_se(0xfffffffaU / 10U + !!(0xfffffffaU % 10U) == 429496729U); | |
196 | ||
197 | /* overflow test with rounded division */ | |
198 | assert_se(0xfffffffdU % 10U == 3U); | |
199 | assert_se(0xfffffffdU / 10U == 429496729U); | |
200 | assert_se(DIV_ROUND_UP(0xfffffffdU, 10U) == 429496730U); | |
201 | assert_se((0xfffffffdU + 10U - 1U) / 10U == 0U); | |
202 | assert_se(0xfffffffdU / 10U + !!(0xfffffffdU % 10U) == 429496730U); | |
203 | } | |
204 | ||
144e51ec | 205 | static void test_u64log2(void) { |
09c984c6 ZJS |
206 | log_info("/* %s */", __func__); |
207 | ||
bdf7026e TA |
208 | assert_se(u64log2(0) == 0); |
209 | assert_se(u64log2(8) == 3); | |
210 | assert_se(u64log2(9) == 3); | |
211 | assert_se(u64log2(15) == 3); | |
212 | assert_se(u64log2(16) == 4); | |
213 | assert_se(u64log2(1024*1024) == 20); | |
214 | assert_se(u64log2(1024*1024+5) == 20); | |
144e51ec CR |
215 | } |
216 | ||
2a371001 | 217 | static void test_protect_errno(void) { |
09c984c6 ZJS |
218 | log_info("/* %s */", __func__); |
219 | ||
2a371001 ZJS |
220 | errno = 12; |
221 | { | |
222 | PROTECT_ERRNO; | |
223 | errno = 11; | |
224 | } | |
bdf7026e | 225 | assert_se(errno == 12); |
2a371001 ZJS |
226 | } |
227 | ||
840f606d LP |
228 | static void test_unprotect_errno_inner_function(void) { |
229 | PROTECT_ERRNO; | |
230 | ||
231 | errno = 2222; | |
232 | } | |
233 | ||
234 | static void test_unprotect_errno(void) { | |
235 | log_info("/* %s */", __func__); | |
236 | ||
237 | errno = 4711; | |
238 | ||
239 | PROTECT_ERRNO; | |
240 | ||
241 | errno = 815; | |
242 | ||
243 | UNPROTECT_ERRNO; | |
244 | ||
245 | assert_se(errno == 4711); | |
246 | ||
247 | test_unprotect_errno_inner_function(); | |
248 | ||
249 | assert_se(errno == 4711); | |
250 | } | |
251 | ||
cabb7806 | 252 | static void test_in_set(void) { |
09c984c6 ZJS |
253 | log_info("/* %s */", __func__); |
254 | ||
cabb7806 LP |
255 | assert_se(IN_SET(1, 1)); |
256 | assert_se(IN_SET(1, 1, 2, 3, 4)); | |
257 | assert_se(IN_SET(2, 1, 2, 3, 4)); | |
258 | assert_se(IN_SET(3, 1, 2, 3, 4)); | |
259 | assert_se(IN_SET(4, 1, 2, 3, 4)); | |
260 | assert_se(!IN_SET(0, 1)); | |
261 | assert_se(!IN_SET(0, 1, 2, 3, 4)); | |
262 | } | |
263 | ||
8fe90522 | 264 | static void test_log2i(void) { |
09c984c6 ZJS |
265 | log_info("/* %s */", __func__); |
266 | ||
8fe90522 ZJS |
267 | assert_se(log2i(1) == 0); |
268 | assert_se(log2i(2) == 1); | |
269 | assert_se(log2i(3) == 1); | |
270 | assert_se(log2i(4) == 2); | |
271 | assert_se(log2i(32) == 5); | |
272 | assert_se(log2i(33) == 5); | |
273 | assert_se(log2i(63) == 5); | |
274 | assert_se(log2i(INT_MAX) == sizeof(int)*8-2); | |
275 | } | |
276 | ||
7f6bfc56 ZJS |
277 | static void test_eqzero(void) { |
278 | const uint32_t zeros[] = {0, 0, 0}; | |
279 | const uint32_t ones[] = {1, 1}; | |
280 | const uint32_t mixed[] = {0, 1, 0, 0, 0}; | |
281 | const uint8_t longer[] = {[55] = 255}; | |
282 | ||
283 | log_info("/* %s */", __func__); | |
284 | ||
285 | assert_se(eqzero(zeros)); | |
286 | assert_se(!eqzero(ones)); | |
287 | assert_se(!eqzero(mixed)); | |
288 | assert_se(!eqzero(longer)); | |
289 | } | |
290 | ||
ee05e779 ZJS |
291 | static void test_raw_clone(void) { |
292 | pid_t parent, pid, pid2; | |
293 | ||
09c984c6 ZJS |
294 | log_info("/* %s */", __func__); |
295 | ||
ee05e779 ZJS |
296 | parent = getpid(); |
297 | log_info("before clone: getpid()→"PID_FMT, parent); | |
298 | assert_se(raw_getpid() == parent); | |
299 | ||
8869a0b4 | 300 | pid = raw_clone(0); |
e50221bf | 301 | assert_se(pid >= 0); |
ee05e779 ZJS |
302 | |
303 | pid2 = raw_getpid(); | |
304 | log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT, | |
305 | pid, getpid(), pid2); | |
0289a5bc | 306 | if (pid == 0) { |
e50221bf | 307 | assert_se(pid2 != parent); |
0289a5bc FB |
308 | _exit(EXIT_SUCCESS); |
309 | } else { | |
310 | int status; | |
311 | ||
e50221bf | 312 | assert_se(pid2 == parent); |
0289a5bc FB |
313 | waitpid(pid, &status, __WCLONE); |
314 | assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); | |
315 | } | |
96f64eb5 MG |
316 | |
317 | errno = 0; | |
318 | assert_se(raw_clone(CLONE_FS|CLONE_NEWNS) == -1); | |
319 | assert_se(errno == EINVAL); | |
ee05e779 ZJS |
320 | } |
321 | ||
d9ab2bcf LP |
322 | static void test_physical_memory(void) { |
323 | uint64_t p; | |
324 | char buf[FORMAT_BYTES_MAX]; | |
325 | ||
09c984c6 ZJS |
326 | log_info("/* %s */", __func__); |
327 | ||
d9ab2bcf LP |
328 | p = physical_memory(); |
329 | assert_se(p > 0); | |
330 | assert_se(p < UINT64_MAX); | |
331 | assert_se(p % page_size() == 0); | |
332 | ||
d8cf2ac7 LP |
333 | log_info("Memory: %s (%" PRIu64 ")", format_bytes(buf, sizeof(buf), p), p); |
334 | } | |
335 | ||
336 | static void test_physical_memory_scale(void) { | |
337 | uint64_t p; | |
338 | ||
09c984c6 ZJS |
339 | log_info("/* %s */", __func__); |
340 | ||
d8cf2ac7 LP |
341 | p = physical_memory(); |
342 | ||
343 | assert_se(physical_memory_scale(0, 100) == 0); | |
344 | assert_se(physical_memory_scale(100, 100) == p); | |
345 | ||
346 | log_info("Memory original: %" PRIu64, physical_memory()); | |
347 | log_info("Memory scaled by 50%%: %" PRIu64, physical_memory_scale(50, 100)); | |
348 | log_info("Memory divided by 2: %" PRIu64, physical_memory() / 2); | |
349 | log_info("Page size: %zu", page_size()); | |
350 | ||
351 | /* There might be an uneven number of pages, hence permit these calculations to be half a page off... */ | |
352 | assert_se(page_size()/2 + physical_memory_scale(50, 100) - p/2 <= page_size()); | |
353 | assert_se(physical_memory_scale(200, 100) == p*2); | |
354 | ||
355 | assert_se(physical_memory_scale(0, 1) == 0); | |
356 | assert_se(physical_memory_scale(1, 1) == p); | |
357 | assert_se(physical_memory_scale(2, 1) == p*2); | |
358 | ||
359 | assert_se(physical_memory_scale(0, 2) == 0); | |
360 | ||
361 | assert_se(page_size()/2 + physical_memory_scale(1, 2) - p/2 <= page_size()); | |
362 | assert_se(physical_memory_scale(2, 2) == p); | |
363 | assert_se(physical_memory_scale(4, 2) == p*2); | |
364 | ||
365 | assert_se(physical_memory_scale(0, UINT32_MAX) == 0); | |
366 | assert_se(physical_memory_scale(UINT32_MAX, UINT32_MAX) == p); | |
367 | ||
368 | /* overflow */ | |
369 | assert_se(physical_memory_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); | |
83f8e808 LP |
370 | } |
371 | ||
372 | static void test_system_tasks_max(void) { | |
373 | uint64_t t; | |
374 | ||
09c984c6 ZJS |
375 | log_info("/* %s */", __func__); |
376 | ||
83f8e808 LP |
377 | t = system_tasks_max(); |
378 | assert_se(t > 0); | |
379 | assert_se(t < UINT64_MAX); | |
380 | ||
381 | log_info("Max tasks: %" PRIu64, t); | |
382 | } | |
383 | ||
384 | static void test_system_tasks_max_scale(void) { | |
385 | uint64_t t; | |
386 | ||
09c984c6 ZJS |
387 | log_info("/* %s */", __func__); |
388 | ||
83f8e808 LP |
389 | t = system_tasks_max(); |
390 | ||
391 | assert_se(system_tasks_max_scale(0, 100) == 0); | |
392 | assert_se(system_tasks_max_scale(100, 100) == t); | |
393 | ||
394 | assert_se(system_tasks_max_scale(0, 1) == 0); | |
395 | assert_se(system_tasks_max_scale(1, 1) == t); | |
396 | assert_se(system_tasks_max_scale(2, 1) == 2*t); | |
397 | ||
398 | assert_se(system_tasks_max_scale(0, 2) == 0); | |
399 | assert_se(system_tasks_max_scale(1, 2) == t/2); | |
400 | assert_se(system_tasks_max_scale(2, 2) == t); | |
401 | assert_se(system_tasks_max_scale(3, 2) == (3*t)/2); | |
402 | assert_se(system_tasks_max_scale(4, 2) == t*2); | |
403 | ||
404 | assert_se(system_tasks_max_scale(0, UINT32_MAX) == 0); | |
405 | assert_se(system_tasks_max_scale((UINT32_MAX-1)/2, UINT32_MAX-1) == t/2); | |
406 | assert_se(system_tasks_max_scale(UINT32_MAX, UINT32_MAX) == t); | |
407 | ||
408 | /* overflow */ | |
d8cf2ac7 | 409 | |
83f8e808 | 410 | assert_se(system_tasks_max_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); |
d9ab2bcf LP |
411 | } |
412 | ||
1146b664 LP |
413 | static void test_foreach_pointer(void) { |
414 | int a, b, c, *i; | |
415 | size_t k = 0; | |
416 | ||
463f9ce3 LP |
417 | log_info("/* %s */", __func__); |
418 | ||
1146b664 LP |
419 | FOREACH_POINTER(i, &a, &b, &c) { |
420 | switch (k) { | |
421 | ||
422 | case 0: | |
423 | assert_se(i == &a); | |
424 | break; | |
425 | ||
426 | case 1: | |
427 | assert_se(i == &b); | |
428 | break; | |
429 | ||
430 | case 2: | |
431 | assert_se(i == &c); | |
432 | break; | |
433 | ||
434 | default: | |
435 | assert_not_reached("unexpected index"); | |
436 | break; | |
437 | } | |
438 | ||
439 | k++; | |
440 | } | |
441 | ||
442 | assert(k == 3); | |
443 | ||
444 | FOREACH_POINTER(i, &b) { | |
445 | assert(k == 3); | |
446 | assert(i == &b); | |
447 | k = 4; | |
448 | } | |
449 | ||
450 | assert(k == 4); | |
451 | ||
452 | FOREACH_POINTER(i, NULL, &c, NULL, &b, NULL, &a, NULL) { | |
453 | switch (k) { | |
454 | ||
455 | case 4: | |
456 | assert_se(i == NULL); | |
457 | break; | |
458 | ||
459 | case 5: | |
460 | assert_se(i == &c); | |
461 | break; | |
462 | ||
463 | case 6: | |
464 | assert_se(i == NULL); | |
465 | break; | |
466 | ||
467 | case 7: | |
468 | assert_se(i == &b); | |
469 | break; | |
470 | ||
471 | case 8: | |
472 | assert_se(i == NULL); | |
473 | break; | |
474 | ||
475 | case 9: | |
476 | assert_se(i == &a); | |
477 | break; | |
478 | ||
479 | case 10: | |
480 | assert_se(i == NULL); | |
481 | break; | |
482 | ||
483 | default: | |
484 | assert_not_reached("unexpected index"); | |
485 | break; | |
486 | } | |
487 | ||
488 | k++; | |
489 | } | |
490 | ||
491 | assert(k == 11); | |
492 | } | |
493 | ||
463f9ce3 LP |
494 | static void test_ptr_to_int(void) { |
495 | log_info("/* %s */", __func__); | |
496 | ||
497 | /* Primary reason to have this test is to validate that pointers are large enough to hold entire int range */ | |
498 | assert_se(PTR_TO_INT(INT_TO_PTR(0)) == 0); | |
499 | assert_se(PTR_TO_INT(INT_TO_PTR(1)) == 1); | |
500 | assert_se(PTR_TO_INT(INT_TO_PTR(-1)) == -1); | |
501 | assert_se(PTR_TO_INT(INT_TO_PTR(INT_MAX)) == INT_MAX); | |
502 | assert_se(PTR_TO_INT(INT_TO_PTR(INT_MIN)) == INT_MIN); | |
503 | } | |
504 | ||
539ad707 | 505 | int main(int argc, char *argv[]) { |
09c984c6 | 506 | test_setup_logging(LOG_INFO); |
9480794b | 507 | |
625e870b | 508 | test_align_power2(); |
7242d742 | 509 | test_max(); |
fb835651 | 510 | test_container_of(); |
180a60bc | 511 | test_div_round_up(); |
144e51ec | 512 | test_u64log2(); |
2a371001 | 513 | test_protect_errno(); |
840f606d | 514 | test_unprotect_errno(); |
cabb7806 | 515 | test_in_set(); |
8fe90522 | 516 | test_log2i(); |
7f6bfc56 | 517 | test_eqzero(); |
ee05e779 | 518 | test_raw_clone(); |
d9ab2bcf | 519 | test_physical_memory(); |
d8cf2ac7 | 520 | test_physical_memory_scale(); |
83f8e808 LP |
521 | test_system_tasks_max(); |
522 | test_system_tasks_max_scale(); | |
1146b664 | 523 | test_foreach_pointer(); |
463f9ce3 | 524 | test_ptr_to_int(); |
539ad707 TA |
525 | |
526 | return 0; | |
527 | } |