Visual Servoing Platform  version 3.6.1 under development (2024-05-16)
testEndian.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test vpEndian.
33  *
34 *****************************************************************************/
35 
36 #include <iostream>
37 #include <visp3/core/vpConfig.h>
38 #include <visp3/core/vpEndian.h>
39 
40 #if defined(VISP_HAVE_EIGEN3) && defined(VISP_HAVE_CATCH2)
41 #define CATCH_CONFIG_RUNNER
42 #include <catch.hpp>
43 
44 TEST_CASE("Test reinterpret_cast_uchar_to_uint16_LE", "[vpEndian_test]")
45 {
46  unsigned char bitmap[] = {100, 110, 120, 130};
47  uint16_t val01 = vpEndian::reinterpret_cast_uchar_to_uint16_LE(bitmap);
48  uint16_t val12 = vpEndian::reinterpret_cast_uchar_to_uint16_LE(bitmap + 2);
49 
50  CHECK((val01 & 0x00FF) == bitmap[0]);
51  CHECK(((val01 & 0xFF00) >> 8) == bitmap[1]);
52  CHECK((val01 >> 8) == bitmap[1]);
53 
54  CHECK((val12 & 0x00FF) == bitmap[2]);
55  CHECK(((val12 & 0xFF00) >> 8) == bitmap[3]);
56  CHECK((val12 >> 8) == bitmap[3]);
57 }
58 
59 TEST_CASE("Test bitwise shift operators and zero fill", "[vpEndian_test]")
60 {
61  // https://docs.microsoft.com/en-us/cpp/cpp/left-shift-and-right-shift-operators-input-and-output?view=vs-2019
62  // https://devblogs.microsoft.com/cppblog/hello-arm-exploring-undefined-unspecified-and-implementation-defined-behavior-in-c/
63  for (uint16_t i = 0; i < 60000; i += 500) {
64  REQUIRE(((i >> 8) & 0x00FF) == (i >> 8));
65  }
66 }
67 
68 int main(int argc, char *argv[])
69 {
70 #if defined(VISP_LITTLE_ENDIAN)
71  std::cout << "Detected endianess: LE" << std::endl;
72 #elif defined(VISP_BIG_ENDIAN)
73  std::cout << "Detected endianess: BE" << std::endl;
74 #elif defined(VISP_PDB_ENDIAN)
75  std::cout << "Detected endianess: PDB" << std::endl;
76 #else
77  std::cout << "Detected endianess: unknown" << std::endl;
78 #endif
79 
80  Catch::Session session; // There must be exactly one instance
81 
82  // Let Catch (using Clara) parse the command line
83  session.applyCommandLine(argc, argv);
84 
85  int numFailed = session.run();
86 
87  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
88  // This clamping has already been applied, so just return it here
89  // You can also do any post run clean-up here
90  return numFailed;
91 }
92 #else
93 int main() { return EXIT_SUCCESS; }
94 #endif
VISP_EXPORT uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char *const ptr)
Definition: vpEndian.cpp:108