📄 testhercules.cpp
字号:
#include <e32base.h>
#include <e32cons.h>
#include "client.h"
#include "client-server.h"
#include "task.h"
_LIT(KTextExampleCode,"Symbian OS Example Code");
_LIT(KSlayNemeanLion, "Test SlayNemeanLion() passed\n");
_LIT(KSlayHydra, "Test SlayHydra() passed\n");
_LIT(KCaptureCeryneianHind, "Test CaptureCeryneianHind() passed\n");
_LIT(KSlayErymanthianBoar, "Test SlayErymanthianBoar() passed\n");
_LIT(KCleanAugeanStables, "Test CleanAugeanStables() passed\n");
_LIT(KSlayStymphalianBirds, "Test SlayStymphalianBirds() passed\n");
_LIT(KTasksInAdditionOrder, "Tasks in addition order\n");
_LIT(KTasksInAscendingOrder, "Tasks in ascending order\n");
_LIT(KTestArrays, "Array tests passed\n");
_LIT(KTextOK, "All tests Passed OK!\n");
_LIT(KTextPressAnyKey,"[Press any key]");
CConsoleBase* gConsole;
void TestClientServerL()
{
__UHEAP_MARK; // Checks for memory leaks
// Open the client-server session
RHerculesSession session;
User::LeaveIfError(session.Connect());
CleanupClosePushL(session); // Closes the session a leave occurs
// Test the SlayNemeanLion() method
ASSERT(session.SlayNemeanLion(KNemeanLionDes, KNemeanLionVal)==KErrNone);
gConsole->Printf(KSlayNemeanLion);
// Test the SlayHydra() method)
TVersion version(1,0,0);
THydraData hydraData;
hydraData.iHydraVersion = version;
hydraData.iHeadCount = KHydraHeadCount;
ASSERT(session.SlayHydra(hydraData)==KErrNone);
// Check hydraData, which was modified by the server
ASSERT(hydraData.iHeadCount==KNoHeads);
gConsole->Printf(KSlayHydra);
// Test the CaptureCeryneianHind() method
TInt count=KInitialCount;
if (session.CaptureCeryneianHind(count)!=KErrNone)
ASSERT(EFalse);
// Check count which was set by the server
if (count!=KCapturedCount)
ASSERT(EFalse);
gConsole->Printf(KCaptureCeryneianHind);
// Test the SlayErymanthianBoar() method
CHerculesData* data = CHerculesData::NewLC(KHercules, KHeracles, KHerculesDataVal);
if (session.SlayErymanthianBoar(*data)!=KErrNone)
ASSERT(EFalse);
gConsole->Printf(KSlayErymanthianBoar);
// Test the CleanAugeanStables() method
TRequestStatus status;
session.CleanAugeanStables(status);
User::WaitForRequest(status);
ASSERT(KErrNone==status.Int());
gConsole->Printf(KCleanAugeanStables);
// Test the SlayStymphalianBirds() method
TBuf8<12> myBuf(KHercules); // Server reads this data and updates it asynchronously
session.SlayStymphalianBirds(KBirdCount, myBuf, status);
User::WaitForRequest(status);
// Inspect the contents of myBuf, modified by the server
ASSERT(myBuf.Compare(KHercules)==0);
gConsole->Printf(KSlayStymphalianBirds);
// Clean up
CleanupStack::PopAndDestroy(2, &session); // data, session
// Check for memory leaks and end test
__UHEAP_MARKEND;
}
void TestArraysL()
{
__UHEAP_MARK;
// Create the CHerculeanTaskManager object
CHerculeanTaskManager* taskManager = CHerculeanTaskManager::NewLC();
// Add some tasks (OK to add the same one multiple times)
taskManager->AppendTaskL(ESlayHydra);
taskManager->AppendTaskL(ESlayNemeanLion);
taskManager->AppendTaskL(ECaptureCeryneianHind);
taskManager->AppendTaskL(ECleanAugeanStables);
taskManager->AppendTaskL(ECaptureCerberus);
taskManager->AppendTaskL(ECaptureMaresOfDiomedes);
taskManager->AppendTaskL(ESlayHydra);
taskManager->AppendTaskL(ESlayNemeanLion);
taskManager->AppendTaskL(ECaptureOxenOfGeryon);
taskManager->AppendTaskL(ESlayErymanthianBoar);
ASSERT(taskManager->TaskCount()==10);
RBuf taskBuf;
// List the tasks (in the order they were added)
taskManager->ListTasksL(taskBuf);
gConsole->Printf(taskBuf);
gConsole->Printf(KTasksInAdditionOrder);
gConsole->Printf(KTextPressAnyKey);
gConsole->Getch(); // get and ignore character
taskBuf.Zero();
taskBuf.ReAllocL(0);
// Remove both the SlayHydra tasks
taskManager->DeleteTask(ESlayHydra);
ASSERT(taskManager->TaskCount()==8);
// Now list tasks in ascending order
taskManager->ListTasksAscendingL(taskBuf);
gConsole->Printf(taskBuf);
gConsole->Printf(KTasksInAscendingOrder);
gConsole->Printf(KTextPressAnyKey);
gConsole->Getch(); // get and ignore character
taskBuf.Zero();
taskBuf.ReAlloc(0); // Free buffer
// Add some more tasks
taskManager->AppendTaskL(ECleanAugeanStables);
taskManager->AppendTaskL(ECancelCleanAugeanStables);
ASSERT(taskManager->TaskCount()==10);
// List the tasks in ascending order
taskManager->ListTasksAscendingL(taskBuf);
gConsole->Printf(taskBuf);
gConsole->Printf(KTasksInAscendingOrder);
gConsole->Printf(KTextPressAnyKey);
gConsole->Getch(); // get and ignore character
taskBuf.Zero();
taskBuf.ReAlloc(0); // Free buffer
// Test is complete
gConsole->Printf(KTestArrays);
CleanupStack::PopAndDestroy(taskManager);
// Check for memory leaks and end test
__UHEAP_MARKEND;
}
TInt E32Main()
{
__UHEAP_MARK;
CTrapCleanup* theCleanupStack = CTrapCleanup::New();
gConsole = Console::NewL(KTextExampleCode,TSize(KConsFullScreen,KConsFullScreen));
TRAPD(err, TestClientServerL());
ASSERT(KErrNone==err);
// Delay to allow the server thread to shutdown and test for memory leaks
User::After(500000);
TRAP(err, TestArraysL());
ASSERT(KErrNone==err);
// Print closing text and wait for input
gConsole->Printf(KTextOK);
gConsole->Printf(KTextPressAnyKey);
gConsole->Getch(); // get and ignore character
delete gConsole;
delete theCleanupStack;
__UHEAP_MARKEND;
return (err);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -