📄 scheduletest.cpp
字号:
#include "ScheduleTest.h"#include "Work.h"#include "Schedule.h"#include "Scheduler.h"#include <unistd.h>#include <vector>#include <queue>class TestWork : public Work{public: ~TestWork() { }public: bool test;public: TestWork() { test = false; } void execute() { test = true; } string toString() const throw(Error) { return "TestWork"; }};voidScheduleTest::setUp(){}voidScheduleTest::tearDown(){}void ScheduleTest::testSchedule(){ __BEGIN_TEST_CASE VSDateTime::setCurrentDateTime(0); // 贸澜俊 VSDateTime dt(100); // 100檬 第俊 角青窍档废 抗距秦敌促. TestWork* pWork = new TestWork(); Schedule* pSchedule = new Schedule( pWork, dt ); CPPUNIT_ASSERT( pWork != NULL ); CPPUNIT_ASSERT( pSchedule != NULL ); CPPUNIT_ASSERT( !pSchedule->heartbeat() ); CPPUNIT_ASSERT( !pWork->test ); VSDateTime::setCurrentDateTime(50); // 50 (50盒? 50矫埃?) 第俊 CPPUNIT_ASSERT( !pSchedule->heartbeat() ); // 酒流 角青登绢急 救 等促. CPPUNIT_ASSERT( !pWork->test ); VSDateTime::setCurrentDateTime(101); // 100檬 (100盒? 100矫埃?) 第俊 CPPUNIT_ASSERT( pSchedule->heartbeat() ); // 角青登绢具 茄促. CPPUNIT_ASSERT( pWork->test ); SAFE_DELETE( pSchedule ); __END_TEST_CASE}void ScheduleTest::testScheduler(){ __BEGIN_TEST_CASE VSDateTime::setCurrentDateTime( 0 ); Scheduler* pScheduler = new Scheduler(); TestWork* pWork1 = new TestWork(); Schedule* pSchedule1 = new Schedule( pWork1, VSDateTime(100) ); // 100檬 第俊 角青窍档废 抗距秦敌促. pScheduler->addSchedule( pSchedule1 ); TestWork* pWork2 = new TestWork(); Schedule* pSchedule2 = new Schedule( pWork2, VSDateTime(200) ); TestWork* pWork = NULL; pScheduler->addSchedule( pSchedule2 ); CPPUNIT_ASSERT( pScheduler->getSize() == 2 ); // 滴俺 促 肋 甸绢啊具 茄促. pScheduler->heartbeat(); CPPUNIT_ASSERT( !pWork1->test ); CPPUNIT_ASSERT( !pWork2->test ); VSDateTime::setCurrentDateTime(50); // 50檬 第俊 pScheduler->heartbeat(); CPPUNIT_ASSERT( pScheduler->getSize() == 2 ); CPPUNIT_ASSERT( !pWork1->test ); // 酒公巴档 角青登瘤 臼疽绢具 茄促. CPPUNIT_ASSERT( !pWork2->test ); VSDateTime::setCurrentDateTime(101); // 100檬 第俊 pWork = dynamic_cast<TestWork*>(pScheduler->heartbeat()); CPPUNIT_ASSERT( pWork1 == pWork ); CPPUNIT_ASSERT( pScheduler->getSize() == 1 ); // 茄俺绰 瘤况脸绢具 茄促. CPPUNIT_ASSERT( pWork1->test ); // 1锅 胶纳临父 角青登菌绢具 茄促. CPPUNIT_ASSERT( !pWork2->test ); VSDateTime::setCurrentDateTime(151); // 150檬 第俊 pScheduler->heartbeat(); CPPUNIT_ASSERT( pScheduler->getSize() == 1 ); // 茄俺绰 瘤况脸绢具 茄促. CPPUNIT_ASSERT( pWork1->test ); CPPUNIT_ASSERT( !pWork2->test ); VSDateTime::setCurrentDateTime(201); // 200檬 第俊 pWork = dynamic_cast<TestWork*>(pScheduler->heartbeat()); CPPUNIT_ASSERT( pWork2 == pWork ); CPPUNIT_ASSERT( pScheduler->getSize() == 0 ); // 滴俺 促 瘤况脸绢具 茄促. CPPUNIT_ASSERT( pWork1->test ); CPPUNIT_ASSERT( pWork2->test ); SAFE_DELETE( pScheduler ); __END_TEST_CASE}class Less {public : bool operator () ( int i1, int i2 ) { return i1 > i2; }};void ScheduleTest::testPQ(){ priority_queue< int, vector<int>, Less > pq; pq.push( 5 ); pq.push( 2 ); pq.push( 6 ); pq.push( 4 ); pq.push( 3 ); pq.push( 1 ); pq.push( 7 ); CPPUNIT_ASSERT( pq.top() == 1 ); pq.pop(); CPPUNIT_ASSERT( pq.top() == 2 ); pq.pop(); CPPUNIT_ASSERT( pq.top() == 3 ); pq.pop(); CPPUNIT_ASSERT( pq.top() == 4 ); pq.pop(); CPPUNIT_ASSERT( pq.top() == 5 ); pq.pop(); CPPUNIT_ASSERT( pq.top() == 6 ); pq.pop(); CPPUNIT_ASSERT( pq.top() == 7 ); pq.pop(); CPPUNIT_ASSERT( pq.empty() );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -