sourcehook/test/testhookmangen.cpp
author Pavol Marko <pavolmarko@gmail.com>
Wed Nov 07 16:56:03 2007 +0000 (2007-11-07)
branchhookman_autogen
changeset 448 742b470c0e5c
parent 445 4c2924c393ed
child 449 78c6dfc7fff5
permissions -rw-r--r--
ForcedByRef works; next step will be: returns!
     1 #include <string>
     2 #include "sourcehook.h"
     3 #include "sourcehook_test.h"
     4 #include "testevents.h"
     5 #include "sourcehook_hookmangen.h"
     6 #include "sh_memory.h"
     7 #include "sh_pagealloc.h"
     8 
     9 // TESTHOOKMANGEN
    10 // Test automatic hookman generation
    11 //  Tests void and non-void functions
    12 //  0 to 6 params:
    13 //   integer-type, float-type, plain-old-data struct and objects with ctors/dtors
    14 //   both byval and byref
    15 //  also tests ignore/supercede
    16 //  also tests recalls
    17 
    18 // :TODO: test override as well
    19 
    20 namespace
    21 {
    22 	StateList g_States;
    23 	SourceHook::ISourceHook *g_SHPtr;
    24 	SourceHook::Plugin g_PLID;
    25 
    26 	// PtrBuf(ptr) gives ptrs unique numbers
    27 	// in the order they appear
    28 	SourceHook::List<const void*> g_PtrHash;
    29 
    30 	bool g_Inside_LeafFunc = false;			// inside a hook or a func
    31 
    32 
    33 	// POD / Object types
    34 	template <int MYSIZE>
    35 	struct POD
    36 	{
    37 		char x[MYSIZE];
    38 
    39 		bool operator==(const POD<MYSIZE> &other)
    40 		{
    41 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
    42 		}
    43 
    44 		bool operator==(char other)
    45 		{
    46 			for (int i = 0; i < MYSIZE; ++i)
    47 			{
    48 				if (x[i] != other)
    49 					return false;
    50 			}
    51 			return true;
    52 		}
    53 	};
    54 
    55 	template <int MYSIZE>
    56 		std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
    57 	{
    58 		os << "Some POD!";
    59 		return os;
    60 	}
    61 
    62 	MAKE_STATE_1(State_ObjOCtor_Called, int /*MYSIZE*/);
    63 	MAKE_STATE_1(State_ObjCCtor_Called, int /*MYSIZE*/);
    64 	MAKE_STATE_1(State_ObjODtor_Called, int /*MYSIZE*/);
    65 	MAKE_STATE_1(State_ObjAssignOp_Called, int /*MYSIZE*/);
    66 
    67 	template <int MYSIZE>
    68 	struct Object
    69 	{
    70 		char x[MYSIZE];
    71 
    72 		Object(char initch)
    73 		{
    74 			memset(reinterpret_cast<void*>(x), initch, MYSIZE);
    75 			if (!g_Inside_LeafFunc)
    76 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
    77 		}
    78 
    79 		Object()
    80 		{
    81 			if (!g_Inside_LeafFunc)
    82 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
    83 		}
    84 
    85 		Object(const Object<MYSIZE> & other)
    86 		{
    87 			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
    88 			if (!g_Inside_LeafFunc)
    89 				ADD_STATE(State_ObjCCtor_Called(MYSIZE));
    90 		}
    91 
    92 		~Object()
    93 		{
    94 			if (!g_Inside_LeafFunc)
    95 				ADD_STATE(State_ObjODtor_Called(MYSIZE));
    96 		}
    97 
    98 		Object & operator = (const Object &other)
    99 		{
   100 			if (!g_Inside_LeafFunc)
   101 				ADD_STATE(State_ObjAssignOp_Called(MYSIZE));
   102 
   103 			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
   104 
   105 			return *this;
   106 		}
   107 
   108 		bool operator==(const Object<MYSIZE> &other)
   109 		{
   110 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
   111 		}
   112 	};
   113 
   114 	template <int MYSIZE>
   115 	std::ostream& operator <<(std::ostream &os, const Object<MYSIZE> &obj)
   116 	{
   117 		os << "Some Obj" << static_cast<int>(obj.x[0]);
   118 		return os;
   119 	}
   120 
   121 	// Because of some weird bug in MSVC < 1400
   122 #define MAKE_PODRET(size) \
   123 	struct PodRet##size \
   124 	{ \
   125 		POD<size> actPod; \
   126 		bool operator==(const PodRet##size &other) { return actPod == other.actPod; } \
   127 		bool operator==(char other) { return actPod == other; } \
   128 	}; \
   129 	std::ostream& operator <<(std::ostream &os, const PodRet##size &obj) \
   130 	{ \
   131 		os << obj.actPod; \
   132 		return os; \
   133 	} \
   134 	template <> struct MakeRet< PodRet##size > \
   135 	{ \
   136 		static PodRet##size Do(int a) \
   137 		{ \
   138 			PodRet##size x; \
   139 			memset(reinterpret_cast<void*>(x.actPod.x), a, size); \
   140 			return x; \
   141 		} \
   142 	};
   143 
   144 #define MAKE_OBJRET(size) \
   145 	struct ObjRet##size \
   146 	{ \
   147 		Object<size> actObj; \
   148 		bool operator==(const ObjRet##size &other) { return actObj == other.actObj; } \
   149 		bool operator==(char other) { return actObj == other; } \
   150 	}; \
   151 	std::ostream& operator <<(std::ostream &os, const ObjRet##size &obj) \
   152 	{ \
   153 		os << obj.actObj; \
   154 		return os; \
   155 	} \
   156 	template <> struct MakeRet< ObjRet##size > \
   157 	{ \
   158 		static ObjRet##size Do(int a) \
   159 		{ \
   160 			ObjRet##size x; \
   161 			memset(reinterpret_cast<void*>(x.actObj.x), a, size); \
   162 			return x; \
   163 		} \
   164 	};
   165 
   166 
   167 
   168 	// "Increment" something:  (reproducible change for recall tests)
   169 	//  integer: ++
   170 	//  float: += 1.3
   171 	//  pod/object: x[i]++, 0 <= i < MYSIZE
   172 	template <class T>
   173 	struct Increment
   174 	{
   175 		static void Incr(T &what)	
   176 		{
   177 			++what;
   178 		}
   179 	};
   180 
   181 	template<>
   182 	struct Increment<float>
   183 	{
   184 		static void Incr(float &what)	
   185 		{
   186 			what += 1.3f;
   187 		}
   188 	};
   189 
   190 	template<>
   191 	struct Increment<double>
   192 	{
   193 		static void Incr(double &what)	
   194 		{
   195 			what += 1.3;
   196 		}
   197 	};
   198 
   199 	template<int MYSIZE>
   200 	struct Increment< POD<MYSIZE> >
   201 	{
   202 		static void Incr(POD<MYSIZE> &what)	
   203 		{
   204 			for (int i = 0; i < MYSIZE; ++i)
   205 				++ what.x[i];
   206 		}
   207 	};
   208 
   209 	template<int MYSIZE>
   210 	struct Increment< Object<MYSIZE> >
   211 	{
   212 		static void Incr(Object<MYSIZE> &what)	
   213 		{
   214 			for (int i = 0; i < MYSIZE; ++i)
   215 				++ what.x[i];
   216 		}
   217 	};
   218 
   219 	#include "testhookmangen.h"
   220 
   221 	template <class T>
   222 	int PtrBuf(T ptr)
   223 	{
   224 		int a = 0;
   225 
   226 		const void *vptr = reinterpret_cast<const void*>(ptr);
   227 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end(); ++iter)
   228 		{
   229 			if (*iter == vptr)
   230 				return a;
   231 			else
   232 				++a;
   233 		}
   234 		g_PtrHash.push_back(vptr);
   235 		return static_cast<int>(g_PtrHash.size()) - 1;
   236 	}
   237 
   238 	template <class T>
   239 	T PtrBufPtr(T ptr)
   240 	{
   241 		PtrBuf(ptr);
   242 		return ptr;
   243 	}
   244 
   245 	void PtrBuf_Clear(int leave_in = 0)
   246 	{
   247 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end();)
   248 		{
   249 			if (--leave_in < 0)
   250 				iter = g_PtrHash.erase(iter);
   251 			else
   252 				++iter;
   253 		}
   254 	}
   255 
   256 
   257 	// MyDelegate base class for other delegates
   258 	class MyDelegate : public SourceHook::ISHDelegate
   259 	{
   260 		// Unneeded
   261 		//  because we don't use old SH_REMOVE_HOOK syntax
   262 		virtual bool IsEqual(SourceHook::ISHDelegate *pOtherDeleg)
   263 		{
   264 			return false;
   265 		}
   266 
   267 		virtual void DeleteThis()
   268 		{
   269 			delete this;
   270 		}
   271 	};
   272 
   273 	THGM_MAKE_TEST0_void(0);
   274 	THGM_SETUP_PI0(0);
   275 
   276 	THGM_MAKE_TEST1_void(1, char);
   277 	THGM_SETUP_PI1(1, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   278 
   279 	THGM_MAKE_TEST1_void(2, short);
   280 	THGM_SETUP_PI1(2, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   281 
   282 	THGM_MAKE_TEST1_void(3, int);
   283 	THGM_SETUP_PI1(3, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   284 
   285 	THGM_MAKE_TEST1_void(4, float);
   286 	THGM_SETUP_PI1(4, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   287 
   288 	THGM_MAKE_TEST1_void(5, double);
   289 	THGM_SETUP_PI1(5, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   290 	
   291 	THGM_MAKE_TEST5_void(6, char, short, int, float, double);
   292 	THGM_SETUP_PI5(6,
   293 		char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   294 		short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   295 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   296 		float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal,
   297 		double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal
   298 		);
   299 
   300 	THGM_MAKE_TEST2_void(7, char&, double&);
   301 	THGM_SETUP_PI2(7,
   302 		char&, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByRef,
   303 		double&, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByRef
   304 		);
   305 
   306 	THGM_MAKE_TEST1_void(8, POD<7>);
   307 	THGM_SETUP_PI1(8, POD<7>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   308 
   309 	THGM_MAKE_TEST1_void(9, POD<600>);
   310 	THGM_SETUP_PI1(9, POD<600>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   311 
   312 	THGM_MAKE_TEST1_void(10, POD<600> &);
   313 	THGM_SETUP_PI1(10, POD<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef);
   314 
   315 	THGM_MAKE_TEST2_void(11, Object<3>, Object<600>&);
   316 	THGM_SETUP_PI2(11,
   317 		Object<3>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor,
   318 		Object<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor
   319 		);
   320 
   321 	THGM_MAKE_TEST0(101, char);
   322 	THGM_SETUP_PI0(101);
   323 	THGM_SETUP_RI(101, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   324 
   325 	THGM_MAKE_TEST0(102, short);
   326 	THGM_SETUP_PI0(102);
   327 	THGM_SETUP_RI(102, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   328 
   329 	THGM_MAKE_TEST0(103, int);
   330 	THGM_SETUP_PI0(103);
   331 	THGM_SETUP_RI(103, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   332 
   333 	THGM_MAKE_TEST0(104, float);
   334 	THGM_SETUP_PI0(104);
   335 	THGM_SETUP_RI(104, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   336 
   337 	THGM_MAKE_TEST0(105, double);
   338 	THGM_SETUP_PI0(105);
   339 	THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   340 
   341 	// pod 1-13
   342 	MAKE_PODRET(1);
   343 	THGM_MAKE_TEST1(106, PodRet1, int);
   344 	THGM_SETUP_PI1(106,
   345 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   346 		);
   347 	THGM_SETUP_RI(106, PodRet1, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   348 
   349 	MAKE_PODRET(4);
   350 	THGM_MAKE_TEST1(107, PodRet4, int);
   351 	THGM_SETUP_PI1(107,
   352 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   353 		);
   354 	THGM_SETUP_RI(107, PodRet4, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   355 
   356 	MAKE_PODRET(8);
   357 	THGM_MAKE_TEST1(108, PodRet8, int);
   358 	THGM_SETUP_PI1(108,
   359 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   360 		);
   361 	THGM_SETUP_RI(108, PodRet8, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   362 
   363 	MAKE_PODRET(13);
   364 	THGM_MAKE_TEST1(109, PodRet13, int);
   365 	THGM_SETUP_PI1(109,
   366 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   367 		);
   368 	THGM_SETUP_RI(109, PodRet13, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   369 
   370 	MAKE_OBJRET(13);
   371 	THGM_MAKE_TEST1(110, ObjRet13, int);
   372 	THGM_SETUP_PI1(110,
   373 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   374 		);
   375 	THGM_SETUP_RI(110, ObjRet13, SourceHook::PassInfo::PassType_Object,
   376 		SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
   377 		SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
   378 
   379 	MAKE_OBJRET(111);
   380 	ObjRet111 g_O111_0;
   381 	ObjRet111 g_O111_1;
   382 	ObjRet111 g_O111_2;
   383 	ObjRet111 g_O111_3;
   384 	ObjRet111 g_O111_4;
   385 
   386 	template <>
   387 	struct MakeRet< ObjRet111& >
   388 	{
   389 		static ObjRet111 &Do(int a)
   390 		{
   391 			switch (a)
   392 			{
   393 			case 0:
   394 				return g_O111_0;
   395 			case 1:
   396 				return g_O111_1;
   397 			case 2:
   398 				return g_O111_2;
   399 			case 3:
   400 				return g_O111_3;
   401 			default:
   402 				return g_O111_4;
   403 			}
   404 		}
   405 	};
   406 
   407 	THGM_MAKE_TEST0(111, ObjRet111& );
   408 	THGM_SETUP_PI0(111);
   409 	THGM_SETUP_RI(111, ObjRet111& , SourceHook::PassInfo::PassType_Object,
   410 		SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
   411 		SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
   412 }
   413 
   414 bool TestHookManGen(std::string &error)
   415 {
   416 	GET_SHPTR(g_SHPtr);
   417 	g_PLID = 1337;
   418 
   419 	// 5 Global constructors (g_O111_*)
   420 	CHECK_STATES((&g_States,
   421 		new State_ObjOCtor_Called(111),
   422 		new State_ObjOCtor_Called(111),
   423 		new State_ObjOCtor_Called(111),
   424 		new State_ObjOCtor_Called(111),
   425 		new State_ObjOCtor_Called(111),
   426 		NULL), "GlobCtors");
   427 
   428 	THGM_DO_TEST_void(0, ());
   429 
   430 	THGM_DO_TEST_void(1, (100));
   431 	
   432 	THGM_DO_TEST_void(2, (0x1F00));
   433 
   434 	THGM_DO_TEST_void(3, (0x1F000000));
   435 
   436 	THGM_DO_TEST_void(4, (0.5f));
   437 
   438 	THGM_DO_TEST_void(5, (5.5));
   439 
   440 	THGM_DO_TEST_void(6, (100, 0x1f00, 0x1f000000, 0.5f, 5.5));
   441 
   442 	char a = 5;
   443 	double b = 233.33;
   444 	THGM_DO_TEST_void(7, (a, b));
   445 
   446 	POD<7> pod7 = MakeRet< POD<7> >::Do(78);
   447 	THGM_DO_TEST_void(8, (pod7));
   448 
   449 	POD<600> pod600 = MakeRet< POD<600> >::Do(34);
   450 	THGM_DO_TEST_void(9, (pod600));
   451 	
   452 	THGM_DO_TEST_void(10, (pod600));
   453 
   454 	// Test11: Special: constructors/destructors
   455 	PtrBuf_Clear();
   456 	Object<3> obj3(33);
   457 	Object<600> obj600(21);
   458 
   459 	CHECK_STATES((&g_States,
   460 		new State_ObjOCtor_Called(3),
   461 		new State_ObjOCtor_Called(600),
   462 		NULL), "Test11 Part0");
   463 
   464 	setuppi_11();
   465 	g_Genc11 = new SourceHook::Impl::GenContext(&protoinfo_11, 0, 0, g_SHPtr);
   466 	g_Genc_ad11.set(g_Genc11);
   467 	SourceHook::HookManagerPubFunc myhookman11 = g_Genc11->Generate();
   468 	int hook1_11, hook2_11, hook3_11, hook4_11;
   469 	
   470 	TestClass11 *pTest11 = new TestClass11;
   471 	CAutoPtrDestruction<TestClass11> apd11(pTest11);
   472 	
   473 	/* no hooks - no hooks */
   474 	PtrBuf_Clear();
   475 	pTest11->Func(obj3, obj600);
   476 
   477 	g_Inside_LeafFunc = true;
   478 	CHECK_STATES((&g_States,
   479 		new State_ObjCCtor_Called(3),
   480 		new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   481 		new State_ObjODtor_Called(3),
   482 		NULL), "Test" "11" " Part1");
   483 	g_Inside_LeafFunc = false;
   484 
   485 	/* hook1 - no hooks */
   486 	THGM_ADD_HOOK(11, 1);
   487 	
   488 	PtrBuf_Clear();
   489 	pTest11->Func(obj3, obj600);
   490 	g_Inside_LeafFunc = true;
   491 	CHECK_STATES((&g_States,
   492 		new State_ObjCCtor_Called(3),
   493 
   494 		new State_ObjCCtor_Called(3),
   495 		new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
   496 		new State_ObjODtor_Called(3),
   497 
   498 		new State_ObjCCtor_Called(3),
   499 		new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   500 		new State_ObjODtor_Called(3),
   501 
   502 		new State_ObjODtor_Called(3),
   503 		NULL), "Test" "11" " Part2");
   504 	g_Inside_LeafFunc = false;
   505 
   506 	THGM_REMOVE_HOOK(11, 1);
   507 
   508 	/* hook1, hook2 - hook3, hook4 */
   509 	THGM_ADD_HOOK(11, 1);
   510 	THGM_ADD_HOOK(11, 2);
   511 	THGM_ADD_HOOK(11, 3);
   512 	THGM_ADD_HOOK(11, 4);
   513 	PtrBuf_Clear();
   514 	pTest11->Func(obj3, obj600);
   515 	g_Inside_LeafFunc = true;
   516 	CHECK_STATES((&g_States,
   517 		new State_ObjCCtor_Called(3),
   518 
   519 		new State_ObjCCtor_Called(3),
   520 		new	State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
   521 		new State_ObjODtor_Called(3),
   522 
   523 		new State_ObjCCtor_Called(3),
   524 		new State_Deleg2_11(pTest11, 1, ParamState_m11 (obj3, obj600)),
   525 		new State_ObjODtor_Called(3),
   526 
   527 		new State_ObjCCtor_Called(3),
   528 		new State_Deleg3_11(pTest11, 2, ParamState_m11 (obj3, obj600)),
   529 		new State_ObjODtor_Called(3),
   530 
   531 		new State_ObjCCtor_Called(3),
   532 		new State_Deleg4_11(pTest11, 3, ParamState_m11 (obj3, obj600)),
   533 		new State_ObjODtor_Called(3),
   534 
   535 		new State_ObjODtor_Called(3),
   536 	NULL), "Test" "11" " Part3");
   537 	g_Inside_LeafFunc = false;
   538 
   539 	/* hook1 - hook3, hook4 WITH RECALLS */
   540 	THGM_REMOVE_HOOK(11, 2);
   541 	PtrBuf_Clear();
   542 	TestClass11::ms_DoRecall = true;
   543 	pTest11->Func(obj3, obj600);
   544 	g_Inside_LeafFunc = true;
   545 	CHECK_STATES((&g_States,
   546 		new State_ObjCCtor_Called(3),
   547 
   548 		new State_ObjCCtor_Called(3),
   549 		new	State_Deleg1_11(pTest11, 0 /* first deleg ptr */, ParamState_m11 (obj3, obj600)),
   550 
   551 		// recall !
   552 			// second hookfunc -> new copy
   553 			new State_ObjCCtor_Called(3),
   554 
   555 			// in second hookfunc now
   556 			//   -> calls orig func
   557 
   558 			new State_ObjCCtor_Called(3),
   559 			new State_Func11(pTest11, ParamState_m11 (obj3, obj600)(1)),
   560 			new State_ObjODtor_Called(3),
   561 
   562 			// calls first posthook
   563 			new State_ObjCCtor_Called(3),
   564 			new State_Deleg3_11(pTest11, 1 /* second deleg ptr */, ParamState_m11 (obj3, obj600)(1)),
   565 
   566 			// recall!
   567 				// third hookfunc -> new copy
   568 				new State_ObjCCtor_Called(3),
   569 
   570 				// calls second posthook
   571 
   572 				new State_ObjCCtor_Called(3),
   573 				new State_Deleg4_11(pTest11, 2 /* third deleg ptr */, ParamState_m11 (obj3, obj600)(2)),
   574 
   575 				// recall!
   576 					// fourth hookfunc -> new copy
   577 					new State_ObjCCtor_Called(3),
   578 
   579 					// has nothing to do though!
   580 
   581 					// fourth hookfunc done -> ret
   582 					new State_ObjODtor_Called(3),
   583 
   584 				// third hookfunc done -> ret
   585 				new State_ObjODtor_Called(3),
   586 				// ret from hookhandler which did the recall
   587 				new State_ObjODtor_Called(3),
   588 
   589 			// second hookfunc done -> ret
   590 			new State_ObjODtor_Called(3),
   591 			// ret from hookhandler which did the recall
   592 			new State_ObjODtor_Called(3),
   593 
   594 		// deleg1's instance
   595 		new State_ObjODtor_Called(3),
   596 		// first hookfunc done -> ret
   597 		new State_ObjODtor_Called(3),
   598 		NULL), "Test" "11" " Part4");
   599 	g_Inside_LeafFunc = false;
   600 
   601 	THGM_REMOVE_HOOK(11, 1);
   602 	THGM_REMOVE_HOOK(11, 3);
   603 	THGM_REMOVE_HOOK(11, 4);
   604 
   605 	THGM_DO_TEST(101, ());
   606 
   607 	THGM_DO_TEST(102, ());
   608 
   609 	THGM_DO_TEST(103, ());
   610 
   611 	THGM_DO_TEST(104, ());
   612 
   613 	THGM_DO_TEST(105, ());
   614 
   615 	// pod returns
   616 	THGM_DO_TEST(106, (5));
   617 	THGM_DO_TEST(107, (5));
   618 	THGM_DO_TEST(108, (5));
   619 	THGM_DO_TEST(109, (5));
   620 
   621 	// Test110: Special: constructors/destructors on return
   622 	PtrBuf_Clear();
   623 	ObjRet13 obj13;
   624 
   625 	CHECK_STATES((&g_States,
   626 		new State_ObjOCtor_Called(13),
   627 		NULL), "Test110 Part0");
   628 
   629 	setuppi_110();
   630 	setupri_110();
   631 
   632 	g_Genc110 = new SourceHook::Impl::GenContext(&protoinfo_110, 0, 0, g_SHPtr);
   633 	g_Genc_ad110.set(g_Genc110);
   634 	SourceHook::HookManagerPubFunc myhookman110 = g_Genc110->Generate();
   635 	int hook1_110, hook2_110, hook3_110, hook4_110;
   636 
   637 	TestClass110 *pTest110 = new TestClass110;
   638 	CAutoPtrDestruction<TestClass110> apd110(pTest110);
   639 
   640 	/* no hooks - no hooks */
   641 	PtrBuf_Clear();
   642 	obj13 = pTest110->Func(5);
   643 
   644 	g_Inside_LeafFunc = true;
   645 	CHECK_STATES((&g_States,
   646 		new State_Func110(pTest110, ParamState_m110 (5)),
   647 		new State_ObjOCtor_Called(13),			// MakeRet: Construction of x
   648 		new State_ObjCCtor_Called(13),			// Return from MakeRet	-> construct temporary object in our stack
   649 		new State_ObjODtor_Called(13),			// MakeRet: Destruction of x
   650 
   651 		new State_ObjAssignOp_Called(13),		// assign: obj13 = temporary object in our stack
   652 		new State_ObjODtor_Called(13),			// Func110: destruction of temporary object
   653 		NULL), "Test" "110" " Part1");
   654 	g_Inside_LeafFunc = false;
   655 
   656 	/* hook1 - no hooks */
   657 	THGM_ADD_HOOK(110, 1);
   658 
   659 	PtrBuf_Clear();
   660 	obj13 = pTest110->Func(5);
   661 	g_Inside_LeafFunc = true;
   662 	CHECK_STATES((&g_States,
   663 		// HookFunc: construct orig_ret/override_ret/plugin_ret objects
   664 		new State_ObjOCtor_Called(13),
   665 		new State_ObjOCtor_Called(13),
   666 		new State_ObjOCtor_Called(13),
   667 
   668 		// Calling delegate
   669 			new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
   670 
   671 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   672 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   673 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   674 
   675 		// back in hookfunc
   676 		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   677 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   678 
   679 		// Calling orig function Func110
   680 			new State_Func110(pTest110, ParamState_m110 (5)),
   681 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   682 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   683 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   684 
   685 		// back in hookfunc
   686 		new State_ObjAssignOp_Called(13),	// assign: orig_ret = temporary object in HookFunc's stack
   687 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   688 
   689 		// hookfunc is returning:
   690 		new State_ObjCCtor_Called(13),		// copy to temp object in our stack
   691 
   692 		// hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
   693 		new State_ObjODtor_Called(13),
   694 		new State_ObjODtor_Called(13),
   695 		new State_ObjODtor_Called(13),
   696 		
   697 		// we are in our function: assign 
   698 		new State_ObjAssignOp_Called(13),	// assign: obj13 = temporary object in our stack
   699 		new State_ObjODtor_Called(13),		// Func110: destruction of temporary object
   700 
   701 		NULL), "Test" "11" " Part2");
   702 	
   703 	CHECK_COND(obj13 == 0, "Test" "11" " Part 2.1");
   704 	g_Inside_LeafFunc = false;
   705 
   706 	THGM_REMOVE_HOOK(11, 1);
   707 
   708 	/* hook1, hook2 - hook3, hook4 */
   709 	THGM_ADD_HOOK(110, 1);
   710 	THGM_ADD_HOOK(110, 2);
   711 	THGM_ADD_HOOK(110, 3);
   712 	THGM_ADD_HOOK(110, 4);
   713 
   714 	PtrBuf_Clear();
   715 	obj13 = pTest110->Func(5);
   716 	g_Inside_LeafFunc = true;
   717 	CHECK_STATES((&g_States,
   718 		// HookFunc: construct orig_ret/override_ret/plugin_ret objects
   719 		new State_ObjOCtor_Called(13),
   720 		new State_ObjOCtor_Called(13),
   721 		new State_ObjOCtor_Called(13),
   722 
   723 		// Calling delegate1
   724 			new State_Deleg1_110(pTest110, 0, ParamState_m110 (5)),
   725 
   726 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   727 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   728 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   729 
   730 		// back in hookfunc
   731 		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   732 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   733 
   734 
   735 		// Calling delegate2
   736 			new State_Deleg2_110(pTest110, 1, ParamState_m110 (5)),
   737 
   738 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   739 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   740 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   741 
   742 		// back in hookfunc
   743 		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   744 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   745 
   746 		// hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret
   747 		new State_ObjAssignOp_Called(13),
   748 
   749 		// SUPERCEDE -> orig function is not called
   750 		// instead: orig_ret = override_ret
   751 		new State_ObjAssignOp_Called(13),
   752 
   753 		// Calling delegate3
   754 			new State_Deleg3_110(pTest110, 2, ParamState_m110 (5)),
   755 
   756 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   757 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   758 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   759 
   760 		// back in hookfunc
   761 		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   762 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   763 
   764 		// Calling delegate4
   765 			new State_Deleg4_110(pTest110, 3, ParamState_m110 (5)),
   766 
   767 			new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   768 			new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   769 			new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   770 
   771 		// back in hookfunc
   772 		new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   773 		new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   774 
   775 		// hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret (yes really, we overwrite the old value!)
   776 		new State_ObjAssignOp_Called(13),
   777 
   778 		// hookfunc is returning:
   779 		new State_ObjCCtor_Called(13),		// copy to temp object in our stack
   780 
   781 		// hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
   782 		new State_ObjODtor_Called(13),
   783 		new State_ObjODtor_Called(13),
   784 		new State_ObjODtor_Called(13),
   785 
   786 		// we are in our function: assign 
   787 		new State_ObjAssignOp_Called(13),	// assign: obj13 = temporary object in our stack
   788 		new State_ObjODtor_Called(13),		// Func110: destruction of temporary object
   789 
   790 		NULL), "Test" "11" " Part3");
   791 
   792 	CHECK_COND(obj13 == 4, "Test" "11" " Part 3.1");
   793 	g_Inside_LeafFunc = false;
   794 
   795 	THGM_REMOVE_HOOK(110, 1);
   796 	THGM_REMOVE_HOOK(110, 2);
   797 	THGM_REMOVE_HOOK(110, 3);
   798 	THGM_REMOVE_HOOK(110, 4);
   799 
   800 
   801 	// RefRet
   802 	THGM_DO_TEST(111, ());
   803 
   804 	// Shutdown now!
   805 	// If we don't SH will auto-shutdown _after_ genc's destructor is called
   806 	//  -> crash
   807 
   808 	Test_CompleteShutdown(g_SHPtr);
   809 
   810 	CHECK_COND(sizeof(PodRet1) == 1, "WTF!");
   811 	CHECK_COND(sizeof(PodRet4) == 4, "WTF!");
   812 	CHECK_COND(sizeof(PodRet8) == 8, "WTF!");
   813 	CHECK_COND(sizeof(PodRet13) == 13, "WTF!");
   814 
   815 	return true;
   816 }
   817 
   818 bool TestCPageAlloc(std::string &error)
   819 {
   820 	using namespace SourceHook;
   821 
   822 	CPageAlloc alloc;
   823 	int i;
   824 	size_t ps = alloc.GetPageSize();
   825 
   826 	char *test1[8];
   827 
   828 	for (i = 0; i < 8; ++i)
   829 		test1[i] = (char*) alloc.Alloc(ps / 4);
   830 
   831 	CHECK_COND(test1[1] == test1[0] + ps/4, "Part 1.1");
   832 	CHECK_COND(test1[2] == test1[1] + ps/4, "Part 1.2");
   833 	CHECK_COND(test1[3] == test1[2] + ps/4, "Part 1.3");
   834 
   835 	CHECK_COND(test1[5] == test1[4] + ps/4, "Part 1.4");
   836 	CHECK_COND(test1[6] == test1[5] + ps/4, "Part 1.5");
   837 	CHECK_COND(test1[7] == test1[6] + ps/4, "Part 1.6");
   838 
   839 	void *test2 = alloc.Alloc(ps * 3);
   840 
   841 	alloc.SetRW(test2);
   842 	memset(test2, 0, ps * 3);			// should not crash :)
   843 	alloc.SetRE(test2);
   844 
   845 	alloc.Free(test2);
   846 
   847 	// Dealloc a ps/4 block and place two ps/8 blocks into it
   848 	alloc.Free(test1[2]);
   849 
   850 	char *test3[2];
   851 	test3[0] = (char*) alloc.Alloc(ps / 8);
   852 	test3[1] = (char*) alloc.Alloc(ps / 8);
   853 
   854 	CHECK_COND(test3[0] == test1[2], "Part 2.1");
   855 	CHECK_COND(test3[1] == test1[2] + ps/8, "Part 2.2");
   856 
   857 	// Isolated
   858 	char *test4[8];
   859 	for (i = 0; i < 8; ++i)
   860 		test4[i] = (char*) alloc.AllocIsolated(ps / 4);
   861 
   862 	// -> The difference is at least one page
   863 	CHECK_COND(static_cast<size_t>(abs(test4[1] - test4[0])) >= ps, "Part 3.1");
   864 	CHECK_COND(static_cast<size_t>(abs(test4[2] - test4[1])) >= ps, "Part 3.2");
   865 	CHECK_COND(static_cast<size_t>(abs(test4[3] - test4[2])) >= ps, "Part 3.3");
   866 
   867 	CHECK_COND(static_cast<size_t>(abs(test4[5] - test4[4])) >= ps, "Part 3.4");
   868 	CHECK_COND(static_cast<size_t>(abs(test4[6] - test4[5])) >= ps, "Part 3.5");
   869 	CHECK_COND(static_cast<size_t>(abs(test4[7] - test4[6])) >= ps, "Part 3.6");
   870 
   871 	// Thus i can set everything except for test4[2] to RE and still write to test4[2]
   872 
   873 	alloc.SetRW(test4[2]);
   874 
   875 	alloc.SetRE(test4[0]);
   876 	alloc.SetRE(test4[1]);
   877 	alloc.SetRE(test4[3]);
   878 	alloc.SetRE(test4[4]);
   879 	alloc.SetRE(test4[5]);
   880 	alloc.SetRE(test4[6]);
   881 	alloc.SetRE(test4[7]);
   882 
   883 	memset((void*)test4[2], 0, ps / 4);
   884 
   885 	return true;
   886 }