📄 test.cpp
字号:
pCopy2->GetPoint(((double)n / 10) / .7 - .3 / .7, &p3);
if(p1.GetDistanceSquared(&p2) > .00001)
throw "wrong answer";
if(p1.GetDistanceSquared(&p3) > .00001)
throw "wrong answer";
}
Point3D coeff[7];
pOrig->ToPolynomial(coeff);
GBezier* pAnother = GBezier::FromPolynomial(coeff, 7);
for(n = 0; n < 10; n++)
{
pOrig->GetPoint((double)n / 10, &p1);
pAnother->GetPoint((double)n / 10, &p2);
if(p1.GetDistanceSquared(&p2) > .00001)
throw "wrong answer";
}
delete(pOrig);
delete(pCopy);
delete(pCopy2);
delete(pAnother);
}
void TestGPolynomial()
{
GPolynomial gp(2, 3);
int degrees[2];
degrees[0] = 0;
degrees[1] = 0;
gp.SetCoefficient(degrees, 1);
degrees[0] = 1;
degrees[1] = 0;
gp.SetCoefficient(degrees, 2);
degrees[0] = 2;
degrees[1] = 0;
gp.SetCoefficient(degrees, 3);
degrees[0] = 0;
degrees[1] = 1;
gp.SetCoefficient(degrees, 4);
degrees[0] = 1;
degrees[1] = 1;
gp.SetCoefficient(degrees, 5);
degrees[0] = 2;
degrees[1] = 1;
gp.SetCoefficient(degrees, 6);
degrees[0] = 0;
degrees[1] = 2;
gp.SetCoefficient(degrees, 7);
degrees[0] = 1;
degrees[1] = 2;
gp.SetCoefficient(degrees, 8);
degrees[0] = 2;
degrees[1] = 2;
gp.SetCoefficient(degrees, 9);
double vars[2];
vars[0] = 7;
vars[1] = 11;
double val = gp.Eval(vars);
// 1 + 2 * (7) + 3 * (7 * 7) +
// 4 * (11) + 5 * (11 * 7) + 6 * (11 * 7 * 7) +
// 7 * (11 * 11) + 8 * (11 * 11 * 7) + 9 * (11 * 11 * 7 * 7)
// = 64809
if(val != 64809)
throw "wrong answer";
}
#define TEST_SOCKET_PORT 4464
void TestGSocketSerial(bool bGash)
{
// Establish a socket connection
Holder<GSocketServer*> hServer(bGash ? GSocketServer::HostGashSocket(TEST_SOCKET_PORT, 5000) : GSocketServer::HostTCPSocket(TEST_SOCKET_PORT));
GSocketServer* pServer = hServer.Get();
if(!pServer)
throw "failed to init the server";
Holder<GSocketClient*> hClient(bGash ? GSocketClient::ConnectToGashSocket("localhost", TEST_SOCKET_PORT, 5000) : GSocketClient::ConnectToTCPSocket("localhost", TEST_SOCKET_PORT));
GSocketClient* pClient = hClient.Get();
if(!pClient)
throw "failed to make the client";
// Send a bunch of data
int i;
char szBuf[5000];
for(i = 0; i < 5000; i++)
szBuf[i] = (char)i;
pClient->Send(szBuf, 5000);
#ifdef WIN32
GWindows::YieldToWindows();
#endif // WIN32
for(i = 10; i < 60; i++)
{
if(!pClient->Send(szBuf + i, i))
throw "failed";
#ifdef WIN32
GWindows::YieldToWindows();
#endif // WIN32
}
pClient->Send(szBuf, 5000);
#ifdef WIN32
GWindows::YieldToWindows();
#endif // WIN32
// Wait for the data to arrive
int nTimeout;
for(nTimeout = 500; nTimeout > 0; nTimeout--)
{
if(pServer->GetMessageCount() == 52)
break;
GThread::sleep(50);
}
if(pServer->GetMessageCount() != 52)
throw "failed";
// Check the data and send some of it back to the client
int nSize, nConnection;
{
ArrayHolder<unsigned char*> hData = pServer->GetNextMessage(&nSize, &nConnection);
unsigned char* pData = hData.Get();
if(!pData || pData == (unsigned char*)szBuf)
throw "failed";
if(nSize != 5000)
throw "failed";
if(pData[122] != (char)122)
throw "failed";
}
for(i = 10; i < 60; i++)
{
ArrayHolder<unsigned char*> hData = pServer->GetNextMessage(&nSize, &nConnection);
unsigned char* pData = hData.Get();
if(nSize != i || !pData)
throw "failed";
if(pData[0] != (char)i)
throw "failed";
if(pData[i - 1] != (char)(i + i - 1))
throw "failed";
if(!pServer->Send(pData, 10, nConnection))
throw "failed";
#ifdef WIN32
GWindows::YieldToWindows();
#endif // WIN32
}
{
ArrayHolder<unsigned char*> hData = pServer->GetNextMessage(&nSize, &nConnection);
unsigned char* pData = hData.Get();
if(!pData || pData == (unsigned char*)szBuf)
throw "failed";
if(nSize != 5000)
throw "failed";
if(pData[122] != (char)122)
throw "failed";
}
// Wait for the data to arrive
for(nTimeout = 500; nTimeout > 0; nTimeout--)
{
if(pClient->GetMessageCount() == 50)
break;
GThread::sleep(50);
}
if(pClient->GetMessageCount() != 50)
throw "failed";
// Check the data
for(i = 10; i < 60; i++)
{
ArrayHolder<unsigned char*> hData = pClient->GetNextMessage(&nSize);
unsigned char* pData = hData.Get();
if(nSize != 10 || !pData)
throw "failed";
if(pData[0] != (char)i)
throw "failed";
if(pData[9] != (char)(i + 9))
throw "failed";
}
}
struct TestGSocketParallelData
{
};
unsigned int TestGSocketParallelClientThread(void* pParam)
{
TestGSocketParallelData* pData = (TestGSocketParallelData*)pParam;
Holder<GSocketClient*> hClient(GSocketClient::ConnectToGashSocket("localhost", TEST_SOCKET_PORT, 5000));
GSocketClient* pClient = hClient.Get();
char szBuf[1025];
memset(szBuf, 'f', 1025);
int n;
for(n = 0; n < 100; n++)
pClient->Send(szBuf, 1025);
return 0;
}
void TestGSocketParallel()
{
// Establish a socket connection
Holder<GSocketServer*> hServer(GSocketServer::HostGashSocket(TEST_SOCKET_PORT, 5000));
GSocketServer* pServer = hServer.Get();
TestGSocketParallelData sData;
if(!pServer)
throw "failed";
GThread::SpawnThread(TestGSocketParallelClientThread, &sData);
GThread::SpawnThread(TestGSocketParallelClientThread, &sData);
GThread::SpawnThread(TestGSocketParallelClientThread, &sData);
GThread::SpawnThread(TestGSocketParallelClientThread, &sData);
GThread::SpawnThread(TestGSocketParallelClientThread, &sData);
int i;
int nSize, nConnection;
for(i = 0; i < 500; i++)
{
ArrayHolder<unsigned char*> hData = pServer->GetNextMessage(&nSize, &nConnection);
if(!hData.Get())
{
i--;
GThread::sleep(0);
continue;
}
if(nSize != 1025)
throw "failed";
}
}
void TestGSocket()
{
TestGSocketSerial(true);
TestGSocketParallel();
}
void TestGCompress()
{
srand(0);
int nSize = 4096;
int nRands = 2;
ArrayHolder<unsigned char*> hOrig(new unsigned char[nSize]);
unsigned char* pOrig = hOrig.Get();
int n;
for(n = 0; n < nSize; n++)
pOrig[n] = (unsigned char)(rand() % nRands + 'A');
int nCompressedSize, nFinalSize;
ArrayHolder<unsigned char*> hCompressed(GCompress::Compress(pOrig, nSize, &nCompressedSize));
ArrayHolder<unsigned char*> hFinal(GCompress::Decompress(hCompressed.Get(), nCompressedSize, &nFinalSize));
unsigned char* pFinal = hFinal.Get();
if(nFinalSize != nSize)
throw "failed";
if(memcmp(pFinal, pOrig, nSize) != 0)
throw "failed";
}
// *******************************************************************************
// *******************************************************************************
// *******************************************************************************
typedef void (*ClassTestFunc)();
struct ClassTest
{
const char* szName;
ClassTestFunc pTest;
};
static struct ClassTest testTable[] =
{
{"GAVLTree", TestGAvlTree},
{"GBezier", TestGBezier},
{"GCompress", TestGCompress},
{"GDataBase", TestGDataBase},
{"GDynamicArray", TestGDynamicArray},
{"GFourier", TestGFourier},
{"GHashTable", TestGHashTable},
{"GKeyPair", TestGKeyPair},
{"GLList", TestGLList},
{"GMath", TestGMath},
{"GMatrix", TestGMatrix},
{"GNeighborClusterer", TestGNeighborClusterer},
{"GNeighborFinder", TestGNeighborFinder},
{"GPolynomial", TestGPolynomial},
{"GPrecalculatedTrigTable", TestGPrecalculatedTrigTable},
{"GQueue", TestGQueue},
{"GSocket", TestGSocket},
{"GSpinLock", TestGSpinLock},
{"GStack", TestGStack},
{"GString", TestGString},
{"GTrie", TestGTrie},
};
int GetTestCount()
{
return (sizeof(testTable) / sizeof(struct ClassTest));
}
const char* GetTestName(int nTest)
{
return testTable[nTest].szName;
}
bool RunTest(int nTest)
{
const char* szName = GetTestName(nTest);
printf(szName);
int nSpaces = 70 - strlen(szName);
for( ; nSpaces > 0; nSpaces--)
printf(" ");
fflush(stdout);
ClassTestFunc pTest = testTable[nTest].pTest;
bool bPass = false;
try
{
pTest();
bPass = true;
}
catch(const char* /*szError*/)
{
}
catch(const wchar_t* /*wszError*/)
{
}
catch(...)
{
}
if(bPass)
printf("Passed\n");
else
printf("FAILED!!!\n");
return bPass;
}
void RunAllTests()
{
int nCount = GetTestCount();
int n;
for(n = 0; n < nCount; n++)
RunTest(n);
}
// ------------------------------------------------------------------------
TestView::TestView(TestController* pController)
: ViewBase()
{
m_pImage = new GImage();
m_pImage->SetSize(400, 200);
GRect r;
r.Set(10, 85, 380, 30);
m_pImage->DrawHardText(&r, "See console window.", 0xff0000ff, 1);
}
TestView::~TestView()
{
delete(m_pImage);
}
/*virtual*/ void TestView::Draw(SDL_Surface *pScreen)
{
// Clear the screen
SDL_FillRect(pScreen, NULL/*&r*/, 0x000000);
// Draw the dialog
BlitImage(pScreen, m_screenRect.x + 200, m_screenRect.y + 200, m_pImage);
}
void TestView::OnChar(char c)
{
}
void TestView::OnMouseDown(int x, int y)
{
}
void TestView::OnMouseUp(int x, int y)
{
}
bool TestView::OnMousePos(int x, int y)
{
return false;
}
// -------------------------------------------------------------------------------
TestController::TestController()
: ControllerBase()
{
m_pView = new TestView(this);
}
TestController::~TestController()
{
delete(m_pView);
}
void TestController::RunModal()
{
m_pView->Update();
RunAllTests();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -