MRPT  2.0.1
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-2020, 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  {
122  std::chrono::time_point<mrpt::Clock> t = mrpt::Clock::now();
123  const auto t_org = t;
125  EXPECT_NE(t, t_org);
127  EXPECT_EQ(t, t_org);
128  }
129 }
EXPECT_TRUE(mrpt::system::fileExists(ini_fil))
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:39
TEST(bits, reverseBytes)
static time_point now() noexcept
Returns the current time using the currently selected Clock source.
Definition: Clock.cpp:94
void reverseBytesInPlace(bool &v_in_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) ...
int val
Definition: mrpt_jpeglib.h:957
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
Dest bit_cast(const Source &source)
Definition: bit_cast.h:67



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020