test_endianness.c (1694B)
1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr) 2 * 3 * The RSys library is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published 5 * by the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * The RSys library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "endianness.h" 17 18 int 19 main(int argc, char** argv) 20 { 21 uint16_t ui16 = 0x0123; 22 uint32_t ui32 = 0x01234567; 23 uint64_t ui64 = 0x0123456789ABCDEF; 24 (void)argc, (void)argv; 25 26 CHK(byte_swap_16(ui16) == 0x2301); 27 CHK(byte_swap_32(ui32) == 0x67452301); 28 CHK(byte_swap_64(ui64) == 0xEFCDAB8967452301); 29 30 #if BYTE_ORDER == LITTLE_ENDIAN 31 CHK(little_endian_16(ui16) == ui16); 32 CHK(little_endian_32(ui32) == ui32); 33 CHK(little_endian_64(ui64) == ui64); 34 CHK(big_endian_16(ui16) == byte_swap_16(ui16)); 35 CHK(big_endian_32(ui32) == byte_swap_32(ui32)); 36 CHK(big_endian_64(ui64) == byte_swap_64(ui64)); 37 38 #elif BYTE_ORDER == BIG_ENDIAN 39 CHK(little_endian_16(ui16) == byte_swap_16(ui16)); 40 CHK(little_endian_32(ui32) == byte_swap_32(ui32)); 41 CHK(little_endian_64(ui64) == byte_swap_64(ui64)); 42 CHK(big_endian_16(ui16) == ui16); 43 CHK(big_endian_32(ui32) == ui32); 44 CHK(big_endian_64(ui64) == ui64); 45 46 #else 47 #error "Undefined endianness" 48 #endif 49 50 return 0; 51 }