sourcehook/test/test3.cpp
branchsourcemm-1.6.0
changeset 347 e82968c21606
child 361 f34718303c8f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcehook/test/test3.cpp	Tue Sep 18 20:03:47 2007 +0000
     1.3 @@ -0,0 +1,238 @@
     1.4 +#include <string>
     1.5 +#include "sourcehook_test.h"
     1.6 +#include "testevents.h"
     1.7 +
     1.8 +// TEST3
     1.9 +// Tests with inheritance / thisptroffsets
    1.10 +
    1.11 +namespace
    1.12 +{
    1.13 +	StateList g_States;
    1.14 +	SourceHook::ISourceHook *g_SHPtr;
    1.15 +	SourceHook::Plugin g_PLID;
    1.16 +
    1.17 +	MAKE_STATE_1(State_Func1_Called, void*);			// param1: This pointer
    1.18 +	MAKE_STATE_1(State_Func2_Called, void*);			// param1: This pointer
    1.19 +	MAKE_STATE_1(State_Func3_Called, void*);			// param1: This pointer
    1.20 +
    1.21 +	MAKE_STATE_1(State_Func1H_Called, void*);
    1.22 +	MAKE_STATE_1(State_Func2H_Called, void*);
    1.23 +	MAKE_STATE_1(State_Func3H_Called, void*);
    1.24 +
    1.25 +	class Base1
    1.26 +	{
    1.27 +		int a;
    1.28 +	public:
    1.29 +		virtual void Func1()
    1.30 +		{
    1.31 +			ADD_STATE(State_Func1_Called(reinterpret_cast<void*>(this)));
    1.32 +		}
    1.33 +	};
    1.34 +	class Base2
    1.35 +	{
    1.36 +		int b;
    1.37 +	public:
    1.38 +		virtual void Func2()
    1.39 +		{
    1.40 +			ADD_STATE(State_Func2_Called(reinterpret_cast<void*>(this)));
    1.41 +		}
    1.42 +	};
    1.43 +	class Derived : public Base1, public Base2
    1.44 +	{
    1.45 +		int c;
    1.46 +	public:
    1.47 +		virtual void Func3()
    1.48 +		{
    1.49 +			ADD_STATE(State_Func3_Called(reinterpret_cast<void*>(this)));
    1.50 +		}
    1.51 +	};
    1.52 +
    1.53 +	SH_DECL_HOOK0_void(Derived, Func1, SH_NOATTRIB, 0);
    1.54 +	SH_DECL_HOOK0_void(Derived, Func2, SH_NOATTRIB, 0);
    1.55 +	SH_DECL_HOOK0_void(Derived, Func3, SH_NOATTRIB, 0);
    1.56 +	SH_DECL_HOOK0_void(Base1, Func1, SH_NOATTRIB, 0);
    1.57 +	SH_DECL_HOOK0_void(Base2, Func2, SH_NOATTRIB, 0);
    1.58 +
    1.59 +	void Handler_Func1()
    1.60 +	{
    1.61 +		ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
    1.62 +	}
    1.63 +	void Handler_Func2()
    1.64 +	{
    1.65 +		ADD_STATE(State_Func2H_Called(META_IFACEPTR(void)));
    1.66 +	}
    1.67 +	void Handler_Func3()
    1.68 +	{
    1.69 +		ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
    1.70 +	}
    1.71 +}
    1.72 +
    1.73 +bool TestThisPtrOffs(std::string &error)
    1.74 +{
    1.75 +	GET_SHPTR(g_SHPtr);
    1.76 +	g_PLID = 1337;
    1.77 +
    1.78 +	Derived inst;
    1.79 +	Derived *pD = &inst;
    1.80 +	Base1 *pB1 = pD;
    1.81 +	Base2 *pB2 = pD;
    1.82 +
    1.83 +	// It should be:
    1.84 +	// pB1 = pD
    1.85 +	// pB2 > pB1
    1.86 +
    1.87 +	// 1)
    1.88 +	//  Get a callclass for pD
    1.89 +	//  Verify whether the this pointers are correct
    1.90 +	//  Also call them normally to make sure that we aren't messing it up ;)
    1.91 +	SourceHook::CallClass<Derived> *pD_CC = SH_GET_CALLCLASS(pD);
    1.92 +
    1.93 +	SH_CALL(pD_CC, &Derived::Func1)();
    1.94 +	SH_CALL(pD_CC, &Derived::Func2)();
    1.95 +	SH_CALL(pD_CC, &Derived::Func3)();
    1.96 +	pD->Func1();
    1.97 +	pD->Func2();
    1.98 +	pD->Func3();
    1.99 +
   1.100 +	CHECK_STATES((&g_States,
   1.101 +		new State_Func1_Called(pB1),
   1.102 +		new State_Func2_Called(pB2),
   1.103 +		new State_Func3_Called(pD),
   1.104 +		new State_Func1_Called(pB1),
   1.105 +		new State_Func2_Called(pB2),
   1.106 +		new State_Func3_Called(pD),
   1.107 +		NULL), "Part 1");
   1.108 +
   1.109 +	SH_CALL(pD_CC, &Base1::Func1)();
   1.110 +	SH_CALL(pD_CC, &Base2::Func2)();
   1.111 +
   1.112 +	CHECK_STATES((&g_States,
   1.113 +		new State_Func1_Called(pB1),
   1.114 +		new State_Func2_Called(pB2),
   1.115 +		NULL), "Part 1.1");
   1.116 +
   1.117 +	// 2)
   1.118 +	//   Get callclasses for the other ones and verify it as well
   1.119 +
   1.120 +	SourceHook::CallClass<Base1> *pB1_CC = SH_GET_CALLCLASS(pB1);
   1.121 +	SourceHook::CallClass<Base2> *pB2_CC = SH_GET_CALLCLASS(pB2);
   1.122 +
   1.123 +	SH_CALL(pB1_CC, &Base1::Func1)();
   1.124 +	SH_CALL(pB2_CC, &Base2::Func2)();
   1.125 +
   1.126 +	CHECK_STATES((&g_States,
   1.127 +		new State_Func1_Called(pB1),
   1.128 +		new State_Func2_Called(pB2),
   1.129 +		NULL), "Part 2");
   1.130 +
   1.131 +
   1.132 +	// 3) Add hooks on them (referring to them through pD1 / Derived)
   1.133 +	//   Check whether the hooks are called with the correct this pointer
   1.134 +
   1.135 +	SH_ADD_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
   1.136 +	SH_ADD_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
   1.137 +	SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   1.138 +
   1.139 +	pD->Func1();
   1.140 +	pD->Func2();
   1.141 +	pD->Func3();
   1.142 +	pB1->Func1();
   1.143 +	pB2->Func2();
   1.144 +
   1.145 +	// The handlers should always be called with the pointer to Derived
   1.146 +	CHECK_STATES((&g_States,
   1.147 +		new State_Func1H_Called(pD),
   1.148 +		new State_Func1_Called(pB1),
   1.149 +		new State_Func2H_Called(pD),
   1.150 +		new State_Func2_Called(pB2),
   1.151 +		new State_Func3H_Called(pD),
   1.152 +		new State_Func3_Called(pD),
   1.153 +		new State_Func1H_Called(pD),
   1.154 +		new State_Func1_Called(pB1),
   1.155 +		new State_Func2H_Called(pD),
   1.156 +		new State_Func2_Called(pB2),
   1.157 +		NULL), "Part 3");
   1.158 +
   1.159 +	SH_REMOVE_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
   1.160 +	SH_REMOVE_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
   1.161 +	SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   1.162 +
   1.163 +	// 4)
   1.164 +	//   Now add the hooks on Base1 and Base2 and check again
   1.165 +
   1.166 +	// Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
   1.167 +	SH_ADD_HOOK_STATICFUNC(Base1, Func1, pD, Handler_Func1, false);
   1.168 +	SH_ADD_HOOK_STATICFUNC(Base2, Func2, pD, Handler_Func2, false);
   1.169 +	SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   1.170 +
   1.171 +	pD->Func1();
   1.172 +	pD->Func2();
   1.173 +	pD->Func3();
   1.174 +	pB1->Func1();
   1.175 +	pB2->Func2();
   1.176 +
   1.177 +	// This time, the handlers for Func1 should be called with pB1 and the handlers
   1.178 +	// for Func2 should be called with pB2
   1.179 +	CHECK_STATES((&g_States,
   1.180 +		new State_Func1H_Called(pB1),
   1.181 +		new State_Func1_Called(pB1),
   1.182 +		new State_Func2H_Called(pB2),
   1.183 +		new State_Func2_Called(pB2),
   1.184 +		new State_Func3H_Called(pD),
   1.185 +		new State_Func3_Called(pD),
   1.186 +		new State_Func1H_Called(pB1),
   1.187 +		new State_Func1_Called(pB1),
   1.188 +		new State_Func2H_Called(pB2),
   1.189 +		new State_Func2_Called(pB2),
   1.190 +		NULL), "Part 4");
   1.191 +
   1.192 +	SH_REMOVE_HOOK_STATICFUNC(Base1, Func1, pD, Handler_Func1, false);
   1.193 +	SH_REMOVE_HOOK_STATICFUNC(Base2, Func2, pD, Handler_Func2, false);
   1.194 +	SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   1.195 +
   1.196 +
   1.197 +	// 5)
   1.198 +	//   Add some hooks, and use callclasses
   1.199 +
   1.200 +	// 5.1) First off, add all of them on pD
   1.201 +	SH_ADD_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
   1.202 +	SH_ADD_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
   1.203 +	SH_ADD_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   1.204 +
   1.205 +	pD->Func1();
   1.206 +	pD->Func2();
   1.207 +	pD->Func3();
   1.208 +
   1.209 +	CHECK_STATES((&g_States,
   1.210 +		new State_Func1H_Called(pD),
   1.211 +		new State_Func1_Called(pB1),
   1.212 +		new State_Func2H_Called(pD),
   1.213 +		new State_Func2_Called(pB2),
   1.214 +		new State_Func3H_Called(pD),
   1.215 +		new State_Func3_Called(pD),
   1.216 +		NULL), "Part 5.1");
   1.217 +
   1.218 +	SH_CALL(pD_CC, &Derived::Func1)();
   1.219 +	SH_CALL(pD_CC, &Derived::Func2)();
   1.220 +	SH_CALL(pD_CC, &Derived::Func3)();
   1.221 +	SH_CALL(pB1_CC, &Base1::Func1)();
   1.222 +	SH_CALL(pB2_CC, &Base2::Func2)();
   1.223 +
   1.224 +	CHECK_STATES((&g_States,
   1.225 +		new State_Func1_Called(pB1),
   1.226 +		new State_Func2_Called(pB2),
   1.227 +		new State_Func3_Called(pD),
   1.228 +		new State_Func1_Called(pB1),
   1.229 +		new State_Func2_Called(pB2),
   1.230 +		NULL), "Part 5.2");
   1.231 +
   1.232 +	SH_REMOVE_HOOK_STATICFUNC(Derived, Func1, pD, Handler_Func1, false);
   1.233 +	SH_REMOVE_HOOK_STATICFUNC(Derived, Func2, pD, Handler_Func2, false);
   1.234 +	SH_REMOVE_HOOK_STATICFUNC(Derived, Func3, pD, Handler_Func3, false);
   1.235 +
   1.236 +	SH_RELEASE_CALLCLASS(pB1_CC);
   1.237 +	SH_RELEASE_CALLCLASS(pB2_CC);
   1.238 +	SH_RELEASE_CALLCLASS(pD_CC);
   1.239 +
   1.240 +	return true;
   1.241 +}