JayBeams  0.1
Another project to have fun coding.
ut_seconds_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 works for jb::itch5::seconds_field
7  * as expected.
8  */
9 BOOST_AUTO_TEST_CASE(decode_seconds_field) {
11  using jb::itch5::decoder;
12  using namespace std::chrono;
13 
14  char buffer[32];
15  std::memset(buffer, 0, sizeof(buffer));
16  std::memcpy(buffer, "\x00\x00\x64\x59", 4); // 07:08:09
17 
18  auto expected =
19  duration_cast<seconds>(hours(7) + minutes(8) + seconds(9)).count();
20 
21  auto actual = decoder<true, seconds_field>::r(16, buffer, 0);
22  BOOST_CHECK_EQUAL(actual.int_seconds(), expected);
23 
24  actual = decoder<false, seconds_field>::r(16, buffer, 0);
25  BOOST_CHECK_EQUAL(actual.int_seconds(), expected);
26 
27  // In the following tests we are simply checking the range, so zero
28  // out the buffer to avoid range errors due to uninitialized memory.
29  std::memset(buffer, 0, sizeof(buffer));
30  BOOST_CHECK_NO_THROW((decoder<true, seconds_field>::r(16, buffer, 2)));
31  BOOST_CHECK_NO_THROW((decoder<true, seconds_field>::r(16, buffer, 12)));
32  BOOST_CHECK_THROW(
33  (decoder<true, seconds_field>::r(16, buffer, 13)), std::runtime_error);
34  BOOST_CHECK_NO_THROW((decoder<false, seconds_field>::r(16, buffer, 13)));
35 }
36 
37 /**
38  * @test Verify that the jb::itch5::decoder detects out of range
39  * errors for jb::itch5::seconds_field.
40  */
41 BOOST_AUTO_TEST_CASE(decode_seconds_field_range) {
43  using jb::itch5::decoder;
44  char buffer[32];
45 
46  std::memset(buffer, 0, sizeof(buffer));
47  std::memcpy(buffer, "\x00\x01\x51\x80", 4); // 24:00:00
48 
49  BOOST_CHECK_THROW(
50  (decoder<true, seconds_field>::r(16, buffer, 0)), std::runtime_error);
51  BOOST_CHECK_NO_THROW((decoder<false, seconds_field>::r(16, buffer, 0)));
52 }
53 
54 /**
55  * @test Verify that jb::itch5::seconds_field iostream operator works
56  * as expected.
57  */
58 BOOST_AUTO_TEST_CASE(stream_seconds_field) {
59  using namespace std::chrono;
61 
62  {
63  auto nn = duration_cast<seconds>(hours(7) + minutes(8) + seconds(9));
64  std::ostringstream os;
65  os << seconds_field(nn);
66  BOOST_CHECK_EQUAL(os.str(), "07:08:09");
67  }
68 
69  {
70  auto nn = duration_cast<seconds>(hours(9) + minutes(30) + seconds(0));
71  std::ostringstream os;
72  os << seconds_field{nn};
73  BOOST_CHECK_EQUAL(os.str(), "09:30:00");
74  }
75 
76  {
77  auto nn = duration_cast<seconds>(hours(15) + minutes(59) + seconds(59));
78  std::ostringstream os;
79  os << seconds_field{nn};
80  BOOST_CHECK_EQUAL(os.str(), "15:59:59");
81  }
82 
83  {
84  auto nn = duration_cast<seconds>(hours(16) + minutes(0) + seconds(0));
85  std::ostringstream os;
86  os << seconds_field{nn};
87  BOOST_CHECK_EQUAL(os.str(), "16:00:00");
88  }
89 }
Represent a ITCH-5.0 seconds_field.
BOOST_AUTO_TEST_CASE(decode_seconds_field)
Define the interface to decode ITCH-5.0 messages and message fields.
Definition: decoder.hpp:24