TimerTest.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "TimerTest.h"
  2. #include <unistd.h>
  3. #include <cmath>
  4. #include <iomanip>
  5. using namespace std;
  6. using namespace NICE;
  7. CPPUNIT_TEST_SUITE_REGISTRATION( TimerTest );
  8. void TimerTest::setUp() {
  9. }
  10. void TimerTest::tearDown() {
  11. }
  12. void TimerTest::testTimer() {
  13. struct timeval t;
  14. t.tv_sec=123;
  15. t.tv_usec=456789;
  16. CPPUNIT_ASSERT_EQUAL(123.456789, Timer::convertTime(t));
  17. Timer timer;
  18. CPPUNIT_ASSERT_EQUAL(0, timer.getCounter());
  19. timer.start();
  20. CPPUNIT_ASSERT_EQUAL(0, timer.getCounter());
  21. usleep(40000);
  22. timer.stop();
  23. CPPUNIT_ASSERT_EQUAL(1, timer.getCounter());
  24. timer.start();
  25. CPPUNIT_ASSERT_EQUAL(1, timer.getCounter());
  26. usleep(40000);
  27. timer.stop();
  28. CPPUNIT_ASSERT_EQUAL(2, timer.getCounter());
  29. CPPUNIT_ASSERT_MESSAGE(
  30. "process time should be 0 during sleep",
  31. std::abs(0.0 - timer.getSum()) < 1E-20);
  32. CPPUNIT_ASSERT_MESSAGE(
  33. "process time should be 0 during sleep",
  34. std::abs(0.0 - timer.getMean()) < 1E-20);
  35. CPPUNIT_ASSERT_MESSAGE(
  36. "time measurement wrong (can depend on machine and current system load)",
  37. std::abs(0.082 - timer.getSumAbsolute()) < 0.01);
  38. CPPUNIT_ASSERT_MESSAGE(
  39. "time measurement wrong (can depend on machine and current system load)",
  40. std::abs(0.041 - timer.getMeanAbsolute()) < 0.005);
  41. }