sourcehook/test/testrefret.cpp
branchsourcemm-1.6.0
changeset 428 40a9d2f61121
parent 370 4dc28ab1c722
child 429 cadecde4b802
     1.1 --- a/sourcehook/test/testrefret.cpp	Thu Oct 04 19:46:28 2007 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,202 +0,0 @@
     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 Whatever : public Test
    1.56 -	{
    1.57 -	};
    1.58 -
    1.59 -	class CHook
    1.60 -	{
    1.61 -	public:
    1.62 -		int m_Var;
    1.63 -		virtual int& Func1_Pre1()
    1.64 -		{
    1.65 -			ADD_STATE(State_Func1_Pre1(&m_Var));
    1.66 -			RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
    1.67 -		}
    1.68 -		virtual int &Func1_Pre2()
    1.69 -		{
    1.70 -			int &overrideret = META_RESULT_OVERRIDE_RET(int&);
    1.71 -			overrideret = 1337;
    1.72 -			ADD_STATE(State_Func1_Pre2(META_RESULT_STATUS, &overrideret, &g_Var));
    1.73 -			RETURN_META_VALUE(MRES_SUPERCEDE, g_Var);
    1.74 -		}
    1.75 -		virtual int& Func1_Post1()
    1.76 -		{
    1.77 -			ADD_STATE(State_Func1_Post1(&META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
    1.78 -			RETURN_META_NOREF(MRES_IGNORED, int&);
    1.79 -		}
    1.80 -		virtual int& Func1_Post2()
    1.81 -		{
    1.82 -			ADD_STATE(State_Func1_Post2(&m_Var));
    1.83 -			RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
    1.84 -		}
    1.85 -
    1.86 -		virtual const int& Func2_Pre1(int p1)
    1.87 -		{
    1.88 -			ADD_STATE(State_Func2_Pre1(p1, &m_Var));
    1.89 -			RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, m_Var, static_cast<const int& (Test::*)(int)>(&Test::Func2), (1337));
    1.90 -		}
    1.91 -
    1.92 -		virtual const int& Func2_Post1(int p1)
    1.93 -		{
    1.94 -			ADD_STATE(State_Func2_Post1(p1, &META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
    1.95 -			RETURN_META_NOREF(MRES_IGNORED, const int&);
    1.96 -		}
    1.97 -	};
    1.98 -
    1.99 -	SH_DECL_HOOK0(Test, Func1, SH_NOATTRIB, 0, int&);
   1.100 -	SH_DECL_HOOK1(Test, Func2, SH_NOATTRIB, 0, const int&, int);
   1.101 -}
   1.102 -
   1.103 -bool TestRefRet(std::string &error)
   1.104 -{
   1.105 -	GET_SHPTR(g_SHPtr);
   1.106 -	g_PLID = 1;
   1.107 -
   1.108 -	Whatever test;
   1.109 -	Test *pTest = &test;
   1.110 -	CHook hook;
   1.111 -
   1.112 -	int &ret1 = pTest->Func1();
   1.113 -	ADD_STATE(State_Func1_Ret(&ret1));
   1.114 -
   1.115 -	CHECK_STATES((&g_States,
   1.116 -		new State_Func1(&test.m_Var1),
   1.117 -		new State_Func1_Ret(&test.m_Var1),
   1.118 -		NULL), "Part 1");
   1.119 -
   1.120 -	// Now add Func1_Pre1, which supercedes and returns hook.m_Var
   1.121 -	SH_ADD_HOOK(Test, Func1, &test, SH_MEMBER(&hook, &CHook::Func1_Pre1), false);
   1.122 -
   1.123 -	int &ret2 = pTest->Func1();
   1.124 -	ADD_STATE(State_Func1_Ret(&ret2));
   1.125 -	
   1.126 -	CHECK_STATES((&g_States,
   1.127 -		new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   1.128 -		new State_Func1(&test.m_Var1),			// Function says that it's going to return test.m_Var1
   1.129 -		new State_Func1_Ret(&hook.m_Var),		// hook.m_Var is returned
   1.130 -		NULL), "Part 2");
   1.131 -
   1.132 -	// Now add Func1_Post1, which only reports orig ret and override ret
   1.133 -	SH_ADD_HOOK(Test, Func1, &test, SH_MEMBER(&hook, &CHook::Func1_Post1), true);
   1.134 -
   1.135 -	int &ret3 = pTest->Func1();
   1.136 -	ADD_STATE(State_Func1_Ret(&ret3));
   1.137 -	
   1.138 -	CHECK_STATES((&g_States,
   1.139 -		new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   1.140 -		new State_Func1(&test.m_Var1),			// Function says that it's going to return test.m_Var1
   1.141 -		new State_Func1_Post1(&test.m_Var1, &hook.m_Var),	// origret(=p1) is what it wanted to
   1.142 -															// return, overrideret(=p2) is pre1's var
   1.143 -		new State_Func1_Ret(&hook.m_Var),		// hook.m_Var is returned
   1.144 -		NULL), "Part 3");
   1.145 -
   1.146 -	// Now add Func1_Pre2, which supercedes and returns g_Var (it also sets the override ret from pre1 to 1337)
   1.147 -	// and add Func1_Post2, which overrides and returns hook.m_Var again.
   1.148 -
   1.149 -	SH_ADD_HOOK(Test, Func1, &test, SH_MEMBER(&hook, &CHook::Func1_Pre2), false);
   1.150 -	SH_ADD_HOOK(Test, Func1, &test, SH_MEMBER(&hook, &CHook::Func1_Post2), true);
   1.151 -
   1.152 -	int &ret4 = pTest->Func1();
   1.153 -	ADD_STATE(State_Func1_Ret(&ret4));
   1.154 -
   1.155 -	CHECK_STATES((&g_States,
   1.156 -		new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   1.157 -		new State_Func1_Pre2(MRES_OVERRIDE,			// current status
   1.158 -							&hook.m_Var,			// override ret (which it set to 1337)
   1.159 -							&g_Var),				// what it's going to ret, AND supercede with
   1.160 -
   1.161 -		new State_Func1_Post1(&g_Var, &g_Var),	// origret(=p1) is what pre2 superceded with,
   1.162 -												//  so  overrideret(=p2) has to be the same
   1.163 -		new State_Func1_Post2(&hook.m_Var),			// post2 is going to override with hook.m_Var again
   1.164 -		new State_Func1_Ret(&hook.m_Var),		// ==>>> hook.m_Var is returned
   1.165 -		NULL), "Part 4");
   1.166 -
   1.167 -	CHECK_COND(hook.m_Var == 1337, "Part 4.1");
   1.168 -	
   1.169 -	// Through a callclass
   1.170 -	int &ret5 = SH_CALL(pTest, &Test::Func1)();
   1.171 -	ADD_STATE(State_Func1_Ret(&ret5));
   1.172 -
   1.173 -	CHECK_STATES((&g_States,
   1.174 -		new State_Func1(&test.m_Var1),
   1.175 -		new State_Func1_Ret(&test.m_Var1),
   1.176 -		NULL), "Part 5");
   1.177 -
   1.178 -
   1.179 -	////////////////////////////////////////////////////////////////////////////////////////
   1.180 -	// Func2 tests
   1.181 -	const int &ret21 = pTest->Func2(500);
   1.182 -	ADD_STATE(State_Func2_Ret(&ret21));
   1.183 -
   1.184 -	CHECK_STATES((&g_States,
   1.185 -		new State_Func2(500, &test.m_Var2),
   1.186 -		new State_Func2_Ret(&test.m_Var2),
   1.187 -		NULL), "Part 6");
   1.188 -
   1.189 -	SH_ADD_HOOK(Test, Func2, &test, SH_MEMBER(&hook, &CHook::Func2_Pre1), false);
   1.190 -	SH_ADD_HOOK(Test, Func2, &test, SH_MEMBER(&hook, &CHook::Func2_Post1), true);
   1.191 -
   1.192 -	const int &ret22 = pTest->Func2(500);
   1.193 -	ADD_STATE(State_Func2_Ret(&ret22));
   1.194 -
   1.195 -	CHECK_STATES((&g_States,
   1.196 -		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.197 -		new State_Func2(1337, &test.m_Var2),		// p1 was 1337; it's going to ret test.m_Var2
   1.198 -		new State_Func2_Post1(1337,					// p1 was 1337
   1.199 -			&test.m_Var2,							// orig ret was test.m_Var2
   1.200 -			&hook.m_Var),							// override ret was hook.m_Var
   1.201 -		new State_Func2_Ret(&hook.m_Var),			// really returned hook.m_Var
   1.202 -		NULL), "Part 7");
   1.203 -	
   1.204 -   	return true;
   1.205 -}