Skip to content

Commit

Permalink
Implement the 'Unit Clear' message.
Browse files Browse the repository at this point in the history
  • Loading branch information
coryan committed May 28, 2017
1 parent b5f53fc commit 4a586ff
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ jb_itch5_unit_tests = \

jb_pitch2_unit_tests = \
jb/pitch2/ut_auction_update_message \
jb/pitch2/ut_time_message
jb/pitch2/ut_time_message \
jb/pitch2/ut_unit_clear_message

jb_testing_unit_tests = \
jb/testing/ut_check_close_enough \
Expand Down Expand Up @@ -476,10 +477,12 @@ nobase_jb_libjb_itch5_a_HEADERS = \
jb_libjb_pitch2_adir = $(includedir)
jb_libjb_pitch2_a_SOURCES = \
jb/pitch2/auction_update_message.cpp \
jb/pitch2/time_message.cpp
jb/pitch2/time_message.cpp \
jb/pitch2/unit_clear_message.cpp
jb_libjb_pitch2_a_HEADERS = \
jb/pitch2/auction_update_message.hpp \
jb/pitch2/time_message.hpp
jb/pitch2/time_message.hpp \
jb/pitch2/unit_clear_message.hpp

################################################################
# jb/libjb_fftw.a
Expand Down Expand Up @@ -1236,6 +1239,11 @@ jb_pitch2_ut_time_message_CPPFLAGS = $(UT_CPPFLAGS) -DBOOST_TEST_MODULE=jb_pitch
jb_pitch2_ut_time_message_LDFLAGS = $(jb_ut_pitch2_ldflags)
jb_pitch2_ut_time_message_LDADD = $(jb_ut_pitch2_ldadd)

jb_pitch2_ut_unit_clear_message_SOURCES = jb/pitch2/ut_unit_clear_message.cpp
jb_pitch2_ut_unit_clear_message_CPPFLAGS = $(UT_CPPFLAGS) -DBOOST_TEST_MODULE=jb_pitch2_ut_unit_clear_message
jb_pitch2_ut_unit_clear_message_LDFLAGS = $(jb_ut_pitch2_ldflags)
jb_pitch2_ut_unit_clear_message_LDADD = $(jb_ut_pitch2_ldadd)

################################################################
# testing program and unit tests
################################################################
Expand Down
16 changes: 16 additions & 0 deletions jb/pitch2/unit_clear_message.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <jb/pitch2/unit_clear_message.hpp>

#include <ostream>

namespace jb {
namespace pitch2 {

std::ostream& operator<<(std::ostream& os, unit_clear_message const& x) {
return os << "length=" << static_cast<int>(x.length.value())
<< ",message_type=" << static_cast<int>(x.message_type.value())
<< ",time_offset=" << x.time_offset.value();
}

} // namespace pitch2
} // namespace jb

34 changes: 34 additions & 0 deletions jb/pitch2/unit_clear_message.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef jb_pitch2_unit_clear_message_hpp
#define jb_pitch2_unit_clear_message_hpp

#include <boost/endian/buffers.hpp>

#include <iosfwd>
#include <utility>

namespace jb {
namespace pitch2 {

/**
* Represent the 'Unit Clear' message in the PITCH-2.X protocol.
*
* A full description of the fields can be found in the BATS PITCH-2.X
* specification:
* https://www.batstrading.com/resources/membership/BATS_MC_PITCH_Specification.pdf
*/
struct unit_clear_message {
/// Define the messsage type
constexpr static int type = 0x97;

boost::endian::little_uint8_buf_t length;
boost::endian::little_uint8_buf_t message_type;
boost::endian::little_int32_buf_t time_offset;
};

/// Streaming operator for jb::pitch2::unit_clear_message.
std::ostream& operator<<(std::ostream& os, unit_clear_message const& x);

} // namespace pitch2
} // namespace jb

#endif // jb_pitch2_unit_clear_message_hpp
30 changes: 30 additions & 0 deletions jb/pitch2/ut_unit_clear_message.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <jb/pitch2/unit_clear_message.hpp>

#include <boost/test/unit_test.hpp>
#include <type_traits>

/**
* @test Verify that jb::pitch2::unit_clear_message works as expected.
*/
BOOST_AUTO_TEST_CASE(unit_clear_message_basic) {
using namespace jb::pitch2;
BOOST_CHECK_EQUAL(true, std::is_pod<unit_clear_message>::value);
BOOST_CHECK_EQUAL(sizeof(unit_clear_message), std::size_t(6));

char const buf[] =
u8"\x06" // Length (6)
"\x97" // Message Type (0x97)
"\x18\xD2\x06\x00" // Time Offset (447,000 ns)
;
unit_clear_message msg;
BOOST_REQUIRE_EQUAL(sizeof(buf) - 1, sizeof(msg));
std::memcpy(&msg, buf, sizeof(msg));
BOOST_CHECK_EQUAL(int(msg.length.value()), 6);
BOOST_CHECK_EQUAL(int(msg.message_type.value()), 0x97);
BOOST_CHECK_EQUAL(msg.time_offset.value(), 447000);

std::ostringstream os;
os << msg;
BOOST_CHECK_EQUAL(
os.str(), std::string("length=6,message_type=151,time_offset=447000"));
}

0 comments on commit 4a586ff

Please sign in to comment.