Skip to content

A C++11-compliant fake clock useful for C++ unit tests that depend on time.

License

Notifications You must be signed in to change notification settings

korfuri/fake_clock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libfake_clock

A clock class that is convenient for testing.

This class satisfies the TrivialClock requirement and as such can be used in place of any standard clock (e.g. std::chrono::system_clock). This is useful to test C++ code that manipulates time, without having to use sleep() in your unittests.

The clock uses an uint64_t internally, so it can store all nanoseconds in a century. This is consistent with the precision required of std::chrono::nanoseconds in C++11.

Example usage:

   fake_clock::time_point t1 = fake_clock::now();
   fake_clock::advance(std::chrono::milliseconds(100));
   fake_clock::time_point t2 = fake_clock::now();
   auto elapsed_us = std::chrono::duration_cast<
          std::chrono::microseconds>(t2 - t1).count();
   assert(100000 == elapsed_us);

For a more advanced and practical example, see my prometheus client implementation for which I originally wrote this.

About

A C++11-compliant fake clock useful for C++ unit tests that depend on time.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published