#include <iostream>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpEndian.h>
#if defined(VISP_HAVE_EIGEN3) && defined(VISP_HAVE_CATCH2)
#include <catch_amalgamated.hpp>
#ifdef ENABLE_VISP_NAMESPACE
#endif
TEST_CASE("Test reinterpret_cast_uchar_to_uint16_LE", "[vpEndian_test]")
{
unsigned char bitmap[] = { 100, 110, 120, 130 };
CHECK((val01 & 0x00FF) == bitmap[0]);
CHECK(((val01 & 0xFF00) >> 8) == bitmap[1]);
CHECK((val01 >> 8) == bitmap[1]);
CHECK((val12 & 0x00FF) == bitmap[2]);
CHECK(((val12 & 0xFF00) >> 8) == bitmap[3]);
CHECK((val12 >> 8) == bitmap[3]);
}
TEST_CASE("Test bitwise shift operators and zero fill", "[vpEndian_test]")
{
for (uint16_t i = 0; i < 60000; i += 500) {
REQUIRE(((i >> 8) & 0x00FF) == (i >> 8));
}
}
TEST_CASE("Test endianness conversion", "[vpEndian_test]")
{
SECTION("LE --> BE --> LE / BE --> LE --> BE")
{
SECTION("uint16_t")
{
uint16_t val_LE = 123;
}
SECTION("int16_t")
{
int16_t val_LE = -123;
}
SECTION("uint32_t")
{
uint32_t val_LE = 123456;
}
SECTION("int32_t")
{
int32_t val_LE = -123456;
}
SECTION("uint64_t")
{
uint64_t val_LE = 12345678900;
}
SECTION("int64_t")
{
int64_t val_LE = -12345678900;
}
SECTION("float")
{
float val_LE = 3.14f;
CHECK(val_LE == Catch::Approx((
vpEndian::swapFloat(val_LE_2_BE))).epsilon(std::numeric_limits<float>::epsilon()));
}
SECTION("double")
{
double val_LE = 3.14;
CHECK(val_LE == Catch::Approx((
vpEndian::swapDouble(val_LE_2_BE))).epsilon(std::numeric_limits<double>::epsilon()));
}
}
}
TEST_CASE("Test endianness detection", "[vpEndian_test]")
{
#ifdef VISP_BIG_ENDIAN
CHECK(is_big_endian);
#else
CHECK_FALSE(is_big_endian);
#endif
}
int main(int argc, char *argv[])
{
#if defined(VISP_LITTLE_ENDIAN)
std::cout << "Detected endianess: LE" << std::endl;
#elif defined(VISP_BIG_ENDIAN)
std::cout << "Detected endianess: BE" << std::endl;
#elif defined(VISP_PDB_ENDIAN)
std::cout << "Detected endianess: PDB" << std::endl;
#else
std::cout << "Detected endianess: unknown" << std::endl;
#endif
Catch::Session session;
session.applyCommandLine(argc, argv);
int numFailed = session.run();
return numFailed;
}
#else
int main() { return EXIT_SUCCESS; }
#endif
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()