Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
test_os_posix_libc_macros.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25/**
26 * [Operating Systems](https://sourceforge.net/p/predef/wiki/OperatingSystems/)
27 * predefined macros.
28 * - BSD included from <sys/param.h>
29 */
30#include <sys/param.h>
31
32/**
33 * [Unix standards](https://sourceforge.net/p/predef/wiki/Standards/)
34 * require the existence macros in the <unistd.h> header file.
35 */
36#include <unistd.h>
37
38/**
39 * [GNU glibc](https://sourceforge.net/p/predef/wiki/Libraries/)
40 *
41 * GLIBC macros have to be included from the <features.h> header file.
42 * Include <limits.h> header file instead, which included <features.h> on GLIBC (see e.g. paragraph 4/6 in ISO/IEC 9899:1999).
43 */
44#include <climits>
45
46/**
47 * [glibc 1.3.4 Feature Test Macros](https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html)
48 * _FILE_OFFSET_BITS
49 * - available if _POSIX_C_SOURCE >= 200112L
50 * - _FILE_OFFSET_BITS == 64 implies using all 64-bit file-function and -type variants on 32-bit platforms
51 * - _FILE_OFFSET_BITS == 64 has no effect on 64-bit platforms, already using the 64-bit variants
52 * - _FILE_OFFSET_BITS is favored over _LARGEFILE64_SOURCE
53 *
54 * _TIME_BITS
55 * - introduced in glibc 2.34, tackling year 2038 issue
56 * - _TIME_BITS available for Linux with kernel >= 5.1
57 * - _TIME_BITS == 64 requires _FILE_OFFSET_BITS to be 64 as well
58 *
59 * glibc D.2.1 64-bit time symbol handling in the GNU C Library
60 * __TIMESIZE == 64 uses 64-bit time_t version
61 */
62extern "C" {
63 // don't include anything yes as we like to grab the vanilla values first
64 extern int printf(const char * format, ...);
65}
66int my_strcmp(const char *, const char *);
67
68#define MACRO_STRINGIFY(item) "" #item
69#define COND_CODE(macro, code) \
70 do { \
71 if (my_strcmp("" #macro, MACRO_STRINGIFY(macro))) { \
72 (code); \
73 } \
74 } while (0)
75
76#define PRINT_COND(macro) \
77 do { \
78 if (my_strcmp("" #macro, MACRO_STRINGIFY(macro))) { \
79 printf("- %s\t%s\n", #macro, MACRO_STRINGIFY(macro)); \
80 } else { \
81 printf("- %s\t-\n", #macro); \
82 } \
83 } while (0)
84
86 printf("Operating System\n");
87 PRINT_COND(BSD);
88 PRINT_COND(__FreeBSD__);
89 PRINT_COND(__NetBSD__);
90 PRINT_COND(__OpenBSD__);
91 PRINT_COND(__bsdi__);
92 PRINT_COND(__DragonFly__);
93 PRINT_COND(_SYSTYPE_BSD);
94
95 PRINT_COND(__CYGWIN__);
96
97 PRINT_COND(__GNU__);
98 PRINT_COND(__gnu_hurd__);
99
100 PRINT_COND(__gnu_linux__);
101 PRINT_COND(__linux__);
102 PRINT_COND(__APPLE__);
103
104 PRINT_COND(__QNX__);
105 PRINT_COND(__QNXNTO__);
106
107 PRINT_COND(sun);
108 PRINT_COND(__sun);
109
110 PRINT_COND(__CYGWIN__);
111 PRINT_COND(_WIN32); // for 32- and 64-bit environments
112 PRINT_COND(_WIN64); // for 64-bit environments only
113 printf("\n");
114
115 printf("Unix Standards Inputs\n");
116 PRINT_COND(_POSIX_C_SOURCE);
117 PRINT_COND(_FILE_OFFSET_BITS);
118 PRINT_COND(_LARGEFILE64_SOURCE);
119 PRINT_COND(_TIME_BITS);
120 PRINT_COND(__TIMESIZE);
121 printf("\n");
122
123 printf("Unix Standards Outputs\n");
124 PRINT_COND(_POSIX_VERSION);
125 PRINT_COND(_POSIX2_C_VERSION);
126 PRINT_COND(_XOPEN_VERSION);
127 PRINT_COND(__LSB_VERSION__);
128 printf("\n");
129}
130
132 printf("GLIBC C Library Outputs\n");
133 PRINT_COND(__GNU_LIBRARY__);
134 PRINT_COND(__GLIBC__);
135 PRINT_COND(__GLIBC_MINOR__);
136 printf("\n");
137
138 printf("Bionic C Library Outputs\n");
139 PRINT_COND(__BIONIC__);
140 printf("\n");
141
142 printf("uClibc C Library Outputs\n");
143 PRINT_COND(__UCLIBC__);
144 printf("\n");
145
146 printf("GNU C++ Library Outputs\n");
147 PRINT_COND(__GLIBCPP__);
148 PRINT_COND(__GLIBCXX__);
149 printf("\n");
150
151 printf("C++ Library Outputs\n");
152 PRINT_COND(_LIBCPP_VERSION);
153 PRINT_COND(_LIBCPP_ABI_VERSION);
154 printf("\n");
155
156 printf("\n");
157
158}
159
160
162
163#include <jau/basic_types.hpp>
164
165#include <cstring>
166
167int my_strcmp(const char *s1, const char *s2) {
168 return ::strcmp(s1, s2);
169}
170
171/**
172 * Resembling the GNU/Linux bits/types.h,
173 * documenting whether time_t is 32-bit (arm-32) or 64-bit (arm-64, x86_64, ..).
174 */
175static int sizeof_time_t() {
176/* X32 kernel interface is 64-bit. */
177#if defined __x86_64__ && defined __ILP32__
178 // 64 bit size
179 #if __WORDSIZE == 32
180 return sizeof( __int64_t );
181 #else
182 return sizeof( long int );
183 #endif
184#else
185 // 32 bit or 64 bit
186 return sizeof( long int );
187#endif
188}
189
190/**
191 * Resembling the GNU/Linux bits/types.h,
192 * documenting whether tv_nsec of struct timespec is 32-bit (arm-32) or 64-bit (arm-64, x86_64, ..).
193 */
194static int sizeof_tv_nsec() {
195#if __WORDSIZE == 64 \
196 || (defined __SYSCALL_WORDSIZE && __SYSCALL_WORDSIZE == 64) \
197 || __TIMESIZE == 32
198 // 32 bit or 64 bit: __syscall_slong_t
199 return sizeof( int64_t );
200#else
201 // 32 bit or 64 bit
202 return sizeof( long int );
203#endif
204}
205
206TEST_CASE( "Unix StandardsTest 01.00", "[unix][posix]" ) {
208 {
209 using time_t_type = decltype(timespec::tv_sec);
210 INFO_STR(" tv_sec: sizeof=" + std::to_string( sizeof( time_t_type ) ) + ", signed " + std::to_string( std::is_signed_v<time_t_type>) );
211 CHECK( sizeof_time_t() == sizeof( time_t_type ) );
212 CHECK( true == std::is_signed_v<time_t_type> );
213
214 using ns_type = decltype(timespec::tv_nsec);
215 INFO_STR(" tv_nsec: sizeof=" + std::to_string( sizeof( ns_type ) ) + ", signed " + std::to_string( std::is_signed_v<ns_type>) );
216 CHECK( sizeof_tv_nsec() == sizeof( ns_type ) );
217 CHECK( true == std::is_signed_v<ns_type> );
218 }
219 {
220 INFO_STR(" off_t sizeof=" + std::to_string( sizeof( off_t ) ) + ", signed " + std::to_string( std::is_signed_v<off_t>) );
221 INFO_STR(" off64_t sizeof=" + std::to_string( sizeof( off64_t ) ) + ", signed " + std::to_string( std::is_signed_v<off64_t>) );
222 REQUIRE( 8 == sizeof(off64_t) );
223 }
224}
225
226TEST_CASE( "Standard C Library 01.01", "[libc]" ) {
227 print_libc();
228}
#define INFO_STR(msg)
Definition: catch2_ext.hpp:75
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
int my_strcmp(const char *, const char *)
static int sizeof_time_t()
Resembling the GNU/Linux bits/types.h, documenting whether time_t is 32-bit (arm-32) or 64-bit (arm-6...
#define PRINT_COND(macro)
int printf(const char *format,...)
Operating Systems predefined macros.
void print_libc()
void print_unix_std()
TEST_CASE("Unix StandardsTest 01.00", "[unix][posix]")
static int sizeof_tv_nsec()
Resembling the GNU/Linux bits/types.h, documenting whether tv_nsec of struct timespec is 32-bit (arm-...