You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
3.8 KiB

#include <cxxtest/TestSuite.h>
#include "Fixture.hpp"
#include "Timer.hpp"
#include "Thread.hpp"
#include <signal.h>
#include <climits>
class TestTimer : public CxxTest::TestSuite
{
private:
class DummyTimerUser : public TimerUser
{
public:
13 years ago
DummyTimerUser()
: m_counter(0)
{
TRACE;
}
~DummyTimerUser()
{
TRACE;
}
void timerExpired()
{
TRACE;
m_counter++;
}
void timerDestroyed()
{
TRACE;
m_counter += 100;
}
int m_counter;
}; // class TimerUser
public:
13 years ago
void testBasicTimer( void )
{
TEST_HEADER;
DummyTimerUser timerUser;
Timer timer;
13 years ago
timer_t timerId = timer.createTimer( &timerUser );
timer.setTimer(timerId, 2);
sleep( 4 );
13 years ago
// one expiration, no destroy
TS_ASSERT_EQUALS( timerUser.m_counter, 1 );
}
13 years ago
void testStopTimer( void )
{
TEST_HEADER;
DummyTimerUser timerUser;
Timer timer;
13 years ago
timer_t timerId = timer.createTimer( &timerUser );
timer.setTimer(timerId, 10);
sleep( 2 );
timer.stopTimer( timerId );
13 years ago
// no expiration, just destroy
TS_ASSERT_EQUALS( timerUser.m_counter, 100 );
}
13 years ago
void testPeriodicTimer( void )
{
TEST_HEADER;
DummyTimerUser timerUser;
Timer timer;
13 years ago
timer_t timerId = timer.createTimer( &timerUser );
// after 1 sec, expire periodically at each sec
timer.setTimer(timerId, 1, 0, 1, 0);
sleep(4);
timer.stopTimer( timerId );
13 years ago
// 1 destroy(stop) + 3 expiration (+- 1)
TS_ASSERT_DELTA( timerUser.m_counter, 103, 1 );
}
void testOneUserManyTimers( void )
{
TEST_HEADER;
DummyTimerUser timerUser;
Timer timer;
13 years ago
timer_t timerId = timer.createTimer( &timerUser );
timer.setTimer(timerId, 1);
13 years ago
timer_t timerId2 = timer.createTimer( &timerUser );
timer.setTimer(timerId2, 2);
13 years ago
timer_t timerId3 = timer.createTimer( &timerUser );
timer.setTimer(timerId3, 3);
sleep(4);
timer.stopTimer( timerId );
13 years ago
TS_ASSERT_EQUALS( timerUser.m_counter, 100 + 1 + 1 + 1 );
}
void testMenyUserManyTimers( void )
14 years ago
{
TEST_HEADER;
DummyTimerUser timerUser;
DummyTimerUser timerUser2;
DummyTimerUser timerUser3;
Timer timer;
13 years ago
timer_t timerId = timer.createTimer( &timerUser );
timer.setTimer(timerId, 1);
timer_t timerId2 = timer.createTimer( &timerUser );
timer.setTimer(timerId2, 2);
timer_t timerId3 = timer.createTimer( &timerUser );
timer.setTimer(timerId3, 3);
14 years ago
13 years ago
timer_t timerId4 = timer.createTimer( &timerUser2 );
timer.setTimer(timerId4, 1);
timer_t timerId5 = timer.createTimer( &timerUser2 );
timer.setTimer(timerId5, 2);
14 years ago
sleep(4);
13 years ago
TS_ASSERT_EQUALS( timerUser.m_counter, 1 + 1 + 1 );
TS_ASSERT_EQUALS( timerUser2.m_counter, 1 + 1 );
TS_ASSERT_EQUALS( timerUser3.m_counter, 0 );
14 years ago
}
13 years ago
void test2Timer( void )
{
TEST_HEADER;
DummyTimerUser timerUser;
Timer timer;
Timer timer2;
13 years ago
timer_t timerId = timer.createTimer( &timerUser );
timer.setTimer(timerId, 1);
timer_t timerId2 = timer.createTimer( &timerUser );
timer.setTimer(timerId2, 2);
timer_t timerId3 = timer.createTimer( &timerUser );
timer.setTimer(timerId3, 3);
14 years ago
13 years ago
timer_t timerId4 = timer.createTimer( &timerUser );
timer2.setTimer(timerId4, 1);
timer_t timerId5 = timer.createTimer( &timerUser );
timer2.setTimer(timerId5, 2);
sleep(4);
13 years ago
TS_ASSERT_EQUALS( timerUser.m_counter, (1 + 1 + 1) + (1 + 1) );
}
13 years ago
void testStopTimerWhichIsNotInTheMap()
{
TEST_HEADER;
Timer timer;
13 years ago
timer_t timerId = (void*)1234;
TS_ASSERT_EQUALS(timer.stopTimer( timerId ), false);
}
void testSetTimerWhichIsNotInTheMap()
{
TEST_HEADER;
Timer timer;
timer_t timerId = (void*)1234;
TS_ASSERT_EQUALS(timer.setTimer( timerId, 1 ), false);
}
};