.
[glibc/history.git] / string / tst-endian.c
blobc34dc456a773371d5ac8ee892743e4cc21880983
1 #include <byteswap.h>
2 #include <endian.h>
3 #include <inttypes.h>
4 #include <stdio.h>
7 static int
8 do_test (void)
10 int result = 0;
12 for (uint64_t i = 0; i < (~UINT64_C (0)) >> 2; i = (i << 1) + 3)
14 if (i < UINT64_C (65536))
16 if (htobe16 (be16toh (i)) != i)
18 printf ("htobe16 (be16toh (%" PRIx64 ")) == %" PRIx16 "\n",
19 i, (uint16_t) htobe16 (be16toh (i)));
20 result = 1;
22 if (htole16 (le16toh (i)) != i)
24 printf ("htole16 (le16toh (%" PRIx64 ")) == %" PRIx16 "\n",
25 i, (uint16_t) htole16 (le16toh (i)));
26 result = 1;
29 uint16_t n[2];
30 n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_16 (i);
31 n[__BYTE_ORDER == __BIG_ENDIAN] = i;
32 if (htole16 (i) != n[0])
34 printf ("htole16 (%" PRIx64 ") == %" PRIx16 " != %" PRIx16 "\n",
35 i, (uint16_t) htole16 (i), n[0]);
36 result = 1;
38 if (htobe16 (i) != n[1])
40 printf ("htobe16 (%" PRIx64 ") == %" PRIx16 " != %" PRIx16 "\n",
41 i, (uint16_t) htobe16 (i), n[1]);
42 result = 1;
46 if (i < UINT64_C (4294967296))
48 if (htobe32 (be32toh (i)) != i)
50 printf ("htobe32 (be32toh (%" PRIx64 ")) == %" PRIx32 "\n",
51 i, (uint32_t) htobe32 (be32toh (i)));
52 result = 1;
54 if (htole32 (le32toh (i)) != i)
56 printf ("htole32 (le32toh (%" PRIx64 ")) == %" PRIx32 "\n",
57 i, (uint32_t) htole32 (le32toh (i)));
58 result = 1;
61 uint32_t n[2];
62 n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_32 (i);
63 n[__BYTE_ORDER == __BIG_ENDIAN] = i;
64 if (htole32 (i) != n[0])
66 printf ("htole32 (%" PRIx64 ") == %" PRIx32 " != %" PRIx32 "\n",
67 i, (uint32_t) htole32 (i), n[0]);
68 result = 1;
70 if (htobe32 (i) != n[1])
72 printf ("htobe32 (%" PRIx64 ") == %" PRIx32 " != %" PRIx32 "\n",
73 i, (uint32_t) htobe32 (i), n[1]);
74 result = 1;
78 if (htobe64 (be64toh (i)) != i)
80 printf ("htobe64 (be64toh (%" PRIx64 ")) == %" PRIx64 "\n",
81 i, htobe64 (be64toh (i)));
82 result = 1;
84 if (htole64 (le64toh (i)) != i)
86 printf ("htole64 (le64toh (%" PRIx64 ")) == %" PRIx64 "\n",
87 i, htole64 (le64toh (i)));
88 result = 1;
91 uint64_t n[2];
92 n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_64 (i);
93 n[__BYTE_ORDER == __BIG_ENDIAN] = i;
94 if (htole64 (i) != n[0])
96 printf ("htole64 (%" PRIx64 ") == %" PRIx64 " != %" PRIx64 "\n",
97 i, htole64 (i), n[0]);
98 result = 1;
100 if (htobe64 (i) != n[1])
102 printf ("htobe64 (%" PRIx64 ") == %" PRIx64 " != %" PRIx64 "\n",
103 i, htobe64 (i), n[1]);
104 result = 1;
108 return result;
111 #define TEST_FUNCTION do_test ()
112 #include "../test-skeleton.c"