Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
catchEndian.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Test vpEndian.
32 */
33
37#include <iostream>
38#include <visp3/core/vpConfig.h>
39#include <visp3/core/vpEndian.h>
40
41#if defined(VISP_HAVE_EIGEN3) && defined(VISP_HAVE_CATCH2)
42
43#include <catch_amalgamated.hpp>
44
45#ifdef ENABLE_VISP_NAMESPACE
46using namespace VISP_NAMESPACE_NAME;
47#endif
48
49TEST_CASE("Test reinterpret_cast_uchar_to_uint16_LE", "[vpEndian_test]")
50{
51 unsigned char bitmap[] = { 100, 110, 120, 130 };
52 uint16_t val01 = vpEndian::reinterpret_cast_uchar_to_uint16_LE(bitmap);
53 uint16_t val12 = vpEndian::reinterpret_cast_uchar_to_uint16_LE(bitmap + 2);
54
55 CHECK((val01 & 0x00FF) == bitmap[0]);
56 CHECK(((val01 & 0xFF00) >> 8) == bitmap[1]);
57 CHECK((val01 >> 8) == bitmap[1]);
58
59 CHECK((val12 & 0x00FF) == bitmap[2]);
60 CHECK(((val12 & 0xFF00) >> 8) == bitmap[3]);
61 CHECK((val12 >> 8) == bitmap[3]);
62}
63
64TEST_CASE("Test bitwise shift operators and zero fill", "[vpEndian_test]")
65{
66 // https://docs.microsoft.com/en-us/cpp/cpp/left-shift-and-right-shift-operators-input-and-output?view=vs-2019
67 // https://devblogs.microsoft.com/cppblog/hello-arm-exploring-undefined-unspecified-and-implementation-defined-behavior-in-c/
68 for (uint16_t i = 0; i < 60000; i += 500) {
69 REQUIRE(((i >> 8) & 0x00FF) == (i >> 8));
70 }
71}
72
73TEST_CASE("Test endianness conversion", "[vpEndian_test]")
74{
75 SECTION("LE --> BE --> LE / BE --> LE --> BE")
76 {
77 SECTION("uint16_t")
78 {
79 uint16_t val_LE = 123;
80 uint16_t val_LE_2_BE = vpEndian::swap16bits(val_LE);
81 CHECK(val_LE == vpEndian::swap16bits(val_LE_2_BE));
82 }
83 SECTION("int16_t")
84 {
85 int16_t val_LE = -123;
86 int16_t val_LE_2_BE = static_cast<int16_t>(vpEndian::swap16bits(static_cast<uint16_t>(val_LE)));
87 CHECK(val_LE == static_cast<int16_t>(vpEndian::swap16bits(static_cast<uint16_t>(val_LE_2_BE))));
88 }
89
90 SECTION("uint32_t")
91 {
92 uint32_t val_LE = 123456;
93 uint32_t val_LE_2_BE = vpEndian::swap32bits(val_LE);
94 CHECK(val_LE == vpEndian::swap32bits(val_LE_2_BE));
95 }
96 SECTION("int32_t")
97 {
98 int32_t val_LE = -123456;
99 int32_t val_LE_2_BE = static_cast<int32_t>(vpEndian::swap32bits(static_cast<uint32_t>(val_LE)));
100 CHECK(val_LE == static_cast<int32_t>(vpEndian::swap32bits(static_cast<uint32_t>(val_LE_2_BE))));
101 }
102
103 SECTION("uint64_t")
104 {
105 uint64_t val_LE = 12345678900;
106 uint64_t val_LE_2_BE = vpEndian::swap64bits(val_LE);
107 CHECK(val_LE == vpEndian::swap64bits(val_LE_2_BE));
108 }
109 SECTION("int64_t")
110 {
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))));
114 }
115
116 SECTION("float")
117 {
118 float val_LE = 3.14f;
119 float val_LE_2_BE = vpEndian::swapFloat(val_LE);
120 CHECK(val_LE == Catch::Approx((vpEndian::swapFloat(val_LE_2_BE))).epsilon(std::numeric_limits<float>::epsilon()));
121 }
122 SECTION("double")
123 {
124 double val_LE = 3.14;
125 double val_LE_2_BE = vpEndian::swapDouble(val_LE);
126 CHECK(val_LE == Catch::Approx((vpEndian::swapDouble(val_LE_2_BE))).epsilon(std::numeric_limits<double>::epsilon()));
127 }
128 }
129}
130
131TEST_CASE("Test endianness detection", "[vpEndian_test]")
132{
133 bool is_big_endian = vpEndian::isBigEndian();
134#ifdef VISP_BIG_ENDIAN
135 CHECK(is_big_endian);
136#else
137 CHECK_FALSE(is_big_endian);
138#endif
139}
140
141int main(int argc, char *argv[])
142{
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;
149#else
150 std::cout << "Detected endianess: unknown" << std::endl;
151#endif
152
153 Catch::Session session;
154 session.applyCommandLine(argc, argv);
155 int numFailed = session.run();
156 return numFailed;
157}
158
159#else
160int main() { return EXIT_SUCCESS; }
161#endif
VISP_EXPORT float swapFloat(float f)
Definition vpEndian.cpp:104
VISP_EXPORT uint32_t swap32bits(uint32_t val)
Definition vpEndian.cpp:60
VISP_EXPORT uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char *const ptr)
Definition vpEndian.cpp:161
VISP_EXPORT double swapDouble(double d)
Definition vpEndian.cpp:128
VISP_EXPORT uint64_t swap64bits(uint64_t val)
Definition vpEndian.cpp:76
VISP_EXPORT uint16_t swap16bits(uint16_t val)
Definition vpEndian.cpp:48
VISP_EXPORT bool isBigEndian()
Definition vpEndian.cpp:175