sourcehook/test/testmulti.cpp
branchsourcemm-1.6.0
changeset 347 e82968c21606
child 361 f34718303c8f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcehook/test/testmulti.cpp	Tue Sep 18 20:03:47 2007 +0000
     1.3 @@ -0,0 +1,106 @@
     1.4 +#include <string>
     1.5 +#include "sourcehook.h"
     1.6 +#include "sourcehook_test.h"
     1.7 +#include "testevents.h"
     1.8 +
     1.9 +// This should probably be done the other way round!
    1.10 +#ifdef __linux__
    1.11 +#define _snprintf snprintf
    1.12 +#endif
    1.13 +
    1.14 +namespace
    1.15 +{
    1.16 +	SourceHook::ISourceHook *g_SHPtr;
    1.17 +	SourceHook::Plugin g_PLID;
    1.18 +
    1.19 +	unsigned int g_callcount[10];
    1.20 +
    1.21 +	class VMultiTest
    1.22 +	{
    1.23 +	public:
    1.24 +		VMultiTest(unsigned int index) : m_idx(index)
    1.25 +		{
    1.26 +		}
    1.27 +	public:
    1.28 +		virtual void HookTarget()
    1.29 +		{
    1.30 +		}
    1.31 +		virtual void Notify()
    1.32 +		{
    1.33 +			g_callcount[this->m_idx]++;
    1.34 +		}
    1.35 +	private:
    1.36 +		unsigned int m_idx;
    1.37 +	};
    1.38 +
    1.39 +	void HookFunction()
    1.40 +	{
    1.41 +		VMultiTest *pv = META_IFACEPTR(VMultiTest);
    1.42 +		pv->Notify();
    1.43 +	}
    1.44 +
    1.45 +	SH_DECL_HOOK0_void(VMultiTest, HookTarget, SH_NOATTRIB, false);
    1.46 +};
    1.47 +
    1.48 +bool TestMulti(std::string &error)
    1.49 +{
    1.50 +	GET_SHPTR(g_SHPtr);
    1.51 +	g_PLID = 1337;
    1.52 +
    1.53 +	VMultiTest **pv = new VMultiTest *[10];
    1.54 +	for (unsigned int i=0; i<10; i++)
    1.55 +		pv[i] = new VMultiTest(i);
    1.56 +	
    1.57 +
    1.58 +	for (unsigned int i=0; i<10; i++)
    1.59 +		SH_ADD_HOOK_STATICFUNC(VMultiTest, HookTarget, pv[i], HookFunction, false);
    1.60 +
    1.61 +	pv[0]->HookTarget();
    1.62 +
    1.63 +	if (g_callcount[0] != 1)
    1.64 +	{
    1.65 +		error.assign("g_callcount[0] != 0");
    1.66 +		return false;
    1.67 +	}
    1.68 +
    1.69 +	for (unsigned int i=1; i<10; i++)
    1.70 +	{
    1.71 +		if (g_callcount[i])
    1.72 +		{
    1.73 +			error.assign("g_callcount[n] != 0");
    1.74 +			return false;
    1.75 +		}
    1.76 +	}
    1.77 +
    1.78 +	SH_REMOVE_HOOK_STATICFUNC(VMultiTest, HookTarget, pv[0], HookFunction, false);
    1.79 +
    1.80 +	for (unsigned int i=1; i<10; i++)
    1.81 +		pv[i]->HookTarget();
    1.82 +
    1.83 +	if (g_callcount[0] != 1)
    1.84 +	{
    1.85 +		error.assign("g_callcount[0] != 0");
    1.86 +		return false;
    1.87 +	}
    1.88 +
    1.89 +	for (unsigned int i=1; i<10; i++)
    1.90 +	{
    1.91 +		if (g_callcount[i] != 1)
    1.92 +		{
    1.93 +			char err[256];
    1.94 +			_snprintf(err, sizeof(err)-1, "g_callcount[%d] != 1", i);
    1.95 +			error.assign(err);
    1.96 +			return false;
    1.97 +		}
    1.98 +	}
    1.99 +
   1.100 +	for (unsigned int i=1; i<10; i++)
   1.101 +	{
   1.102 +		SH_REMOVE_HOOK_STATICFUNC(VMultiTest, HookTarget, pv[1], HookFunction, false);
   1.103 +		delete pv[i];
   1.104 +	}
   1.105 +
   1.106 +	delete [] pv;
   1.107 +
   1.108 +	return true;
   1.109 +}