Back to index

glibc  2.9
tst-endian.c
Go to the documentation of this file.
00001 #include <byteswap.h>
00002 #include <endian.h>
00003 #include <inttypes.h>
00004 #include <stdio.h>
00005 
00006 
00007 static int
00008 do_test (void)
00009 {
00010   int result = 0;
00011 
00012   for (uint64_t i = 0; i < (~UINT64_C (0)) >> 2; i = (i << 1) + 3)
00013     {
00014       if (i < UINT64_C (65536))
00015        {
00016          if (htobe16 (be16toh (i)) != i)
00017            {
00018              printf ("htobe16 (be16toh (%" PRIx64 ")) == %" PRIx16 "\n",
00019                     i, (uint16_t) htobe16 (be16toh (i)));
00020              result = 1;
00021            }
00022          if (htole16 (le16toh (i)) != i)
00023            {
00024              printf ("htole16 (le16toh (%" PRIx64 ")) == %" PRIx16 "\n",
00025                     i, (uint16_t) htole16 (le16toh (i)));
00026              result = 1;
00027            }
00028 
00029          uint16_t n[2];
00030          n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_16 (i);
00031          n[__BYTE_ORDER == __BIG_ENDIAN] = i;
00032          if (htole16 (i) != n[0])
00033            {
00034              printf ("htole16 (%" PRIx64 ") == %" PRIx16 " != %" PRIx16 "\n",
00035                     i, (uint16_t) htole16 (i), n[0]);
00036              result = 1;
00037            }
00038          if (htobe16 (i) != n[1])
00039            {
00040              printf ("htobe16 (%" PRIx64 ") == %" PRIx16 " != %" PRIx16 "\n",
00041                     i, (uint16_t) htobe16 (i), n[1]);
00042              result = 1;
00043            }
00044        }
00045 
00046       if (i < UINT64_C (4294967296))
00047        {
00048          if (htobe32 (be32toh (i)) != i)
00049            {
00050              printf ("htobe32 (be32toh (%" PRIx64 ")) == %" PRIx32 "\n",
00051                     i, (uint32_t) htobe32 (be32toh (i)));
00052              result = 1;
00053            }
00054          if (htole32 (le32toh (i)) != i)
00055            {
00056              printf ("htole32 (le32toh (%" PRIx64 ")) == %" PRIx32 "\n",
00057                     i, (uint32_t) htole32 (le32toh (i)));
00058              result = 1;
00059            }
00060 
00061          uint32_t n[2];
00062          n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_32 (i);
00063          n[__BYTE_ORDER == __BIG_ENDIAN] = i;
00064          if (htole32 (i) != n[0])
00065            {
00066              printf ("htole32 (%" PRIx64 ") == %" PRIx32 " != %" PRIx32 "\n",
00067                     i, (uint32_t) htole32 (i), n[0]);
00068              result = 1;
00069            }
00070          if (htobe32 (i) != n[1])
00071            {
00072              printf ("htobe32 (%" PRIx64 ") == %" PRIx32 " != %" PRIx32 "\n",
00073                     i, (uint32_t) htobe32 (i), n[1]);
00074              result = 1;
00075            }
00076        }
00077 
00078       if (htobe64 (be64toh (i)) != i)
00079        {
00080          printf ("htobe64 (be64toh (%" PRIx64 ")) == %" PRIx64 "\n",
00081                 i, htobe64 (be64toh (i)));
00082          result = 1;
00083        }
00084       if (htole64 (le64toh (i)) != i)
00085        {
00086          printf ("htole64 (le64toh (%" PRIx64 ")) == %" PRIx64 "\n",
00087                 i, htole64 (le64toh (i)));
00088          result = 1;
00089        }
00090 
00091       uint64_t n[2];
00092       n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_64 (i);
00093       n[__BYTE_ORDER == __BIG_ENDIAN] = i;
00094       if (htole64 (i) != n[0])
00095        {
00096          printf ("htole64 (%" PRIx64 ") == %" PRIx64 " != %" PRIx64 "\n",
00097                 i, htole64 (i), n[0]);
00098          result = 1;
00099        }
00100       if (htobe64 (i) != n[1])
00101        {
00102          printf ("htobe64 (%" PRIx64 ") == %" PRIx64 " != %" PRIx64 "\n",
00103                 i, htobe64 (i), n[1]);
00104          result = 1;
00105        }
00106     }
00107 
00108   return result;
00109 }
00110 
00111 #define TEST_FUNCTION do_test ()
00112 #include "../test-skeleton.c"