core/sourcehook/test/testrefret.cpp
changeset 539 db914129ae01
parent 387 093c72464b35
child 733 2365240861a2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/sourcehook/test/testrefret.cpp	Fri Nov 14 04:04:02 2008 -0600
     1.3 @@ -0,0 +1,205 @@
     1.4 +#include "testevents.h"
     1.5 +#include "sourcehook_test.h"
     1.6 +
     1.7 +// Tests support for functions which return references
     1.8 +
     1.9 +namespace
    1.10 +{
    1.11 +	StateList g_States;
    1.12 +	SourceHook::ISourceHook *g_SHPtr;
    1.13 +	SourceHook::Plugin g_PLID;
    1.14 +
    1.15 +	MAKE_STATE_1(State_Func1_Pre1, int*);			// p1: the ref Func1_Pre1 is going to return
    1.16 +	MAKE_STATE_3(State_Func1_Pre2, META_RES, int*, int*);	// p1: current status
    1.17 +															// p2: override ret
    1.18 +															// p3: what this handler is going to supercede with
    1.19 +	MAKE_STATE_1(State_Func1, int*);				// p1: the ref Func1 is going to return
    1.20 +	MAKE_STATE_2(State_Func1_Post1, int*, int*);	// p1: orig_ret; p2: override_ret
    1.21 +	MAKE_STATE_1(State_Func1_Post2, int*);			// p1: what it's going to return
    1.22 +	MAKE_STATE_1(State_Func1_Ret, int*);			// p1: the ref it returned
    1.23 +
    1.24 +
    1.25 +	MAKE_STATE_2(State_Func2_Pre1, int, const int*);				// p1: func's p1; p2: what it's going to ret
    1.26 +	MAKE_STATE_2(State_Func2, int, const int*);						// p1: func's p1; p2: what it's going to ret
    1.27 +	MAKE_STATE_3(State_Func2_Post1, int, const int*, const int*);	// p1: func's p1; p2: orig ret; p3: override ret
    1.28 +	MAKE_STATE_1(State_Func2_Ret, const int*);						// p1: ret
    1.29 +
    1.30 +	int g_Var;
    1.31 +
    1.32 +	class Test
    1.33 +	{
    1.34 +	public:
    1.35 +		int m_Var1;
    1.36 +		int m_Var2;
    1.37 +
    1.38 +		Test() : m_Var1(87)
    1.39 +		{
    1.40 +		}
    1.41 +
    1.42 +		virtual int& Func1()
    1.43 +		{
    1.44 +			ADD_STATE(State_Func1(&m_Var1));
    1.45 +			return m_Var1;
    1.46 +		}
    1.47 +		virtual const int& Func2(int p1)
    1.48 +		{
    1.49 +			ADD_STATE(State_Func2(p1, &m_Var2));
    1.50 +			m_Var2 = p1;
    1.51 +			return m_Var2;
    1.52 +		}
    1.53 +	};
    1.54 +
    1.55 +	class CHook
    1.56 +	{
    1.57 +	public:
    1.58 +		int m_Var;
    1.59 +		virtual int& Func1_Pre1()
    1.60 +		{
    1.61 +			ADD_STATE(State_Func1_Pre1(&m_Var));
    1.62 +			RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
    1.63 +		}
    1.64 +		virtual int &Func1_Pre2()
    1.65 +		{
    1.66 +			int &overrideret = META_RESULT_OVERRIDE_RET(int&);
    1.67 +			overrideret = 1337;
    1.68 +			ADD_STATE(State_Func1_Pre2(META_RESULT_STATUS, &overrideret, &g_Var));
    1.69 +			RETURN_META_VALUE(MRES_SUPERCEDE, g_Var);
    1.70 +		}
    1.71 +		virtual int& Func1_Post1()
    1.72 +		{
    1.73 +			ADD_STATE(State_Func1_Post1(&META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
    1.74 +			RETURN_META_NOREF(MRES_IGNORED, int&);
    1.75 +		}
    1.76 +		virtual int& Func1_Post2()
    1.77 +		{
    1.78 +			ADD_STATE(State_Func1_Post2(&m_Var));
    1.79 +			RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
    1.80 +		}
    1.81 +
    1.82 +		virtual const int& Func2_Pre1(int p1)
    1.83 +		{
    1.84 +			ADD_STATE(State_Func2_Pre1(p1, &m_Var));
    1.85 +			RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, m_Var, static_cast<const int& (Test::*)(int)>(&Test::Func2), (1337));
    1.86 +		}
    1.87 +
    1.88 +		virtual const int& Func2_Post1(int p1)
    1.89 +		{
    1.90 +			ADD_STATE(State_Func2_Post1(p1, &META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
    1.91 +			RETURN_META_NOREF(MRES_IGNORED, const int&);
    1.92 +		}
    1.93 +	};
    1.94 +
    1.95 +	SH_DECL_HOOK0(Test, Func1, SH_NOATTRIB, 0, int&);
    1.96 +	SH_DECL_HOOK1(Test, Func2, SH_NOATTRIB, 0, const int&, int);
    1.97 +
    1.98 +	Test *MyTestFactory()
    1.99 +	{
   1.100 +		return new Test;
   1.101 +	}
   1.102 +}
   1.103 +
   1.104 +bool TestRefRet(std::string &error)
   1.105 +{
   1.106 +	GET_SHPTR(g_SHPtr);
   1.107 +	g_PLID = 1;
   1.108 +
   1.109 +	Test *pTest = MyTestFactory();
   1.110 +	CAutoPtrDestruction<Test> apd(pTest);
   1.111 +	CHook hook;
   1.112 +
   1.113 +	int &ret1 = pTest->Func1();
   1.114 +	ADD_STATE(State_Func1_Ret(&ret1));
   1.115 +
   1.116 +	CHECK_STATES((&g_States,
   1.117 +		new State_Func1(&pTest->m_Var1),
   1.118 +		new State_Func1_Ret(&pTest->m_Var1),
   1.119 +		NULL), "Part 1");
   1.120 +
   1.121 +	// Now add Func1_Pre1, which supercedes and returns hook.m_Var
   1.122 +	SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre1), false);
   1.123 +
   1.124 +	int &ret2 = pTest->Func1();
   1.125 +	ADD_STATE(State_Func1_Ret(&ret2));
   1.126 +	
   1.127 +	CHECK_STATES((&g_States,
   1.128 +		new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   1.129 +		new State_Func1(&pTest->m_Var1),			// Function says that it's going to return pTest->m_Var1
   1.130 +		new State_Func1_Ret(&hook.m_Var),		// hook.m_Var is returned
   1.131 +		NULL), "Part 2");
   1.132 +
   1.133 +	// Now add Func1_Post1, which only reports orig ret and override ret
   1.134 +	SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post1), true);
   1.135 +
   1.136 +	int &ret3 = pTest->Func1();
   1.137 +	ADD_STATE(State_Func1_Ret(&ret3));
   1.138 +	
   1.139 +	CHECK_STATES((&g_States,
   1.140 +		new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   1.141 +		new State_Func1(&pTest->m_Var1),			// Function says that it's going to return pTest->m_Var1
   1.142 +		new State_Func1_Post1(&pTest->m_Var1, &hook.m_Var),	// origret(=p1) is what it wanted to
   1.143 +															// return, overrideret(=p2) is pre1's var
   1.144 +		new State_Func1_Ret(&hook.m_Var),		// hook.m_Var is returned
   1.145 +		NULL), "Part 3");
   1.146 +
   1.147 +	// Now add Func1_Pre2, which supercedes and returns g_Var (it also sets the override ret from pre1 to 1337)
   1.148 +	// and add Func1_Post2, which overrides and returns hook.m_Var again.
   1.149 +
   1.150 +	SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre2), false);
   1.151 +	SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post2), true);
   1.152 +
   1.153 +	int &ret4 = pTest->Func1();
   1.154 +	ADD_STATE(State_Func1_Ret(&ret4));
   1.155 +
   1.156 +	CHECK_STATES((&g_States,
   1.157 +		new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   1.158 +		new State_Func1_Pre2(MRES_OVERRIDE,			// current status
   1.159 +							&hook.m_Var,			// override ret (which it set to 1337)
   1.160 +							&g_Var),				// what it's going to ret, AND supercede with
   1.161 +
   1.162 +		new State_Func1_Post1(&g_Var, &g_Var),	// origret(=p1) is what pre2 superceded with,
   1.163 +												//  so  overrideret(=p2) has to be the same
   1.164 +		new State_Func1_Post2(&hook.m_Var),			// post2 is going to override with hook.m_Var again
   1.165 +		new State_Func1_Ret(&hook.m_Var),		// ==>>> hook.m_Var is returned
   1.166 +		NULL), "Part 4");
   1.167 +
   1.168 +	CHECK_COND(hook.m_Var == 1337, "Part 4.1");
   1.169 +	
   1.170 +	// Through a callclass
   1.171 +	SourceHook::CallClass<Test> *cc1 = SH_GET_CALLCLASS(pTest);
   1.172 +	int &ret5 = SH_CALL(cc1, &Test::Func1)();
   1.173 +	ADD_STATE(State_Func1_Ret(&ret5));
   1.174 +
   1.175 +	CHECK_STATES((&g_States,
   1.176 +		new State_Func1(&pTest->m_Var1),
   1.177 +		new State_Func1_Ret(&pTest->m_Var1),
   1.178 +		NULL), "Part 5");
   1.179 +
   1.180 +	SH_RELEASE_CALLCLASS(cc1);
   1.181 +
   1.182 +	////////////////////////////////////////////////////////////////////////////////////////
   1.183 +	// Func2 tests
   1.184 +	const int &ret21 = pTest->Func2(500);
   1.185 +	ADD_STATE(State_Func2_Ret(&ret21));
   1.186 +
   1.187 +	CHECK_STATES((&g_States,
   1.188 +		new State_Func2(500, &pTest->m_Var2),
   1.189 +		new State_Func2_Ret(&pTest->m_Var2),
   1.190 +		NULL), "Part 6");
   1.191 +
   1.192 +	SH_ADD_HOOK(Test, Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_Pre1), false);
   1.193 +	SH_ADD_HOOK(Test, Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_Post1), true);
   1.194 +
   1.195 +	const int &ret22 = pTest->Func2(500);
   1.196 +	ADD_STATE(State_Func2_Ret(&ret22));
   1.197 +
   1.198 +	CHECK_STATES((&g_States,
   1.199 +		new State_Func2_Pre1(500, &hook.m_Var),		// p1 was 500; it's going to override with hook.m_Var; and also change p1 to 1337
   1.200 +		new State_Func2(1337, &pTest->m_Var2),		// p1 was 1337; it's going to ret pTest->m_Var2
   1.201 +		new State_Func2_Post1(1337,					// p1 was 1337
   1.202 +			&pTest->m_Var2,							// orig ret was pTest->m_Var2
   1.203 +			&hook.m_Var),							// override ret was hook.m_Var
   1.204 +		new State_Func2_Ret(&hook.m_Var),			// really returned hook.m_Var
   1.205 +		NULL), "Part 7");
   1.206 +	
   1.207 +   	return true;
   1.208 +}