JayBeams  0.1
Another project to have fun coding.
ut_base_decoders.cpp
Go to the documentation of this file.
2 
3 #include <boost/test/unit_test.hpp>
4 
5 /**
6  * @test Verify that base decoders work as expected.
7  */
8 BOOST_AUTO_TEST_CASE(decode_uint8) {
9  using jb::itch5::decoder;
10  // The actual buffer size is 32 bytes, but we treat them as 16 byte
11  // buffers. This is because some of the tests verify we (a) do
12  // detect attempts to read past the end of the array, and (b) when
13  // checking is disabled, we do allow reading past the 'end'.
14  // Using a larger buffer allows such tests without involving
15  // undefined behavior.
16  char buffer[32];
17  buffer[1] = 20;
18  buffer[3] = 25;
19 
20  auto actual = decoder<true, std::uint8_t>::r(16, buffer, 1);
21  BOOST_CHECK_EQUAL(actual, 20);
22 
23  actual = decoder<false, std::uint8_t>::r(16, buffer, 3);
24  BOOST_CHECK_EQUAL(actual, 25);
25 
26  BOOST_CHECK_NO_THROW((decoder<true, std::uint8_t>::r(16, buffer, 0)));
27  BOOST_CHECK_NO_THROW((decoder<true, std::uint8_t>::r(16, buffer, 8)));
28  BOOST_CHECK_NO_THROW((decoder<true, std::uint8_t>::r(16, buffer, 15)));
29  BOOST_CHECK_THROW(
30  (decoder<true, std::uint8_t>::r(16, buffer, 16)), std::runtime_error);
31  BOOST_CHECK_NO_THROW((decoder<false, std::uint8_t>::r(16, buffer, 16)));
32 }
33 
34 /**
35  * @test Verify that base decoders work as expected.
36  */
37 BOOST_AUTO_TEST_CASE(decode_uint16) {
38  using jb::itch5::decoder;
39  char buffer[32];
40  buffer[0] = 10;
41  buffer[1] = 20;
42 
43  auto actual = decoder<true, std::uint16_t>::r(16, buffer, 0);
44  BOOST_CHECK_EQUAL(actual, 10 * 256 + 20);
45 
46  actual = decoder<false, std::uint16_t>::r(16, buffer, 0);
47  BOOST_CHECK_EQUAL(actual, 10 * 256 + 20);
48 
49  BOOST_CHECK_NO_THROW((decoder<true, std::uint16_t>::r(16, buffer, 0)));
50  BOOST_CHECK_NO_THROW((decoder<true, std::uint16_t>::r(16, buffer, 8)));
51  BOOST_CHECK_NO_THROW((decoder<true, std::uint16_t>::r(16, buffer, 14)));
52  BOOST_CHECK_THROW(
53  (decoder<true, std::uint16_t>::r(16, buffer, 15)), std::runtime_error);
54  BOOST_CHECK_NO_THROW((decoder<false, std::uint16_t>::r(16, buffer, 15)));
55 }
56 
57 /**
58  * @test Verify that base decoders work as expected.
59  */
60 BOOST_AUTO_TEST_CASE(decode_uint32) {
61  using jb::itch5::decoder;
62  char buffer[32];
63  buffer[0] = 10;
64  buffer[1] = 20;
65  buffer[2] = 30;
66  buffer[3] = 40;
67 
68  auto actual = decoder<true, std::uint32_t>::r(16, buffer, 0);
69  BOOST_CHECK_EQUAL(actual, ((10 * 256 + 20) * 256 + 30) * 256 + 40);
70 
71  actual = decoder<false, std::uint32_t>::r(16, buffer, 0);
72  BOOST_CHECK_EQUAL(actual, ((10 * 256 + 20) * 256 + 30) * 256 + 40);
73 
74  BOOST_CHECK_NO_THROW((decoder<true, std::uint32_t>::r(16, buffer, 0)));
75  BOOST_CHECK_NO_THROW((decoder<true, std::uint32_t>::r(16, buffer, 8)));
76  BOOST_CHECK_NO_THROW((decoder<true, std::uint32_t>::r(16, buffer, 12)));
77  BOOST_CHECK_THROW(
78  (decoder<true, std::uint32_t>::r(16, buffer, 13)), std::runtime_error);
79  BOOST_CHECK_NO_THROW((decoder<false, std::uint32_t>::r(16, buffer, 13)));
80 }
81 
82 /**
83  * @test Verify that base decoders work as expected.
84  */
85 BOOST_AUTO_TEST_CASE(decode_uint64) {
86  using jb::itch5::decoder;
87  char buffer[32];
88  int values[] = {10, 20, 30, 40, 15, 25, 35, 45};
89  std::uint64_t expected = 0;
90  for (int i = 0; i != 8; ++i) {
91  buffer[i] = values[i];
92  expected = expected * 256 + values[i];
93  }
94 
95  auto actual = decoder<true, std::uint64_t>::r(16, buffer, 0);
96  BOOST_CHECK_EQUAL(actual, expected);
97 
98  actual = decoder<false, std::uint64_t>::r(16, buffer, 0);
99  BOOST_CHECK_EQUAL(actual, expected);
100 
101  BOOST_CHECK_NO_THROW((decoder<true, std::uint64_t>::r(16, buffer, 2)));
102  BOOST_CHECK_NO_THROW((decoder<true, std::uint64_t>::r(16, buffer, 7)));
103  BOOST_CHECK_THROW(
104  (decoder<true, std::uint64_t>::r(16, buffer, 9)), std::runtime_error);
105  BOOST_CHECK_NO_THROW((decoder<false, std::uint64_t>::r(16, buffer, 9)));
106 }
BOOST_AUTO_TEST_CASE(decode_uint8)
Define the interface to decode ITCH-5.0 messages and message fields.
Definition: decoder.hpp:24