1//
2// ByteOrderTest.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "ByteOrderTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/ByteOrder.h"
15
16
17using Poco::ByteOrder;
18using Poco::Int16;
19using Poco::UInt16;
20using Poco::Int32;
21using Poco::UInt32;
22#if defined(POCO_HAVE_INT64)
23using Poco::Int64;
24using Poco::UInt64;
25#endif
26
27
28ByteOrderTest::ByteOrderTest(const std::string& rName): CppUnit::TestCase(rName)
29{
30}
31
32
33ByteOrderTest::~ByteOrderTest()
34{
35}
36
37
38void ByteOrderTest::testByteOrderFlip()
39{
40 {
41 Int16 norm = (Int16) 0xAABB;
42 Int16 flip = ByteOrder::flipBytes(norm);
43 assertTrue (UInt16(flip) == 0xBBAA);
44 flip = ByteOrder::flipBytes(flip);
45 assertTrue (flip == norm);
46 }
47 {
48 UInt16 norm = (UInt16) 0xAABB;
49 UInt16 flip = ByteOrder::flipBytes(norm);
50 assertTrue (flip == 0xBBAA);
51 flip = ByteOrder::flipBytes(flip);
52 assertTrue (flip == norm);
53 }
54 {
55 Int32 norm = 0xAABBCCDD;
56 Int32 flip = ByteOrder::flipBytes(norm);
57 assertTrue (UInt32(flip) == 0xDDCCBBAA);
58 flip = ByteOrder::flipBytes(flip);
59 assertTrue (flip == norm);
60 }
61 {
62 UInt32 norm = 0xAABBCCDD;
63 UInt32 flip = ByteOrder::flipBytes(norm);
64 assertTrue (flip == 0xDDCCBBAA);
65 flip = ByteOrder::flipBytes(flip);
66 assertTrue (flip == norm);
67 }
68 {
69 unsigned char c = 0x00;
70 float norm = 0;
71 unsigned char* normP = reinterpret_cast<unsigned char*>(&norm);
72 for (unsigned i = 0; i < sizeof(float); i++)
73 {
74 normP[i] |= c;
75 c += 0x11;
76 }
77
78 float flip = ByteOrder::flipBytes(norm);
79 unsigned char* flipP = reinterpret_cast<unsigned char*>(&flip);
80 for (unsigned i = 0; i < sizeof(float); i++)
81 {
82 assertTrue (normP[i] == flipP[sizeof(float) - 1 - i]);
83 }
84
85 flip = ByteOrder::flipBytes(flip);
86 assertTrue (flip == norm);
87 }
88 {
89 unsigned char c = 0x00;
90 double norm = 0;
91 unsigned char* normP = reinterpret_cast<unsigned char*>(&norm);
92 for (unsigned i = 0; i < sizeof(double); i++)
93 {
94 normP[i] |= c;
95 c += 0x11;
96 }
97
98 double flip = ByteOrder::flipBytes(norm);
99 unsigned char* flipP = reinterpret_cast<unsigned char*>(&flip);
100 for (unsigned i = 0; i < sizeof(double); i++)
101 {
102 assertTrue (normP[i] == flipP[sizeof(double) - 1 - i]);
103 }
104
105 flip = ByteOrder::flipBytes(flip);
106 assertTrue (flip == norm);
107 }
108 #if defined(POCO_HAVE_INT64)
109 {
110 Int64 norm = (Int64(0x8899AABB) << 32) + 0xCCDDEEFF;
111 Int64 flip = ByteOrder::flipBytes(norm);
112 assertTrue (flip == (Int64(0xFFEEDDCC) << 32) + 0xBBAA9988);
113 flip = ByteOrder::flipBytes(flip);
114 assertTrue (flip == norm);
115 }
116 {
117 UInt64 norm = (UInt64(0x8899AABB) << 32) + 0xCCDDEEFF;
118 UInt64 flip = ByteOrder::flipBytes(norm);
119 assertTrue (flip == (UInt64(0xFFEEDDCC) << 32) + 0xBBAA9988);
120 flip = ByteOrder::flipBytes(flip);
121 assertTrue (flip == norm);
122 }
123 #endif
124}
125
126
127void ByteOrderTest::testByteOrderBigEndian()
128{
129#if defined(POCO_ARCH_BIG_ENDIAN)
130 //
131 // big-endian systems
132 //
133 {
134 Int16 norm = 4;
135 Int16 flip = ByteOrder::toBigEndian(norm);
136 assertTrue (norm == flip);
137 }
138 {
139 UInt16 norm = 4;
140 UInt16 flip = ByteOrder::toBigEndian(norm);
141 assertTrue (norm == flip);
142 }
143 {
144 Int32 norm = 4;
145 Int32 flip = ByteOrder::toBigEndian(norm);
146 assertTrue (norm == flip);
147 }
148 {
149 UInt32 norm = 4;
150 UInt32 flip = ByteOrder::toBigEndian(norm);
151 assertTrue (norm == flip);
152 }
153 #if defined(POCO_HAVE_INT64)
154 {
155 Int64 norm = 4;
156 Int64 flip = ByteOrder::toBigEndian(norm);
157 assertTrue (norm == flip);
158 }
159 {
160 UInt64 norm = 4;
161 UInt64 flip = ByteOrder::toBigEndian(norm);
162 assertTrue (norm == flip);
163 }
164 #endif
165
166 {
167 Int16 norm = 4;
168 Int16 flip = ByteOrder::fromBigEndian(norm);
169 assertTrue (norm == flip);
170 }
171 {
172 UInt16 norm = 4;
173 UInt16 flip = ByteOrder::fromBigEndian(norm);
174 assertTrue (norm == flip);
175 }
176 {
177 Int32 norm = 4;
178 Int32 flip = ByteOrder::fromBigEndian(norm);
179 assertTrue (norm == flip);
180 }
181 {
182 UInt32 norm = 4;
183 UInt32 flip = ByteOrder::fromBigEndian(norm);
184 assertTrue (norm == flip);
185 }
186 #if defined(POCO_HAVE_INT64)
187 {
188 Int64 norm = 4;
189 Int64 flip = ByteOrder::fromBigEndian(norm);
190 assertTrue (norm == flip);
191 }
192 {
193 UInt64 norm = 4;
194 UInt64 flip = ByteOrder::fromBigEndian(norm);
195 assertTrue (norm == flip);
196 }
197 #endif
198#else
199 //
200 // little-endian systems
201 //
202 {
203 Int16 norm = 4;
204 Int16 flip = ByteOrder::toBigEndian(norm);
205 assertTrue (norm != flip);
206 flip = ByteOrder::flipBytes(flip);
207 assertTrue (norm == flip);
208 }
209 {
210 UInt16 norm = 4;
211 UInt16 flip = ByteOrder::toBigEndian(norm);
212 assertTrue (norm != flip);
213 flip = ByteOrder::flipBytes(flip);
214 assertTrue (norm == flip);
215 }
216 {
217 Int32 norm = 4;
218 Int32 flip = ByteOrder::toBigEndian(norm);
219 assertTrue (norm != flip);
220 flip = ByteOrder::flipBytes(flip);
221 assertTrue (norm == flip);
222 }
223 {
224 UInt32 norm = 4;
225 UInt32 flip = ByteOrder::toBigEndian(norm);
226 assertTrue (norm != flip);
227 flip = ByteOrder::flipBytes(flip);
228 assertTrue (norm == flip);
229 }
230 #if defined(POCO_HAVE_INT64)
231 {
232 Int64 norm = 4;
233 Int64 flip = ByteOrder::toBigEndian(norm);
234 assertTrue (norm != flip);
235 flip = ByteOrder::flipBytes(flip);
236 assertTrue (norm == flip);
237 }
238 {
239 UInt64 norm = 4;
240 UInt64 flip = ByteOrder::toBigEndian(norm);
241 assertTrue (norm != flip);
242 flip = ByteOrder::flipBytes(flip);
243 assertTrue (norm == flip);
244 }
245 #endif
246
247 {
248 Int16 norm = 4;
249 Int16 flip = ByteOrder::fromBigEndian(norm);
250 assertTrue (norm != flip);
251 flip = ByteOrder::flipBytes(flip);
252 assertTrue (norm == flip);
253 }
254 {
255 UInt16 norm = 4;
256 UInt16 flip = ByteOrder::fromBigEndian(norm);
257 assertTrue (norm != flip);
258 flip = ByteOrder::flipBytes(flip);
259 assertTrue (norm == flip);
260 }
261 {
262 Int32 norm = 4;
263 Int32 flip = ByteOrder::fromBigEndian(norm);
264 assertTrue (norm != flip);
265 flip = ByteOrder::flipBytes(flip);
266 assertTrue (norm == flip);
267 }
268 {
269 UInt32 norm = 4;
270 UInt32 flip = ByteOrder::fromBigEndian(norm);
271 assertTrue (norm != flip);
272 flip = ByteOrder::flipBytes(flip);
273 assertTrue (norm == flip);
274 }
275 #if defined(POCO_HAVE_INT64)
276 {
277 Int64 norm = 4;
278 Int64 flip = ByteOrder::fromBigEndian(norm);
279 assertTrue (norm != flip);
280 flip = ByteOrder::flipBytes(flip);
281 assertTrue (norm == flip);
282 }
283 {
284 UInt64 norm = 4;
285 UInt64 flip = ByteOrder::fromBigEndian(norm);
286 assertTrue (norm != flip);
287 flip = ByteOrder::flipBytes(flip);
288 assertTrue (norm == flip);
289 }
290 #endif
291#endif
292}
293
294
295void ByteOrderTest::testByteOrderLittleEndian()
296{
297#if defined(POCO_ARCH_LITTLE_ENDIAN)
298 //
299 // big-endian systems
300 //
301 {
302 Int16 norm = 4;
303 Int16 flip = ByteOrder::toLittleEndian(norm);
304 assertTrue (norm == flip);
305 }
306 {
307 UInt16 norm = 4;
308 UInt16 flip = ByteOrder::toLittleEndian(norm);
309 assertTrue (norm == flip);
310 }
311 {
312 Int32 norm = 4;
313 Int32 flip = ByteOrder::toLittleEndian(norm);
314 assertTrue (norm == flip);
315 }
316 {
317 UInt32 norm = 4;
318 UInt32 flip = ByteOrder::toLittleEndian(norm);
319 assertTrue (norm == flip);
320 }
321 #if defined(POCO_HAVE_INT64)
322 {
323 Int64 norm = 4;
324 Int64 flip = ByteOrder::toLittleEndian(norm);
325 assertTrue (norm == flip);
326 }
327 {
328 UInt64 norm = 4;
329 UInt64 flip = ByteOrder::toLittleEndian(norm);
330 assertTrue (norm == flip);
331 }
332 #endif
333
334 {
335 Int16 norm = 4;
336 Int16 flip = ByteOrder::toLittleEndian(norm);
337 assertTrue (norm == flip);
338 }
339 {
340 UInt16 norm = 4;
341 UInt16 flip = ByteOrder::toLittleEndian(norm);
342 assertTrue (norm == flip);
343 }
344 {
345 Int32 norm = 4;
346 Int32 flip = ByteOrder::toLittleEndian(norm);
347 assertTrue (norm == flip);
348 }
349 {
350 UInt32 norm = 4;
351 UInt32 flip = ByteOrder::toLittleEndian(norm);
352 assertTrue (norm == flip);
353 }
354 #if defined(POCO_HAVE_INT64)
355 {
356 Int64 norm = 4;
357 Int64 flip = ByteOrder::toLittleEndian(norm);
358 assertTrue (norm == flip);
359 }
360 {
361 UInt64 norm = 4;
362 UInt64 flip = ByteOrder::toLittleEndian(norm);
363 assertTrue (norm == flip);
364 }
365 #endif
366#else
367 //
368 // little-endian systems
369 //
370 {
371 Int16 norm = 4;
372 Int16 flip = ByteOrder::toLittleEndian(norm);
373 assertTrue (norm != flip);
374 flip = ByteOrder::flipBytes(flip);
375 assertTrue (norm == flip);
376 }
377 {
378 UInt16 norm = 4;
379 UInt16 flip = ByteOrder::toLittleEndian(norm);
380 assertTrue (norm != flip);
381 flip = ByteOrder::flipBytes(flip);
382 assertTrue (norm == flip);
383 }
384 {
385 Int32 norm = 4;
386 Int32 flip = ByteOrder::toLittleEndian(norm);
387 assertTrue (norm != flip);
388 flip = ByteOrder::flipBytes(flip);
389 assertTrue (norm == flip);
390 }
391 {
392 UInt32 norm = 4;
393 UInt32 flip = ByteOrder::toLittleEndian(norm);
394 assertTrue (norm != flip);
395 flip = ByteOrder::flipBytes(flip);
396 assertTrue (norm == flip);
397 }
398 #if defined(POCO_HAVE_INT64)
399 {
400 Int64 norm = 4;
401 Int64 flip = ByteOrder::toLittleEndian(norm);
402 assertTrue (norm != flip);
403 flip = ByteOrder::flipBytes(flip);
404 assertTrue (norm == flip);
405 }
406 {
407 UInt64 norm = 4;
408 UInt64 flip = ByteOrder::toLittleEndian(norm);
409 assertTrue (norm != flip);
410 flip = ByteOrder::flipBytes(flip);
411 assertTrue (norm == flip);
412 }
413 #endif
414
415 {
416 Int16 norm = 4;
417 Int16 flip = ByteOrder::fromLittleEndian(norm);
418 assertTrue (norm != flip);
419 flip = ByteOrder::flipBytes(flip);
420 assertTrue (norm == flip);
421 }
422 {
423 UInt16 norm = 4;
424 UInt16 flip = ByteOrder::fromLittleEndian(norm);
425 assertTrue (norm != flip);
426 flip = ByteOrder::flipBytes(flip);
427 assertTrue (norm == flip);
428 }
429 {
430 Int32 norm = 4;
431 Int32 flip = ByteOrder::fromLittleEndian(norm);
432 assertTrue (norm != flip);
433 flip = ByteOrder::flipBytes(flip);
434 assertTrue (norm == flip);
435 }
436 {
437 UInt32 norm = 4;
438 UInt32 flip = ByteOrder::fromLittleEndian(norm);
439 assertTrue (norm != flip);
440 flip = ByteOrder::flipBytes(flip);
441 assertTrue (norm == flip);
442 }
443 #if defined(POCO_HAVE_INT64)
444 {
445 Int64 norm = 4;
446 Int64 flip = ByteOrder::fromLittleEndian(norm);
447 assertTrue (norm != flip);
448 flip = ByteOrder::flipBytes(flip);
449 assertTrue (norm == flip);
450 }
451 {
452 UInt64 norm = 4;
453 UInt64 flip = ByteOrder::fromLittleEndian(norm);
454 assertTrue (norm != flip);
455 flip = ByteOrder::flipBytes(flip);
456 assertTrue (norm == flip);
457 }
458 #endif
459#endif
460}
461
462
463void ByteOrderTest::testByteOrderNetwork()
464{
465#if defined(POCO_ARCH_BIG_ENDIAN)
466 //
467 // big-endian systems
468 //
469 {
470 Int16 norm = 4;
471 Int16 flip = ByteOrder::toNetwork(norm);
472 assertTrue (norm == flip);
473 }
474 {
475 UInt16 norm = 4;
476 UInt16 flip = ByteOrder::toNetwork(norm);
477 assertTrue (norm == flip);
478 }
479 {
480 Int32 norm = 4;
481 Int32 flip = ByteOrder::toNetwork(norm);
482 assertTrue (norm == flip);
483 }
484 {
485 UInt32 norm = 4;
486 UInt32 flip = ByteOrder::toNetwork(norm);
487 assertTrue (norm == flip);
488 }
489 #if defined(POCO_HAVE_INT64)
490 {
491 Int64 norm = 4;
492 Int64 flip = ByteOrder::toNetwork(norm);
493 assertTrue (norm == flip);
494 }
495 {
496 UInt64 norm = 4;
497 UInt64 flip = ByteOrder::toNetwork(norm);
498 assertTrue (norm == flip);
499 }
500 #endif
501
502 {
503 Int16 norm = 4;
504 Int16 flip = ByteOrder::fromNetwork(norm);
505 assertTrue (norm == flip);
506 }
507 {
508 UInt16 norm = 4;
509 UInt16 flip = ByteOrder::fromNetwork(norm);
510 assertTrue (norm == flip);
511 }
512 {
513 Int32 norm = 4;
514 Int32 flip = ByteOrder::fromNetwork(norm);
515 assertTrue (norm == flip);
516 }
517 {
518 UInt32 norm = 4;
519 UInt32 flip = ByteOrder::fromNetwork(norm);
520 assertTrue (norm == flip);
521 }
522 #if defined(POCO_HAVE_INT64)
523 {
524 Int64 norm = 4;
525 Int64 flip = ByteOrder::fromNetwork(norm);
526 assertTrue (norm == flip);
527 }
528 {
529 UInt64 norm = 4;
530 UInt64 flip = ByteOrder::fromNetwork(norm);
531 assertTrue (norm == flip);
532 }
533 #endif
534#else
535 //
536 // little-endian systems
537 //
538 {
539 Int16 norm = 4;
540 Int16 flip = ByteOrder::toNetwork(norm);
541 assertTrue (norm != flip);
542 flip = ByteOrder::flipBytes(flip);
543 assertTrue (norm == flip);
544 }
545 {
546 UInt16 norm = 4;
547 UInt16 flip = ByteOrder::toNetwork(norm);
548 assertTrue (norm != flip);
549 flip = ByteOrder::flipBytes(flip);
550 assertTrue (norm == flip);
551 }
552 {
553 Int32 norm = 4;
554 Int32 flip = ByteOrder::toNetwork(norm);
555 assertTrue (norm != flip);
556 flip = ByteOrder::flipBytes(flip);
557 assertTrue (norm == flip);
558 }
559 {
560 UInt32 norm = 4;
561 UInt32 flip = ByteOrder::toNetwork(norm);
562 assertTrue (norm != flip);
563 flip = ByteOrder::flipBytes(flip);
564 assertTrue (norm == flip);
565 }
566 #if defined(POCO_HAVE_INT64)
567 {
568 Int64 norm = 4;
569 Int64 flip = ByteOrder::toNetwork(norm);
570 assertTrue (norm != flip);
571 flip = ByteOrder::flipBytes(flip);
572 assertTrue (norm == flip);
573 }
574 {
575 UInt64 norm = 4;
576 UInt64 flip = ByteOrder::toNetwork(norm);
577 assertTrue (norm != flip);
578 flip = ByteOrder::flipBytes(flip);
579 assertTrue (norm == flip);
580 }
581 #endif
582
583 {
584 Int16 norm = 4;
585 Int16 flip = ByteOrder::fromNetwork(norm);
586 assertTrue (norm != flip);
587 flip = ByteOrder::flipBytes(flip);
588 assertTrue (norm == flip);
589 }
590 {
591 UInt16 norm = 4;
592 UInt16 flip = ByteOrder::fromNetwork(norm);
593 assertTrue (norm != flip);
594 flip = ByteOrder::flipBytes(flip);
595 assertTrue (norm == flip);
596 }
597 {
598 Int32 norm = 4;
599 Int32 flip = ByteOrder::fromNetwork(norm);
600 assertTrue (norm != flip);
601 flip = ByteOrder::flipBytes(flip);
602 assertTrue (norm == flip);
603 }
604 {
605 UInt32 norm = 4;
606 UInt32 flip = ByteOrder::fromNetwork(norm);
607 assertTrue (norm != flip);
608 flip = ByteOrder::flipBytes(flip);
609 assertTrue (norm == flip);
610 }
611 #if defined(POCO_HAVE_INT64)
612 {
613 Int64 norm = 4;
614 Int64 flip = ByteOrder::fromNetwork(norm);
615 assertTrue (norm != flip);
616 flip = ByteOrder::flipBytes(flip);
617 assertTrue (norm == flip);
618 }
619 {
620 UInt64 norm = 4;
621 UInt64 flip = ByteOrder::fromNetwork(norm);
622 assertTrue (norm != flip);
623 flip = ByteOrder::flipBytes(flip);
624 assertTrue (norm == flip);
625 }
626 #endif
627#endif
628}
629
630
631void ByteOrderTest::setUp()
632{
633}
634
635
636void ByteOrderTest::tearDown()
637{
638}
639
640
641CppUnit::Test* ByteOrderTest::suite()
642{
643 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ByteOrderTest");
644
645 CppUnit_addTest(pSuite, ByteOrderTest, testByteOrderFlip);
646 CppUnit_addTest(pSuite, ByteOrderTest, testByteOrderBigEndian);
647 CppUnit_addTest(pSuite, ByteOrderTest, testByteOrderLittleEndian);
648 CppUnit_addTest(pSuite, ByteOrderTest, testByteOrderNetwork);
649
650 return pSuite;
651}
652