1
0
mirror of https://github.com/alliedmodders/metamod-source.git synced 2024-12-03 15:24:15 +01:00
HLMetaModOfficial/sourcehook/test/testmulti.cpp
David Anderson b6f713be5f experimental patch for unhook exclusivity bug
added multi-hook single-remove test cases (crummy but works)

--HG--
extra : convert_revision : svn%3Ac2935e3e-5518-0410-8daf-afa5dab7d4e3/trunk%40178
2006-01-24 20:02:19 +00:00

102 lines
1.7 KiB
C++

#include <string>
#include "sourcehook.h"
#include "sourcehook_test.h"
#include "testevents.h"
namespace
{
SourceHook::ISourceHook *g_SHPtr;
SourceHook::Plugin g_PLID;
unsigned int g_callcount[10];
class VMultiTest
{
public:
VMultiTest(unsigned int index) : m_idx(index)
{
}
public:
virtual void HookTarget()
{
}
virtual void Notify()
{
g_callcount[this->m_idx]++;
}
private:
unsigned int m_idx;
};
void HookFunction()
{
VMultiTest *pv = META_IFACEPTR(VMultiTest);
pv->Notify();
}
SH_DECL_HOOK0_void(VMultiTest, HookTarget, SH_NOATTRIB, false);
};
bool TestMulti(std::string &error)
{
GET_SHPTR(g_SHPtr);
g_PLID = 1337;
VMultiTest **pv = new VMultiTest *[10];
for (unsigned int i=0; i<10; i++)
pv[i] = new VMultiTest(i);
for (unsigned int i=0; i<10; i++)
SH_ADD_HOOK_STATICFUNC(VMultiTest, HookTarget, pv[i], HookFunction, false);
pv[0]->HookTarget();
if (g_callcount[0] != 1)
{
error.assign("g_callcount[0] != 0");
return false;
}
for (unsigned int i=1; i<10; i++)
{
if (g_callcount[i])
{
error.assign("g_callcount[n] != 0");
return false;
}
}
SH_REMOVE_HOOK_STATICFUNC(VMultiTest, HookTarget, pv[0], HookFunction, false);
for (unsigned int i=1; i<10; i++)
pv[i]->HookTarget();
if (g_callcount[0] != 1)
{
error.assign("g_callcount[0] != 0");
return false;
}
for (unsigned int i=1; i<10; i++)
{
if (g_callcount[i] != 1)
{
char err[256];
_snprintf(err, sizeof(err)-1, "g_callcount[%d] != 1", i);
error.assign(err);
return false;
}
}
for (unsigned int i=1; i<10; i++)
{
SH_REMOVE_HOOK_STATICFUNC(VMultiTest, HookTarget, pv[1], HookFunction, false);
delete pv[i];
}
delete [] pv;
return true;
}