1 /*- 2 * Copyright (c) 2021 M. Warner Losh <imp@FreeBSD.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7 #include <sys/endian.h> 8 9 #include <atf-c.h> 10 11 ATF_TC(sys_endian); 12 ATF_TC_HEAD(sys_endian, tc) 13 { 14 atf_tc_set_md_var(tc, "descr", "Test swapping macros in <byteswap.h>"); 15 } 16 17 ATF_TC_BODY(sys_endian, tc) 18 { 19 /* FreeBSD sys/endian.h doesn't define the {__,}bswap_{16,32,64} */ 20 #ifdef __bswap_16 21 atf_tc_fail_nonfatal("__bswap_16 defined"); 22 #endif 23 #ifdef bswap_16 24 atf_tc_fail_nonfatal("bswap_16 defined"); 25 #endif 26 #ifdef __bswap_32 27 atf_tc_fail_nonfatal("__bswap_32 defined"); 28 #endif 29 #ifdef bswap_32 30 atf_tc_fail_nonfatal("bswap_32 defined"); 31 #endif 32 #ifdef __bswap_64 33 atf_tc_fail_nonfatal("__bswap_64 defined"); 34 #endif 35 #ifdef bswap_64 36 atf_tc_fail_nonfatal("bswap_64 defined"); 37 #endif 38 39 /* FreeBSD sys/endian.h does define bswap{16,32,64} */ 40 #ifndef bswap16 41 atf_tc_fail_nonfatal("bswap16 not defined"); 42 #endif 43 #ifndef bswap32 44 atf_tc_fail_nonfatal("bswap32 not defined"); 45 #endif 46 #ifndef bswap64 47 atf_tc_fail_nonfatal("bswap64 not defined"); 48 #endif 49 50 /* 51 * FreeBSD defines with one underscore 52 * We don't test for two since that doesn't interfere 53 */ 54 #ifndef _BIG_ENDIAN 55 atf_tc_fail_nonfatal("_BIG_ENDIAN not defined"); 56 #endif 57 #ifndef _LITTLE_ENDIAN 58 atf_tc_fail_nonfatal("_LITTLE_ENDIAN not defined"); 59 #endif 60 #ifndef _PDP_ENDIAN 61 atf_tc_fail_nonfatal("_PDP_ENDIAN not defined"); 62 #endif 63 #ifndef _BYTE_ORDER 64 atf_tc_fail_nonfatal("_BYTE_ORDER not defined"); 65 #endif 66 67 /* order to host */ 68 #ifdef _BYTE_ORDER 69 #if _BYTE_ORDER == _BIG_ENDIAN 70 #define H16(x) be16toh(x) 71 #define H32(x) be32toh(x) 72 #define H64(x) be64toh(x) 73 #define O16(x) le16toh(x) 74 #define O32(x) le32toh(x) 75 #define O64(x) le64toh(x) 76 #else 77 #define H16(x) le16toh(x) 78 #define H32(x) le32toh(x) 79 #define H64(x) le64toh(x) 80 #define O16(x) be16toh(x) 81 #define O32(x) be32toh(x) 82 #define O64(x) be64toh(x) 83 #endif 84 #endif 85 ATF_REQUIRE(H16(0x1234) == 0x1234); 86 ATF_REQUIRE(H32(0x12345678ul) == 0x12345678ul); 87 ATF_REQUIRE(H64(0x123456789abcdef0ull) == 0x123456789abcdef0ull); 88 ATF_REQUIRE(O16(0x1234) == __bswap16(0x1234)); 89 ATF_REQUIRE(O32(0x12345678ul) == __bswap32(0x12345678ul)); 90 ATF_REQUIRE(O64(0x123456789abcdef0ull) == __bswap64(0x123456789abcdef0ull)); 91 #undef H16 92 #undef H32 93 #undef H64 94 #undef O16 95 #undef O32 96 #undef O64 97 98 /* host to order */ 99 #ifdef _BYTE_ORDER 100 #if _BYTE_ORDER == _BIG_ENDIAN 101 #define H16(x) htobe16(x) 102 #define H32(x) htobe32(x) 103 #define H64(x) htobe64(x) 104 #define O16(x) htole16(x) 105 #define O32(x) htole32(x) 106 #define O64(x) htole64(x) 107 #else 108 #define H16(x) htole16(x) 109 #define H32(x) htole32(x) 110 #define H64(x) htole64(x) 111 #define O16(x) htobe16(x) 112 #define O32(x) htobe32(x) 113 #define O64(x) htobe64(x) 114 #endif 115 #endif 116 ATF_REQUIRE(H16(0x1234) == 0x1234); 117 ATF_REQUIRE(H32(0x12345678ul) == 0x12345678ul); 118 ATF_REQUIRE(H64(0x123456789abcdef0ull) == 0x123456789abcdef0ull); 119 ATF_REQUIRE(O16(0x1234) == __bswap16(0x1234)); 120 ATF_REQUIRE(O32(0x12345678ul) == __bswap32(0x12345678ul)); 121 ATF_REQUIRE(O64(0x123456789abcdef0ull) == __bswap64(0x123456789abcdef0ull)); 122 #undef H16 123 #undef H32 124 #undef H64 125 #undef O16 126 #undef O32 127 #undef O64 128 } 129 130 ATF_TP_ADD_TCS(tp) 131 { 132 133 ATF_TP_ADD_TC(tp, sys_endian); 134 135 return atf_no_error(); 136 } 137