rippled
beast_abstract_clock_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 // MODULES: ../impl/chrono_io.cpp
21 
22 #include <ripple/beast/clock/abstract_clock.h>
23 #include <ripple/beast/clock/manual_clock.h>
24 #include <ripple/beast/unit_test.h>
25 #include <sstream>
26 #include <string>
27 #include <thread>
28 
29 namespace beast {
30 
31 class abstract_clock_test : public unit_test::suite
32 {
33 public:
34  template <class Clock>
35  void
37  {
38  testcase(name);
39 
40  auto const t1(c.now());
42  auto const t2(c.now());
43 
44  log << "t1= " << t1.time_since_epoch().count()
45  << ", t2= " << t2.time_since_epoch().count()
46  << ", elapsed= " << (t2 - t1).count() << std::endl;
47 
48  pass();
49  }
50 
51  void
53  {
54  testcase("manual");
55 
56  using clock_type = manual_clock<std::chrono::steady_clock>;
57  clock_type c;
58 
60 
61  auto c1 = c.now().time_since_epoch();
62  c.set(clock_type::time_point(std::chrono::seconds(1)));
63  auto c2 = c.now().time_since_epoch();
64  c.set(clock_type::time_point(std::chrono::seconds(2)));
65  auto c3 = c.now().time_since_epoch();
66 
67  log << "[" << c1.count() << "," << c2.count() << "," << c3.count()
68  << "]" << std::endl;
69 
70  pass();
71  }
72 
73  void
74  run() override
75  {
76  test("steady_clock", get_abstract_clock<std::chrono::steady_clock>());
77  test("system_clock", get_abstract_clock<std::chrono::system_clock>());
78  test(
79  "high_resolution_clock",
80  get_abstract_clock<std::chrono::high_resolution_clock>());
81 
82  test_manual();
83  }
84 };
85 
86 BEAST_DEFINE_TESTSUITE_MANUAL(abstract_clock, chrono, beast);
87 
88 } // namespace beast
sstream
std::this_thread::sleep_for
T sleep_for(T... args)
std::string
STL class.
beast::abstract_clock_test::test
void test(std::string name, abstract_clock< Clock > &c)
Definition: beast_abstract_clock_test.cpp:36
std::chrono::milliseconds
std::stringstream
STL class.
beast::abstract_clock::now
virtual time_point now() const =0
Returns the current time.
thread
beast::abstract_clock_test::run
void run() override
Definition: beast_abstract_clock_test.cpp:74
beast::abstract_clock_test
Definition: beast_abstract_clock_test.cpp:31
beast::abstract_clock
Abstract interface to a clock.
Definition: abstract_clock.h:57
std::endl
T endl(T... args)
beast::manual_clock< std::chrono::steady_clock >
beast::BEAST_DEFINE_TESTSUITE_MANUAL
BEAST_DEFINE_TESTSUITE_MANUAL(abstract_clock, chrono, beast)
beast::abstract_clock_test::test_manual
void test_manual()
Definition: beast_abstract_clock_test.cpp:52
beast
Definition: base_uint.h:641
string