Subversion Repositories Games.Descent

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "dxxsconf.h"
  2.  
  3. #include "serial.h"
  4.  
  5. #define BOOST_TEST_DYN_LINK
  6. #define BOOST_TEST_MODULE Rebirth serial
  7. #include <boost/test/unit_test.hpp>
  8.  
  9. static_assert(!serial::is_message<int>::value, "");
  10. static_assert(serial::is_message<serial::message<int>>::value, "");
  11.  
  12. assert_equal(serial::detail::size_base<4>::maximum_size, 4, "");
  13. assert_equal((serial::detail::size_base<4, 2>::minimum_size), 2, "");
  14.  
  15. static_assert(!serial::is_cxx_array<int>::value, "");
  16. static_assert(!serial::is_cxx_array<int[1]>::value, "");
  17. static_assert(!serial::is_cxx_array<int *>::value, "");
  18. static_assert(!serial::is_cxx_array<std::array<int, 1> *>::value, "");
  19. static_assert(!serial::is_cxx_array<std::array<int, 1> &>::value, "");
  20. static_assert(serial::is_cxx_array<std::array<int, 1>>::value, "");
  21. static_assert(serial::is_cxx_array<const std::array<int, 1>>::value, "");
  22.  
  23. static_assert(serial::udt_message_compatible_same_type<const int, int>::value, "");
  24. static_assert(serial::udt_message_compatible_same_type<const int &, int>::value, "");
  25. static_assert(serial::udt_message_compatible_same_type<const int &&, int>::value, "");
  26.  
  27. namespace {
  28.  
  29. struct simple_serial_test_struct
  30. {
  31.         uint16_t a, b;
  32. };
  33.  
  34. DEFINE_SERIAL_MUTABLE_UDT_TO_MESSAGE(simple_serial_test_struct, s, (s.a, s.b));
  35.  
  36. }
  37.  
  38. BOOST_AUTO_TEST_CASE(read_uint16_10)
  39. {
  40.         constexpr uint8_t buf[2]{1, 0};
  41.         serial::reader::bytebuffer_t b(buf);
  42.         uint16_t u = 0;
  43.         process_integer(b, u);
  44.         BOOST_TEST(u == 1);
  45. }
  46.  
  47. BOOST_AUTO_TEST_CASE(read_uint16_01)
  48. {
  49.         constexpr uint8_t buf[2]{0, 1};
  50.         serial::reader::bytebuffer_t b(buf);
  51.         uint16_t u = 0;
  52.         process_integer(b, u);
  53.         BOOST_TEST(u == 0x100);
  54. }
  55.  
  56. BOOST_AUTO_TEST_CASE(read_uint32_0010)
  57. {
  58.         constexpr uint8_t buf[4]{0, 0, 1, 0};
  59.         serial::reader::bytebuffer_t b(buf);
  60.         uint32_t u = 0;
  61.         process_integer(b, u);
  62.         BOOST_TEST(u == 0x10000);
  63. }
  64.  
  65. BOOST_AUTO_TEST_CASE(read_uint8_array_to_std_array)
  66. {
  67.         constexpr uint8_t buf[4]{1, 2, 3, 4};
  68.         serial::reader::bytebuffer_t b(buf);
  69.         std::array<uint8_t, 3> u{};
  70.         process_array(b, u);
  71.         BOOST_TEST(u[0] == 1);
  72.         BOOST_TEST(u[1] == 2);
  73.         BOOST_TEST(u[2] == 3);
  74. }
  75.  
  76. BOOST_AUTO_TEST_CASE(read_uint8_array_to_uint16_array)
  77. {
  78.         constexpr uint8_t buf[]{1, 2, 3, 4, 5, 6, 7, 8};
  79.         serial::reader::bytebuffer_t b(buf);
  80.         std::array<uint16_t, 4> u{};
  81.         process_buffer(b, u[0], u[1], u[3], u[2]);
  82.         BOOST_TEST(u[0] == 0x201);
  83.         BOOST_TEST(u[1] == 0x403);
  84.         BOOST_TEST(u[2] == 0x807);
  85.         BOOST_TEST(u[3] == 0x605);
  86. }
  87.  
  88. BOOST_AUTO_TEST_CASE(read_uint8_array_to_uint32_array)
  89. {
  90.         constexpr uint8_t buf[]{1, 2, 3, 4, 5, 6, 7, 8};
  91.         serial::reader::bytebuffer_t b(buf);
  92.         std::array<uint32_t, 2> u{};
  93.         process_buffer(b, u);
  94.         BOOST_TEST(u[0] == 0x4030201);
  95.         BOOST_TEST(u[1] == 0x8070605);
  96. }
  97.  
  98. BOOST_AUTO_TEST_CASE(read_struct)
  99. {
  100.         constexpr uint8_t buf[]{1, 2, 3, 4};
  101.         serial::reader::bytebuffer_t b(buf);
  102.         simple_serial_test_struct s{0x1234, 0x5678};
  103.         process_buffer(b, s);
  104.         BOOST_TEST(s.a == 0x201);
  105.         BOOST_TEST(s.b == 0x403);
  106. }
  107.  
  108. BOOST_AUTO_TEST_CASE(read_sign_extend)
  109. {
  110.         constexpr int8_t expected = -5;
  111.         constexpr uint8_t buf[2]{static_cast<uint8_t>(expected), 0xff};
  112.         serial::reader::bytebuffer_t b(buf);
  113.         int8_t value = 1;
  114.         process_buffer(b, serial::sign_extend<int16_t>(value));
  115.         BOOST_TEST(value == expected);
  116. }
  117.  
  118. BOOST_AUTO_TEST_CASE(read_pad)
  119. {
  120.         constexpr uint8_t buf[2]{0, 0xff};
  121.         serial::reader::bytebuffer_t b(buf);
  122.         process_buffer(b, serial::pad<2>());
  123. }
  124.  
  125. BOOST_AUTO_TEST_CASE(write_uint16)
  126. {
  127.         uint8_t buf[2]{};
  128.         serial::writer::bytebuffer_t b(buf);
  129.         constexpr uint16_t u = 0x100;
  130.         process_integer(b, u);
  131.         constexpr uint8_t expected[] = {0, 1};
  132.         BOOST_TEST(buf == expected);
  133. }
  134.  
  135. BOOST_AUTO_TEST_CASE(write_uint32)
  136. {
  137.         uint8_t buf[4]{};
  138.         serial::writer::bytebuffer_t b(buf);
  139.         constexpr uint32_t u = 0x12345678;
  140.         process_integer(b, u);
  141.         BOOST_TEST(buf[0] == 0x78);
  142.         BOOST_TEST(buf[1] == 0x56);
  143.         BOOST_TEST(buf[2] == 0x34);
  144.         BOOST_TEST(buf[3] == 0x12);
  145. }
  146.  
  147. BOOST_AUTO_TEST_CASE(write_uint8_array)
  148. {
  149.         uint8_t buf[4]{0xff, 0xfe, 0xfd, 0xfc};
  150.         serial::writer::bytebuffer_t b(buf);
  151.         constexpr std::array<uint8_t, 3> u{{1, 2, 3}};
  152.         process_array(b, u);
  153.         BOOST_TEST(buf[0] == 1);
  154.         BOOST_TEST(buf[1] == 2);
  155.         BOOST_TEST(buf[2] == 3);
  156.         BOOST_TEST(buf[3] == 0xfc);     // check that the field was not modified
  157. }
  158.  
  159. BOOST_AUTO_TEST_CASE(write_uint16_array)
  160. {
  161.         uint8_t buf[]{81, 82, 83, 84, 85, 86, 87, 88};
  162.         serial::writer::bytebuffer_t b(buf);
  163.         constexpr std::array<uint16_t, 4> u{{0x201, 0x403, 0x605, 0x807}};
  164.         process_buffer(b, u[0], u[1], u[3], u[2]);
  165.         BOOST_TEST(buf[0] == 1);
  166.         BOOST_TEST(buf[1] == 2);
  167.         BOOST_TEST(buf[2] == 3);
  168.         BOOST_TEST(buf[3] == 4);
  169.         BOOST_TEST(buf[4] == 7);
  170.         BOOST_TEST(buf[5] == 8);
  171.         BOOST_TEST(buf[6] == 5);
  172.         BOOST_TEST(buf[7] == 6);
  173. }
  174.  
  175. BOOST_AUTO_TEST_CASE(write_sign_extend)
  176. {
  177.         uint8_t buf[2]{};
  178.         serial::writer::bytebuffer_t b(buf);
  179.         constexpr int8_t value = 0x100 - 5;
  180.         process_buffer(b, serial::sign_extend<int16_t>(value));
  181.         BOOST_TEST(buf[0] == 0xfb);
  182.         BOOST_TEST(buf[1] == 0xff);
  183. }
  184.  
  185. BOOST_AUTO_TEST_CASE(write_pad)
  186. {
  187.         uint8_t buf[2]{};
  188.         serial::writer::bytebuffer_t b(buf);
  189.         process_buffer(b, serial::pad<2, 0x85>());
  190.         BOOST_TEST(buf[0] == 0x85);
  191.         BOOST_TEST(buf[1] == 0x85);
  192. }
  193.