test.cpp

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

CPP
728
字号
// --------------------------------------------------------
// This demo file is dedicated to the Public Domain. See:
// http://creativecommons.org/licenses/publicdomain
// --------------------------------------------------------

#include "Test.h"
#ifndef WIN32
#include <unistd.h>
#endif // !WIN32
#include <stdio.h>
#include <math.h>
#include <wchar.h>
#include "../GClasses/GArff.h"
#include "../GClasses/GArray.h"
#include "../GClasses/GBigNumber.h"
#include "../GClasses/GRayTrace.h"
#include "../GClasses/GAVLTree.h"
#include "../GClasses/GBezier.h"
#include "../GClasses/GCluster.h"
#include "../GClasses/GDataBase.h"
#include "../GClasses/GDate.h"
#include "../GClasses/GSocket.h"
#include "../GClasses/GFourier.h"
#include "../GClasses/GGraph.h"
#include "../GClasses/GGreedySearch.h"
#include "../GClasses/GHashTable.h"
#include "../GClasses/GKeyPair.h"
#include "../GClasses/GKNN.h"
#include "../GClasses/GLList.h"
#include "../GClasses/GMacros.h"
#include "../GClasses/GManifold.h"
#include "../GClasses/GMath.h"
#include "../GClasses/GMatrix.h"
#include "../GClasses/GPolynomial.h"
#include "../GClasses/GQueue.h"
#include "../GClasses/GRegion.h"
#include "../GClasses/GRelationalTable.h"
#include "../GClasses/GSpinLock.h"
#include "../GClasses/GStabSearch.h"
#include "../GClasses/GStack.h"
#include "../GClasses/GString.h"
#include "../GClasses/GThread.h"
#include "../GClasses/GTime.h"
#include "../GClasses/GTrie.h"
#include "../GClasses/GTrigTable.h"
#include "../GClasses/GWindows.h"


void TestGActionGreedySearch()
{
	GActionGreedySearch::Test();
}

void TestGArffData()
{
	GArffData::Test();
}

void TestGAStarSearch()
{
	GAStarSearch::Test();
}

void TestGAvlTree()
{
	GAVLTree::Test();
}

void TestGKeyPair()
{
	GKeyPair::Test();
}

void TestGHashTable()
{
	GHashTable::Test();
}

void TestGNeighborFinder()
{
	GNeighborFinder::Test();
}

void TestGPCA()
{
	GPCA::Test();
}

void TestGSpinLock()
{
	GSpinLock::Test();
}

void TestGStabSearch()
{
	GStabSearch::Test();
}

void TestGDataBase()
{
	GDataBase::Test();
}

void TestGMatrix()
{
	GMatrix::Test();
}

void TestGPrecalculatedTrigTable()
{
	GPrecalculatedTrigTable::Test();
}

void TestGFourier()
{
	GFourier::Test();
}

void TestGGraphCut()
{
	GGraphCut::Test();
}

void TestGQueue()
{
	GQueue::Test();
}

void TestGNeighborClusterer()
{
	GNeighborClusterer::Test();
}

void TestGDynamicArray()
{
	GDynamicArray::Test();
}

void TestGRelationalTable()
{
	GRelationalTable::Test();
}

void TestGStack()
{
	GStack stack(1026);
	int nValue = 0;
	int nTmp;
	int n;
	for(n = 0; n < 274333; n++)
	{
		stack.Push(nValue);
		nValue++;
		if((rand() * 8 / RAND_MAX) == 0)
		{
			if(!stack.Pop(&nTmp))
				throw "wrong answer";
			nValue--;
			if(nTmp != nValue)
				throw "wrong answer";
		}
	}
	while(nValue > 0)
	{
		nValue--;
		if(!stack.Pop(&nTmp))
			throw "wrong answer";
		if(nTmp != nValue)
			throw "wrong answer";
	}
}

void TestGString()
{
	GString s;
	if(wcscmp(s.GetString(), L"") != 0)
		throw "wrong answer";
	s.Add(L"abcd");
	s.Add("1234");
	if(s.CompareTo(L"abcd1234") != 0)
		throw "wrong answer";
	if(s.GetLength() != 8)
		throw "wrong answer";
}

void TestGSubImageFinder()
{
	GSubImageFinder::Test();
}

void TestGLList()
{
	class MyBucket : public GBucket
	{
	public:
		char m_c;
		MyBucket(char c) : GBucket() { m_c = c; }
		virtual ~MyBucket() { }
		virtual int Compare(GBucket* pBucket) { return ((m_c < ((MyBucket*)pBucket)->m_c) ? -1 : (m_c > ((MyBucket*)pBucket)->m_c ? 1 : 0)); }
	};

	GLList llist;
	int n;
	for(n = 'z'; n >= 'a'; n--)
	{
		MyBucket* pNewBucket = new MyBucket(n);
		llist.Link(pNewBucket);
	}
	llist.Sort();
	MyBucket* pBucket;
	n = 'a';
	for(pBucket = (MyBucket*)llist.GetFirst(); pBucket; pBucket = (MyBucket*)llist.GetNext(pBucket))
	{
		if(pBucket->m_c != n)
			throw "wrong answer";
		n++;
	}
	GBucket* pFBucket = llist.Unlink(llist.GetBucket(4));
	n = 'a';
	for(pBucket = (MyBucket*)llist.GetFirst(); pBucket; pBucket = (MyBucket*)llist.GetNext(pBucket))
	{
		if(pBucket->m_c != n)
			throw "wrong answer";
		n++;
		if(n == 'f')
			n++;
	}
	llist.Insert(llist.GetBucket(4), pFBucket);
	n = 'a';
	for(pBucket = (MyBucket*)llist.GetFirst(); pBucket; pBucket = (MyBucket*)llist.GetNext(pBucket))
	{
		if(pBucket->m_c != n)
			throw "wrong answer";
		n++;
	}
	if(llist.GetCount() != 26)
		throw "wrong answer";
}

bool IsInArray(unsigned int* pnArray, unsigned int nVal, int nArraySize)
{
	int n;
	for(n = 0; n < nArraySize; n++)
		if(pnArray[n] == nVal)
			return true;
	return false;
}

void TestGTrie()
{
	const int nTestSize = 1024;
	unsigned int nArray[nTestSize];
	int n;
	GTrie trie;
	for(n = 0; n < nTestSize; n++)
	{
		unsigned int nRand = (rand() & 0xff) | ((rand() & 0xff) << 8) | ((rand() & 0xff) << 16) | ((rand() & 0xff) << 24);
		nArray[n] = nRand;
		trie.Add(nRand);
	}
	if(trie.GetCount() != nTestSize)
		throw "wrong answer";
	for(n = 0; n < nTestSize; n++)
	{
		if(!trie.Check(nArray[n]))
			throw "wrong answer";
	}
	for(n = 0; n < nTestSize; n++)
	{
		unsigned int nRand = (rand() & 0xff) | ((rand() & 0xff) << 8) | ((rand() & 0xff) << 16) | ((rand() & 0xff) << 24);
		if(trie.Check(nRand) != IsInArray(nArray, nRand, nTestSize))
			throw "wrong answer";
	}
	while(trie.GetCount() > 0)
	{
		if(rand() & 1)
		{
			if(!trie.Remove(trie.GetSmallest()))
				throw "wrong answer";
		}
		else
		{
			if(!trie.Remove(trie.GetBiggest()))
				throw "wrong answer";
		}
	}
	GTrie trie2;
	trie2.Add(237);
	trie2.Add(143);
	trie2.Add(331);
	if(trie2.GetSmallest() != 143)
		throw "wrong answer";
	if(trie2.GetBiggest() != 331)
		throw "wrong answer";
}

void TestGMath()
{
	GMath::Test();
}

void TestGBezier()
{
	srand(1234);
	Point3D p1, p2, p3;
	GBezier* pOrig = new GBezier(7);
	int n;
	for(n = 0; n < pOrig->GetControlPointCount(); n++)
	{
		p1.m_vals[0] = rand() % 1000000;
		p1.m_vals[1] = rand() % 1000000;
		p1.m_vals[2] = rand() % 1000000;
		pOrig->SetControlPoint(n, &p1, 1);
	}
	GBezier* pCopy = pOrig->Copy();
	pCopy->ElevateDegree();
	if(pCopy->GetControlPointCount() != pOrig->GetControlPointCount() + 1)
		throw "wrong answer";
	for(n = 0; n < 10; n++)
	{
		pOrig->GetPoint((double)n / 10, &p1);
		pCopy->GetPoint((double)n / 10, &p2);
		if(p1.GetDistanceSquared(&p2) > .00001)
			throw "wrong answer";
	}
	GBezier* pCopy2 = pCopy->Copy();
	pCopy->GetSegment(.3, false);
	pCopy2->GetSegment(.3, true);
	for(n = 0; n < 10; n++)
	{
		pOrig->GetPoint((double)n / 10, &p1);
		pCopy->GetPoint(((double)n / 10) / .3, &p2);
		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);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?