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