test.cpp

来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C++ 代码 · 共 728 行 · 第 1/2 页

CPP
728
字号
	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();
}

// *******************************************************************************
// *******************************************************************************
// *******************************************************************************

typedef void (*ClassTestFunc)();

struct ClassTest
{
	const char* szName;
	ClassTestFunc pTest;
};

static struct ClassTest testTable[] = 
{
	{"GActionGreedySearch", TestGActionGreedySearch},
	{"GArffData", TestGArffData},
	{"GAStarSearch", TestGAStarSearch},
	{"GAVLTree", TestGAvlTree},
	{"GBezier", TestGBezier},
	{"GDataBase", TestGDataBase},
	{"GDate", TestGDate},
	{"GDynamicArray", TestGDynamicArray},
	{"GFourier", TestGFourier},
	{"GGraphCut", TestGGraphCut},
	{"GHashTable", TestGHashTable},
	{"GKeyPair", TestGKeyPair},
	{"GLList", TestGLList},
	{"GMath", TestGMath},
	{"GMatrix", TestGMatrix},
	{"GNeighborClusterer", TestGNeighborClusterer},
	{"GNeighborFinder", TestGNeighborFinder},
	{"GPCA", TestGPCA},
	{"GPolynomial", TestGPolynomial},
	{"GPrecalculatedTrigTable", TestGPrecalculatedTrigTable},
	{"GQueue", TestGQueue},
	{"GRelationalTable", TestGRelationalTable},
	{"GSocket", TestGSocket},
	{"GSpinLock", TestGSpinLock},
	{"GStabSearch", TestGStabSearch},
	{"GStack", TestGStack},
	{"GString", TestGString},
	{"GSubImageFinder", TestGSubImageFinder},
	{"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);
}






// ------------------------------------------------------------------------



class TestView : public ViewBase
{
protected:
	GImage* m_pImage;

public:
	TestView(TestController* pController);
	virtual ~TestView();

protected:
	virtual void Draw(SDL_Surface *pScreen);
};





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);
}



// -------------------------------------------------------------------------------



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 + =
减小字号Ctrl + -
显示快捷键?