📄 test.cpp
字号:
// ********************************************************
// This is demo code. You may derive from, use, modify, and
// distribute it without limitation for any purpose.
// Obviously you don't get a warranty or an assurance of
// fitness for a particular purpose with this code. Your
// welcome to remove this header and claim original
// authorship. I really don't care.
// ********************************************************
#include "Test.h"
#ifndef WIN32
#include <unistd.h>
#endif // !WIN32
#include <stdio.h>
#include <math.h>
#include <wchar.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/GCompress.h"
#include "../GClasses/GDataBase.h"
#include "../GClasses/GSocket.h"
#include "../GClasses/GFourier.h"
#include "../GClasses/GHashTable.h"
#include "../GClasses/GKeyPair.h"
#include "../GClasses/GKNN.h"
#include "../GClasses/GLList.h"
#include "../GClasses/GMacros.h"
#include "../GClasses/GMath.h"
#include "../GClasses/GMatrix.h"
#include "../GClasses/GPolynomial.h"
#include "../GClasses/GQueue.h"
#include "../GClasses/GSpinLock.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 TestGNeighborFinder()
{
GNeighborFinder::Test();
}
void TestGAvlTree()
{
GAVLTree::Test();
}
void TestGKeyPair()
{
GKeyPair::Test();
}
void TestGHashTable()
{
GHashTable::Test();
}
void TestGSpinLock()
{
GSpinLock::Test();
}
void TestGDataBase()
{
GDataBase::Test();
}
void TestGMatrix()
{
GMatrix::Test();
}
void TestGPrecalculatedTrigTable()
{
GPrecalculatedTrigTable::Test();
}
void TestGFourier()
{
GFourier::Test();
}
void TestGQueue()
{
GQueue::Test();
}
void TestGNeighborClusterer()
{
GNeighborClusterer::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 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";
}
int PointerArrayComparer(void* pThis, void* pA, void* pB)
{
if((int)pA > (int)pB)
return 1;
else if((int)pA < (int)pB)
return -1;
else
return 0;
}
void TestGDynamicArray()
{
class IntArray : public GDynamicArray
{
public:
IntArray(int nGrowBy) : GDynamicArray(sizeof(int), nGrowBy) { }
virtual ~IntArray() { }
int GetInt(int nIndex) { return *(int*)_GetCellRef(nIndex); }
void AddInt(int n) { _AddCellByRef(&n); }
void SetInt(int nCell, int n) { _SetCellByRef(nCell, &n); }
};
IntArray arr(64);
arr.SetAllocSize(7000);
int n;
for(n = 0; n < 10000; n++)
arr.AddInt(n);
if(arr.GetSize() != 10000)
throw "failed";
for(n = 0; n < 10000; n++)
{
if(arr.GetInt(n) != n)
throw "failed";
}
for(n = 0; n < 3000; n++)
arr.SetInt(n, 5);
for(n = 0; n < 3000; n++)
{
if(arr.GetInt(n) != 5)
throw "failed";
}
// Add, insert, and then check
GDynamicArray sa(1, 17);
for(n = 0; n < 1000; n++)
{
char cTmp = n + 50;
sa._AddCellByRef(&cTmp);
}
for(n = 0; n < 50; n++)
{
char c = n;
sa._InsertCellByRef(0, &c);
}
for(n = 0; n < 50; n++)
{
char* pData = (char*)sa._GetCellRef(n);
if(*pData != (char)(49 - n))
throw "wrong answer";
}
for(n = 50; n < 1050; n++)
{
if(*(char*)sa._GetCellRef(n) != (char)n)
throw "wrong answer";
}
if(sa.GetSize() != 1050)
throw "wrong answer";
// Delete and then check again
for(n = 0; n < 49; n++)
sa.DeleteCell(1);
if(sa.GetSize() != 1001)
throw "wrong answer";
sa.DeleteCell(0);
for(n = 0; n < 1000; n++)
{
char cTmp = n + 50;
if(*(char*)sa._GetCellRef(n) != cTmp)
throw "wrong answer";
}
// Test GPointerArray::Sort
GPointerArray arr2(64);
for(n = 34; n >= 0; n--)
arr2.AddPointer((void*)n);
arr2.Sort(PointerArrayComparer, NULL);
for(n = 0; n <= 34; n++)
{
if((int)arr2.GetPointer(n) != n)
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";
}
bool IsPrettyClose(double a, double b)
{
a -= b;
if(a > .0000001)
return false;
if(a < -.0000001)
return false;
return true;
}
void TestGMath()
{
// Test Newton's Polynomial
double t0 = 23;
double t1 = 17;
double t2 = 37;
double t3 = 83;
double x0 = 11;
double x1 = 53;
double x2 = 83;
double x3 = 7;
double t[4];
double x[4];
t[0] = t0;
t[1] = t1;
t[2] = t2;
t[3] = t3;
x[0] = x0;
x[1] = x1;
x[2] = x2;
x[3] = x3;
GMath::NewtonPolynomial(t, x, 4);
if(!IsPrettyClose(x[0] + x[1] * t0 + x[2] * t0 * t0 + x[3] * t0 * t0 * t0, x0))
throw "wrong answer";
if(!IsPrettyClose(x[0] + x[1] * t1 + x[2] * t1 * t1 + x[3] * t1 * t1 * t1, x1))
throw "wrong answer";
if(!IsPrettyClose(x[0] + x[1] * t2 + x[2] * t2 * t2 + x[3] * t2 * t2 * t2, x2))
throw "wrong answer";
if(!IsPrettyClose(x[0] + x[1] * t3 + x[2] * t3 * t3 + x[3] * t3 * t3 * t3, x3))
throw "wrong answer";
}
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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -