core/sourcehook/test/testmulti.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Jul 01 02:43:22 2010 -0500 (2010-07-01)
changeset 722 78f730f91439
parent 684 8015336442cb
permissions -rw-r--r--
Fixed problem on OBV where alternately loaded plugins (CreateInterface_MMS) could be incorrectly told they were on the OB engine (bug 4491, r=dvander).

This only happened when there was more than one plugin that loaded this way. And the first plugin to load had to fail on OBV first so that OB could be tried. It was being permanently set to OB for future plugin loads and this caused problems.
     1 #include <string>
     2 #include "sourcehook.h"
     3 #include "sourcehook_test.h"
     4 #include "testevents.h"
     5 
     6 // This should probably be done the other way round!
     7 #if SH_XP == SH_XP_POSIX
     8 #define _snprintf snprintf
     9 #endif
    10 
    11 namespace
    12 {
    13 	SourceHook::ISourceHook *g_SHPtr;
    14 	SourceHook::Plugin g_PLID;
    15 
    16 	unsigned int g_callcount[10];
    17 
    18 	class VMultiTest
    19 	{
    20 	public:
    21 		VMultiTest(unsigned int index) : m_idx(index)
    22 		{
    23 		}
    24 	public:
    25 		virtual void HookTarget()
    26 		{
    27 		}
    28 		virtual void Notify()
    29 		{
    30 			g_callcount[this->m_idx]++;
    31 		}
    32 	private:
    33 		unsigned int m_idx;
    34 	};
    35 
    36 	void HookFunction()
    37 	{
    38 		VMultiTest *pv = META_IFACEPTR(VMultiTest);
    39 		pv->Notify();
    40 	}
    41 
    42 	SH_DECL_HOOK0_void(VMultiTest, HookTarget, SH_NOATTRIB, false);
    43 };
    44 
    45 bool TestMulti(std::string &error)
    46 {
    47 	GET_SHPTR(g_SHPtr);
    48 	g_PLID = 1337;
    49 
    50 	VMultiTest **pv = new VMultiTest *[10];
    51 	for (unsigned int i=0; i<10; i++)
    52 		pv[i] = new VMultiTest(i);
    53 	
    54 
    55 	for (unsigned int i=0; i<10; i++)
    56 		SH_ADD_HOOK(VMultiTest, HookTarget, pv[i], SH_STATIC(HookFunction), false);
    57 
    58 	pv[0]->HookTarget();
    59 
    60 	if (g_callcount[0] != 1)
    61 	{
    62 		error.assign("g_callcount[0] != 0");
    63 		return false;
    64 	}
    65 
    66 	for (unsigned int i=1; i<10; i++)
    67 	{
    68 		if (g_callcount[i])
    69 		{
    70 			error.assign("g_callcount[n] != 0");
    71 			return false;
    72 		}
    73 	}
    74 
    75 	SH_REMOVE_HOOK(VMultiTest, HookTarget, pv[0], SH_STATIC(HookFunction), false);
    76 
    77 	for (unsigned int i=1; i<10; i++)
    78 		pv[i]->HookTarget();
    79 
    80 	if (g_callcount[0] != 1)
    81 	{
    82 		error.assign("g_callcount[0] != 0");
    83 		return false;
    84 	}
    85 
    86 	for (unsigned int i=1; i<10; i++)
    87 	{
    88 		if (g_callcount[i] != 1)
    89 		{
    90 			char err[256];
    91 			_snprintf(err, sizeof(err)-1, "g_callcount[%d] != 1", i);
    92 			error.assign(err);
    93 			return false;
    94 		}
    95 	}
    96 
    97 	for (unsigned int i=0; i<10; i++)
    98 	{
    99 		SH_REMOVE_HOOK(VMultiTest, HookTarget, pv[1], SH_STATIC(HookFunction), false);
   100 		delete pv[i];
   101 	}
   102 
   103 	delete [] pv;
   104 
   105 	return true;
   106 }