JayBeams  0.1
Another project to have fun coding.
ut_timestamp.cpp
Go to the documentation of this file.
1 #include <jb/itch5/timestamp.hpp>
2 
3 #include <boost/test/unit_test.hpp>
4 
5 /**
6  * @test Verify that jb::itch5::decoder works for jb::itch5::timestamp
7  * as expected.
8  */
9 BOOST_AUTO_TEST_CASE(decode_timestamp) {
11  using jb::itch5::decoder;
12  char buffer[32];
13  int values[] = {10, 20, 30, 40, 15, 25};
14  std::uint64_t expected = 0;
15  for (int i = 0; i != 6; ++i) {
16  buffer[i] = values[i];
17  expected = expected * 256 + values[i];
18  }
19 
20  auto actual = decoder<true, timestamp>::r(16, buffer, 0);
21  BOOST_CHECK_EQUAL(actual.ts.count(), expected);
22 
23  actual = decoder<false, timestamp>::r(16, buffer, 0);
24  BOOST_CHECK_EQUAL(actual.ts.count(), expected);
25 
26  // In the following tests we are simply checking the range, so zero
27  // out the buffer to avoid range errors due to uninitialized memory.
28  std::memset(buffer, 0, sizeof(buffer));
29  BOOST_CHECK_NO_THROW((decoder<true, timestamp>::r(16, buffer, 2)));
30  BOOST_CHECK_NO_THROW((decoder<true, timestamp>::r(16, buffer, 10)));
31  BOOST_CHECK_THROW(
32  (decoder<true, timestamp>::r(16, buffer, 11)), std::runtime_error);
33  BOOST_CHECK_NO_THROW((decoder<false, timestamp>::r(16, buffer, 11)));
34 }
35 
36 /**
37  * @test Verify that the jb::itch5::decoder detects out of range
38  * errors for jb::itch5::timestamp.
39  */
40 BOOST_AUTO_TEST_CASE(decode_timestamp_range) {
42  using jb::itch5::decoder;
43  char buffer[32];
44  int values[] = {255, 255, 255, 255, 255, 255};
45  for (int i = 0; i != 6; ++i) {
46  buffer[i] = values[i];
47  }
48 
49  BOOST_CHECK_THROW(
50  (decoder<true, timestamp>::r(16, buffer, 0)), std::runtime_error);
51  BOOST_CHECK_NO_THROW((decoder<false, timestamp>::r(16, buffer, 0)));
52 }
53 
54 /**
55  * @test Verify that jb::itch5::timestamp iostream operator works as expected.
56  */
57 BOOST_AUTO_TEST_CASE(stream_timestamp) {
58  using namespace std::chrono;
60 
61  {
62  auto nn =
63  (duration_cast<nanoseconds>(hours(7)) +
64  duration_cast<nanoseconds>(minutes(8)) +
65  duration_cast<nanoseconds>(seconds(9)) + nanoseconds(20));
66  std::ostringstream os;
67  os << timestamp{nn};
68  BOOST_CHECK_EQUAL(os.str(), "070809.000000020");
69  }
70 
71  {
72  auto nn =
73  (duration_cast<nanoseconds>(hours(9)) +
74  duration_cast<nanoseconds>(minutes(30)) +
75  duration_cast<nanoseconds>(seconds(0)) + nanoseconds(0));
76  std::ostringstream os;
77  os << timestamp{nn};
78  BOOST_CHECK_EQUAL(os.str(), "093000.000000000");
79  }
80 
81  {
82  auto nn =
83  (duration_cast<nanoseconds>(hours(15)) +
84  duration_cast<nanoseconds>(minutes(59)) +
85  duration_cast<nanoseconds>(seconds(59)) + nanoseconds(999999999));
86  std::ostringstream os;
87  os << timestamp{nn};
88  BOOST_CHECK_EQUAL(os.str(), "155959.999999999");
89  }
90 
91  {
92  auto nn =
93  (duration_cast<nanoseconds>(hours(16)) +
94  duration_cast<nanoseconds>(minutes(0)) +
95  duration_cast<nanoseconds>(seconds(0)) + nanoseconds(0));
96  std::ostringstream os;
97  os << timestamp{nn};
98  BOOST_CHECK_EQUAL(os.str(), "160000.000000000");
99  }
100 }
101 
102 /**
103  * @test Verify that jb::itch5::encoder works for jb::itch5::timestamp
104  * as expected.
105  */
106 BOOST_AUTO_TEST_CASE(encode_timestamp) {
107  using jb::itch5::timestamp;
108  using jb::itch5::decoder;
109  using jb::itch5::encoder;
110 
111  using namespace std::chrono;
112  timestamp expected{hours(9) + minutes(31) + seconds(10) + nanoseconds(1234)};
113 
114  char buffer[32];
115  encoder<true, timestamp>::w(16, buffer, 0, expected);
116  auto actual = decoder<true, timestamp>::r(16, buffer, 0);
117  BOOST_CHECK_EQUAL(actual.ts.count(), expected.ts.count());
118 
119  encoder<false, timestamp>::w(16, buffer, 0, expected);
120  actual = decoder<false, timestamp>::r(16, buffer, 0);
121  BOOST_CHECK_EQUAL(actual.ts.count(), expected.ts.count());
122 
123  timestamp ts{seconds(100)};
124  BOOST_CHECK_NO_THROW((encoder<true, timestamp>::w(16, buffer, 2, ts)));
125  BOOST_CHECK_NO_THROW((encoder<true, timestamp>::w(16, buffer, 10, ts)));
126  BOOST_CHECK_THROW(
127  (encoder<true, timestamp>::w(16, buffer, 11, ts)), std::runtime_error);
128  BOOST_CHECK_NO_THROW((encoder<false, timestamp>::w(16, buffer, 11, ts)));
129 }
130 
131 /**
132  * @test Verify that the jb::itch5::decoder detects out of range
133  * errors for jb::itch5::timestamp.
134  */
135 BOOST_AUTO_TEST_CASE(encode_timestamp_range) {
136  using jb::itch5::timestamp;
137  using jb::itch5::encoder;
138  char buffer[32];
139 
140  timestamp ts{std::chrono::hours(48)};
141  BOOST_CHECK_THROW(
142  (encoder<true, timestamp>::w(16, buffer, 0, ts)), std::runtime_error);
143  BOOST_CHECK_NO_THROW((encoder<false, timestamp>::w(16, buffer, 0, ts)));
144 }
BOOST_AUTO_TEST_CASE(decode_timestamp)
Definition: ut_timestamp.cpp:9
TODO(#19) all this code should be replaced with Boost.Endian.
Definition: encoder.hpp:13
Define the interface to decode ITCH-5.0 messages and message fields.
Definition: decoder.hpp:24
Represent a ITCH-5.0 timestamp.
Definition: timestamp.hpp:17