debug: Add fortify dprintf tests
[glibc.git] / string / tst-endian.c
blobb156ede1999da8250314689be824f0d2b30e15d5
1 #include <byteswap.h>
2 #include <endian.h>
3 #include <inttypes.h>
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <libc-diag.h>
8 #if __GNUC_PREREQ (6, 0)
9 /* GCC 6.0 warns on big endian systems about:
10 htobeXX (beXXtoh (i)) != i
11 warning: self-comparison always evaluates to false [-Wtautological-compare]
12 because htobeXX(x) and beXXtoh(x) is defined to (x)
13 in string/endian.h on big endian systems.
14 The same applies to htoleXX/leXXtoh on little endian systems. */
15 # define DIAG_IGNORE_NEEDS_COMMENT_TAUTOLOGICAL_COMPARE() \
16 DIAG_IGNORE_NEEDS_COMMENT (6, "-Wtautological-compare")
17 #else
18 # define DIAG_IGNORE_NEEDS_COMMENT_TAUTOLOGICAL_COMPARE()
19 #endif
21 int
22 do_test (void)
24 int result = 0;
26 for (uint64_t i = 0; i < (~UINT64_C (0)) >> 2; i = (i << 1) + 3)
28 if (i < UINT64_C (65536))
30 DIAG_PUSH_NEEDS_COMMENT;
31 DIAG_IGNORE_NEEDS_COMMENT_TAUTOLOGICAL_COMPARE ();
32 if (htobe16 (be16toh (i)) != i)
34 printf ("htobe16 (be16toh (%" PRIx64 ")) == %" PRIx16 "\n",
35 i, (uint16_t) htobe16 (be16toh (i)));
36 result = 1;
38 if (htole16 (le16toh (i)) != i)
40 printf ("htole16 (le16toh (%" PRIx64 ")) == %" PRIx16 "\n",
41 i, (uint16_t) htole16 (le16toh (i)));
42 result = 1;
44 DIAG_POP_NEEDS_COMMENT;
46 uint16_t n[2];
47 n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_16 (i);
48 n[__BYTE_ORDER == __BIG_ENDIAN] = i;
49 if (htole16 (i) != n[0])
51 printf ("htole16 (%" PRIx64 ") == %" PRIx16 " != %" PRIx16 "\n",
52 i, (uint16_t) htole16 (i), n[0]);
53 result = 1;
55 if (htobe16 (i) != n[1])
57 printf ("htobe16 (%" PRIx64 ") == %" PRIx16 " != %" PRIx16 "\n",
58 i, (uint16_t) htobe16 (i), n[1]);
59 result = 1;
63 if (i < UINT64_C (4294967296))
65 DIAG_PUSH_NEEDS_COMMENT;
66 DIAG_IGNORE_NEEDS_COMMENT_TAUTOLOGICAL_COMPARE ();
67 if (htobe32 (be32toh (i)) != i)
69 printf ("htobe32 (be32toh (%" PRIx64 ")) == %" PRIx32 "\n",
70 i, (uint32_t) htobe32 (be32toh (i)));
71 result = 1;
73 if (htole32 (le32toh (i)) != i)
75 printf ("htole32 (le32toh (%" PRIx64 ")) == %" PRIx32 "\n",
76 i, (uint32_t) htole32 (le32toh (i)));
77 result = 1;
79 DIAG_POP_NEEDS_COMMENT;
81 uint32_t n[2];
82 n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_32 (i);
83 n[__BYTE_ORDER == __BIG_ENDIAN] = i;
84 if (htole32 (i) != n[0])
86 printf ("htole32 (%" PRIx64 ") == %" PRIx32 " != %" PRIx32 "\n",
87 i, (uint32_t) htole32 (i), n[0]);
88 result = 1;
90 if (htobe32 (i) != n[1])
92 printf ("htobe32 (%" PRIx64 ") == %" PRIx32 " != %" PRIx32 "\n",
93 i, (uint32_t) htobe32 (i), n[1]);
94 result = 1;
98 DIAG_PUSH_NEEDS_COMMENT;
99 DIAG_IGNORE_NEEDS_COMMENT_TAUTOLOGICAL_COMPARE ();
100 if (htobe64 (be64toh (i)) != i)
102 printf ("htobe64 (be64toh (%" PRIx64 ")) == %" PRIx64 "\n",
103 i, htobe64 (be64toh (i)));
104 result = 1;
106 if (htole64 (le64toh (i)) != i)
108 printf ("htole64 (le64toh (%" PRIx64 ")) == %" PRIx64 "\n",
109 i, htole64 (le64toh (i)));
110 result = 1;
112 DIAG_POP_NEEDS_COMMENT;
114 uint64_t n[2];
115 n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_64 (i);
116 n[__BYTE_ORDER == __BIG_ENDIAN] = i;
117 if (htole64 (i) != n[0])
119 printf ("htole64 (%" PRIx64 ") == %" PRIx64 " != %" PRIx64 "\n",
120 i, htole64 (i), n[0]);
121 result = 1;
123 if (htobe64 (i) != n[1])
125 printf ("htobe64 (%" PRIx64 ") == %" PRIx64 " != %" PRIx64 "\n",
126 i, htobe64 (i), n[1]);
127 result = 1;
131 return result;
134 #include <support/test-driver.c>