1 #include "testevents.h"
2 #include "sourcehook_test.h"
4 // Tests support for functions which return references
9 SourceHook::ISourceHook *g_SHPtr;
10 SourceHook::Plugin g_PLID;
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
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
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
41 ADD_STATE(State_Func1(&m_Var1));
44 virtual const int& Func2(int p1)
46 ADD_STATE(State_Func2(p1, &m_Var2));
56 virtual int& Func1_Pre1()
58 ADD_STATE(State_Func1_Pre1(&m_Var));
59 RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
61 virtual int &Func1_Pre2()
63 int &overrideret = META_RESULT_OVERRIDE_RET(int&);
65 ADD_STATE(State_Func1_Pre2(META_RESULT_STATUS, &overrideret, &g_Var));
66 RETURN_META_VALUE(MRES_SUPERCEDE, g_Var);
68 virtual int& Func1_Post1()
70 ADD_STATE(State_Func1_Post1(&META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
71 RETURN_META_NOREF(MRES_IGNORED, int&);
73 virtual int& Func1_Post2()
75 ADD_STATE(State_Func1_Post2(&m_Var));
76 RETURN_META_VALUE(MRES_OVERRIDE, m_Var);
79 virtual const int& Func2_Pre1(int p1)
81 ADD_STATE(State_Func2_Pre1(p1, &m_Var));
82 RETURN_META_VALUE_NEWPARAMS(MRES_OVERRIDE, m_Var, static_cast<const int& (Test::*)(int)>(&Test::Func2), (1337));
85 virtual const int& Func2_Post1(int p1)
87 ADD_STATE(State_Func2_Post1(p1, &META_RESULT_ORIG_RET(int&), &META_RESULT_OVERRIDE_RET(int&)));
88 RETURN_META_NOREF(MRES_IGNORED, const int&);
92 SH_DECL_HOOK0(Test, Func1, SH_NOATTRIB, 0, int&);
93 SH_DECL_HOOK1(Test, Func2, SH_NOATTRIB, 0, const int&, int);
96 bool TestRefRet(std::string &error)
105 int &ret1 = pTest->Func1();
106 ADD_STATE(State_Func1_Ret(&ret1));
108 CHECK_STATES((&g_States,
109 new State_Func1(&test.m_Var1),
110 new State_Func1_Ret(&test.m_Var1),
113 // Now add Func1_Pre1, which supercedes and returns hook.m_Var
114 SH_ADD_HOOK_MEMFUNC(Test, Func1, &test, &hook, &CHook::Func1_Pre1, false);
116 int &ret2 = pTest->Func1();
117 ADD_STATE(State_Func1_Ret(&ret2));
119 CHECK_STATES((&g_States,
120 new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
121 new State_Func1(&test.m_Var1), // Function says that it's going to return test.m_Var1
122 new State_Func1_Ret(&hook.m_Var), // hook.m_Var is returned
125 // Now add Func1_Post1, which only reports orig ret and override ret
126 SH_ADD_HOOK_MEMFUNC(Test, Func1, &test, &hook, &CHook::Func1_Post1, true);
128 int &ret3 = pTest->Func1();
129 ADD_STATE(State_Func1_Ret(&ret3));
131 CHECK_STATES((&g_States,
132 new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
133 new State_Func1(&test.m_Var1), // Function says that it's going to return test.m_Var1
134 new State_Func1_Post1(&test.m_Var1, &hook.m_Var), // origret(=p1) is what it wanted to
135 // return, overrideret(=p2) is pre1's var
136 new State_Func1_Ret(&hook.m_Var), // hook.m_Var is returned
139 // Now add Func1_Pre2, which supercedes and returns g_Var (it also sets the override ret from pre1 to 1337)
140 // and add Func1_Post2, which overrides and returns hook.m_Var again.
142 SH_ADD_HOOK_MEMFUNC(Test, Func1, &test, &hook, &CHook::Func1_Pre2, false);
143 SH_ADD_HOOK_MEMFUNC(Test, Func1, &test, &hook, &CHook::Func1_Post2, true);
145 int &ret4 = pTest->Func1();
146 ADD_STATE(State_Func1_Ret(&ret4));
148 CHECK_STATES((&g_States,
149 new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
150 new State_Func1_Pre2(MRES_OVERRIDE, // current status
151 &hook.m_Var, // override ret (which it set to 1337)
152 &g_Var), // what it's going to ret, AND supercede with
154 new State_Func1_Post1(&g_Var, &g_Var), // origret(=p1) is what pre2 superceded with,
155 // so overrideret(=p2) has to be the same
156 new State_Func1_Post2(&hook.m_Var), // post2 is going to override with hook.m_Var again
157 new State_Func1_Ret(&hook.m_Var), // ==>>> hook.m_Var is returned
160 CHECK_COND(hook.m_Var == 1337, "Part 4.1");
162 // Through a callclass
163 SourceHook::CallClass<Test> *cc1 = SH_GET_CALLCLASS(&test);
164 int &ret5 = SH_CALL(cc1, &Test::Func1)();
165 ADD_STATE(State_Func1_Ret(&ret5));
167 CHECK_STATES((&g_States,
168 new State_Func1(&test.m_Var1),
169 new State_Func1_Ret(&test.m_Var1),
172 SH_RELEASE_CALLCLASS(cc1);
174 ////////////////////////////////////////////////////////////////////////////////////////
176 const int &ret21 = pTest->Func2(500);
177 ADD_STATE(State_Func2_Ret(&ret21));
179 CHECK_STATES((&g_States,
180 new State_Func2(500, &test.m_Var2),
181 new State_Func2_Ret(&test.m_Var2),
184 SH_ADD_HOOK_MEMFUNC(Test, Func2, &test, &hook, &CHook::Func2_Pre1, false);
185 SH_ADD_HOOK_MEMFUNC(Test, Func2, &test, &hook, &CHook::Func2_Post1, true);
187 const int &ret22 = pTest->Func2(500);
188 ADD_STATE(State_Func2_Ret(&ret22));
190 CHECK_STATES((&g_States,
191 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
192 new State_Func2(1337, &test.m_Var2), // p1 was 1337; it's going to ret test.m_Var2
193 new State_Func2_Post1(1337, // p1 was 1337
194 &test.m_Var2, // orig ret was test.m_Var2
195 &hook.m_Var), // override ret was hook.m_Var
196 new State_Func2_Ret(&hook.m_Var), // really returned hook.m_Var