38#include <visp3/core/vpConfig.h>
39#include <visp3/core/vpEndian.h>
41#if defined(VISP_HAVE_EIGEN3) && defined(VISP_HAVE_CATCH2)
43#include <catch_amalgamated.hpp>
45#ifdef ENABLE_VISP_NAMESPACE
49TEST_CASE(
"Test reinterpret_cast_uchar_to_uint16_LE",
"[vpEndian_test]")
51 unsigned char bitmap[] = { 100, 110, 120, 130 };
55 CHECK((val01 & 0x00FF) == bitmap[0]);
56 CHECK(((val01 & 0xFF00) >> 8) == bitmap[1]);
57 CHECK((val01 >> 8) == bitmap[1]);
59 CHECK((val12 & 0x00FF) == bitmap[2]);
60 CHECK(((val12 & 0xFF00) >> 8) == bitmap[3]);
61 CHECK((val12 >> 8) == bitmap[3]);
64TEST_CASE(
"Test bitwise shift operators and zero fill",
"[vpEndian_test]")
68 for (uint16_t i = 0;
i < 60000;
i += 500) {
69 REQUIRE(((i >> 8) & 0x00FF) == (i >> 8));
73TEST_CASE(
"Test endianness conversion",
"[vpEndian_test]")
75 SECTION(
"LE --> BE --> LE / BE --> LE --> BE")
79 uint16_t val_LE = 123;
85 int16_t val_LE = -123;
92 uint32_t val_LE = 123456;
98 int32_t val_LE = -123456;
100 CHECK(val_LE ==
static_cast<int32_t
>(
vpEndian::swap32bits(
static_cast<uint32_t
>(val_LE_2_BE))));
105 uint64_t val_LE = 12345678900;
111 int64_t val_LE = -12345678900;
112 int64_t val_LE_2_BE =
static_cast<int64_t
>(
vpEndian::swap64bits(
static_cast<uint64_t
>(val_LE)));
113 CHECK(val_LE ==
static_cast<int64_t
>(
vpEndian::swap64bits(
static_cast<uint64_t
>(val_LE_2_BE))));
118 float val_LE = 3.14f;
120 CHECK(val_LE == Catch::Approx((
vpEndian::swapFloat(val_LE_2_BE))).epsilon(std::numeric_limits<float>::epsilon()));
124 double val_LE = 3.14;
126 CHECK(val_LE == Catch::Approx((
vpEndian::swapDouble(val_LE_2_BE))).epsilon(std::numeric_limits<double>::epsilon()));
131TEST_CASE(
"Test endianness detection",
"[vpEndian_test]")
134#ifdef VISP_BIG_ENDIAN
135 CHECK(is_big_endian);
137 CHECK_FALSE(is_big_endian);
141int main(
int argc,
char *argv[])
143#if defined(VISP_LITTLE_ENDIAN)
144 std::cout <<
"Detected endianess: LE" << std::endl;
145#elif defined(VISP_BIG_ENDIAN)
146 std::cout <<
"Detected endianess: BE" << std::endl;
147#elif defined(VISP_PDB_ENDIAN)
148 std::cout <<
"Detected endianess: PDB" << std::endl;
150 std::cout <<
"Detected endianess: unknown" << std::endl;
153 Catch::Session session;
154 session.applyCommandLine(argc, argv);
155 int numFailed = session.run();
160int main() {
return EXIT_SUCCESS; }
VISP_EXPORT float swapFloat(float f)
VISP_EXPORT uint32_t swap32bits(uint32_t val)
VISP_EXPORT uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char *const ptr)
VISP_EXPORT double swapDouble(double d)
VISP_EXPORT uint64_t swap64bits(uint64_t val)
VISP_EXPORT uint16_t swap16bits(uint16_t val)
VISP_EXPORT bool isBigEndian()