Skip to content

Instantly share code, notes, and snippets.

@Rolias
Created January 21, 2012 20:09
Show Gist options
  • Save Rolias/1653787 to your computer and use it in GitHub Desktop.
Save Rolias/1653787 to your computer and use it in GitHub Desktop.
Unit Tests for the "TaskBase" class in Syncor's Standard Library.
/*
* TestTaskBase.cpp
*
* Created on: Mar 2, 2010
* Author: Tod Gentille
* Syncor Systems, Inc.
*/
#include <UnitTest++.h>
#include <Startup/TaskBase.h>
#include <iostream>
#include <ucos.h>
#include <utils.h>
#include <Bsp.h>
using namespace std;
using namespace SyncorLibrary;
class SomeConcreteTask: public TaskBase
{
public:
SomeConcreteTask() :
_stayAlive(true)
{
}
bool _stayAlive;
void Initialize(const void * const pd)
{
while (_stayAlive)
{
OSTimeDly(1);;
}
}
};
void ShowRamAtStart()
{
DWORD ram_at_start = spaceleft();
cout << "start:" << ram_at_start << endl;
}
struct MemoryChecker
{
DWORD _ramAtStart;
DWORD _ramAtEnd;
} _memoryChecker;
struct TaskBaseFixture
{
SomeConcreteTask _myTask;
SomeConcreteTask _mySecondTask;
};
SUITE(TaskBaseClass)
{
TEST_FIXTURE(TaskBaseFixture, CanSetPriorityToMax)
{
//Get the memory after the constructor for _myTask has run
//This has to be done inside a test.
_memoryChecker._ramAtStart = spaceleft();
int expected = _myTask.GetMaxTaskPriority();
_myTask.SetTaskPriority(expected);
CHECK_EQUAL(expected, _myTask.GetTaskPriority());
}
TEST_FIXTURE(TaskBaseFixture, CanSetPriorityToMin)
{
int expected = _myTask.GetMinTaskPriority();
_myTask.SetTaskPriority(expected);
CHECK_EQUAL(expected, _myTask.GetTaskPriority());
}
TEST_FIXTURE(TaskBaseFixture, CantSetPriorityBelowMin)
{
int expected = _myTask.GetMinTaskPriority();
_myTask.SetTaskPriority(expected - 1);
CHECK_EQUAL(expected, _myTask.GetTaskPriority());
}
TEST_FIXTURE(TaskBaseFixture, TaskCanBeSafelyStartedTwiceWithNoEffect)
{
_myTask.SetTaskPriority(HTTP_PRIO - 1);
_myTask.Startup();
_myTask.Startup();
CHECK_EQUAL(true, _myTask.TaskInitialized() );
_myTask._stayAlive = false;
OSTimeDly(2);
}
TEST_FIXTURE(TaskBaseFixture, TaskCanBeStartedAndStopped)
{
_myTask.SetTaskPriority(HTTP_PRIO - 1);
CHECK_EQUAL(false, _myTask.TaskInitialized() );
_myTask.Startup();
CHECK_EQUAL(true,_myTask.TaskInitialized());
_myTask._stayAlive = false;
OSTimeDly(2); //wait for it to die.
CHECK_EQUAL(false, _myTask.TaskInitialized() );
}
TEST_FIXTURE(TaskBaseFixture, TaskPriorityCanChangeAfterTaskStarts)
{
_myTask.SetTaskPriority(MAIN_PRIO - 1);
_myTask.Startup();
OSTimeDly(1);
CHECK_EQUAL(true,_myTask.TaskInitialized());
int initial_priority = _myTask.GetTaskPriority();
_myTask.SetTaskPriority(initial_priority - 1);
int new_priority = _myTask.GetTaskPriority();
CHECK (new_priority < initial_priority);
_myTask._stayAlive = false;
OSTimeDly(2);
CHECK_EQUAL(false, _myTask.TaskInitialized() );
}
TEST_FIXTURE(TaskBaseFixture, TwoTasksStartedAtSamePriorityUseDifferentPriorities)
{
_myTask.SetTaskPriority(MAIN_PRIO - 1);
_mySecondTask.SetTaskPriority(MAIN_PRIO - 1);
_myTask.Startup();
_mySecondTask.Startup();
OSTimeDly(1);
CHECK_EQUAL(true,_myTask.TaskInitialized());
CHECK_EQUAL(true,_mySecondTask.TaskInitialized());
CHECK(_myTask.GetTaskPriority() != _mySecondTask.GetTaskPriority());
_myTask._stayAlive = false;
_mySecondTask._stayAlive = false;
OSTimeDly(2); //wait for it to die.
CHECK_EQUAL(false, _myTask.TaskInitialized() );
CHECK_EQUAL(false, _mySecondTask.TaskInitialized() );
}
TEST_FIXTURE(TaskBaseFixture, TwoTasksWontStartIfAHigherPriorityIsNotAvailable)
{
_myTask.SetTaskPriority(_myTask.GetMaxTaskPriority());
//NOTE: using max priority from _myTask not _mySecondTask on purpose
_mySecondTask.SetTaskPriority(_myTask.GetMaxTaskPriority());
_myTask.Startup();
_mySecondTask.Startup();
CHECK_EQUAL(true,_myTask.TaskInitialized());
CHECK_EQUAL(false,_mySecondTask.TaskInitialized());
CHECK(_myTask.GetTaskPriority() == _mySecondTask.GetTaskPriority());
_myTask._stayAlive = false;
_mySecondTask._stayAlive = false;
OSTimeDly(2); //wait for it to die.
CHECK_EQUAL(false, _myTask.TaskInitialized() );
CHECK_EQUAL(false, _mySecondTask.TaskInitialized() );
}
TEST( VerifyNoMemoryLeaksForTaskStacksWhenTaskIsNotRunning)
{
OSTimeDly(2);
DWORD ram_at_start = spaceleft();
SomeConcreteTask* local_task = new SomeConcreteTask();
DWORD ram_at_middle = spaceleft();
local_task->SetTaskPriority(HTTP_PRIO - 1);
local_task->Startup();
OSTimeDly(2);
local_task->_stayAlive = false;
OSTimeDly(2);
delete local_task; //destructor will run and since task is done it will clean up stack memory
OSTimeDly(2);
DWORD ram_at_end = spaceleft();
cout << "start:" << ram_at_start << " middle:" << ram_at_middle << " end:" << ram_at_end << endl;
CHECK(ram_at_start == ram_at_end);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment