// Copyright 2010-2011 Vicente J. Botet Escriba // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt #define BOOST_CHRONO_VERSION 2 #include #include #include #include "../cycle_count.hpp" #include #include #include #include #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) #define NOTHING "" #endif using namespace boost::chrono; template void check_invariants() { BOOST_CHRONO_STATIC_ASSERT((boost::is_same::value), NOTHING, ()); BOOST_CHRONO_STATIC_ASSERT((boost::is_same::value), NOTHING, ()); BOOST_CHRONO_STATIC_ASSERT((boost::is_same::value), NOTHING, ()); BOOST_CHRONO_STATIC_ASSERT(Stopwatch::is_steady == Stopwatch::clock::is_steady, NOTHING, ()); } template void check_default_constructor() { Stopwatch sw; BOOST_TEST(sw.is_running()); } template void check_dont_start_constructor() { Stopwatch sw(boost::chrono::dont_start); BOOST_TEST(!sw.is_running()); ex::sleep_for(boost::chrono::milliseconds(100)); typename Stopwatch::duration d=sw.elapsed(); BOOST_TEST(d == Stopwatch::duration::zero()); } #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING template void check_constructor_ec() { boost::system::error_code ec; Stopwatch sw(ec); BOOST_TEST(sw.is_running()); BOOST_TEST(ec.value()==0); } template void check_constructor_throws() { Stopwatch sw(boost::throws()); BOOST_TEST(sw.is_running()); } #endif template void check_elapsed() { Stopwatch sw; BOOST_TEST(sw.is_running()); ex::sleep_for(boost::chrono::milliseconds(100)); typename Stopwatch::duration d=sw.elapsed(); BOOST_TEST(sw.is_running()); BOOST_TEST(d >= boost::chrono::milliseconds(100)); } template void check_start_start() { Stopwatch sw; BOOST_TEST(sw.is_running()); ex::sleep_for(boost::chrono::milliseconds(100)); sw.start(); BOOST_TEST(sw.is_running()); ex::sleep_for(boost::chrono::milliseconds(100)); typename Stopwatch::duration d=sw.elapsed(); BOOST_TEST(sw.is_running()); BOOST_TEST(d >= boost::chrono::milliseconds(100)); BOOST_TEST(d < boost::chrono::milliseconds(200)); } template void check_dont_start_start() { Stopwatch sw(boost::chrono::dont_start); BOOST_TEST(!sw.is_running()); ex::sleep_for(boost::chrono::milliseconds(100)); sw.start(); BOOST_TEST(sw.is_running()); ex::sleep_for(boost::chrono::milliseconds(100)); typename Stopwatch::duration d=sw.elapsed(); BOOST_TEST(sw.is_running()); BOOST_TEST(d >= boost::chrono::milliseconds(100)); BOOST_TEST(d < boost::chrono::milliseconds(200)); } template void check_dont_start_start_stop() { Stopwatch sw(boost::chrono::dont_start); BOOST_TEST(!sw.is_running()); sw.start(); BOOST_TEST(sw.is_running()); ex::sleep_for(boost::chrono::milliseconds(100)); sw.stop(); BOOST_TEST(!sw.is_running()); typename Stopwatch::duration d=sw.elapsed(); BOOST_TEST(!sw.is_running()); BOOST_TEST(d >= boost::chrono::milliseconds(0)); } template void check_dont_start_scoped_run() { Stopwatch sw(boost::chrono::dont_start); BOOST_TEST(!sw.is_running()); { typename Stopwatch::scoped_run _(sw); BOOST_TEST(sw.is_running()); ex::sleep_for(boost::chrono::milliseconds(100)); } BOOST_TEST(!sw.is_running()); typename Stopwatch::duration d=sw.elapsed(); BOOST_TEST(!sw.is_running()); BOOST_TEST(d >= boost::chrono::milliseconds(0)); } template void check_stop() { Stopwatch sw; BOOST_TEST(sw.is_running()); ex::sleep_for(boost::chrono::milliseconds(100)); sw.stop(); BOOST_TEST(!sw.is_running()); typename Stopwatch::duration d=sw.elapsed(); BOOST_TEST(!sw.is_running()); BOOST_TEST(d == boost::chrono::milliseconds(0)); } template void check_stop_stop() { Stopwatch sw; BOOST_TEST(sw.is_running()); ex::sleep_for(boost::chrono::milliseconds(100)); sw.stop(); BOOST_TEST(!sw.is_running()); typename Stopwatch::duration d=sw.elapsed(); BOOST_TEST(!sw.is_running()); BOOST_TEST(d == boost::chrono::milliseconds(0)); sw.stop(); BOOST_TEST(!sw.is_running()); d=sw.elapsed(); BOOST_TEST(!sw.is_running()); BOOST_TEST(d == boost::chrono::milliseconds(0)); } struct file_line { file_line(const char* file, std::size_t line) : fmt("%1%[%2%] Elapsed time:") { fmt % file % line; } ~file_line() { std::cout << fmt; } boost::format fmt; }; template void check_file_line2() { Reporter _("%1%\n"); file_line fl(__FILE__, __LINE__); ex::sleep_for(milliseconds(100)); } template void check_file_line() { Reporter rp("%1%[%2%] Elapsed time: %3%\n"); rp.format() % __FILE__ % __LINE__; ex::sleep_for(milliseconds(100)); } template void check_report() { Reporter sw; ex::sleep_for(milliseconds(100)); sw.report(); } template void check_all() { typedef stopclock Reporter; typedef stopclock, elapsed_formatter > ReporterE; check_invariants(); check_default_constructor(); #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING check_constructor_ec(); check_constructor_throws(); #endif check_elapsed(); check_report(); check_file_line(); check_start_start(); check_dont_start_constructor(); check_dont_start_start(); check_dont_start_start_stop(); check_dont_start_scoped_run(); check_stop(); check_stop_stop(); } int main() { typedef stopclock Reporter; static Reporter::formatter_type fmtr; //Reporter _(fmtr); Reporter _; check_all >(); return boost::report_errors(); }