sourcehook/test/testhookmangen.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sat Nov 10 16:32:16 2007 +0000 (2007-11-10)
branchhookman_autogen
changeset 456 9f19985c76aa
parent 455 deec66cea8cc
child 458 4a0c5136f5e1
permissions -rw-r--r--
Removed old g_Genc stuff and fixed GenContext::Equal
     1 #include <string>
     2 #include "sourcehook.h"
     3 #include "sourcehook_test.h"
     4 #include "testevents.h"
     5 #include "sh_memory.h"
     6 #include "sh_pagealloc.h"
     7 
     8 // TESTHOOKMANGEN
     9 // Test automatic hookman generation
    10 //  Tests void and non-void functions
    11 //  0 to 6 params:
    12 //   integer-type, float-type, plain-old-data struct and objects with ctors/dtors
    13 //   both byval and byref
    14 //  also tests ignore/supercede
    15 //  also tests recalls
    16 
    17 // :TODO: test override as well
    18 
    19 namespace
    20 {
    21 	StateList g_States;
    22 	SourceHook::ISourceHook *g_SHPtr;
    23 	SourceHook::Plugin g_PLID;
    24 
    25 	SourceHook::IHookManagerAutoGen *g_HMAGPtr;
    26 
    27 	// PtrBuf(ptr) gives ptrs unique numbers
    28 	// in the order they appear
    29 	SourceHook::List<const void*> g_PtrHash;
    30 
    31 	bool g_Inside_LeafFunc = false;			// inside a hook or a func
    32 
    33 
    34 	// POD / Object types
    35 	template <int MYSIZE>
    36 	struct POD
    37 	{
    38 		char x[MYSIZE];
    39 
    40 		bool operator==(const POD<MYSIZE> &other)
    41 		{
    42 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
    43 		}
    44 
    45 		bool operator==(char other)
    46 		{
    47 			for (int i = 0; i < MYSIZE; ++i)
    48 			{
    49 				if (x[i] != other)
    50 					return false;
    51 			}
    52 			return true;
    53 		}
    54 	};
    55 
    56 	template <int MYSIZE>
    57 		std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
    58 	{
    59 		os << "Some POD!";
    60 		return os;
    61 	}
    62 
    63 	MAKE_STATE_1(State_ObjOCtor_Called, int /*MYSIZE*/);
    64 	MAKE_STATE_1(State_ObjCCtor_Called, int /*MYSIZE*/);
    65 	MAKE_STATE_1(State_ObjODtor_Called, int /*MYSIZE*/);
    66 	MAKE_STATE_1(State_ObjAssignOp_Called, int /*MYSIZE*/);
    67 
    68 	template <int MYSIZE>
    69 	struct Object
    70 	{
    71 		char x[MYSIZE];
    72 
    73 		Object(char initch)
    74 		{
    75 			memset(reinterpret_cast<void*>(x), initch, MYSIZE);
    76 			if (!g_Inside_LeafFunc)
    77 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
    78 		}
    79 
    80 		Object()
    81 		{
    82 			if (!g_Inside_LeafFunc)
    83 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
    84 		}
    85 
    86 		Object(const Object<MYSIZE> & other)
    87 		{
    88 			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
    89 			if (!g_Inside_LeafFunc)
    90 				ADD_STATE(State_ObjCCtor_Called(MYSIZE));
    91 		}
    92 
    93 		~Object()
    94 		{
    95 			if (!g_Inside_LeafFunc)
    96 				ADD_STATE(State_ObjODtor_Called(MYSIZE));
    97 		}
    98 
    99 		Object & operator = (const Object &other)
   100 		{
   101 			if (!g_Inside_LeafFunc)
   102 				ADD_STATE(State_ObjAssignOp_Called(MYSIZE));
   103 
   104 			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
   105 
   106 			return *this;
   107 		}
   108 
   109 		bool operator==(const Object<MYSIZE> &other)
   110 		{
   111 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
   112 		}
   113 	};
   114 
   115 	template <int MYSIZE>
   116 	std::ostream& operator <<(std::ostream &os, const Object<MYSIZE> &obj)
   117 	{
   118 		os << "Some Obj" << static_cast<int>(obj.x[0]);
   119 		return os;
   120 	}
   121 
   122 	// Because of some weird bug in MSVC < 1400
   123 #define MAKE_PODRET(size) \
   124 	struct PodRet##size \
   125 	{ \
   126 		POD<size> actPod; \
   127 		bool operator==(const PodRet##size &other) { return actPod == other.actPod; } \
   128 		bool operator==(char other) { return actPod == other; } \
   129 	}; \
   130 	std::ostream& operator <<(std::ostream &os, const PodRet##size &obj) \
   131 	{ \
   132 		os << obj.actPod; \
   133 		return os; \
   134 	} \
   135 	template <> struct MakeRet< PodRet##size > \
   136 	{ \
   137 		static PodRet##size Do(int a) \
   138 		{ \
   139 			PodRet##size x; \
   140 			memset(reinterpret_cast<void*>(x.actPod.x), a, size); \
   141 			return x; \
   142 		} \
   143 	};
   144 
   145 #define MAKE_OBJRET(size) \
   146 	struct ObjRet##size \
   147 	{ \
   148 		Object<size> actObj; \
   149 		bool operator==(const ObjRet##size &other) { return actObj == other.actObj; } \
   150 		bool operator==(char other) { return actObj == other; } \
   151 	}; \
   152 	std::ostream& operator <<(std::ostream &os, const ObjRet##size &obj) \
   153 	{ \
   154 		os << obj.actObj; \
   155 		return os; \
   156 	} \
   157 	template <> struct MakeRet< ObjRet##size > \
   158 	{ \
   159 		static ObjRet##size Do(int a) NO_OPTIMIZE \
   160 		{ \
   161 			ObjRet##size *x = new ObjRet##size; /* ptr: otherwise gcc optimizes away the temp obj */ \
   162 			CAutoPtrDestruction< ObjRet##size > apd(x); \
   163 			memset(reinterpret_cast<void*>(x->actObj.x), a, size); \
   164 			return *x; \
   165 		} \
   166 	};
   167 
   168 
   169 
   170 	// "Increment" something:  (reproducible change for recall tests)
   171 	//  integer: ++
   172 	//  float: += 1.3
   173 	//  pod/object: x[i]++, 0 <= i < MYSIZE
   174 	template <class T>
   175 	struct Increment
   176 	{
   177 		static void Incr(T &what)	
   178 		{
   179 			++what;
   180 		}
   181 	};
   182 
   183 	template<>
   184 	struct Increment<float>
   185 	{
   186 		static void Incr(float &what)	
   187 		{
   188 			what += 1.3f;
   189 		}
   190 	};
   191 
   192 	template<>
   193 	struct Increment<double>
   194 	{
   195 		static void Incr(double &what)	
   196 		{
   197 			what += 1.3;
   198 		}
   199 	};
   200 
   201 	template<int MYSIZE>
   202 	struct Increment< POD<MYSIZE> >
   203 	{
   204 		static void Incr(POD<MYSIZE> &what)	
   205 		{
   206 			for (int i = 0; i < MYSIZE; ++i)
   207 				++ what.x[i];
   208 		}
   209 	};
   210 
   211 	template<int MYSIZE>
   212 	struct Increment< Object<MYSIZE> >
   213 	{
   214 		static void Incr(Object<MYSIZE> &what)	
   215 		{
   216 			for (int i = 0; i < MYSIZE; ++i)
   217 				++ what.x[i];
   218 		}
   219 	};
   220 
   221 	#include "testhookmangen.h"
   222 
   223 	template <class T>
   224 	int PtrBuf(T ptr)
   225 	{
   226 		int a = 0;
   227 
   228 		const void *vptr = reinterpret_cast<const void*>(ptr);
   229 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end(); ++iter)
   230 		{
   231 			if (*iter == vptr)
   232 				return a;
   233 			else
   234 				++a;
   235 		}
   236 		g_PtrHash.push_back(vptr);
   237 		return static_cast<int>(g_PtrHash.size()) - 1;
   238 	}
   239 
   240 	template <class T>
   241 	T PtrBufPtr(T ptr)
   242 	{
   243 		PtrBuf(ptr);
   244 		return ptr;
   245 	}
   246 
   247 	void PtrBuf_Clear(int leave_in = 0)
   248 	{
   249 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end();)
   250 		{
   251 			if (--leave_in < 0)
   252 				iter = g_PtrHash.erase(iter);
   253 			else
   254 				++iter;
   255 		}
   256 	}
   257 
   258 	// MyDelegate base class for other delegates
   259 	class MyDelegate : public SourceHook::ISHDelegate
   260 	{
   261 		// Unneeded
   262 		//  because we don't use old SH_REMOVE_HOOK syntax
   263 		virtual bool IsEqual(SourceHook::ISHDelegate *pOtherDeleg)
   264 		{
   265 			return false;
   266 		}
   267 
   268 		virtual void DeleteThis()
   269 		{
   270 			delete this;
   271 		}
   272 	};
   273 
   274 	THGM_MAKE_TEST0_void(0);
   275 	THGM_SETUP_PI0(0);
   276 
   277 	THGM_MAKE_TEST1_void(1, char);
   278 	THGM_SETUP_PI1(1, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   279 
   280 	THGM_MAKE_TEST1_void(2, short);
   281 	THGM_SETUP_PI1(2, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   282 
   283 	THGM_MAKE_TEST1_void(3, int);
   284 	THGM_SETUP_PI1(3, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   285 
   286 	THGM_MAKE_TEST1_void(4, float);
   287 	THGM_SETUP_PI1(4, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   288 
   289 	THGM_MAKE_TEST1_void(5, double);
   290 	THGM_SETUP_PI1(5, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   291 	
   292 	THGM_MAKE_TEST5_void(6, char, short, int, float, double);
   293 	THGM_SETUP_PI5(6,
   294 		char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   295 		short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   296 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   297 		float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal,
   298 		double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal
   299 		);
   300 
   301 	THGM_MAKE_TEST2_void(7, char&, double&);
   302 	THGM_SETUP_PI2(7,
   303 		char&, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByRef,
   304 		double&, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByRef
   305 		);
   306 
   307 	THGM_MAKE_TEST1_void(8, POD<7>);
   308 	THGM_SETUP_PI1(8, POD<7>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   309 
   310 	THGM_MAKE_TEST1_void(9, POD<600>);
   311 	THGM_SETUP_PI1(9, POD<600>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   312 
   313 	THGM_MAKE_TEST1_void(10, POD<600> &);
   314 	THGM_SETUP_PI1(10, POD<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef);
   315 
   316 	THGM_MAKE_TEST2_void(11, Object<3>, Object<600>&);
   317 	THGM_SETUP_PI2(11,
   318 		Object<3>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor,
   319 		Object<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor
   320 		);
   321 
   322 	THGM_MAKE_TEST0(101, char);
   323 	THGM_SETUP_PI0(101);
   324 	THGM_SETUP_RI(101, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   325 
   326 	THGM_MAKE_TEST0(102, short);
   327 	THGM_SETUP_PI0(102);
   328 	THGM_SETUP_RI(102, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   329 
   330 	THGM_MAKE_TEST0(103, int);
   331 	THGM_SETUP_PI0(103);
   332 	THGM_SETUP_RI(103, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   333 
   334 	THGM_MAKE_TEST0(104, float);
   335 	THGM_SETUP_PI0(104);
   336 	THGM_SETUP_RI(104, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   337 
   338 	THGM_MAKE_TEST0(105, double);
   339 	THGM_SETUP_PI0(105);
   340 	THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   341 
   342 	// pod 1-13
   343 	MAKE_PODRET(1);
   344 	THGM_MAKE_TEST1(106, PodRet1, int);
   345 	THGM_SETUP_PI1(106,
   346 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   347 		);
   348 	THGM_SETUP_RI(106, PodRet1, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   349 
   350 	MAKE_PODRET(4);
   351 	THGM_MAKE_TEST1(107, PodRet4, int);
   352 	THGM_SETUP_PI1(107,
   353 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   354 		);
   355 	THGM_SETUP_RI(107, PodRet4, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   356 
   357 	MAKE_PODRET(8);
   358 	THGM_MAKE_TEST1(108, PodRet8, int);
   359 	THGM_SETUP_PI1(108,
   360 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   361 		);
   362 	THGM_SETUP_RI(108, PodRet8, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   363 
   364 	MAKE_PODRET(13);
   365 	THGM_MAKE_TEST1(109, PodRet13, int);
   366 	THGM_SETUP_PI1(109,
   367 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   368 		);
   369 	THGM_SETUP_RI(109, PodRet13, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   370 
   371 	MAKE_OBJRET(13);
   372 	THGM_MAKE_TEST1(110, ObjRet13, int);
   373 	THGM_SETUP_PI1(110,
   374 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   375 		);
   376 	THGM_SETUP_RI(110, ObjRet13, SourceHook::PassInfo::PassType_Object,
   377 		SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
   378 		SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
   379 
   380 	MAKE_OBJRET(111);
   381 	ObjRet111 g_O111_0;
   382 	ObjRet111 g_O111_1;
   383 	ObjRet111 g_O111_2;
   384 	ObjRet111 g_O111_3;
   385 	ObjRet111 g_O111_4;
   386 
   387 	template <>
   388 	struct MakeRet< ObjRet111& >
   389 	{
   390 		static ObjRet111 &Do(int a)
   391 		{
   392 			switch (a)
   393 			{
   394 			case 0:
   395 				return g_O111_0;
   396 			case 1:
   397 				return g_O111_1;
   398 			case 2:
   399 				return g_O111_2;
   400 			case 3:
   401 				return g_O111_3;
   402 			default:
   403 				return g_O111_4;
   404 			}
   405 		}
   406 	};
   407 
   408 	THGM_MAKE_TEST0(111, ObjRet111& );
   409 	THGM_SETUP_PI0(111);
   410 	THGM_SETUP_RI(111, ObjRet111& , SourceHook::PassInfo::PassType_Object,
   411 		SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
   412 		SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
   413 }
   414 
   415 bool TestHookManGen(std::string &error)
   416 {
   417 	GET_SHPTR(g_SHPtr);
   418 	GET_HMAG(g_HMAGPtr, g_SHPtr);
   419 	g_PLID = 1337;
   420 
   421 	// 5 Global constructors (g_O111_*)
   422 	CHECK_STATES((&g_States,
   423 		new State_ObjOCtor_Called(111),
   424 		new State_ObjOCtor_Called(111),
   425 		new State_ObjOCtor_Called(111),
   426 		new State_ObjOCtor_Called(111),
   427 		new State_ObjOCtor_Called(111),
   428 		NULL), "GlobCtors");
   429 
   430 	THGM_DO_TEST_void(0, ());
   431 
   432 	THGM_DO_TEST_void(1, (100));
   433 	
   434 	THGM_DO_TEST_void(2, (0x1F00));
   435 
   436 	THGM_DO_TEST_void(3, (0x1F000000));
   437 
   438 	THGM_DO_TEST_void(4, (0.5f));
   439 
   440 	THGM_DO_TEST_void(5, (5.5));
   441 
   442 	THGM_DO_TEST_void(6, (100, 0x1f00, 0x1f000000, 0.5f, 5.5));
   443 
   444 	char a = 5;
   445 	double b = 233.33;
   446 	THGM_DO_TEST_void(7, (a, b));
   447 
   448 	POD<7> pod7 = MakeRet< POD<7> >::Do(78);
   449 	THGM_DO_TEST_void(8, (pod7));
   450 
   451 	POD<600> pod600 = MakeRet< POD<600> >::Do(34);
   452 	THGM_DO_TEST_void(9, (pod600));
   453 	
   454 	THGM_DO_TEST_void(10, (pod600));
   455 
   456 	// Test11: Special: constructors/destructors
   457 	PtrBuf_Clear();
   458 	Object<3> obj3(33);
   459 	Object<600> obj600(21);
   460 
   461 	CHECK_STATES((&g_States,
   462 		new State_ObjOCtor_Called(3),
   463 		new State_ObjOCtor_Called(600),
   464 		NULL), "Test11 Part0");
   465 
   466 	setuppi_11();
   467 	SourceHook::HookManagerPubFunc myhookman11 = g_HMAGPtr->MakeHookMan(&protoinfo_11, 0, 0); \
   468 	CAutoReleaseHookMan arhm_11(myhookman11); \
   469 	int hook1_11, hook2_11, hook3_11, hook4_11;
   470 	
   471 	TestClass11 *pTest11 = new TestClass11;
   472 	CAutoPtrDestruction<TestClass11> apd11(pTest11);
   473 	
   474 	/* no hooks - no hooks */
   475 	PtrBuf_Clear();
   476 	pTest11->Func(obj3, obj600);
   477 
   478 	g_Inside_LeafFunc = true;
   479 	CHECK_STATES((&g_States,
   480 		new State_ObjCCtor_Called(3),
   481 		new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   482 		new State_ObjODtor_Called(3),
   483 		NULL), "Test" "11" " Part1");
   484 	g_Inside_LeafFunc = false;
   485 
   486 	/* hook1 - no hooks */
   487 	THGM_ADD_HOOK(11, 1);
   488 	
   489 	PtrBuf_Clear();
   490 	pTest11->Func(obj3, obj600);
   491 	g_Inside_LeafFunc = true;
   492 	CHECK_STATES((&g_States,
   493 		new State_ObjCCtor_Called(3),
   494 
   495 		new State_ObjCCtor_Called(3),
   496 		new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
   497 		new State_ObjODtor_Called(3),
   498 
   499 		new State_ObjCCtor_Called(3),
   500 		new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   501 		new State_ObjODtor_Called(3),
   502 
   503 		new State_ObjODtor_Called(3),
   504 		NULL), "Test" "11" " Part2");
   505 	g_Inside_LeafFunc = false;
   506 
   507 	THGM_REMOVE_HOOK(11, 1);
   508 
   509 	/* hook1, hook2 - hook3, hook4 */
   510 	THGM_ADD_HOOK(11, 1);
   511 	THGM_ADD_HOOK(11, 2);
   512 	THGM_ADD_HOOK(11, 3);
   513 	THGM_ADD_HOOK(11, 4);
   514 	PtrBuf_Clear();
   515 	pTest11->Func(obj3, obj600);
   516 	g_Inside_LeafFunc = true;
   517 	CHECK_STATES((&g_States,
   518 		new State_ObjCCtor_Called(3),
   519 
   520 		new State_ObjCCtor_Called(3),
   521 		new	State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
   522 		new State_ObjODtor_Called(3),
   523 
   524 		new State_ObjCCtor_Called(3),
   525 		new State_Deleg2_11(pTest11, 1, ParamState_m11 (obj3, obj600)),
   526 		new State_ObjODtor_Called(3),
   527 
   528 		new State_ObjCCtor_Called(3),
   529 		new State_Deleg3_11(pTest11, 2, ParamState_m11 (obj3, obj600)),
   530 		new State_ObjODtor_Called(3),
   531 
   532 		new State_ObjCCtor_Called(3),
   533 		new State_Deleg4_11(pTest11, 3, ParamState_m11 (obj3, obj600)),
   534 		new State_ObjODtor_Called(3),
   535 
   536 		new State_ObjODtor_Called(3),
   537 	NULL), "Test" "11" " Part3");
   538 	g_Inside_LeafFunc = false;
   539 
   540 	/* hook1 - hook3, hook4 WITH RECALLS */
   541 	THGM_REMOVE_HOOK(11, 2);
   542 	PtrBuf_Clear();
   543 	TestClass11::ms_DoRecall = true;
   544 	pTest11->Func(obj3, obj600);
   545 	g_Inside_LeafFunc = true;
   546 	CHECK_STATES((&g_States,
   547 		new State_ObjCCtor_Called(3),
   548 
   549 		new State_ObjCCtor_Called(3),
   550 		new	State_Deleg1_11(pTest11, 0 /* first deleg ptr */, ParamState_m11 (obj3, obj600)),
   551 
   552 		// recall !
   553 			// second hookfunc -> new copy
   554 			new State_ObjCCtor_Called(3),
   555 
   556 			// in second hookfunc now
   557 			//   -> calls orig func
   558 
   559 			new State_ObjCCtor_Called(3),
   560 			new State_Func11(pTest11, ParamState_m11 (obj3, obj600)(1)),
   561 			new State_ObjODtor_Called(3),
   562 
   563 			// calls first posthook
   564 			new State_ObjCCtor_Called(3),
   565 			new State_Deleg3_11(pTest11, 1 /* second deleg ptr */, ParamState_m11 (obj3, obj600)(1)),
   566 
   567 			// recall!
   568 				// third hookfunc -> new copy
   569 				new State_ObjCCtor_Called(3),
   570 
   571 				// calls second posthook
   572 
   573 				new State_ObjCCtor_Called(3),
   574 				new State_Deleg4_11(pTest11, 2 /* third deleg ptr */, ParamState_m11 (obj3, obj600)(2)),
   575 
   576 				// recall!
   577 					// fourth hookfunc -> new copy
   578 					new State_ObjCCtor_Called(3),
   579 
   580 					// has nothing to do though!
   581 
   582 					// fourth hookfunc done -> ret
   583 					new State_ObjODtor_Called(3),
   584 
   585 				// third hookfunc done -> ret
   586 				new State_ObjODtor_Called(3),
   587 				// ret from hookhandler which did the recall
   588 				new State_ObjODtor_Called(3),
   589 
   590 			// second hookfunc done -> ret
   591 			new State_ObjODtor_Called(3),
   592 			// ret from hookhandler which did the recall
   593 			new State_ObjODtor_Called(3),
   594 
   595 		// deleg1's instance
   596 		new State_ObjODtor_Called(3),
   597 		// first hookfunc done -> ret
   598 		new State_ObjODtor_Called(3),
   599 		NULL), "Test" "11" " Part4");
   600 	g_Inside_LeafFunc = false;
   601 
   602 	THGM_REMOVE_HOOK(11, 1);
   603 	THGM_REMOVE_HOOK(11, 3);
   604 	THGM_REMOVE_HOOK(11, 4);
   605 
   606 	THGM_DO_TEST(101, ());
   607 
   608 	THGM_DO_TEST(102, ());
   609 
   610 	THGM_DO_TEST(103, ());
   611 
   612 	THGM_DO_TEST(104, ());
   613 
   614 	THGM_DO_TEST(105, ());
   615 
   616 	// pod returns
   617 	THGM_DO_TEST(106, (5));
   618 	THGM_DO_TEST(107, (5));
   619 	THGM_DO_TEST(108, (5));
   620 	THGM_DO_TEST(109, (5));
   621 
   622 	// Test110: Special: constructors/destructors on return
   623 	PtrBuf_Clear();
   624 	ObjRet13 obj13;
   625 
   626 	CHECK_STATES((&g_States,
   627 		new State_ObjOCtor_Called(13),
   628 		NULL), "Test110 Part0");
   629 
   630 	setuppi_110();
   631 	setupri_110();
   632 
   633 	SourceHook::HookManagerPubFunc myhookman110 = g_HMAGPtr->MakeHookMan(&protoinfo_110, 0, 0); \
   634 	CAutoReleaseHookMan arhm_110(myhookman110); \
   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 }