usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-endian.c
blobb0156121d6a87fe4f7a83fb9c56ff91a04fb231a
1 /* Test of <endian.h> substitute.
2 Copyright (C) 2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation, either version 3 of the License,
7 or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Collin Funk <collin.funk1@gmail.com>, 2024. */
19 #include <config.h>
21 /* Specification. */
22 #include <endian.h>
24 /* Check for uint16_t and uint32_t. */
25 uint16_t t1;
26 uint32_t t2;
28 /* The next POSIX revision requires 64-bit types. Gnulib doesn't. */
29 #if 0
30 uint64_t t3;
31 #endif
33 /* "These macros shall be suitable for use in #if preprocessing directives." */
34 #if BYTE_ORDER == LITTLE_ENDIAN
35 int a = 17;
36 #endif
37 #if BYTE_ORDER == BIG_ENDIAN
38 int a = 19;
39 #endif
41 /* "The macros BIG_ENDIAN and LITTLE_ENDIAN shall have distinct values." */
42 static_assert (LITTLE_ENDIAN != BIG_ENDIAN);
44 static_assert (BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == BIG_ENDIAN);
46 #include <stdint.h>
48 #include "macros.h"
50 /* Test byte order conversion functions with constant values. */
51 static void
52 test_convert_constant (void)
54 #if BYTE_ORDER == BIG_ENDIAN
55 /* 16-bit. */
56 ASSERT (be16toh (UINT16_C (0x1234)) == UINT16_C (0x1234));
57 ASSERT (htobe16 (UINT16_C (0x1234)) == UINT16_C (0x1234));
58 ASSERT (le16toh (UINT16_C (0x1234)) == UINT16_C (0x3412));
59 ASSERT (htole16 (UINT16_C (0x1234)) == UINT16_C (0x3412));
61 /* 32-bit. */
62 ASSERT (be32toh (UINT32_C (0x12345678)) == UINT32_C (0x12345678));
63 ASSERT (htobe32 (UINT32_C (0x12345678)) == UINT32_C (0x12345678));
64 ASSERT (le32toh (UINT32_C (0x12345678)) == UINT32_C (0x78563412));
65 ASSERT (htole32 (UINT32_C (0x12345678)) == UINT32_C (0x78563412));
67 /* 64-bit. */
68 # ifdef UINT64_MAX
69 ASSERT (be64toh (UINT64_C (0x1234567890ABCDEF))
70 == UINT64_C (0x1234567890ABCDEF));
71 ASSERT (htobe64 (UINT64_C (0x1234567890ABCDEF))
72 == UINT64_C (0x1234567890ABCDEF));
73 ASSERT (le64toh (UINT64_C (0x1234567890ABCDEF))
74 == UINT64_C (0xEFCDAB9078563412));
75 ASSERT (htole64 (UINT64_C (0x1234567890ABCDEF))
76 == UINT64_C (0xEFCDAB9078563412));
77 # endif
78 #else
79 /* 16-bit. */
80 ASSERT (be16toh (UINT16_C (0x1234)) == UINT16_C (0x3412));
81 ASSERT (htobe16 (UINT16_C (0x1234)) == UINT16_C (0x3412));
82 ASSERT (le16toh (UINT16_C (0x1234)) == UINT16_C (0x1234));
83 ASSERT (htole16 (UINT16_C (0x1234)) == UINT16_C (0x1234));
85 /* 32-bit. */
86 ASSERT (be32toh (UINT32_C (0x12345678)) == UINT32_C (0x78563412));
87 ASSERT (htobe32 (UINT32_C (0x12345678)) == UINT32_C (0x78563412));
88 ASSERT (le32toh (UINT32_C (0x12345678)) == UINT32_C (0x12345678));
89 ASSERT (htole32 (UINT32_C (0x12345678)) == UINT32_C (0x12345678));
91 /* 64-bit. */
92 # ifdef UINT64_MAX
93 ASSERT (be64toh (UINT64_C (0x1234567890ABCDEF))
94 == UINT64_C (0xEFCDAB9078563412));
95 ASSERT (htobe64 (UINT64_C (0x1234567890ABCDEF))
96 == UINT64_C (0xEFCDAB9078563412));
97 ASSERT (le64toh (UINT64_C (0x1234567890ABCDEF))
98 == UINT64_C (0x1234567890ABCDEF));
99 ASSERT (htole64 (UINT64_C (0x1234567890ABCDEF))
100 == UINT64_C (0x1234567890ABCDEF));
101 # endif
102 #endif
105 /* Test that the byte order conversion functions evaluate their
106 arguments once. */
107 static void
108 test_convert_eval_once (void)
110 /* 16-bit. */
112 uint16_t value = 0;
113 ASSERT (be16toh (value++) == 0);
114 ASSERT (value == 1);
117 uint16_t value = 0;
118 ASSERT (htobe16 (value++) == 0);
119 ASSERT (value == 1);
122 uint16_t value = 0;
123 ASSERT (le16toh (value++) == 0);
124 ASSERT (value == 1);
127 uint16_t value = 0;
128 ASSERT (htole16 (value++) == 0);
129 ASSERT (value == 1);
132 /* 32-bit. */
134 uint32_t value = 0;
135 ASSERT (be32toh (value++) == 0);
136 ASSERT (value == 1);
139 uint32_t value = 0;
140 ASSERT (htobe32 (value++) == 0);
141 ASSERT (value == 1);
144 uint32_t value = 0;
145 ASSERT (le32toh (value++) == 0);
146 ASSERT (value == 1);
149 uint32_t value = 0;
150 ASSERT (htole32 (value++) == 0);
151 ASSERT (value == 1);
154 /* 64-bit. */
155 #ifdef UINT64_MAX
157 uint64_t value = 0;
158 ASSERT (be64toh (value++) == 0);
159 ASSERT (value == 1);
162 uint64_t value = 0;
163 ASSERT (htobe64 (value++) == 0);
164 ASSERT (value == 1);
167 uint64_t value = 0;
168 ASSERT (le64toh (value++) == 0);
169 ASSERT (value == 1);
172 uint64_t value = 0;
173 ASSERT (htole64 (value++) == 0);
174 ASSERT (value == 1);
176 #endif
179 /* Test that the byte order conversion functions accept floating-point
180 arguments. */
181 static void
182 test_convert_double (void)
184 /* 16-bit. */
185 ASSERT (be16toh (0.0) == 0);
186 ASSERT (htobe16 (0.0) == 0);
187 ASSERT (le16toh (0.0) == 0);
188 ASSERT (htole16 (0.0) == 0);
190 /* 32-bit. */
191 ASSERT (be32toh (0.0) == 0);
192 ASSERT (htobe32 (0.0) == 0);
193 ASSERT (le32toh (0.0) == 0);
194 ASSERT (htole32 (0.0) == 0);
196 /* 64-bit. */
197 #ifdef UINT64_MAX
198 ASSERT (be64toh (0.0) == 0);
199 ASSERT (htobe64 (0.0) == 0);
200 ASSERT (le64toh (0.0) == 0);
201 ASSERT (htole64 (0.0) == 0);
202 #endif
206 main (void)
208 test_convert_constant ();
209 test_convert_eval_once ();
210 test_convert_double ();
212 return test_exit_status;