]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sizeof.c
Merge pull request #14992 from keszybz/syslog-address-length-fix
[thirdparty/systemd.git] / src / test / test-sizeof.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sched.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8
9 #define __STDC_WANT_IEC_60559_TYPES_EXT__
10 #include <float.h>
11
12 #include "time-util.h"
13
14 /* Print information about various types. Useful when diagnosing
15 * gcc diagnostics on an unfamiliar architecture. */
16
17 #pragma GCC diagnostic ignored "-Wtype-limits"
18
19 #define info(t) \
20 printf("%s → %zu bits%s, %zu byte alignment\n", STRINGIFY(t), \
21 sizeof(t)*CHAR_BIT, \
22 strstr(STRINGIFY(t), "signed") ? "" : \
23 (t)-1 < (t)0 ? ", signed" : ", unsigned", \
24 __alignof__(t))
25
26 enum Enum {
27 enum_value,
28 };
29
30 enum BigEnum {
31 big_enum_value = UINT64_C(1),
32 };
33
34 enum BigEnum2 {
35 big_enum2_pos = UINT64_C(1),
36 big_enum2_neg = UINT64_C(-1),
37 };
38
39 int main(void) {
40 info(char);
41 info(signed char);
42 info(unsigned char);
43 info(short unsigned);
44 info(unsigned);
45 info(long unsigned);
46 info(long long unsigned);
47 info(__syscall_ulong_t);
48 info(__syscall_slong_t);
49
50 info(float);
51 info(double);
52 info(long double);
53
54 #ifdef FLT128_MAX
55 info(_Float128);
56 info(_Float64);
57 info(_Float64x);
58 info(_Float32);
59 info(_Float32x);
60 #endif
61
62 info(size_t);
63 info(ssize_t);
64 info(time_t);
65 info(usec_t);
66 info(__time_t);
67 info(pid_t);
68 info(uid_t);
69 info(gid_t);
70 info(socklen_t);
71
72 info(__cpu_mask);
73
74 info(enum Enum);
75 info(enum BigEnum);
76 info(enum BigEnum2);
77 assert_cc(sizeof(enum BigEnum2) == 8);
78 printf("big_enum2_pos → %zu\n", sizeof(big_enum2_pos));
79 printf("big_enum2_neg → %zu\n", sizeof(big_enum2_neg));
80
81 return 0;
82 }