ByteOrderConversionTest.cpp
Go to the documentation of this file.
1 //
3 // © Copyright 2022 SCHUNK Mobile Greifsysteme GmbH, Lauffen/Neckar Germany
4 // © Copyright 2022 FZI Forschungszentrum Informatik, Karlsruhe, Germany
5 //
6 // This file is part of the Schunk SVH Library.
7 //
8 // The Schunk SVH Library is free software: you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or (at your
11 // option) any later version.
12 //
13 // The Schunk SVH Library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 // Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along with
19 // the Schunk SVH Library. If not, see <https://www.gnu.org/licenses/>.
20 //
22 
23 //----------------------------------------------------------------------
30 //----------------------------------------------------------------------
32 
33 #include <boost/test/unit_test.hpp>
34 
36 
37 BOOST_AUTO_TEST_SUITE(ts_ByteOrderConversion)
38 
39 
41 {
42  float test_float = 15.08;
43  int test_int = 1508;
44  u_int8_t test_uint = 128;
45  u_int16_t test_uint2 = 128;
46  ArrayBuilder ab;
47  ab << test_int << test_float << test_uint << test_uint2;
48 
49  // std::cout << "ArrayBuilder read the values " << test_int << " , " << test_float << " , " <<
50  // (int)test_uint << " , is currently at POS(r/w): "<<(int)ab.read_pos<<"/"<<(int)ab.write_pos<< "
51  // And contains the " << (int)ab.array.size() << " Byte long Byte array: " << ab << std::endl;
52 
53  float test_float_out = 0.0;
54  int test_int_out = 0;
55  u_int8_t test_uint_out = 0;
56  u_int16_t test_uint2_out = 2;
57  int test_to_mouch = 0;
58 
59  ab >> test_int_out >> test_float_out >> test_uint_out >> test_uint2_out >> test_to_mouch;
60 
61  // std::cout << "ArrayBuilder reconstructed the values: "<< test_int_out << " , " <<
62  // test_float_out << " , " << (int)test_uint_out << " , is currently at POS(r/w):
63  // "<<(int)ab.read_pos<<"/"<<(int)ab.write_pos<< " And contains the " << (int)ab.array.size() << "
64  // Byte long Byte array: " << ab << std::endl; std::cout << "This value should contain 0: "<<
65  // test_to_mouch << std::endl;
66 
67  BOOST_CHECK_EQUAL(test_float, test_float_out);
68  BOOST_CHECK_EQUAL(test_int, test_int_out);
69  BOOST_CHECK_EQUAL(test_uint, test_uint_out);
70  BOOST_CHECK_EQUAL(test_uint2, test_uint2_out);
71  BOOST_CHECK_EQUAL(test_to_mouch, 0);
72 }
73 
74 BOOST_AUTO_TEST_CASE(ConvertVectorsTest)
75 {
76  ArrayBuilder ab;
77  std::vector<u_int8_t> test_uint8_vector;
78  std::vector<u_int16_t> test_uint16_vector;
79  std::vector<u_int8_t> test_uint8_vector_out(4, 0);
80  std::vector<u_int16_t> test_uint16_vector_out(4, 0);
81 
82  // Test if the Vectors Work with the ArrayBuilder
83  test_uint8_vector.push_back(1);
84  test_uint8_vector.push_back(2);
85  test_uint8_vector.push_back(3);
86  test_uint8_vector.push_back(4);
87  ab << test_uint8_vector;
88 
89  // std::cout << "Put a vector with the uint8_t type and the data 1,2,3,4 into the array builder,
90  // RAW Data: "<< ab << std::endl;
91 
92  test_uint16_vector.push_back(5);
93  test_uint16_vector.push_back(6);
94  test_uint16_vector.push_back(7);
95  test_uint16_vector.push_back(8);
96  ab << test_uint16_vector;
97 
98  // std::cout << "Added a vector with the uint16_t type and the data 5,6,7,8 into the array
99  // builder, RAW Data: "<< ab << std::endl;
100 
101  // Read out the arrays aggain
102  ab >> test_uint8_vector_out;
103  ab >> test_uint16_vector_out;
104 
105 
106  // std::cout << "Test_uint_16_vector was reconstructed with the elements: ";
107  // for (std::vector<u_int16_t>::const_iterator it = test_uint16_vector_out.begin() ; it !=
108  // test_uint16_vector_out.end(); ++it)
109  // {
110  // std::cout << (int)*it << ", ";
111  // }
112  // std::cout << std::endl;
113 
114  // std::cout << "Test_uint_8_vector was reconstructed with the elements: ";
115  // for (std::vector<u_int8_t>::const_iterator it = test_uint8_vector_out.begin() ; it !=
116  // test_uint8_vector_out.end(); ++it)
117  // {
118  // std::cout << (int)*it << ", ";
119  // }
120  // std::cout << std::endl;
121 
122 
123  BOOST_CHECK_EQUAL_COLLECTIONS(test_uint8_vector.begin(),
124  test_uint8_vector.end(),
125  test_uint8_vector_out.begin(),
126  test_uint8_vector_out.end());
127  BOOST_CHECK_EQUAL_COLLECTIONS(test_uint16_vector.begin(),
128  test_uint16_vector.end(),
129  test_uint16_vector_out.begin(),
130  test_uint16_vector_out.end());
131 }
132 
133 BOOST_AUTO_TEST_CASE(ReadBackTest)
134 {
135  ArrayBuilder ab;
136 
137  u_int32_t size = 12;
138  u_int32_t size_peek = 0;
139  u_int32_t size_out = 0;
140 
141  ab << size;
142  size_peek = ab.readBack<u_int32_t>();
143  ab >> size_out;
144 
145  // std::cout << "Size: "<<(int)size << " Size_peek "<< size_peek << " size_out" << size_out <<
146  // std::endl;
147  BOOST_CHECK_EQUAL(size, size_out);
148  BOOST_CHECK_EQUAL(size, size_peek);
149 
150  ab >> size_out;
151 
152  // This should return zero as the read pointer is empty.,However,readback should still function as
153  // the last element written is still there
154  BOOST_CHECK_EQUAL(size_out, 0);
155  size_peek = ab.readBack<u_int32_t>();
156  BOOST_CHECK_EQUAL(size, size_peek);
157 }
158 
159 
160 BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(ConvertTest)


schunk_svh_library
Author(s): Georg Heppner, Lars Pfotzer, Felix Exner, Johannes Mangler, Stefan Scherzinger, Pascal Becker
autogenerated on Fri Apr 14 2023 02:26:23