1
0
mirror of https://github.com/alliedmodders/metamod-source.git synced 2024-12-05 17:24:15 +01:00
HLMetaModOfficial/core-legacy/sourcehook/test/testlist.cpp
David Anderson 1d9949e1a0 Renamed core-1.4 to core-legacy.
--HG--
rename : core-1.4/CPlugin.cpp => core-legacy/CPlugin.cpp
rename : core-1.4/CPlugin.h => core-legacy/CPlugin.h
rename : core-1.4/CSmmAPI.cpp => core-legacy/CSmmAPI.cpp
rename : core-1.4/CSmmAPI.h => core-legacy/CSmmAPI.h
rename : core-1.4/IPluginManager.h => core-legacy/IPluginManager.h
rename : core-1.4/ISmmAPI.h => core-legacy/ISmmAPI.h
rename : core-1.4/ISmmPlugin.h => core-legacy/ISmmPlugin.h
rename : core-1.4/Makefile => core-legacy/Makefile
rename : core-1.4/concommands.cpp => core-legacy/concommands.cpp
rename : core-1.4/concommands.h => core-legacy/concommands.h
rename : core-1.4/convar_smm.h => core-legacy/convar_smm.h
rename : core-1.4/msvc7/sourcemm.sln => core-legacy/msvc7/sourcemm.sln
rename : core-1.4/msvc7/sourcemm.vcproj => core-legacy/msvc7/sourcemm.vcproj
rename : core-1.4/msvc8/sourcemm.sln => core-legacy/msvc8/sourcemm.sln
rename : core-1.4/msvc8/sourcemm.vcproj => core-legacy/msvc8/sourcemm.vcproj
rename : core-1.4/msvc9/sourcemm.sln => core-legacy/msvc9/sourcemm.sln
rename : core-1.4/msvc9/sourcemm.vcproj => core-legacy/msvc9/sourcemm.vcproj
rename : core-1.4/oslink.cpp => core-legacy/oslink.cpp
rename : core-1.4/oslink.h => core-legacy/oslink.h
rename : core-1.4/sourcehook.cpp => core-legacy/sourcehook.cpp
rename : core-1.4/sourcehook/FastDelegate.h => core-legacy/sourcehook/FastDelegate.h
rename : core-1.4/sourcehook/generate/FastDelegate.h => core-legacy/sourcehook/generate/FastDelegate.h
rename : core-1.4/sourcehook/generate/FastDelegate.hxx => core-legacy/sourcehook/generate/FastDelegate.hxx
rename : core-1.4/sourcehook/generate/generate => core-legacy/sourcehook/generate/generate
rename : core-1.4/sourcehook/generate/generate.bat => core-legacy/sourcehook/generate/generate.bat
rename : core-1.4/sourcehook/generate/sh_memfuncinfo.h => core-legacy/sourcehook/generate/sh_memfuncinfo.h
rename : core-1.4/sourcehook/generate/sh_memfuncinfo.hxx => core-legacy/sourcehook/generate/sh_memfuncinfo.hxx
rename : core-1.4/sourcehook/generate/shworker.bin => core-legacy/sourcehook/generate/shworker.bin
rename : core-1.4/sourcehook/generate/shworker.exe => core-legacy/sourcehook/generate/shworker.exe
rename : core-1.4/sourcehook/generate/shworker/Makefile => core-legacy/sourcehook/generate/shworker/Makefile
rename : core-1.4/sourcehook/generate/shworker/fd_hopter.cpp => core-legacy/sourcehook/generate/shworker/fd_hopter.cpp
rename : core-1.4/sourcehook/generate/shworker/msvc7/shworker.vcproj => core-legacy/sourcehook/generate/shworker/msvc7/shworker.vcproj
rename : core-1.4/sourcehook/generate/shworker/msvc8/shworker.vcproj => core-legacy/sourcehook/generate/shworker/msvc8/shworker.vcproj
rename : core-1.4/sourcehook/generate/shworker/shworker.cpp => core-legacy/sourcehook/generate/shworker/shworker.cpp
rename : core-1.4/sourcehook/generate/sourcehook.h => core-legacy/sourcehook/generate/sourcehook.h
rename : core-1.4/sourcehook/generate/sourcehook.hxx => core-legacy/sourcehook/generate/sourcehook.hxx
rename : core-1.4/sourcehook/sh_list.h => core-legacy/sourcehook/sh_list.h
rename : core-1.4/sourcehook/sh_memfuncinfo.h => core-legacy/sourcehook/sh_memfuncinfo.h
rename : core-1.4/sourcehook/sh_memory.h => core-legacy/sourcehook/sh_memory.h
rename : core-1.4/sourcehook/sh_stack.h => core-legacy/sourcehook/sh_stack.h
rename : core-1.4/sourcehook/sh_string.h => core-legacy/sourcehook/sh_string.h
rename : core-1.4/sourcehook/sh_tinyhash.h => core-legacy/sourcehook/sh_tinyhash.h
rename : core-1.4/sourcehook/sh_vector.h => core-legacy/sourcehook/sh_vector.h
rename : core-1.4/sourcehook/sourcehook.cpp => core-legacy/sourcehook/sourcehook.cpp
rename : core-1.4/sourcehook/sourcehook.h => core-legacy/sourcehook/sourcehook.h
rename : core-1.4/sourcehook/sourcehook_impl.h => core-legacy/sourcehook/sourcehook_impl.h
rename : core-1.4/sourcehook/test/Makefile => core-legacy/sourcehook/test/Makefile
rename : core-1.4/sourcehook/test/main.cpp => core-legacy/sourcehook/test/main.cpp
rename : core-1.4/sourcehook/test/msvc7/test.vcproj => core-legacy/sourcehook/test/msvc7/test.vcproj
rename : core-1.4/sourcehook/test/msvc8/test.vcproj => core-legacy/sourcehook/test/msvc8/test.vcproj
rename : core-1.4/sourcehook/test/sourcehook_test.h => core-legacy/sourcehook/test/sourcehook_test.h
rename : core-1.4/sourcehook/test/test1.cpp => core-legacy/sourcehook/test/test1.cpp
rename : core-1.4/sourcehook/test/test2.cpp => core-legacy/sourcehook/test/test2.cpp
rename : core-1.4/sourcehook/test/test3.cpp => core-legacy/sourcehook/test/test3.cpp
rename : core-1.4/sourcehook/test/test4.cpp => core-legacy/sourcehook/test/test4.cpp
rename : core-1.4/sourcehook/test/testbail.cpp => core-legacy/sourcehook/test/testbail.cpp
rename : core-1.4/sourcehook/test/testbail.h => core-legacy/sourcehook/test/testbail.h
rename : core-1.4/sourcehook/test/testbail2.cpp => core-legacy/sourcehook/test/testbail2.cpp
rename : core-1.4/sourcehook/test/testevents.h => core-legacy/sourcehook/test/testevents.h
rename : core-1.4/sourcehook/test/testlist.cpp => core-legacy/sourcehook/test/testlist.cpp
rename : core-1.4/sourcehook/test/testmanual.cpp => core-legacy/sourcehook/test/testmanual.cpp
rename : core-1.4/sourcehook/test/testmulti.cpp => core-legacy/sourcehook/test/testmulti.cpp
rename : core-1.4/sourcehook/test/testrecall.cpp => core-legacy/sourcehook/test/testrecall.cpp
rename : core-1.4/sourcehook/test/testreentr.cpp => core-legacy/sourcehook/test/testreentr.cpp
rename : core-1.4/sourcehook/test/testref.cpp => core-legacy/sourcehook/test/testref.cpp
rename : core-1.4/sourcehook/test/testrefret.cpp => core-legacy/sourcehook/test/testrefret.cpp
rename : core-1.4/sourcemm.cpp => core-legacy/sourcemm.cpp
rename : core-1.4/sourcemm.h => core-legacy/sourcemm.h
rename : core-1.4/svn_version.h => core-legacy/svn_version.h
rename : core-1.4/svn_version.tpl => core-legacy/svn_version.tpl
rename : core-1.4/util.cpp => core-legacy/util.cpp
rename : core-1.4/util.h => core-legacy/util.h
rename : core-1.4/version.rc => core-legacy/version.rc
rename : core-1.4/vsp_listener.cpp => core-legacy/vsp_listener.cpp
rename : core-1.4/vsp_listener.h => core-legacy/vsp_listener.h
2008-11-14 05:02:00 -06:00

283 lines
6.3 KiB
C++

#include <string>
#include "sh_list.h"
#include "sh_stack.h"
#include "sh_tinyhash.h"
#include "sh_vector.h"
#include "testevents.h"
// TEST LIST
// Tests sh_list, sh_tinyhash, sh_vector
// :TODO: vector test, list insert test
namespace
{
struct Hmm
{
Hmm *m_This;
int m_Int;
Hmm(const Hmm &other) : m_Int(other.m_Int)
{
m_This = this;
}
Hmm(int i) : m_Int(i)
{
m_This = this;
}
Hmm() : m_Int(0)
{
m_This = this;
}
void operator = (const Hmm &other)
{
m_Int = other.m_Int;
}
operator int () const
{
return m_Int;
}
};
#define LIST_THIS_CHECK(lst, err) \
for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter) \
CHECK_COND(&(*iter) == iter->m_This, err);
bool DoTestList(std::string &error)
{
typedef SourceHook::List<Hmm> ListType;
ListType lst;
CHECK_COND(lst.empty(), "Part1");
for (int i = 1; i <= 100; ++i)
lst.push_back(i);
LIST_THIS_CHECK(lst, "PartA1");
CHECK_COND(!lst.empty(), "Part2");
lst.clear();
CHECK_COND(lst.empty(), "Part3");
for (int i = 1; i <= 100; ++i)
lst.push_back(i);
CHECK_COND(lst.back() == 100, "Part4");
LIST_THIS_CHECK(lst, "PartA2");
int ver = 1;
for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter)
CHECK_COND(*iter == ver++, "Part5");
CHECK_COND(ver == 101, "Part 6");
ListType::iterator iter50 = lst.find(50);
CHECK_COND(*iter50 == 50, "Part7");
iter50 = lst.erase(iter50);
CHECK_COND(*iter50 == 51, "Part8");
CHECK_COND(*--iter50 == 49, "Part8.2");
lst.remove(80);
ver = 1;
for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter)
{
CHECK_COND(*iter == ver++, "Part9");
if (ver == 50 || ver == 80)
++ver;
}
CHECK_COND(ver == 101, "Part10");
LIST_THIS_CHECK(lst, "PartA3");
ListType lst2;
lst = lst2;
CHECK_COND(lst.empty(), "Part11");
for (int i = 1; i <= 100; ++i)
lst.push_back(i);
lst2 = lst;
CHECK_COND(lst2.size() == 100, "Part11.2");
LIST_THIS_CHECK(lst, "PartA4");
LIST_THIS_CHECK(lst2, "PartA5");
ver = 1;
for (ListType::iterator iter = lst2.begin(); iter != lst2.end(); ++iter)
CHECK_COND(*iter == ver++, "Part12");
lst.clear();
for (int i = 401; i <= 500; ++i)
lst.push_back(i);
lst = lst2;
CHECK_COND(lst2.size() == 100, "Part13");
ver = 1;
for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter)
CHECK_COND(*iter == ver++, "Part14");
LIST_THIS_CHECK(lst, "PartA6");
LIST_THIS_CHECK(lst2, "PartA7");
return true;
}
bool DoTestTinyHash(std::string &error)
{
const int mymax = 5000;
typedef SourceHook::THash<int, int> HashType;
HashType hash;
for (int i = 1; i <= mymax; ++i)
hash[i] = i + 5000;
for (int i = 1; i <= mymax; ++i)
CHECK_COND(hash[i] == i + 5000, "Part1");
// Find
int ver = 1;
HashType::iterator iter;
for (iter = hash.begin(); iter != hash.end(); ++iter)
CHECK_COND(iter->key == ver && iter->val == (ver++) + 5000, "Part2");
CHECK_COND(ver == mymax+1, "Part2.1");
iter = hash.find(300);
CHECK_COND(iter != hash.end() && iter->val == 300+5000, "Part3.1");
iter = hash.find(mymax+200);
CHECK_COND(iter == hash.end(), "Part3.2");
HashType hash2;
for (int i = 1; i <= mymax; ++i)
hash2[i] = i + 5000;
hash2.erase(mymax - 100);
CHECK_COND(hash2.find(mymax - 101) != hash2.end(), "Part 4.1");
CHECK_COND(hash2.find(mymax - 99) != hash2.end(), "Part 4.2");
CHECK_COND(hash2.find(mymax - 100) == hash2.end(), "Part 4.3");
hash2.erase(mymax - 99);
CHECK_COND(hash2.find(mymax - 101) != hash2.end(), "Part 4.4");
CHECK_COND(hash2.find(mymax - 99) == hash2.end(), "Part 4.5");
CHECK_COND(hash2.find(mymax - 100) == hash2.end(), "Part 4.6");
return true;
}
bool DoTestStack(std::string &error)
{
typedef SourceHook::CStack<int> IntStack;
IntStack stk;
int i;
CHECK_COND(stk.size() == 0 && stk.empty(), "A0");
for (i = 0; i < 5000; ++i)
stk.push(i);
CHECK_COND(stk.front() == 4999, "1");
CHECK_COND(stk.size() == 5000 && !stk.empty(), "A1");
IntStack::iterator iter;
i = 0;
for (iter = stk.begin(); iter != stk.end(); ++iter, ++i)
CHECK_COND(*iter == i, "2");
i = 0;
for (iter = stk.begin(); iter != stk.end(); iter++, ++i)
CHECK_COND(*iter == i, "3");
--iter;
iter--;
*iter = 'g'+'a'+'b'+'e'+'n';
stk.pop();
CHECK_COND(stk.size() == 4999 && !stk.empty(), "A2");
CHECK_COND(stk.front() == 'g'+'a'+'b'+'e'+'n', "4");
IntStack stk2(stk);
CHECK_COND(stk2.size() == 4999 && !stk2.empty(), "A3");
IntStack::iterator iter2 = stk2.begin();
for (iter = stk.begin(); iter != stk.end(); ++iter, iter2++)
CHECK_COND(*iter == *iter2, "5");
while (!stk2.empty())
stk2.pop();
CHECK_COND(stk2.size() == 0 && stk2.empty(), "A4");
stk = stk2;
CHECK_COND(stk.size() == 0 && stk.empty(), "A5");
return true;
}
bool DoTestVec(std::string &error)
{
typedef SourceHook::CVector<int> IntVector;
IntVector vec1;
IntVector::iterator iter;
int i;
CHECK_COND(vec1.size() == 0 && vec1.empty(), "V1");
for (i = 0; i < 500; ++i)
vec1.push_back(i);
CHECK_COND(vec1.size() == 500 && !vec1.empty(), "V2");
for (i = 0; i < 500; ++i)
CHECK_COND(vec1[i] == i, "V3");
for (i = 0, iter = vec1.begin(); iter != vec1.end(); ++iter, ++i)
CHECK_COND(*iter == i, "V4");
vec1.resize(1000);
for (i = 0; i < 500; ++i)
CHECK_COND(vec1[i] == i, "V5.1");
for (i = 500; i < 1000; ++i)
CHECK_COND(vec1[i] == 0, "V5.2");
vec1.resize(200);
for (i = 0; i < 200; ++i)
CHECK_COND(vec1[i] == i, "V6");
vec1.resize(500, 0x12345678);
for (i = 0; i < 200; ++i)
CHECK_COND(vec1[i] == i, "V7.1");
for (i = 200; i < 500; ++i)
CHECK_COND(vec1[i] == 0x12345678, "V7.2");
IntVector vec2(vec1);
CHECK_COND(vec2.size() == vec1.size() && vec2.empty() == vec1.empty(), "V8.0");
for (i = 0; i < 200; ++i)
CHECK_COND(vec2[i] == i, "V8.1");
for (i = 200; i < 500; ++i)
CHECK_COND(vec2[i] == 0x12345678, "V8.2");
vec1.clear();
CHECK_COND(vec1.size() == 0 && vec1.empty() && vec1.begin() == vec1.end(), "V9");
vec2 = vec1;
CHECK_COND(vec2.size() == 0 && vec2.empty() && vec2.begin() == vec2.end(), "V9");
return true;
}
}
bool TestList(std::string &error)
{
if (!DoTestList(error))
return false;
if (!DoTestTinyHash(error))
return false;
if (!DoTestStack(error))
return false;
if (!DoTestVec(error))
return false;
return true;
}