JayBeams  0.1
Another project to have fun coding.
ut_short_string_field.cpp
Go to the documentation of this file.
2 
3 #include <boost/test/unit_test.hpp>
4 
5 /**
6  * @test Verify that jb::itch5::decoder for jb::itch5::short_string_field
7  * works as expected.
8  */
9 BOOST_AUTO_TEST_CASE(decode_short_string_field) {
10  typedef jb::itch5::short_string_field<4> tested;
11  using jb::itch5::decoder;
12  char buffer[32];
13 
14  {
15  std::memset(buffer, 0, sizeof(buffer));
16  std::memcpy(buffer, "AB ", 4);
17  auto actual = decoder<true, tested>::r(16, buffer, 0);
18  BOOST_CHECK_EQUAL(actual.c_str(), "AB");
19 
20  actual = decoder<false, tested>::r(16, buffer, 0);
21  BOOST_CHECK_EQUAL(actual.c_str(), "AB");
22 
23  BOOST_CHECK_NO_THROW((decoder<true, tested>::r(16, buffer, 2)));
24  BOOST_CHECK_THROW(
25  (decoder<true, tested>::r(16, buffer, 13)), std::runtime_error);
26  BOOST_CHECK_NO_THROW((decoder<false, tested>::r(16, buffer, 13)));
27  }
28 
29  {
30  std::memset(buffer, 0, sizeof(buffer));
31  std::memcpy(buffer, "ABCD", 4);
32  auto actual = decoder<true, tested>::r(16, buffer, 0);
33  BOOST_CHECK_EQUAL(actual.c_str(), "ABCD");
34 
35  actual = decoder<false, tested>::r(16, buffer, 0);
36  BOOST_CHECK_EQUAL(actual.c_str(), "ABCD");
37  }
38 }
39 
40 /**
41  * @test Verify that value validators in jb::itch5::decoder works work
42  * as expected.
43  */
44 BOOST_AUTO_TEST_CASE(validate_short_string_field) {
45  struct simple_validator {
46  bool operator()(char const* rhs) const {
47  return std::string(rhs) == "AA" or std::string(rhs) == "ABCD";
48  }
49  };
50 
51  using namespace jb::itch5;
53 
54  char buffer[32];
55  std::memset(buffer, 0, sizeof(buffer));
56  std::memcpy(buffer, "ABCD", 4);
57  auto actual = decoder<true, tested>::r(16, buffer, 0);
58  BOOST_CHECK_EQUAL(actual.c_str(), "ABCD");
59 
60  std::memset(buffer, 0, sizeof(buffer));
61  std::memcpy(buffer, "ABC", 4);
62  BOOST_CHECK_THROW(
63  (decoder<true, tested>::r(16, buffer, 0)), std::runtime_error);
64  BOOST_CHECK_NO_THROW((decoder<false, tested>::r(16, buffer, 0)));
65 }
66 
67 /**
68  * @test Verify that jb::itch5::short_string_field iostream operator
69  * works as expected.
70  */
71 BOOST_AUTO_TEST_CASE(stream_short_string_field) {
72  typedef jb::itch5::short_string_field<4> tested;
73  using jb::itch5::decoder;
74  char buffer[32];
75 
76  {
77  std::memset(buffer, 0, sizeof(buffer));
78  std::memcpy(buffer, "AB ", 4);
79  auto actual = decoder<true, tested>::r(16, buffer, 0);
80 
81  std::ostringstream os;
82  os << actual;
83  BOOST_CHECK_EQUAL(os.str(), "AB");
84  }
85 
86  {
87  std::memset(buffer, 0, sizeof(buffer));
88  std::memcpy(buffer, "ABCD", 4);
89  auto actual = decoder<true, tested>::r(16, buffer, 0);
90 
91  std::ostringstream os;
92  os << actual;
93  BOOST_CHECK_EQUAL(os.str(), "ABCD");
94  }
95 }
Contains classes and functions to parse NASDAQ ITCH-5.0 messages, more information about ITCH-5...
Define the interface to decode ITCH-5.0 messages and message fields.
Definition: decoder.hpp:24
BOOST_AUTO_TEST_CASE(decode_short_string_field)