lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <byteswap.h> |
| 2 | #include <endian.h> |
| 3 | #include <inttypes.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | static int |
| 8 | do_test (void) |
| 9 | { |
| 10 | int result = 0; |
| 11 | |
| 12 | for (uint64_t i = 0; i < (~UINT64_C (0)) >> 2; i = (i << 1) + 3) |
| 13 | { |
| 14 | if (i < UINT64_C (65536)) |
| 15 | { |
| 16 | if (htobe16 (be16toh (i)) != i) |
| 17 | { |
| 18 | printf ("htobe16 (be16toh (%" PRIx64 ")) == %" PRIx16 "\n", |
| 19 | i, (uint16_t) htobe16 (be16toh (i))); |
| 20 | result = 1; |
| 21 | } |
| 22 | if (htole16 (le16toh (i)) != i) |
| 23 | { |
| 24 | printf ("htole16 (le16toh (%" PRIx64 ")) == %" PRIx16 "\n", |
| 25 | i, (uint16_t) htole16 (le16toh (i))); |
| 26 | result = 1; |
| 27 | } |
| 28 | |
| 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]) |
| 33 | { |
| 34 | printf ("htole16 (%" PRIx64 ") == %" PRIx16 " != %" PRIx16 "\n", |
| 35 | i, (uint16_t) htole16 (i), n[0]); |
| 36 | result = 1; |
| 37 | } |
| 38 | if (htobe16 (i) != n[1]) |
| 39 | { |
| 40 | printf ("htobe16 (%" PRIx64 ") == %" PRIx16 " != %" PRIx16 "\n", |
| 41 | i, (uint16_t) htobe16 (i), n[1]); |
| 42 | result = 1; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if (i < UINT64_C (4294967296)) |
| 47 | { |
| 48 | if (htobe32 (be32toh (i)) != i) |
| 49 | { |
| 50 | printf ("htobe32 (be32toh (%" PRIx64 ")) == %" PRIx32 "\n", |
| 51 | i, (uint32_t) htobe32 (be32toh (i))); |
| 52 | result = 1; |
| 53 | } |
| 54 | if (htole32 (le32toh (i)) != i) |
| 55 | { |
| 56 | printf ("htole32 (le32toh (%" PRIx64 ")) == %" PRIx32 "\n", |
| 57 | i, (uint32_t) htole32 (le32toh (i))); |
| 58 | result = 1; |
| 59 | } |
| 60 | |
| 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]) |
| 65 | { |
| 66 | printf ("htole32 (%" PRIx64 ") == %" PRIx32 " != %" PRIx32 "\n", |
| 67 | i, (uint32_t) htole32 (i), n[0]); |
| 68 | result = 1; |
| 69 | } |
| 70 | if (htobe32 (i) != n[1]) |
| 71 | { |
| 72 | printf ("htobe32 (%" PRIx64 ") == %" PRIx32 " != %" PRIx32 "\n", |
| 73 | i, (uint32_t) htobe32 (i), n[1]); |
| 74 | result = 1; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if (htobe64 (be64toh (i)) != i) |
| 79 | { |
| 80 | printf ("htobe64 (be64toh (%" PRIx64 ")) == %" PRIx64 "\n", |
| 81 | i, htobe64 (be64toh (i))); |
| 82 | result = 1; |
| 83 | } |
| 84 | if (htole64 (le64toh (i)) != i) |
| 85 | { |
| 86 | printf ("htole64 (le64toh (%" PRIx64 ")) == %" PRIx64 "\n", |
| 87 | i, htole64 (le64toh (i))); |
| 88 | result = 1; |
| 89 | } |
| 90 | |
| 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]) |
| 95 | { |
| 96 | printf ("htole64 (%" PRIx64 ") == %" PRIx64 " != %" PRIx64 "\n", |
| 97 | i, htole64 (i), n[0]); |
| 98 | result = 1; |
| 99 | } |
| 100 | if (htobe64 (i) != n[1]) |
| 101 | { |
| 102 | printf ("htobe64 (%" PRIx64 ") == %" PRIx64 " != %" PRIx64 "\n", |
| 103 | i, htobe64 (i), n[1]); |
| 104 | result = 1; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | #define TEST_FUNCTION do_test () |
| 112 | #include "../test-skeleton.c" |