libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 #include <boost/test/unit_test.hpp> 00029 #include <boost/test/test_tools.hpp> 00030 00031 #include <limits> 00032 00033 #include <boost/thread.hpp> 00034 00035 #include "../../include/moost/thread/xtime_util.hpp" 00036 #include "../../include/moost/timer.h" 00037 00038 using namespace moost; 00039 using namespace moost::thread; 00040 00041 BOOST_AUTO_TEST_SUITE( timer_test ) 00042 00043 struct Fixture 00044 { 00045 Fixture() 00046 : timer_(48) 00047 { 00048 } 00049 00050 ~Fixture() 00051 { 00052 } 00053 00054 timer timer_; 00055 }; 00056 00057 struct FixtureThreshold 00058 { 00059 FixtureThreshold() 00060 : timer_(48, 10, 5) 00061 { 00062 } 00063 00064 ~FixtureThreshold() 00065 { 00066 } 00067 00068 timer timer_; 00069 }; 00070 00071 BOOST_FIXTURE_TEST_CASE( test_timer_empty, Fixture ) 00072 { 00073 // test preconditions of timer 00074 BOOST_CHECK_EQUAL(timer_.avg_time(), -1); 00075 BOOST_CHECK_EQUAL(timer_.min_time(), -1); 00076 BOOST_CHECK_EQUAL(timer_.max_time(), -1); 00077 BOOST_CHECK_EQUAL(timer_.count_per_second(), 0); 00078 } 00079 00080 BOOST_FIXTURE_TEST_CASE( test_timer_time, Fixture ) 00081 { 00082 { 00083 timer::scoped_time lock(timer_); 00084 boost::thread::sleep(xtime_util::add_ms(xtime_util::now(), 100)); 00085 } 00086 double time = timer_.avg_time(); 00087 double eps = 0.0000001; 00088 BOOST_CHECK( fabs(timer_.avg_time() - time) < eps ); 00089 BOOST_CHECK( fabs(timer_.min_time() - time) < eps ); 00090 BOOST_CHECK( fabs(timer_.max_time() - time) < eps ); 00091 //BOOST_CHECK(timer_.count_per_second() > 1); 00092 } 00093 00094 00095 BOOST_FIXTURE_TEST_CASE( test_past_threshold, FixtureThreshold ) 00096 { 00097 std::vector< std::pair<int, boost::posix_time::ptime> > past_thresholds = timer_.past_threshold_times(2); 00098 BOOST_REQUIRE( past_thresholds.empty() ); 00099 00100 { 00101 timer::scoped_time lock(timer_); 00102 boost::thread::sleep(xtime_util::add_ms(xtime_util::now(), 1)); 00103 } 00104 00105 int longTimeMs = 25; 00106 { 00107 timer::scoped_time lock(timer_); 00108 boost::thread::sleep(xtime_util::add_ms(xtime_util::now(), longTimeMs)); 00109 } 00110 00111 past_thresholds = timer_.past_threshold_times(2); 00112 BOOST_REQUIRE( past_thresholds.size() == 1 ); 00113 int tolerance = 1; //ms 00114 BOOST_CHECK( abs( past_thresholds[0].first - longTimeMs ) < tolerance ); 00115 } 00116 00117 BOOST_FIXTURE_TEST_CASE( test_past_threshold_rollover, FixtureThreshold ) 00118 { 00119 { 00120 timer::scoped_time lock(timer_); 00121 boost::thread::sleep(xtime_util::add_ms(xtime_util::now(), 1)); 00122 } 00123 00124 int longTimeMs = 25; 00125 for ( int i = 0; i < 6; ++i ) // 6 times, but the size of the threshold vector is 5! 00126 { 00127 timer::scoped_time lock(timer_); 00128 boost::thread::sleep(xtime_util::add_ms(xtime_util::now(), longTimeMs+i)); 00129 } 00130 00131 std::vector< std::pair<int, boost::posix_time::ptime> > past_thresholds = timer_.past_threshold_times(10); 00132 BOOST_REQUIRE( past_thresholds.size() == 5 ); // the size of the threshold vector is 5! 00133 00134 boost::posix_time::time_duration dur; 00135 int totMs; 00136 int tolerance = 1; //ms 00137 00138 for ( int i = 0; i < 4; ++i ) 00139 { 00140 dur = past_thresholds[i].second - past_thresholds[i+1].second; 00141 totMs = static_cast<int>(dur.total_milliseconds()); 00142 BOOST_CHECK( abs(totMs - (longTimeMs + 5 - i)) < tolerance ); 00143 } 00144 00145 } 00146 00147 /*BOOST_FIXTURE_TEST_CASE( test_timer_rollover, Fixture ) 00148 { 00149 for (int i = 0; i != 49; ++i) 00150 { 00151 timer::scoped_time lock(timer_); 00152 boost::thread::sleep(xtime_util::add_ms(xtime_util::now(), 10)); 00153 } 00154 int cps = timer_.count_per_second(); 00155 int time = timer_.avg_time(); 00156 BOOST_CHECK_PREDICATE( std::greater_equal<int>(), (time) (8) ); 00157 BOOST_CHECK_PREDICATE( std::less_equal<int>(), (time) (12) ); 00158 BOOST_CHECK_PREDICATE( std::greater<int>(), (timer_.min_time()) (5) ); 00159 BOOST_CHECK_PREDICATE( std::less<int>(), (timer_.max_time()) (15) ); 00160 BOOST_CHECK_PREDICATE( std::greater<int>(), (cps) (95) ); 00161 BOOST_CHECK_PREDICATE( std::less<int>(), (cps) (105) ); 00162 }*/ 00163 00164 BOOST_AUTO_TEST_SUITE_END()