endian_test.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "catch.hpp"
  2. #include "endian.hh"
  3. using namespace Endian;
  4. union T16 {
  5. B16 be;
  6. L16 le;
  7. };
  8. union T32 {
  9. B32 be;
  10. L32 le;
  11. };
  12. TEST_CASE("endian: bswap")
  13. {
  14. CHECK(bswap16(0x1122) == 0x2211);
  15. CHECK(bswap32(0x11223344) == 0x44332211);
  16. CHECK(bswap64(0x1122334455667788) == 0x8877665544332211);
  17. CHECK(bswap(uint16_t(0x1234)) == 0x3412);
  18. CHECK(bswap(uint32_t(0x12345678)) == 0x78563412);
  19. CHECK(bswap(uint64_t(0x123456789abcdef0)) == 0xf0debc9a78563412);
  20. BSwap swapper;
  21. CHECK(swapper(uint16_t(0xaabb)) == 0xbbaa);
  22. CHECK(swapper(uint32_t(0xaabbccdd)) == 0xddccbbaa);
  23. CHECK(swapper(uint64_t(0xaabbccddeeff0011)) == 0x1100ffeeddccbbaa);
  24. }
  25. // TODO better coverage for aligned vs unaligned versions of the functions
  26. TEST_CASE("endian: 16-bit")
  27. {
  28. T16 t;
  29. REQUIRE(sizeof(t) == 2);
  30. t.le = 0x1234;
  31. CHECK(t.le == 0x1234);
  32. CHECK(t.be == 0x3412);
  33. CHECK(read_UA_L16(&t) == 0x1234);
  34. CHECK(read_UA_B16(&t) == 0x3412);
  35. t.be = 0x1234;
  36. CHECK(t.le == 0x3412);
  37. CHECK(t.be == 0x1234);
  38. CHECK(read_UA_L16(&t) == 0x3412);
  39. CHECK(read_UA_B16(&t) == 0x1234);
  40. write_UA_L16(&t, 0xaabb);
  41. CHECK(t.le == 0xaabb);
  42. CHECK(t.be == 0xbbaa);
  43. CHECK(read_UA_L16(&t) == 0xaabb);
  44. CHECK(read_UA_B16(&t) == 0xbbaa);
  45. write_UA_B16(&t, 0xaabb);
  46. CHECK(t.le == 0xbbaa);
  47. CHECK(t.be == 0xaabb);
  48. CHECK(read_UA_L16(&t) == 0xbbaa);
  49. CHECK(read_UA_B16(&t) == 0xaabb);
  50. }
  51. TEST_CASE("endian: 32-bit")
  52. {
  53. T32 t;
  54. REQUIRE(sizeof(t) == 4);
  55. t.le = 0x12345678;
  56. CHECK(t.le == 0x12345678);
  57. CHECK(t.be == 0x78563412);
  58. CHECK(read_UA_L32(&t) == 0x12345678);
  59. CHECK(read_UA_B32(&t) == 0x78563412);
  60. t.be = 0x12345678;
  61. CHECK(t.le == 0x78563412);
  62. CHECK(t.be == 0x12345678);
  63. CHECK(read_UA_L32(&t) == 0x78563412);
  64. CHECK(read_UA_B32(&t) == 0x12345678);
  65. write_UA_L32(&t, 0xaabbccdd);
  66. CHECK(t.le == 0xaabbccdd);
  67. CHECK(t.be == 0xddccbbaa);
  68. CHECK(read_UA_L32(&t) == 0xaabbccdd);
  69. CHECK(read_UA_B32(&t) == 0xddccbbaa);
  70. write_UA_B32(&t, 0xaabbccdd);
  71. CHECK(t.le == 0xddccbbaa);
  72. CHECK(t.be == 0xaabbccdd);
  73. CHECK(read_UA_L32(&t) == 0xddccbbaa);
  74. CHECK(read_UA_B32(&t) == 0xaabbccdd);
  75. }
  76. #if 0
  77. // Small functions to inspect code quality
  78. uint16_t testSwap16(uint16_t x) { return bswap16(x); }
  79. uint16_t testSwap16() { return bswap16(0x1234); }
  80. uint32_t testSwap32(uint32_t x) { return bswap32(x); }
  81. uint32_t testSwap32() { return bswap32(0x12345678); }
  82. void test1(T16& t, uint16_t x) { t.le = x; }
  83. void test2(T16& t, uint16_t x) { t.be = x; }
  84. uint16_t test3(T16& t) { return t.le; }
  85. uint16_t test4(T16& t) { return t.be; }
  86. void testA(uint16_t& s, uint16_t x) { write_UA_L16(&s, x); }
  87. void testB(uint16_t& s, uint16_t x) { write_UA_B16(&s, x); }
  88. uint16_t testC(uint16_t& s) { return read_UA_L16(&s); }
  89. uint16_t testD(uint16_t& s) { return read_UA_B16(&s); }
  90. void test1(T32& t, uint32_t x) { t.le = x; }
  91. void test2(T32& t, uint32_t x) { t.be = x; }
  92. uint32_t test3(T32& t) { return t.le; }
  93. uint32_t test4(T32& t) { return t.be; }
  94. void testA(uint32_t& s, uint32_t x) { write_UA_L32(&s, x); }
  95. void testB(uint32_t& s, uint32_t x) { write_UA_B32(&s, x); }
  96. uint32_t testC(uint32_t& s) { return read_UA_L32(&s); }
  97. uint32_t testD(uint32_t& s) { return read_UA_B32(&s); }
  98. #endif