MRPT  1.9.9
reverse_bytes_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
11 #include <mrpt/core/bit_cast.h>
13 
14 // Load data from constant file and check for exact match.
16 {
17  {
18  uint8_t val = 0x88, val_r;
19  const uint8_t val_r_ok = 0x88;
20 
21  mrpt::reverseBytes(val, val_r);
22  EXPECT_EQ(val_r, val_r_ok);
24  EXPECT_EQ(val, val_r_ok);
25  }
26  {
27  int8_t val = 0x66, val_r;
28  const int8_t val_r_ok = 0x66;
29 
30  mrpt::reverseBytes(val, val_r);
31  EXPECT_EQ(val_r, val_r_ok);
33  EXPECT_EQ(val, val_r_ok);
34  }
35  {
36  uint16_t val = 0x1122, val_r;
37  const uint16_t val_r_ok = 0x2211;
38 
39  mrpt::reverseBytes(val, val_r);
40  EXPECT_EQ(val_r, val_r_ok);
42  EXPECT_EQ(val, val_r_ok);
43  }
44  {
45  int16_t val = 0x1122, val_r;
46  const int16_t val_r_ok = 0x2211;
47 
48  mrpt::reverseBytes(val, val_r);
49  EXPECT_EQ(val_r, val_r_ok);
51  EXPECT_EQ(val, val_r_ok);
52  }
53  {
54  uint32_t val = UINT32_C(0x11223344), val_r;
55  const uint32_t val_r_ok = UINT32_C(0x44332211);
56 
57  mrpt::reverseBytes(val, val_r);
58  EXPECT_EQ(val_r, val_r_ok);
60  EXPECT_EQ(val, val_r_ok);
61  }
62  {
63  int32_t val = INT32_C(0x11223344), val_r;
64  const int32_t val_r_ok = INT32_C(0x44332211);
65 
66  mrpt::reverseBytes(val, val_r);
67  EXPECT_EQ(val_r, val_r_ok);
69  EXPECT_EQ(val, val_r_ok);
70  }
71  {
72  uint64_t val = UINT64_C(0x1122334455667788), val_r;
73  const uint64_t val_r_ok = UINT64_C(0x8877665544332211);
74 
75  mrpt::reverseBytes(val, val_r);
76  EXPECT_EQ(val_r, val_r_ok);
78  EXPECT_EQ(val, val_r_ok);
79  }
80  {
81  int64_t val = INT64_C(0x1122334455667766), val_r;
82  const int64_t val_r_ok = INT64_C(0x6677665544332211);
83 
84  mrpt::reverseBytes(val, val_r);
85  EXPECT_EQ(val_r, val_r_ok);
87  EXPECT_EQ(val, val_r_ok);
88  }
89  {
90  bool valTrue = true;
91  bool valFalse = false;
93  mrpt::reverseBytesInPlace(valFalse);
94  EXPECT_TRUE(valTrue);
95  EXPECT_FALSE(valFalse);
96  }
97  {
98  uint8_t val = UINT8_C(0xc1);
99 
101  EXPECT_EQ(val, 0xc1);
102  }
103  {
104  // 1.0 == 0x3F800000
105  float val = 1;
106  const uint32_t val_r_ok = UINT32_C(0x803f);
107 
109  auto val_check = bit_cast<uint32_t>(val);
110  EXPECT_EQ(val_check, val_r_ok);
111  }
112  {
113  // 1.0 == 0x3ff0000000000000
114  double val = 1;
115  const uint64_t val_r_ok = UINT32_C(0xf03f);
116 
118  auto val_check = bit_cast<uint64_t>(val);
119  EXPECT_EQ(val_check, val_r_ok);
120  }
121 }
unsigned __int16 uint16_t
Definition: rptypes.h:47
void reverseBytes(const T &v_in, T &v_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) ...
Definition: reverse_bytes.h:36
signed char int8_t
Definition: rptypes.h:43
TEST(bits, reverseBytes)
void reverseBytesInPlace(bool &v_in_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) ...
unsigned char uint8_t
Definition: rptypes.h:44
__int16 int16_t
Definition: rptypes.h:46
__int64 int64_t
Definition: rptypes.h:52
int val
Definition: mrpt_jpeglib.h:957
__int32 int32_t
Definition: rptypes.h:49
unsigned __int64 uint64_t
Definition: rptypes.h:53
Dest bit_cast(const Source &source)
Definition: bit_cast.h:67
unsigned __int32 uint32_t
Definition: rptypes.h:50



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019