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