1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcehook/test/testrefret.cpp Tue Sep 18 20:03:47 2007 +0000
1.3 @@ -0,0 +1,200 @@
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 +
1.99 +bool TestRefRet(std::string &error)
1.100 +{
1.101 + GET_SHPTR(g_SHPtr);
1.102 + g_PLID = 1;
1.103 +
1.104 + Test test;
1.105 + Test *pTest = &test;
1.106 + CHook hook;
1.107 +
1.108 + int &ret1 = pTest->Func1();
1.109 + ADD_STATE(State_Func1_Ret(&ret1));
1.110 +
1.111 + CHECK_STATES((&g_States,
1.112 + new State_Func1(&test.m_Var1),
1.113 + new State_Func1_Ret(&test.m_Var1),
1.114 + NULL), "Part 1");
1.115 +
1.116 + // Now add Func1_Pre1, which supercedes and returns hook.m_Var
1.117 + SH_ADD_HOOK_MEMFUNC(Test, Func1, &test, &hook, &CHook::Func1_Pre1, false);
1.118 +
1.119 + int &ret2 = pTest->Func1();
1.120 + ADD_STATE(State_Func1_Ret(&ret2));
1.121 +
1.122 + CHECK_STATES((&g_States,
1.123 + new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
1.124 + new State_Func1(&test.m_Var1), // Function says that it's going to return test.m_Var1
1.125 + new State_Func1_Ret(&hook.m_Var), // hook.m_Var is returned
1.126 + NULL), "Part 2");
1.127 +
1.128 + // Now add Func1_Post1, which only reports orig ret and override ret
1.129 + SH_ADD_HOOK_MEMFUNC(Test, Func1, &test, &hook, &CHook::Func1_Post1, true);
1.130 +
1.131 + int &ret3 = pTest->Func1();
1.132 + ADD_STATE(State_Func1_Ret(&ret3));
1.133 +
1.134 + CHECK_STATES((&g_States,
1.135 + new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
1.136 + new State_Func1(&test.m_Var1), // Function says that it's going to return test.m_Var1
1.137 + new State_Func1_Post1(&test.m_Var1, &hook.m_Var), // origret(=p1) is what it wanted to
1.138 + // return, overrideret(=p2) is pre1's var
1.139 + new State_Func1_Ret(&hook.m_Var), // hook.m_Var is returned
1.140 + NULL), "Part 3");
1.141 +
1.142 + // Now add Func1_Pre2, which supercedes and returns g_Var (it also sets the override ret from pre1 to 1337)
1.143 + // and add Func1_Post2, which overrides and returns hook.m_Var again.
1.144 +
1.145 + SH_ADD_HOOK_MEMFUNC(Test, Func1, &test, &hook, &CHook::Func1_Pre2, false);
1.146 + SH_ADD_HOOK_MEMFUNC(Test, Func1, &test, &hook, &CHook::Func1_Post2, true);
1.147 +
1.148 + int &ret4 = pTest->Func1();
1.149 + ADD_STATE(State_Func1_Ret(&ret4));
1.150 +
1.151 + CHECK_STATES((&g_States,
1.152 + new State_Func1_Pre1(&hook.m_Var), // Pre1 says that it's going to override with hook.m_Var
1.153 + new State_Func1_Pre2(MRES_OVERRIDE, // current status
1.154 + &hook.m_Var, // override ret (which it set to 1337)
1.155 + &g_Var), // what it's going to ret, AND supercede with
1.156 +
1.157 + new State_Func1_Post1(&g_Var, &g_Var), // origret(=p1) is what pre2 superceded with,
1.158 + // so overrideret(=p2) has to be the same
1.159 + new State_Func1_Post2(&hook.m_Var), // post2 is going to override with hook.m_Var again
1.160 + new State_Func1_Ret(&hook.m_Var), // ==>>> hook.m_Var is returned
1.161 + NULL), "Part 4");
1.162 +
1.163 + CHECK_COND(hook.m_Var == 1337, "Part 4.1");
1.164 +
1.165 + // Through a callclass
1.166 + SourceHook::CallClass<Test> *cc1 = SH_GET_CALLCLASS(&test);
1.167 + int &ret5 = SH_CALL(cc1, &Test::Func1)();
1.168 + ADD_STATE(State_Func1_Ret(&ret5));
1.169 +
1.170 + CHECK_STATES((&g_States,
1.171 + new State_Func1(&test.m_Var1),
1.172 + new State_Func1_Ret(&test.m_Var1),
1.173 + NULL), "Part 5");
1.174 +
1.175 + SH_RELEASE_CALLCLASS(cc1);
1.176 +
1.177 + ////////////////////////////////////////////////////////////////////////////////////////
1.178 + // Func2 tests
1.179 + const int &ret21 = pTest->Func2(500);
1.180 + ADD_STATE(State_Func2_Ret(&ret21));
1.181 +
1.182 + CHECK_STATES((&g_States,
1.183 + new State_Func2(500, &test.m_Var2),
1.184 + new State_Func2_Ret(&test.m_Var2),
1.185 + NULL), "Part 6");
1.186 +
1.187 + SH_ADD_HOOK_MEMFUNC(Test, Func2, &test, &hook, &CHook::Func2_Pre1, false);
1.188 + SH_ADD_HOOK_MEMFUNC(Test, Func2, &test, &hook, &CHook::Func2_Post1, true);
1.189 +
1.190 + const int &ret22 = pTest->Func2(500);
1.191 + ADD_STATE(State_Func2_Ret(&ret22));
1.192 +
1.193 + CHECK_STATES((&g_States,
1.194 + 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.195 + new State_Func2(1337, &test.m_Var2), // p1 was 1337; it's going to ret test.m_Var2
1.196 + new State_Func2_Post1(1337, // p1 was 1337
1.197 + &test.m_Var2, // orig ret was test.m_Var2
1.198 + &hook.m_Var), // override ret was hook.m_Var
1.199 + new State_Func2_Ret(&hook.m_Var), // really returned hook.m_Var
1.200 + NULL), "Part 7");
1.201 +
1.202 + return true;
1.203 +}