Visual Servoing Platform  version 3.6.1 under development (2025-03-13)
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
46 using namespace VISP_NAMESPACE_NAME;
47 #endif
48 
49 TEST_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 
64 TEST_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 
73 int main(int argc, char *argv[])
74 {
75 #if defined(VISP_LITTLE_ENDIAN)
76  std::cout << "Detected endianess: LE" << std::endl;
77 #elif defined(VISP_BIG_ENDIAN)
78  std::cout << "Detected endianess: BE" << std::endl;
79 #elif defined(VISP_PDB_ENDIAN)
80  std::cout << "Detected endianess: PDB" << std::endl;
81 #else
82  std::cout << "Detected endianess: unknown" << std::endl;
83 #endif
84 
85  Catch::Session session;
86  session.applyCommandLine(argc, argv);
87  int numFailed = session.run();
88  return numFailed;
89 }
90 
91 #else
92 int main() { return EXIT_SUCCESS; }
93 #endif
VISP_EXPORT uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char *const ptr)
Definition: vpEndian.cpp:134