• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Peter Dimov
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 
6 #include <boost/endian/conversion.hpp>
7 #include <boost/core/lightweight_test.hpp>
8 #include <boost/config.hpp>
9 #include <boost/cstdint.hpp>
10 #include <cstddef>
11 #include <ostream>
12 #include <iomanip>
13 
14 class byte_span
15 {
16 private:
17 
18     unsigned char const * p_;
19     std::size_t n_;
20 
21 public:
22 
byte_span(unsigned char const * p,std::size_t n)23     byte_span( unsigned char const * p, std::size_t n ): p_( p ), n_( n )
24     {
25     }
26 
byte_span(unsigned char const (& a)[N])27     template<std::size_t N> explicit byte_span( unsigned char const (&a)[ N ] ): p_( a ), n_( N )
28     {
29     }
30 
operator ==(byte_span const & r) const31     bool operator==( byte_span const& r ) const
32     {
33         if( n_ != r.n_ ) return false;
34 
35         for( std::size_t i = 0; i < n_; ++i )
36         {
37             if( p_[ i ] != r.p_[ i ] ) return false;
38         }
39 
40         return true;
41     }
42 
operator <<(std::ostream & os,byte_span s)43     friend std::ostream& operator<<( std::ostream& os, byte_span s )
44     {
45         if( s.n_ == 0 ) return os;
46 
47         os << std::hex << std::setfill( '0' ) << std::uppercase;
48 
49         os << std::setw( 2 ) << +s.p_[ 0 ];
50 
51         for( std::size_t i = 1; i < s.n_; ++i )
52         {
53             os << ':' << std::setw( 2 ) << +s.p_[ i ];
54         }
55 
56         os << std::dec << std::setfill( ' ' ) << std::nouppercase;;
57 
58         return os;
59     }
60 };
61 
main()62 int main()
63 {
64     using namespace boost::endian;
65 
66     // 16
67 
68     {
69         unsigned char v[] = { 0xAA, 0xAA, 0xAA };
70 
71         store_little_s16( v, -3343 );
72 
73         unsigned char w1[] = { 0xF1, 0xF2, 0xAA };
74 
75         BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
76 
77         store_little_u16( v, 0x0201 );
78 
79         unsigned char w2[] = { 0x01, 0x02, 0xAA };
80 
81         BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
82 
83         store_big_s16( v, -3343 );
84 
85         unsigned char w3[] = { 0xF2, 0xF1, 0xAA };
86 
87         BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
88 
89         store_big_u16( v, 0x0201 );
90 
91         unsigned char w4[] = { 0x02, 0x01, 0xAA };
92 
93         BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
94     }
95 
96     // 24
97 
98     {
99         unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA };
100 
101         store_little_s24( v, -789775 );
102 
103         unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xAA };
104 
105         BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
106 
107         store_little_u24( v, 0x030201 );
108 
109         unsigned char w2[] = { 0x01, 0x02, 0x03, 0xAA };
110 
111         BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
112 
113         store_big_s24( v, -789775 );
114 
115         unsigned char w3[] = { 0xF3, 0xF2, 0xF1, 0xAA };
116 
117         BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
118 
119         store_big_u24( v, 0x030201 );
120 
121         unsigned char w4[] = { 0x03, 0x02, 0x01, 0xAA };
122 
123         BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
124     }
125 
126     // 32
127 
128     {
129         unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
130 
131         store_little_s32( v, 0xF4F3F2F1 );
132 
133         unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xAA };
134 
135         BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
136 
137         store_little_u32( v, 0x04030201 );
138 
139         unsigned char w2[] = { 0x01, 0x02, 0x03, 0x04, 0xAA };
140 
141         BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
142 
143         store_big_s32( v, 0xF4F3F2F1 );
144 
145         unsigned char w3[] = { 0xF4, 0xF3, 0xF2, 0xF1, 0xAA };
146 
147         BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
148 
149         store_big_u32( v, 0x04030201 );
150 
151         unsigned char w4[] = { 0x04, 0x03, 0x02, 0x01, 0xAA };
152 
153         BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
154     }
155 
156     // 40
157 
158     {
159         unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
160 
161         store_little_s40( v, -43135012111 );
162 
163         unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xAA };
164 
165         BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
166 
167         store_little_u40( v, 0x0504030201 );
168 
169         unsigned char w2[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0xAA };
170 
171         BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
172 
173         store_big_s40( v, -43135012111 );
174 
175         unsigned char w3[] = { 0xF5, 0xF4, 0xF3, 0xF2, 0xF1, 0xAA };
176 
177         BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
178 
179         store_big_u40( v, 0x0504030201 );
180 
181         unsigned char w4[] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
182 
183         BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
184     }
185 
186     // 48
187 
188     {
189         unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
190 
191         store_little_s48( v, -9938739662095 );
192 
193         unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xAA };
194 
195         BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
196 
197         store_little_u48( v, 0x060504030201 );
198 
199         unsigned char w2[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xAA };
200 
201         BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
202 
203         store_big_s48( v, -9938739662095 );
204 
205         unsigned char w3[] = { 0xF6, 0xF5, 0xF4, 0xF3, 0xF2, 0xF1, 0xAA };
206 
207         BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
208 
209         store_big_u48( v, 0x060504030201 );
210 
211         unsigned char w4[] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
212 
213         BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
214     }
215 
216     // 56
217 
218     {
219         unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
220 
221         store_little_s56( v, -2261738553347343 );
222 
223         unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xAA };
224 
225         BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
226 
227         store_little_u56( v, 0x07060504030201 );
228 
229         unsigned char w2[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xAA };
230 
231         BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
232 
233         store_big_s56( v, -2261738553347343 );
234 
235         unsigned char w3[] = { 0xF7, 0xF6, 0xF5, 0xF4, 0xF3, 0xF2, 0xF1, 0xAA };
236 
237         BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
238 
239         store_big_u56( v, 0x07060504030201 );
240 
241         unsigned char w4[] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
242 
243         BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
244     }
245 
246     // 64
247 
248     {
249         unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
250 
251         store_little_s64( v, 0xF8F7F6F5F4F3F2F1 );
252 
253         unsigned char w1[] = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xAA };
254 
255         BOOST_TEST_EQ( byte_span( v ), byte_span( w1 ) );
256 
257         store_little_u64( v, 0x0807060504030201 );
258 
259         unsigned char w2[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xAA };
260 
261         BOOST_TEST_EQ( byte_span( v ), byte_span( w2 ) );
262 
263         store_big_s64( v, 0xF8F7F6F5F4F3F2F1 );
264 
265         unsigned char w3[] = { 0xF8, 0xF7, 0xF6, 0xF5, 0xF4, 0xF3, 0xF2, 0xF1, 0xAA };
266 
267         BOOST_TEST_EQ( byte_span( v ), byte_span( w3 ) );
268 
269         store_big_u64( v, 0x0807060504030201 );
270 
271         unsigned char w4[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
272 
273         BOOST_TEST_EQ( byte_span( v ), byte_span( w4 ) );
274     }
275 
276     return boost::report_errors();
277 }
278