1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 //       notice, this list of conditions and the following disclaimer.
9 //
10 //     * Redistributions in binary form must reproduce the above copyright
11 //       notice, this list of conditions and the following disclaimer in the
12 //       documentation and/or other materials provided with the distribution.
13 //
14 //     * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 //       its contributors may be used to endorse or promote products derived
16 //       from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #define TEST_NAME "util/endian"
33 #include "util/testing.h"
34 
35 #include "util/endian.h"
36 #include "util/random.h"
37 
38 using namespace colmap;
39 
BOOST_AUTO_TEST_CASE(TestReverseBytes)40 BOOST_AUTO_TEST_CASE(TestReverseBytes) {
41   for (size_t i = 0; i < 256; ++i) {
42     BOOST_CHECK_EQUAL(ReverseBytes<int8_t>(i), static_cast<int8_t>(i));
43     BOOST_CHECK_EQUAL(ReverseBytes<uint8_t>(i), static_cast<uint8_t>(i));
44   }
45 
46   BOOST_CHECK_EQUAL(ReverseBytes<int16_t>(0), 0);
47   BOOST_CHECK_EQUAL(ReverseBytes<int16_t>(1), 256);
48   BOOST_CHECK_EQUAL(ReverseBytes<int16_t>(2), 512);
49   BOOST_CHECK_EQUAL(ReverseBytes<int16_t>(3), 768);
50   BOOST_CHECK_EQUAL(ReverseBytes<int16_t>(256), 1);
51   BOOST_CHECK_EQUAL(ReverseBytes<int16_t>(512), 2);
52   BOOST_CHECK_EQUAL(ReverseBytes<int16_t>(768), 3);
53 
54   BOOST_CHECK_EQUAL(ReverseBytes<uint16_t>(0), 0);
55   BOOST_CHECK_EQUAL(ReverseBytes<uint16_t>(1), 256);
56   BOOST_CHECK_EQUAL(ReverseBytes<uint16_t>(2), 512);
57   BOOST_CHECK_EQUAL(ReverseBytes<uint16_t>(3), 768);
58   BOOST_CHECK_EQUAL(ReverseBytes<uint16_t>(256), 1);
59   BOOST_CHECK_EQUAL(ReverseBytes<uint16_t>(512), 2);
60   BOOST_CHECK_EQUAL(ReverseBytes<uint16_t>(768), 3);
61 
62   BOOST_CHECK_EQUAL(ReverseBytes<int32_t>(0), 0);
63   BOOST_CHECK_EQUAL(ReverseBytes<int32_t>(1), 16777216);
64   BOOST_CHECK_EQUAL(ReverseBytes<int32_t>(2), 33554432);
65   BOOST_CHECK_EQUAL(ReverseBytes<int32_t>(3), 50331648);
66   BOOST_CHECK_EQUAL(ReverseBytes<int32_t>(16777216), 1);
67   BOOST_CHECK_EQUAL(ReverseBytes<int32_t>(33554432), 2);
68   BOOST_CHECK_EQUAL(ReverseBytes<int32_t>(50331648), 3);
69 
70   BOOST_CHECK_EQUAL(ReverseBytes<uint32_t>(0), 0);
71   BOOST_CHECK_EQUAL(ReverseBytes<uint32_t>(1), 16777216);
72   BOOST_CHECK_EQUAL(ReverseBytes<uint32_t>(2), 33554432);
73   BOOST_CHECK_EQUAL(ReverseBytes<uint32_t>(3), 50331648);
74   BOOST_CHECK_EQUAL(ReverseBytes<uint32_t>(16777216), 1);
75   BOOST_CHECK_EQUAL(ReverseBytes<uint32_t>(33554432), 2);
76   BOOST_CHECK_EQUAL(ReverseBytes<uint32_t>(50331648), 3);
77 
78   BOOST_CHECK_EQUAL(ReverseBytes<int64_t>(0), 0);
79   BOOST_CHECK_EQUAL(ReverseBytes<int64_t>(1), 72057594037927936);
80   BOOST_CHECK_EQUAL(ReverseBytes<int64_t>(2), 144115188075855872);
81   BOOST_CHECK_EQUAL(ReverseBytes<int64_t>(3), 216172782113783808);
82   BOOST_CHECK_EQUAL(ReverseBytes<int64_t>(72057594037927936), 1);
83   BOOST_CHECK_EQUAL(ReverseBytes<int64_t>(144115188075855872), 2);
84   BOOST_CHECK_EQUAL(ReverseBytes<int64_t>(216172782113783808), 3);
85 
86   BOOST_CHECK_EQUAL(ReverseBytes<uint64_t>(0), 0);
87   BOOST_CHECK_EQUAL(ReverseBytes<uint64_t>(1), 72057594037927936);
88   BOOST_CHECK_EQUAL(ReverseBytes<uint64_t>(2), 144115188075855872);
89   BOOST_CHECK_EQUAL(ReverseBytes<uint64_t>(3), 216172782113783808);
90   BOOST_CHECK_EQUAL(ReverseBytes<uint64_t>(72057594037927936), 1);
91   BOOST_CHECK_EQUAL(ReverseBytes<uint64_t>(144115188075855872), 2);
92   BOOST_CHECK_EQUAL(ReverseBytes<uint64_t>(216172782113783808), 3);
93 }
94 
BOOST_AUTO_TEST_CASE(TestIsLittleBigEndian)95 BOOST_AUTO_TEST_CASE(TestIsLittleBigEndian) {
96   BOOST_CHECK_NE(IsLittleEndian(), IsBigEndian());
97 }
98 
99 template <typename T>
TestIntNativeToLitteBigEndian()100 void TestIntNativeToLitteBigEndian() {
101   const T x = RandomInteger<T>(std::numeric_limits<T>::lowest(),
102                                std::numeric_limits<T>::max());
103   BOOST_CHECK_EQUAL(LittleEndianToNative<T>(NativeToLittleEndian<T>(x)), x);
104   BOOST_CHECK_EQUAL(BigEndianToNative<T>(NativeToBigEndian<T>(x)), x);
105 }
106 
107 template <typename T>
TestRealNativeToLitteBigEndian()108 void TestRealNativeToLitteBigEndian() {
109   const T x = RandomReal<T>(std::numeric_limits<T>::lowest(),
110                             std::numeric_limits<T>::max());
111   BOOST_CHECK_EQUAL(LittleEndianToNative<T>(NativeToLittleEndian<T>(x)), x);
112   BOOST_CHECK_EQUAL(BigEndianToNative<T>(NativeToBigEndian<T>(x)), x);
113   BOOST_CHECK_EQUAL(NativeToLittleEndian<T>(LittleEndianToNative<T>(x)), x);
114   BOOST_CHECK_EQUAL(NativeToBigEndian<T>(BigEndianToNative<T>(x)), x);
115 }
116 
BOOST_AUTO_TEST_CASE(TestNativeToLitteBigEndian)117 BOOST_AUTO_TEST_CASE(TestNativeToLitteBigEndian) {
118 #ifndef _MSC_VER  // There is no random number generator in MSVC for char's.
119   TestIntNativeToLitteBigEndian<int8_t>();
120 #endif
121   TestIntNativeToLitteBigEndian<int16_t>();
122   TestIntNativeToLitteBigEndian<int32_t>();
123   TestIntNativeToLitteBigEndian<int64_t>();
124 #ifndef _MSC_VER  // There is no random number generator in MSVC for char's.
125   TestIntNativeToLitteBigEndian<uint8_t>();
126 #endif
127   TestIntNativeToLitteBigEndian<uint16_t>();
128   TestIntNativeToLitteBigEndian<uint32_t>();
129   TestIntNativeToLitteBigEndian<uint64_t>();
130   TestRealNativeToLitteBigEndian<float>();
131   TestRealNativeToLitteBigEndian<double>();
132 }
133 
134 template <typename T>
TestIntReadWriteBinaryLittleEndian()135 void TestIntReadWriteBinaryLittleEndian() {
136   std::stringstream file;
137   const T orig_value = RandomInteger<T>(std::numeric_limits<T>::lowest(),
138                                         std::numeric_limits<T>::max());
139   WriteBinaryLittleEndian<T>(&file, orig_value);
140   const T read_value = ReadBinaryLittleEndian<T>(&file);
141   BOOST_CHECK_EQUAL(orig_value, read_value);
142 
143   std::stringstream file_vector;
144   std::vector<T> orig_vector(100);
145   std::generate(orig_vector.begin(), orig_vector.end(), []() {
146     return RandomInteger<T>(std::numeric_limits<T>::lowest(),
147                             std::numeric_limits<T>::max());
148   });
149   WriteBinaryLittleEndian<T>(&file_vector, orig_vector);
150   std::vector<T> read_vector(orig_vector.size());
151   ReadBinaryLittleEndian<T>(&file_vector, &read_vector);
152   for (size_t i = 0; i < orig_vector.size(); ++i) {
153     BOOST_CHECK_EQUAL(orig_vector[i], read_vector[i]);
154   }
155 }
156 
157 template <typename T>
TestFloatReadWriteBinaryLittleEndian()158 void TestFloatReadWriteBinaryLittleEndian() {
159   std::stringstream file;
160   const T orig_value = RandomReal<T>(std::numeric_limits<T>::lowest(),
161                                      std::numeric_limits<T>::max());
162   WriteBinaryLittleEndian<T>(&file, orig_value);
163   const T read_value = ReadBinaryLittleEndian<T>(&file);
164   BOOST_CHECK_EQUAL(orig_value, read_value);
165 
166   std::stringstream file_vector;
167   std::vector<T> orig_vector(100);
168   std::generate(orig_vector.begin(), orig_vector.end(), []() {
169     return RandomReal<T>(std::numeric_limits<T>::lowest(),
170                          std::numeric_limits<T>::max());
171   });
172   WriteBinaryLittleEndian<T>(&file_vector, orig_vector);
173   std::vector<T> read_vector(orig_vector.size());
174   ReadBinaryLittleEndian<T>(&file_vector, &read_vector);
175   for (size_t i = 0; i < orig_vector.size(); ++i) {
176     BOOST_CHECK_EQUAL(orig_vector[i], read_vector[i]);
177   }
178 }
179 
BOOST_AUTO_TEST_CASE(TestReadWriteBinaryLittleEndian)180 BOOST_AUTO_TEST_CASE(TestReadWriteBinaryLittleEndian) {
181 #ifndef _MSC_VER  // There is no random number generator in MSVC for char's.
182   TestIntReadWriteBinaryLittleEndian<int8_t>();
183 #endif
184   TestIntReadWriteBinaryLittleEndian<int16_t>();
185   TestIntReadWriteBinaryLittleEndian<int32_t>();
186   TestIntReadWriteBinaryLittleEndian<int64_t>();
187 #ifndef _MSC_VER  // There is no random number generator in MSVC for char's.
188   TestIntReadWriteBinaryLittleEndian<uint8_t>();
189 #endif
190   TestIntReadWriteBinaryLittleEndian<uint16_t>();
191   TestIntReadWriteBinaryLittleEndian<uint32_t>();
192   TestIntReadWriteBinaryLittleEndian<uint64_t>();
193   TestFloatReadWriteBinaryLittleEndian<float>();
194   TestFloatReadWriteBinaryLittleEndian<double>();
195 }
196