1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright 2016 Joyent, Inc. 14 */ 15 16 /* 17 * Test endian(3C). 18 */ 19 20 #include <sys/types.h> 21 #include <endian.h> 22 #include <sys/debug.h> 23 24 #ifndef BIG_ENDIAN 25 #error "Missing BIG_ENDIAN definition" 26 #endif 27 28 #ifndef LITTLE_ENDIAN 29 #error "Missing LITTLE_ENDIAN definition" 30 #endif 31 32 static void 33 endian_fromhost(void) 34 { 35 uint16_t val16 = 0x1122; 36 uint32_t val32 = 0x11223344; 37 uint64_t val64 = 0x1122334455667788ULL; 38 uint16_t ebe16, ele16, test16; 39 uint32_t ebe32, ele32, test32; 40 uint64_t ebe64, ele64, test64; 41 42 #if defined(_LITTLE_ENDIAN) 43 ebe16 = 0x2211; 44 ebe32 = 0x44332211UL; 45 ebe64 = 0x8877665544332211ULL; 46 ele16 = 0x1122; 47 ele32 = 0x11223344UL; 48 ele64 = 0x1122334455667788ULL; 49 #elif defined(_BIG_ENDIAN) 50 ele16 = 0x2211; 51 ele32 = 0x44332211UL; 52 ele64 = 0x8877665544332211ULL; 53 ebe16 = 0x1122; 54 ebe32 = 0x11223344UL; 55 ebe64 = 0x1122334455667788ULL; 56 #else 57 #error "Unknown byte order" 58 #endif /* _LITTLE_ENDIAN */ 59 60 test16 = htobe16(val16); 61 VERIFY3U(test16, ==, ebe16); 62 test32 = htobe32(val32); 63 VERIFY3U(test32, ==, ebe32); 64 test64 = htobe64(val64); 65 VERIFY3U(test64, ==, ebe64); 66 67 test16 = htole16(val16); 68 VERIFY3U(test16, ==, ele16); 69 test32 = htole32(val32); 70 VERIFY3U(test32, ==, ele32); 71 test64 = htole64(val64); 72 VERIFY3U(test64, ==, ele64); 73 } 74 75 static void 76 endian_frombig(void) 77 { 78 uint16_t val16 = 0x1122; 79 uint32_t val32 = 0x11223344; 80 uint64_t val64 = 0x1122334455667788ULL; 81 uint16_t e16, test16; 82 uint32_t e32, test32; 83 uint64_t e64, test64; 84 85 #if defined(_LITTLE_ENDIAN) 86 e16 = 0x2211; 87 e32 = 0x44332211UL; 88 e64 = 0x8877665544332211ULL; 89 #elif defined(_BIG_ENDIAN) 90 e16 = 0x1122; 91 e32 = 0x11223344UL; 92 e64 = 0x1122334455667788ULL; 93 #else 94 #error "Unknown byte order" 95 #endif /* _LITTLE_ENDIAN */ 96 97 test16 = be16toh(val16); 98 VERIFY3U(test16, ==, e16); 99 test16 = betoh16(val16); 100 VERIFY3U(test16, ==, e16); 101 102 test32 = be32toh(val32); 103 VERIFY3U(test32, ==, e32); 104 test32 = betoh32(val32); 105 VERIFY3U(test32, ==, e32); 106 107 test64 = be64toh(val64); 108 VERIFY3U(test64, ==, e64); 109 test64 = betoh64(val64); 110 VERIFY3U(test64, ==, e64); 111 } 112 113 static void 114 endian_fromlittle(void) 115 { 116 uint16_t val16 = 0x1122; 117 uint32_t val32 = 0x11223344; 118 uint64_t val64 = 0x1122334455667788ULL; 119 uint16_t e16, test16; 120 uint32_t e32, test32; 121 uint64_t e64, test64; 122 123 #if defined(_LITTLE_ENDIAN) 124 e16 = 0x1122; 125 e32 = 0x11223344UL; 126 e64 = 0x1122334455667788ULL; 127 #elif defined(_BIG_ENDIAN) 128 e16 = 0x2211; 129 e32 = 0x44332211UL; 130 e64 = 0x8877665544332211ULL; 131 #else 132 #error "Unknown byte order" 133 #endif /* _LITTLE_ENDIAN */ 134 135 test16 = le16toh(val16); 136 VERIFY3U(test16, ==, e16); 137 test16 = letoh16(val16); 138 VERIFY3U(test16, ==, e16); 139 140 test32 = le32toh(val32); 141 VERIFY3U(test32, ==, e32); 142 test32 = letoh32(val32); 143 VERIFY3U(test32, ==, e32); 144 145 test64 = le64toh(val64); 146 VERIFY3U(test64, ==, e64); 147 test64 = letoh64(val64); 148 VERIFY3U(test64, ==, e64); 149 } 150 151 int 152 main(void) 153 { 154 endian_fromhost(); 155 endian_frombig(); 156 endian_fromlittle(); 157 return (0); 158 } 159