ByteOrderTest.cpp 13 KB

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