sourcehook/test/test3.cpp
branchsourcemm-1.6.0
changeset 428 40a9d2f61121
parent 370 4dc28ab1c722
child 429 cadecde4b802
     1.1 --- a/sourcehook/test/test3.cpp	Thu Oct 04 19:46:28 2007 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,232 +0,0 @@
     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 -	class DerivedDerived : public Derived { };
    1.54 -
    1.55 -	SH_DECL_HOOK0_void(Derived, Func1, SH_NOATTRIB, 0);
    1.56 -	SH_DECL_HOOK0_void(Derived, Func2, SH_NOATTRIB, 0);
    1.57 -	SH_DECL_HOOK0_void(Derived, Func3, SH_NOATTRIB, 0);
    1.58 -	SH_DECL_HOOK0_void(Base1, Func1, SH_NOATTRIB, 0);
    1.59 -	SH_DECL_HOOK0_void(Base2, Func2, SH_NOATTRIB, 0);
    1.60 -
    1.61 -	void Handler_Func1()
    1.62 -	{
    1.63 -		ADD_STATE(State_Func1H_Called(META_IFACEPTR(void)));
    1.64 -	}
    1.65 -	void Handler_Func2()
    1.66 -	{
    1.67 -		ADD_STATE(State_Func2H_Called(META_IFACEPTR(void)));
    1.68 -	}
    1.69 -	void Handler_Func3()
    1.70 -	{
    1.71 -		ADD_STATE(State_Func3H_Called(META_IFACEPTR(void)));
    1.72 -	}
    1.73 -}
    1.74 -
    1.75 -bool TestThisPtrOffs(std::string &error)
    1.76 -{
    1.77 -	GET_SHPTR(g_SHPtr);
    1.78 -	g_PLID = 1337;
    1.79 -
    1.80 -	DerivedDerived inst;
    1.81 -	Derived *pD = &inst;
    1.82 -	Base1 *pB1 = pD;
    1.83 -	Base2 *pB2 = pD;
    1.84 -
    1.85 -	// It should be:
    1.86 -	// pB1 = pD
    1.87 -	// pB2 > pB1
    1.88 -
    1.89 -	// 1)
    1.90 -	//  Get a callclass for pD
    1.91 -	//  Verify whether the this pointers are correct
    1.92 -	//  Also call them normally to make sure that we aren't messing it up ;)
    1.93 -
    1.94 -	SH_CALL(pD, &Derived::Func1)();
    1.95 -	SH_CALL(pD, &Derived::Func2)();
    1.96 -	SH_CALL(pD, &Derived::Func3)();
    1.97 -	pD->Func1();
    1.98 -	pD->Func2();
    1.99 -	pD->Func3();
   1.100 -
   1.101 -	CHECK_STATES((&g_States,
   1.102 -		new State_Func1_Called(pB1),
   1.103 -		new State_Func2_Called(pB2),
   1.104 -		new State_Func3_Called(pD),
   1.105 -		new State_Func1_Called(pB1),
   1.106 -		new State_Func2_Called(pB2),
   1.107 -		new State_Func3_Called(pD),
   1.108 -		NULL), "Part 1");
   1.109 -
   1.110 -	SH_CALL(pD, &Base1::Func1)();
   1.111 -	SH_CALL(pD, &Base2::Func2)();
   1.112 -
   1.113 -	CHECK_STATES((&g_States,
   1.114 -		new State_Func1_Called(pB1),
   1.115 -		new State_Func2_Called(pB2),
   1.116 -		NULL), "Part 1.1");
   1.117 -
   1.118 -	// 2)
   1.119 -	//   Get callclasses for the other ones and verify it as well
   1.120 -
   1.121 -	SH_CALL(pB1, &Base1::Func1)();
   1.122 -	SH_CALL(pB2, &Base2::Func2)();
   1.123 -
   1.124 -	CHECK_STATES((&g_States,
   1.125 -		new State_Func1_Called(pB1),
   1.126 -		new State_Func2_Called(pB2),
   1.127 -		NULL), "Part 2");
   1.128 -
   1.129 -
   1.130 -	// 3) Add hooks on them (referring to them through pD1 / Derived)
   1.131 -	//   Check whether the hooks are called with the correct this pointer
   1.132 -
   1.133 -	SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   1.134 -	SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   1.135 -	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   1.136 -
   1.137 -	pD->Func1();
   1.138 -	pD->Func2();
   1.139 -	pD->Func3();
   1.140 -	pB1->Func1();
   1.141 -	pB2->Func2();
   1.142 -
   1.143 -	// The handlers should always be called with the pointer to Derived
   1.144 -	CHECK_STATES((&g_States,
   1.145 -		new State_Func1H_Called(pD),
   1.146 -		new State_Func1_Called(pB1),
   1.147 -		new State_Func2H_Called(pD),
   1.148 -		new State_Func2_Called(pB2),
   1.149 -		new State_Func3H_Called(pD),
   1.150 -		new State_Func3_Called(pD),
   1.151 -		new State_Func1H_Called(pD),
   1.152 -		new State_Func1_Called(pB1),
   1.153 -		new State_Func2H_Called(pD),
   1.154 -		new State_Func2_Called(pB2),
   1.155 -		NULL), "Part 3");
   1.156 -
   1.157 -	SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   1.158 -	SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   1.159 -	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   1.160 -
   1.161 -	// 4)
   1.162 -	//   Now add the hooks on Base1 and Base2 and check again
   1.163 -
   1.164 -	// Note that the new implicit_cast should convert the pD to Base1*/Base2* :)
   1.165 -	SH_ADD_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
   1.166 -	SH_ADD_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
   1.167 -	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   1.168 -
   1.169 -	pD->Func1();
   1.170 -	pD->Func2();
   1.171 -	pD->Func3();
   1.172 -	pB1->Func1();
   1.173 -	pB2->Func2();
   1.174 -
   1.175 -	// This time, the handlers for Func1 should be called with pB1 and the handlers
   1.176 -	// for Func2 should be called with pB2
   1.177 -	CHECK_STATES((&g_States,
   1.178 -		new State_Func1H_Called(pB1),
   1.179 -		new State_Func1_Called(pB1),
   1.180 -		new State_Func2H_Called(pB2),
   1.181 -		new State_Func2_Called(pB2),
   1.182 -		new State_Func3H_Called(pD),
   1.183 -		new State_Func3_Called(pD),
   1.184 -		new State_Func1H_Called(pB1),
   1.185 -		new State_Func1_Called(pB1),
   1.186 -		new State_Func2H_Called(pB2),
   1.187 -		new State_Func2_Called(pB2),
   1.188 -		NULL), "Part 4");
   1.189 -
   1.190 -	SH_REMOVE_HOOK(Base1, Func1, pD, SH_STATIC(Handler_Func1), false);
   1.191 -	SH_REMOVE_HOOK(Base2, Func2, pD, SH_STATIC(Handler_Func2), false);
   1.192 -	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   1.193 -
   1.194 -
   1.195 -	// 5)
   1.196 -	//   Add some hooks, and use callclasses
   1.197 -
   1.198 -	// 5.1) First off, add all of them on pD
   1.199 -	SH_ADD_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   1.200 -	SH_ADD_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   1.201 -	SH_ADD_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   1.202 -
   1.203 -	pD->Func1();
   1.204 -	pD->Func2();
   1.205 -	pD->Func3();
   1.206 -
   1.207 -	CHECK_STATES((&g_States,
   1.208 -		new State_Func1H_Called(pD),
   1.209 -		new State_Func1_Called(pB1),
   1.210 -		new State_Func2H_Called(pD),
   1.211 -		new State_Func2_Called(pB2),
   1.212 -		new State_Func3H_Called(pD),
   1.213 -		new State_Func3_Called(pD),
   1.214 -		NULL), "Part 5.1");
   1.215 -
   1.216 -	SH_CALL(pD, &Derived::Func1)();
   1.217 -	SH_CALL(pD, &Derived::Func2)();
   1.218 -	SH_CALL(pD, &Derived::Func3)();
   1.219 -	SH_CALL(pB1, &Base1::Func1)();
   1.220 -	SH_CALL(pB2, &Base2::Func2)();
   1.221 -
   1.222 -	CHECK_STATES((&g_States,
   1.223 -		new State_Func1_Called(pB1),
   1.224 -		new State_Func2_Called(pB2),
   1.225 -		new State_Func3_Called(pD),
   1.226 -		new State_Func1_Called(pB1),
   1.227 -		new State_Func2_Called(pB2),
   1.228 -		NULL), "Part 5.2");
   1.229 -
   1.230 -	SH_REMOVE_HOOK(Derived, Func1, pD, SH_STATIC(Handler_Func1), false);
   1.231 -	SH_REMOVE_HOOK(Derived, Func2, pD, SH_STATIC(Handler_Func2), false);
   1.232 -	SH_REMOVE_HOOK(Derived, Func3, pD, SH_STATIC(Handler_Func3), false);
   1.233 -
   1.234 -	return true;
   1.235 -}