core/sourcehook/test/testrefret.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Jun 24 17:23:58 2011 -0500 (2011-06-24)
changeset 761 ba4f58bb1c6a
parent 733 2365240861a2
permissions -rw-r--r--
Triggering build.
     1 #include "testevents.h"
     2 #include "sourcehook_test.h"
     3 
     4 // Tests support for functions which return references
     5 
     6 namespace
     7 {
     8 	StateList g_States;
     9 	SourceHook::ISourceHook *g_SHPtr;
    10 	SourceHook::Plugin g_PLID;
    11 
    12 	MAKE_STATE_1(State_Func1_Pre1, int*);			// p1: the ref Func1_Pre1 is going to return
    13 	MAKE_STATE_3(State_Func1_Pre2, META_RES, int*, int*);	// p1: current status
    14 															// p2: override ret
    15 															// p3: what this handler is going to supercede with
    16 	MAKE_STATE_1(State_Func1, int*);				// p1: the ref Func1 is going to return
    17 	MAKE_STATE_2(State_Func1_Post1, int*, int*);	// p1: orig_ret; p2: override_ret
    18 	MAKE_STATE_1(State_Func1_Post2, int*);			// p1: what it's going to return
    19 	MAKE_STATE_1(State_Func1_Ret, int*);			// p1: the ref it returned
    20 
    21 
    22 	MAKE_STATE_2(State_Func2_Pre1, int, const int*);				// p1: func's p1; p2: what it's going to ret
    23 	MAKE_STATE_2(State_Func2, int, const int*);						// p1: func's p1; p2: what it's going to ret
    24 	MAKE_STATE_3(State_Func2_Post1, int, const int*, const int*);	// p1: func's p1; p2: orig ret; p3: override ret
    25 	MAKE_STATE_1(State_Func2_Ret, const int*);						// p1: ret
    26 
    27 	int g_Var;
    28 
    29 	class Test
    30 	{
    31 	public:
    32 		int m_Var1;
    33 		int m_Var2;
    34 
    35 		Test() : m_Var1(87)
    36 		{
    37 		}
    38 
    39 		virtual int& Func1()
    40 		{
    41 			ADD_STATE(State_Func1(&m_Var1));
    42 			return m_Var1;
    43 		}
    44 		virtual const int& Func2(int p1)
    45 		{
    46 			ADD_STATE(State_Func2(p1, &m_Var2));
    47 			m_Var2 = p1;
    48 			return m_Var2;
    49 		}
    50 	};
    51 
    52 	SH_DECL_HOOK0(Test, Func1, SH_NOATTRIB, 0, int&);
    53 	SH_DECL_MANUALHOOK0(Manual_Test_Func1, 0, 0, 0, int&);
    54 	SH_DECL_HOOK1(Test, Func2, SH_NOATTRIB, 0, const int&, int);
    55 	SH_DECL_MANUALHOOK1(Manual_Test_Func2, 1, 0, 0, const int&, int);
    56 
    57 	class CHook
    58 	{
    59 	public:
    60 		int m_Var;
    61 		virtual int& Func1_Pre1()
    62 		{
    63 			ADD_STATE(State_Func1_Pre1(&m_Var));
    64 			RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
    65 		}
    66 		virtual int &Func1_Pre2()
    67 		{
    68 			int &overrideret = META_RESULT_OVERRIDE_RET(int&);
    69 			overrideret = 1337;
    70 			ADD_STATE(State_Func1_Pre2(META_RESULT_STATUS, &overrideret, &g_Var));
    71 			RETURN_META_VALUE(MRES_SUPERCEDE, g_Var);
    72 		}
    73 		virtual int& Func1_Post1()
    74 		{
    75 			ADD_STATE(State_Func1_Post1(&META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
    76 			RETURN_META_NOREF(MRES_IGNORED, int&);
    77 		}
    78 		virtual int& Func1_Post2()
    79 		{
    80 			ADD_STATE(State_Func1_Post2(&m_Var));
    81 			RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
    82 		}
    83 
    84 		virtual const int& Func2_PreRecall_OverrideRet_Normal(int p1)
    85 		{
    86 			ADD_STATE(State_Func2_Pre1(p1, &m_Var));
    87 			RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, m_Var, static_cast<const int& (Test::*)(int)>(&Test::Func2), (1337));
    88 		}
    89 
    90 		virtual const int& Func2_PreRecall_OverrideRet_Manual(int p1)
    91 		{
    92 			ADD_STATE(State_Func2_Pre1(p1, &m_Var));
    93 			RETURN_META_VALUE_MNEWPARAMS(MRES_OVERRIDE, m_Var, Manual_Test_Func2, (1337));
    94 		}
    95 
    96 		virtual const int& Func2_Post1(int p1)
    97 		{
    98 			ADD_STATE(State_Func2_Post1(p1, &META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
    99 			RETURN_META_NOREF(MRES_IGNORED, const int&);
   100 		}
   101 	};
   102 
   103 	Test *MyTestFactory()
   104 	{
   105 		return new Test;
   106 	}
   107 }
   108 
   109 class TesterRefRet
   110 {
   111 public:
   112 	Test *pTest;
   113 	CHook hook;
   114 
   115 	TesterRefRet()
   116 	{
   117 		pTest = NULL;
   118 	}
   119 
   120 	bool doTests(std::string &error)
   121 	{
   122 		GET_SHPTR(g_SHPtr);
   123 		g_PLID = 1;
   124 
   125 		pTest = MyTestFactory();
   126 		CAutoPtrDestruction<Test> apd(pTest);
   127 
   128 		int &ret1 = pTest->Func1();
   129 		ADD_STATE(State_Func1_Ret(&ret1));
   130 
   131 		CHECK_STATES((&g_States,
   132 			new State_Func1(&pTest->m_Var1),
   133 			new State_Func1_Ret(&pTest->m_Var1),
   134 			NULL), "Part 1");
   135 
   136 		// Now add Func1_Pre1, which supercedes and returns hook.m_Var
   137 		AddHook__Test_Func1__Func1_Pre();
   138 
   139 		int &ret2 = pTest->Func1();
   140 		ADD_STATE(State_Func1_Ret(&ret2));
   141 		
   142 		CHECK_STATES((&g_States,
   143 			new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   144 			new State_Func1(&pTest->m_Var1),			// Function says that it's going to return pTest->m_Var1
   145 			new State_Func1_Ret(&hook.m_Var),		// hook.m_Var is returned
   146 			NULL), "Part 2");
   147 
   148 		// Now add Func1_Post1, which only reports orig ret and override ret
   149 		AddHook__Test_Func1__Func1_Post();
   150 
   151 		int &ret3 = pTest->Func1();
   152 		ADD_STATE(State_Func1_Ret(&ret3));
   153 		
   154 		CHECK_STATES((&g_States,
   155 			new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   156 			new State_Func1(&pTest->m_Var1),			// Function says that it's going to return pTest->m_Var1
   157 			new State_Func1_Post1(&pTest->m_Var1, &hook.m_Var),	// origret(=p1) is what it wanted to
   158 																// return, overrideret(=p2) is pre1's var
   159 			new State_Func1_Ret(&hook.m_Var),		// hook.m_Var is returned
   160 			NULL), "Part 3");
   161 
   162 		// Now add Func1_Pre2, which supercedes and returns g_Var (it also sets the override ret from pre1 to 1337)
   163 		// and add Func1_Post2, which overrides and returns hook.m_Var again.
   164 
   165 		AddHook__Test_Func1__Func1_Pre2();
   166 		AddHook__Test_Func1__Func1_Post2();
   167 
   168 		int &ret4 = pTest->Func1();
   169 		ADD_STATE(State_Func1_Ret(&ret4));
   170 
   171 		CHECK_STATES((&g_States,
   172 			new State_Func1_Pre1(&hook.m_Var),		// Pre1 says that it's going to override with hook.m_Var
   173 			new State_Func1_Pre2(MRES_OVERRIDE,			// current status
   174 								&hook.m_Var,			// override ret (which it set to 1337)
   175 								&g_Var),				// what it's going to ret, AND supercede with
   176 
   177 			new State_Func1_Post1(&g_Var, &g_Var),	// origret(=p1) is what pre2 superceded with,
   178 													//  so  overrideret(=p2) has to be the same
   179 			new State_Func1_Post2(&hook.m_Var),			// post2 is going to override with hook.m_Var again
   180 			new State_Func1_Ret(&hook.m_Var),		// ==>>> hook.m_Var is returned
   181 			NULL), "Part 4");
   182 
   183 		CHECK_COND(hook.m_Var == 1337, "Part 4.1");
   184 		
   185 		// Through a callclass
   186 		SourceHook::CallClass<Test> *cc1 = SH_GET_CALLCLASS(pTest);
   187 		int &ret5 = SH_CALL(cc1, &Test::Func1)();
   188 		ADD_STATE(State_Func1_Ret(&ret5));
   189 
   190 		CHECK_STATES((&g_States,
   191 			new State_Func1(&pTest->m_Var1),
   192 			new State_Func1_Ret(&pTest->m_Var1),
   193 			NULL), "Part 5");
   194 
   195 		SH_RELEASE_CALLCLASS(cc1);
   196 
   197 		////////////////////////////////////////////////////////////////////////////////////////
   198 		// Func2 tests
   199 		const int &ret21 = pTest->Func2(500);
   200 		ADD_STATE(State_Func2_Ret(&ret21));
   201 
   202 		CHECK_STATES((&g_States,
   203 			new State_Func2(500, &pTest->m_Var2),
   204 			new State_Func2_Ret(&pTest->m_Var2),
   205 			NULL), "Part 6");
   206 
   207 		AddHook__Test_Func2__Func2_Pre1();
   208 		AddHook__Test_Func2__Func2_Post1();
   209 
   210 		const int &ret22 = pTest->Func2(500);
   211 		ADD_STATE(State_Func2_Ret(&ret22));
   212 
   213 		CHECK_STATES((&g_States,
   214 			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
   215 			new State_Func2(1337, &pTest->m_Var2),		// p1 was 1337; it's going to ret pTest->m_Var2
   216 			new State_Func2_Post1(1337,					// p1 was 1337
   217 				&pTest->m_Var2,							// orig ret was pTest->m_Var2
   218 				&hook.m_Var),							// override ret was hook.m_Var
   219 			new State_Func2_Ret(&hook.m_Var),			// really returned hook.m_Var
   220 			NULL), "Part 7");
   221 		
   222    		return true;
   223 	}
   224 
   225 	virtual void AddHook__Test_Func1__Func1_Pre() = 0;
   226 	virtual void AddHook__Test_Func1__Func1_Post() = 0;
   227 
   228 	virtual void AddHook__Test_Func1__Func1_Pre2() = 0;
   229 	virtual void AddHook__Test_Func1__Func1_Post2() = 0;
   230 
   231 	virtual void AddHook__Test_Func2__Func2_Pre1() = 0;
   232 	virtual void AddHook__Test_Func2__Func2_Post1() = 0;
   233 };
   234 
   235 class TesterRefRetNonManual : public TesterRefRet
   236 {
   237 public:
   238 	void AddHook__Test_Func1__Func1_Pre()
   239 	{
   240 		SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre1), false);
   241 	}
   242 
   243 	void AddHook__Test_Func1__Func1_Post()
   244 	{
   245 		SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post1), true);
   246 	}
   247 
   248 	void AddHook__Test_Func1__Func1_Pre2()
   249 	{
   250 		SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre2), false);
   251 	}
   252 
   253 	void AddHook__Test_Func1__Func1_Post2()
   254 	{
   255 		SH_ADD_HOOK(Test, Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post2), true);
   256 	}
   257 
   258 	void AddHook__Test_Func2__Func2_Pre1()
   259 	{
   260 		SH_ADD_HOOK(Test, Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_PreRecall_OverrideRet_Normal), false);
   261 	}
   262 
   263 	void AddHook__Test_Func2__Func2_Post1()
   264 	{
   265 		SH_ADD_HOOK(Test, Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_Post1), true);
   266 	}
   267 };
   268 
   269 class TesterRefRetManual : public TesterRefRet
   270 {
   271 public:
   272 	void AddHook__Test_Func1__Func1_Pre()
   273 	{
   274 		SH_ADD_MANUALHOOK(Manual_Test_Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre1), false);
   275 	}
   276 
   277 	void AddHook__Test_Func1__Func1_Post()
   278 	{
   279 		SH_ADD_MANUALHOOK(Manual_Test_Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post1), true);
   280 	}
   281 
   282 	void AddHook__Test_Func1__Func1_Pre2()
   283 	{
   284 		SH_ADD_MANUALHOOK(Manual_Test_Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Pre2), false);
   285 	}
   286 
   287 	void AddHook__Test_Func1__Func1_Post2()
   288 	{
   289 		SH_ADD_MANUALHOOK(Manual_Test_Func1, pTest, SH_MEMBER(&hook, &CHook::Func1_Post2), true);
   290 	}
   291 
   292 	void AddHook__Test_Func2__Func2_Pre1()
   293 	{
   294 		SH_ADD_MANUALHOOK(Manual_Test_Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_PreRecall_OverrideRet_Manual), false);
   295 	}
   296 
   297 	void AddHook__Test_Func2__Func2_Post1()
   298 	{
   299 		SH_ADD_MANUALHOOK(Manual_Test_Func2, pTest, SH_MEMBER(&hook, &CHook::Func2_Post1), true);
   300 	}
   301 };
   302 
   303 bool TestRefRet(std::string &error)
   304 {
   305 	TesterRefRet *testerNonManual = new TesterRefRetNonManual;
   306 	bool resNonManual = testerNonManual->doTests(error);
   307 	if (!resNonManual)
   308 	{
   309 		return false;
   310 	}
   311 
   312 	TesterRefRet *testerManual = new TesterRefRetManual;
   313 	bool resManual = testerManual->doTests(error);
   314 	if (!resManual)
   315 	{
   316 		return false;
   317 	}
   318 	return true;
   319 }