JayBeams  0.1
Another project to have fun coding.
ut_book_depth_statistics.cpp
Go to the documentation of this file.
2 
3 #include <boost/test/unit_test.hpp>
4 
5 /**
6  * @test Verify that jb::book_depth_statistics works as expected.
7  */
8 BOOST_AUTO_TEST_CASE(book_depth_statistics_simple) {
10  jb::book_depth_statistics stats(cfg);
11 
12  stats.sample(1);
13  stats.sample(2);
14  stats.sample(3);
15  stats.sample(4);
16  stats.sample(5);
17 }
18 
19 /**
20  * @test Test jb::book_depth_statistics csv output.
21  */
22 BOOST_AUTO_TEST_CASE(book_depth_statistics_print_csv) {
24  jb::book_depth_statistics stats(cfg);
25  std::ostringstream header;
26  stats.print_csv_header(header);
27  BOOST_CHECK_EQUAL(header.str().substr(0, 5), std::string("Name,"));
28 
29  std::string h = header.str();
30  // save number of headers...
31  int nheaders = std::count(h.begin(), h.end(), ',');
32 
33  std::ostringstream body;
34 
35  // testing header format
36  stats.print_csv("testing", body);
37  BOOST_CHECK_EQUAL(body.str().substr(0, 10), std::string("testing,0,"));
38  std::string b = body.str();
39  int nfields = std::count(b.begin(), b.end(), ',');
40  BOOST_CHECK_EQUAL(nfields, nheaders);
41 
42  // 4 samples, book_depth {2..5}
43  stats.sample(5);
44  stats.sample(2);
45  stats.sample(3);
46  stats.sample(4);
47 
48  // check 4 samples...
49  body.str("");
50  stats.print_csv("testing", body);
51  BOOST_CHECK_EQUAL(body.str().substr(0, 10), std::string("testing,4,"));
52  // ... minBookDepth = 2
53  BOOST_CHECK_EQUAL(body.str().substr(10, 2), std::string("2,"));
54  // ... number of fields = to #headers
55  b = body.str();
56  nfields = std::count(b.begin(), b.end(), ',');
57  BOOST_CHECK_EQUAL(nfields, nheaders);
58 
59  // add one more sample (# 5), book_depth now {1..5}
60  stats.sample(1);
61  body.str("");
62  stats.print_csv("testing", body);
63  BOOST_CHECK_EQUAL(body.str().substr(0, 10), std::string("testing,5,"));
64  BOOST_CHECK_EQUAL(body.str().substr(10, 2), std::string("1,"));
65 
66  BOOST_TEST_MESSAGE("CSV Output for inspection: \n" << h << b);
67 }
68 
69 /**
70  * @test Verify that jb::book_depth_statistics::config works as expected.
71  */
72 BOOST_AUTO_TEST_CASE(book_depth_statististics_config_simple) {
73  typedef jb::book_depth_statistics::config config;
74 
75  BOOST_CHECK_NO_THROW(config().validate());
76  BOOST_CHECK_THROW(config().max_book_depth(0).validate(), jb::usage);
77 }
book_depth_t max_book_depth
BOOST_AUTO_TEST_CASE(book_depth_statistics_simple)
void sample(book_depth_t book_depth)
Record a sample, that is book depth value after the event.
static void print_csv_header(std::ostream &os)
Print a CSV header.
Keep statistics about a feed and its book depth.
void print_csv(std::string const &name, std::ostream &os) const
Print all the measurements in CSV format.
Configure a book_depth_statistics object.
A simple class to communicate the result of parsing the options.
Definition: usage.hpp:11