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. */
24 /* Check for uint16_t and uint32_t. */
28 /* The next POSIX revision requires 64-bit types. Gnulib doesn't. */
33 /* "These macros shall be suitable for use in #if preprocessing directives." */
34 #if BYTE_ORDER == LITTLE_ENDIAN
37 #if BYTE_ORDER == BIG_ENDIAN
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
);
50 /* Test byte order conversion functions with constant values. */
52 test_convert_constant (void)
54 #if BYTE_ORDER == BIG_ENDIAN
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));
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));
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));
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));
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));
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));
105 /* Test that the byte order conversion functions evaluate their
108 test_convert_eval_once (void)
113 ASSERT (be16toh (value
++) == 0);
118 ASSERT (htobe16 (value
++) == 0);
123 ASSERT (le16toh (value
++) == 0);
128 ASSERT (htole16 (value
++) == 0);
135 ASSERT (be32toh (value
++) == 0);
140 ASSERT (htobe32 (value
++) == 0);
145 ASSERT (le32toh (value
++) == 0);
150 ASSERT (htole32 (value
++) == 0);
158 ASSERT (be64toh (value
++) == 0);
163 ASSERT (htobe64 (value
++) == 0);
168 ASSERT (le64toh (value
++) == 0);
173 ASSERT (htole64 (value
++) == 0);
179 /* Test that the byte order conversion functions accept floating-point
182 test_convert_double (void)
185 ASSERT (be16toh (0.0) == 0);
186 ASSERT (htobe16 (0.0) == 0);
187 ASSERT (le16toh (0.0) == 0);
188 ASSERT (htole16 (0.0) == 0);
191 ASSERT (be32toh (0.0) == 0);
192 ASSERT (htobe32 (0.0) == 0);
193 ASSERT (le32toh (0.0) == 0);
194 ASSERT (htole32 (0.0) == 0);
198 ASSERT (be64toh (0.0) == 0);
199 ASSERT (htobe64 (0.0) == 0);
200 ASSERT (le64toh (0.0) == 0);
201 ASSERT (htole64 (0.0) == 0);
208 test_convert_constant ();
209 test_convert_eval_once ();
210 test_convert_double ();
212 return test_exit_status
;