Visual Servoing Platform  version 3.4.0
testEndian.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  unsigned char bitmap[] = {100, 110, 120, 130};
46  uint16_t val01 = vpEndian::reinterpret_cast_uchar_to_uint16_LE(bitmap);
47  uint16_t val12 = vpEndian::reinterpret_cast_uchar_to_uint16_LE(bitmap + 2);
48 
49  CHECK((val01 & 0x00FF) == bitmap[0]);
50  CHECK(((val01 & 0xFF00) >> 8) == bitmap[1]);
51  CHECK((val01 >> 8) == bitmap[1]);
52 
53  CHECK((val12 & 0x00FF) == bitmap[2]);
54  CHECK(((val12 & 0xFF00) >> 8) == bitmap[3]);
55  CHECK((val12 >> 8)== bitmap[3]);
56 }
57 
58 TEST_CASE("Test bitwise shift operators and zero fill", "[vpEndian_test]") {
59  //https://docs.microsoft.com/en-us/cpp/cpp/left-shift-and-right-shift-operators-input-and-output?view=vs-2019
60  //https://devblogs.microsoft.com/cppblog/hello-arm-exploring-undefined-unspecified-and-implementation-defined-behavior-in-c/
61  for (uint16_t i = 0; i < 60000; i += 500) {
62  REQUIRE(((i >> 8) & 0x00FF) == (i >> 8));
63  }
64 }
65 
66 int main(int argc, char *argv[])
67 {
68 #if defined(VISP_LITTLE_ENDIAN)
69  std::cout << "Detected endianess: LE" << std::endl;
70 #elif defined(VISP_BIG_ENDIAN)
71  std::cout << "Detected endianess: BE" << std::endl;
72 #elif defined(VISP_PDB_ENDIAN)
73  std::cout << "Detected endianess: PDB" << std::endl;
74 #else
75  std::cout << "Detected endianess: unknown" << std::endl;
76 #endif
77 
78  Catch::Session session; // There must be exactly one instance
79 
80  // Let Catch (using Clara) parse the command line
81  session.applyCommandLine(argc, argv);
82 
83  int numFailed = session.run();
84 
85  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
86  // This clamping has already been applied, so just return it here
87  // You can also do any post run clean-up here
88  return numFailed;
89 }
90 #else
91 int main()
92 {
93  return 0;
94 }
95 #endif
VISP_EXPORT uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char *const ptr)
Definition: vpEndian.cpp:111