rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit 639e6c20097f82c766d8c0f94c609705ff59359e
parent b38dbe74d038c9fabec0827af23e562cc6558b28
Author: vaplv <vaplv@free.fr>
Date:   Fri, 11 Sep 2020 11:55:33 +0200

Test the endianness functions

Diffstat:
Mcmake/CMakeLists.txt | 1+
Asrc/test_endianness.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -210,6 +210,7 @@ if(NOT NO_TEST) new_test(test_double22) new_test(test_double33 ${MATH_LIB}) new_test(test_double44) + new_test(test_endianness) new_test(test_dynamic_array rsys) new_test(test_float2 ${MATH_LIB}) new_test(test_float3 ${MATH_LIB}) diff --git a/src/test_endianness.c b/src/test_endianness.c @@ -0,0 +1,51 @@ +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) + * + * The RSys library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The RSys library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ + +#include "endianness.h" + +int +main(int argc, char** argv) +{ + uint16_t ui16 = 0x0123; + uint32_t ui32 = 0x01234567; + uint64_t ui64 = 0x0123456789ABCDEF; + (void)argc, (void)argv; + + CHK(byte_swap_16(ui16) == 0x2301); + CHK(byte_swap_32(ui32) == 0x67452301); + CHK(byte_swap_64(ui64) == 0xEFCDAB8967452301); + +#if BYTE_ORDER == LITTLE_ENDIAN + CHK(little_endian_16(ui16) == ui16); + CHK(little_endian_32(ui32) == ui32); + CHK(little_endian_64(ui64) == ui64); + CHK(big_endian_16(ui16) == byte_swap_16(ui16)); + CHK(big_endian_32(ui32) == byte_swap_32(ui32)); + CHK(big_endian_64(ui64) == byte_swap_64(ui64)); + +#elif BYTE_ORDER == BIG_ENDIAN + CHK(little_endian_16(ui16) == byte_swap_16(ui16)); + CHK(little_endian_32(ui32) == byte_swap_32(ui32)); + CHK(little_endian_64(ui64) == byte_swap_64(ui64)); + CHK(big_endian_16(ui16) == ui16); + CHK(big_endian_32(ui32) == ui32); + CHK(big_endian_64(ui64) == ui64); + +#else + #error "Undefined endianness" +#endif + + return 0; +}