core/sourcehook/test/testhookmangen.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Sat May 26 17:07:26 2012 -0400 (2012-05-26)
changeset 780 35f2a207badf
parent 539 db914129ae01
permissions -rw-r--r--
Added support for CS: Global Offensive (bug 5228, r=DS).
     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 #include "sourcehook_pibuilder.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 	SourceHook::IHookManagerAutoGen *g_HMAGPtr;
    27 
    28 	// PtrBuf(ptr) gives ptrs unique numbers
    29 	// in the order they appear
    30 	SourceHook::List<const void*> g_PtrHash;
    31 
    32 	bool g_Inside_LeafFunc = false;			// inside a hook or a func
    33 	bool g_Silent_CtorDtor = false;			// inside a hook or a func
    34 
    35 
    36 	// POD / Object types
    37 	template <int MYSIZE>
    38 	struct POD
    39 	{
    40 		char x[MYSIZE];
    41 
    42 		bool operator==(const POD<MYSIZE> &other)
    43 		{
    44 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
    45 		}
    46 
    47 		bool operator==(char other)
    48 		{
    49 			for (int i = 0; i < MYSIZE; ++i)
    50 			{
    51 				if (x[i] != other)
    52 					return false;
    53 			}
    54 			return true;
    55 		}
    56 	};
    57 
    58 	template <int MYSIZE>
    59 		std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
    60 	{
    61 		os << "Some POD!";
    62 		return os;
    63 	}
    64 
    65 	MAKE_STATE_1(State_ObjOCtor_Called, int /*MYSIZE*/);
    66 	MAKE_STATE_1(State_ObjCCtor_Called, int /*MYSIZE*/);
    67 	MAKE_STATE_1(State_ObjODtor_Called, int /*MYSIZE*/);
    68 	MAKE_STATE_1(State_ObjAssignOp_Called, int /*MYSIZE*/);
    69 
    70 	template <int MYSIZE>
    71 	struct Object
    72 	{
    73 		char x[MYSIZE];
    74 
    75 		Object(char initch)
    76 		{
    77 			memset(reinterpret_cast<void*>(x), initch, MYSIZE);
    78 			if (!g_Inside_LeafFunc && !g_Silent_CtorDtor)
    79 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
    80 		}
    81 
    82 		Object()
    83 		{
    84 			if (!g_Inside_LeafFunc && !g_Silent_CtorDtor)
    85 				ADD_STATE(State_ObjOCtor_Called(MYSIZE));
    86 		}
    87 
    88 		Object(const Object<MYSIZE> & other)
    89 		{
    90 			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
    91 			if (!g_Inside_LeafFunc && !g_Silent_CtorDtor)
    92 				ADD_STATE(State_ObjCCtor_Called(MYSIZE));
    93 		}
    94 
    95 		~Object()
    96 		{
    97 			if (!g_Inside_LeafFunc && !g_Silent_CtorDtor)
    98 				ADD_STATE(State_ObjODtor_Called(MYSIZE));
    99 		}
   100 
   101 		Object & operator = (const Object &other)
   102 		{
   103 			if (!g_Inside_LeafFunc && !g_Silent_CtorDtor)
   104 				ADD_STATE(State_ObjAssignOp_Called(MYSIZE));
   105 
   106 			memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
   107 
   108 			return *this;
   109 		}
   110 
   111 		bool operator==(const Object<MYSIZE> &other)
   112 		{
   113 			return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
   114 		}
   115 	};
   116 
   117 	template <int MYSIZE>
   118 	std::ostream& operator <<(std::ostream &os, const Object<MYSIZE> &obj)
   119 	{
   120 		os << "Some Obj" << static_cast<int>(obj.x[0]);
   121 		return os;
   122 	}
   123 
   124 	// Because of some weird bug in MSVC < 1400
   125 #define MAKE_PODRET(size) \
   126 	struct PodRet##size \
   127 	{ \
   128 		POD<size> actPod; \
   129 		bool operator==(const PodRet##size &other) { return actPod == other.actPod; } \
   130 		bool operator==(char other) { return actPod == other; } \
   131 	}; \
   132 	std::ostream& operator <<(std::ostream &os, const PodRet##size &obj) \
   133 	{ \
   134 		os << obj.actPod; \
   135 		return os; \
   136 	} \
   137 	template <> struct MakeRet< PodRet##size > \
   138 	{ \
   139 		static PodRet##size Do(int a) \
   140 		{ \
   141 			PodRet##size x; \
   142 			memset(reinterpret_cast<void*>(x.actPod.x), a, size); \
   143 			return x; \
   144 		} \
   145 	};
   146 
   147 #define MAKE_OBJRET(size) \
   148 	struct ObjRet##size \
   149 	{ \
   150 		Object<size> actObj; \
   151 		bool operator==(const ObjRet##size &other) { return actObj == other.actObj; } \
   152 		bool operator==(char other) { return actObj == other; } \
   153 	}; \
   154 	std::ostream& operator <<(std::ostream &os, const ObjRet##size &obj) \
   155 	{ \
   156 		os << obj.actObj; \
   157 		return os; \
   158 	} \
   159 	template <> struct MakeRet< ObjRet##size > \
   160 	{ \
   161 		static ObjRet##size Do(int a) NO_OPTIMIZE \
   162 		{ \
   163 			ObjRet##size *x = new ObjRet##size; /* ptr: otherwise gcc optimizes away the temp obj */ \
   164 			CAutoPtrDestruction< ObjRet##size > apd(x); \
   165 			memset(reinterpret_cast<void*>(x->actObj.x), a, size); \
   166 			return *x; \
   167 		} \
   168 	};
   169 
   170 
   171 
   172 	// "Increment" something:  (reproducible change for recall tests)
   173 	//  integer: ++
   174 	//  float: += 1.3
   175 	//  pod/object: x[i]++, 0 <= i < MYSIZE
   176 	template <class T>
   177 	struct Increment
   178 	{
   179 		static void Incr(T &what)	
   180 		{
   181 			++what;
   182 		}
   183 	};
   184 
   185 	template<>
   186 	struct Increment<float>
   187 	{
   188 		static void Incr(float &what)	
   189 		{
   190 			what += 1.3f;
   191 		}
   192 	};
   193 
   194 	template<>
   195 	struct Increment<double>
   196 	{
   197 		static void Incr(double &what)	
   198 		{
   199 			what += 1.3;
   200 		}
   201 	};
   202 
   203 	template<int MYSIZE>
   204 	struct Increment< POD<MYSIZE> >
   205 	{
   206 		static void Incr(POD<MYSIZE> &what)	
   207 		{
   208 			for (int i = 0; i < MYSIZE; ++i)
   209 				++ what.x[i];
   210 		}
   211 	};
   212 
   213 	template<int MYSIZE>
   214 	struct Increment< Object<MYSIZE> >
   215 	{
   216 		static void Incr(Object<MYSIZE> &what)	
   217 		{
   218 			for (int i = 0; i < MYSIZE; ++i)
   219 				++ what.x[i];
   220 		}
   221 	};
   222 
   223 	template<>
   224 	struct Increment<std::string>
   225 	{
   226 		static void Incr(std::string &what)
   227 		{
   228 			what += "!";
   229 		}
   230 	};
   231 
   232 	#include "testhookmangen.h"
   233 
   234 	template <class T>
   235 	int PtrBuf(T ptr)
   236 	{
   237 		int a = 0;
   238 
   239 		const void *vptr = reinterpret_cast<const void*>(ptr);
   240 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end(); ++iter)
   241 		{
   242 			if (*iter == vptr)
   243 				return a;
   244 			else
   245 				++a;
   246 		}
   247 		g_PtrHash.push_back(vptr);
   248 		return static_cast<int>(g_PtrHash.size()) - 1;
   249 	}
   250 
   251 	template <class T>
   252 	T PtrBufPtr(T ptr)
   253 	{
   254 		PtrBuf(ptr);
   255 		return ptr;
   256 	}
   257 
   258 	void PtrBuf_Clear(int leave_in = 0)
   259 	{
   260 		for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end();)
   261 		{
   262 			if (--leave_in < 0)
   263 				iter = g_PtrHash.erase(iter);
   264 			else
   265 				++iter;
   266 		}
   267 	}
   268 
   269 	// MyDelegate base class for other delegates
   270 	class MyDelegate : public SourceHook::ISHDelegate
   271 	{
   272 		// Unneeded
   273 		//  because we don't use old SH_REMOVE_HOOK syntax
   274 		virtual bool IsEqual(SourceHook::ISHDelegate *pOtherDeleg)
   275 		{
   276 			return false;
   277 		}
   278 
   279 		virtual void DeleteThis()
   280 		{
   281 			delete this;
   282 		}
   283 	};
   284 
   285 	THGM_MAKE_TEST0_void(0);
   286 	THGM_SETUP_PI0(0);
   287 
   288 	THGM_MAKE_TEST1_void(1, char);
   289 	THGM_SETUP_PI1(1, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   290 
   291 	THGM_MAKE_TEST1_void(2, short);
   292 	THGM_SETUP_PI1(2, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   293 
   294 	THGM_MAKE_TEST1_void(3, int);
   295 	THGM_SETUP_PI1(3, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   296 
   297 	THGM_MAKE_TEST1_void(4, float);
   298 	THGM_SETUP_PI1(4, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   299 
   300 	THGM_MAKE_TEST1_void(5, double);
   301 	THGM_SETUP_PI1(5, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   302 	
   303 	THGM_MAKE_TEST5_void(6, char, short, int, float, double);
   304 	THGM_SETUP_PI5(6,
   305 		char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   306 		short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   307 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   308 		float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal,
   309 		double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal
   310 		);
   311 
   312 	THGM_MAKE_TEST2_void(7, char&, double&);
   313 	THGM_SETUP_PI2(7,
   314 		char&, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByRef,
   315 		double&, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByRef
   316 		);
   317 
   318 	THGM_MAKE_TEST1_void(8, POD<7>);
   319 	THGM_SETUP_PI1(8, POD<7>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   320 
   321 	THGM_MAKE_TEST1_void(9, POD<600>);
   322 	THGM_SETUP_PI1(9, POD<600>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   323 
   324 	THGM_MAKE_TEST1_void(10, POD<600> &);
   325 	THGM_SETUP_PI1(10, POD<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef);
   326 
   327 	THGM_MAKE_TEST2_void(11, Object<3>, Object<600>&);
   328 	THGM_SETUP_PI2(11,
   329 		Object<3>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor,
   330 		Object<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor
   331 		);
   332 
   333 	THGM_MAKE_TEST0(101, char);
   334 	THGM_SETUP_PI0(101);
   335 	THGM_SETUP_RI(101, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   336 
   337 	THGM_MAKE_TEST0(102, short);
   338 	THGM_SETUP_PI0(102);
   339 	THGM_SETUP_RI(102, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   340 
   341 	THGM_MAKE_TEST0(103, int);
   342 	THGM_SETUP_PI0(103);
   343 	THGM_SETUP_RI(103, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   344 
   345 	THGM_MAKE_TEST0(104, float);
   346 	THGM_SETUP_PI0(104);
   347 	THGM_SETUP_RI(104, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   348 
   349 	THGM_MAKE_TEST0(105, double);
   350 	THGM_SETUP_PI0(105);
   351 	THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   352 
   353 	// pod 1-13
   354 	MAKE_PODRET(1);
   355 	THGM_MAKE_TEST1(106, PodRet1, int);
   356 	THGM_SETUP_PI1(106,
   357 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   358 		);
   359 	THGM_SETUP_RI(106, PodRet1, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   360 
   361 	MAKE_PODRET(4);
   362 	THGM_MAKE_TEST1(107, PodRet4, int);
   363 	THGM_SETUP_PI1(107,
   364 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   365 		);
   366 	THGM_SETUP_RI(107, PodRet4, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   367 
   368 	MAKE_PODRET(8);
   369 	THGM_MAKE_TEST1(108, PodRet8, int);
   370 	THGM_SETUP_PI1(108,
   371 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   372 		);
   373 	THGM_SETUP_RI(108, PodRet8, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   374 
   375 	MAKE_PODRET(13);
   376 	THGM_MAKE_TEST1(109, PodRet13, int);
   377 	THGM_SETUP_PI1(109,
   378 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   379 		);
   380 	THGM_SETUP_RI(109, PodRet13, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   381 
   382 	MAKE_OBJRET(13);
   383 	THGM_MAKE_TEST1(110, ObjRet13, int);
   384 	THGM_SETUP_PI1(110,
   385 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   386 		);
   387 	THGM_SETUP_RI(110, ObjRet13, SourceHook::PassInfo::PassType_Object,
   388 		SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
   389 		SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
   390 
   391 	MAKE_OBJRET(111);
   392 	ObjRet111 g_O111_0;
   393 	ObjRet111 g_O111_1;
   394 	ObjRet111 g_O111_2;
   395 	ObjRet111 g_O111_3;
   396 	ObjRet111 g_O111_4;
   397 
   398 	template <>
   399 	struct MakeRet< ObjRet111& >
   400 	{
   401 		static ObjRet111 &Do(int a)
   402 		{
   403 			switch (a)
   404 			{
   405 			case 0:
   406 				return g_O111_0;
   407 			case 1:
   408 				return g_O111_1;
   409 			case 2:
   410 				return g_O111_2;
   411 			case 3:
   412 				return g_O111_3;
   413 			default:
   414 				return g_O111_4;
   415 			}
   416 		}
   417 	};
   418 
   419 	THGM_MAKE_TEST0(111, ObjRet111& );
   420 	THGM_SETUP_PI0(111);
   421 	THGM_SETUP_RI(111, ObjRet111& , SourceHook::PassInfo::PassType_Object,
   422 		SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
   423 		SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
   424 
   425 
   426 	THGM_MAKE_TEST3_void(150, int, double, int);
   427 	THGM_SETUP_PI3(150,
   428 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   429 		double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal,
   430 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   431 
   432 	// vafmt tests
   433 	THGM_MAKE_TEST0_vafmt_void(200);
   434 	THGM_SETUP_PI0(200);
   435 
   436 	THGM_MAKE_TEST1_vafmt_void(201, char);
   437 	THGM_SETUP_PI1(201, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   438 
   439 	THGM_MAKE_TEST1_vafmt_void(203, int);
   440 	THGM_SETUP_PI1(203, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   441 	
   442 	THGM_MAKE_TEST5_vafmt_void(206, char, short, int, float, double);
   443 	THGM_SETUP_PI5(206,
   444 		char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   445 		short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   446 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
   447 		float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal,
   448 		double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal
   449 		);
   450 
   451 	THGM_MAKE_TEST2_vafmt_void(207, char&, double&);
   452 	THGM_SETUP_PI2(207,
   453 		char&, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByRef,
   454 		double&, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByRef
   455 		);
   456 
   457 	THGM_MAKE_TEST1_vafmt_void(208, POD<7>);
   458 	THGM_SETUP_PI1(208, POD<7>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   459 
   460 	THGM_MAKE_TEST1_vafmt_void(210, POD<600> &);
   461 	THGM_SETUP_PI1(210, POD<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef)
   462 	
   463 	THGM_MAKE_TEST1_vafmt(211, int, int);
   464 	THGM_SETUP_PI1(211,
   465 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   466 		);
   467 	THGM_SETUP_RI(211, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
   468 
   469 	THGM_MAKE_TEST1_vafmt(212, double, int);
   470 	THGM_SETUP_PI1(212,
   471 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   472 		);
   473 	THGM_SETUP_RI(212, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
   474 
   475 	THGM_MAKE_TEST1_vafmt(213, PodRet8, int);
   476 	THGM_SETUP_PI1(213,
   477 		int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal
   478 		);
   479 	THGM_SETUP_RI(213, PodRet8, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
   480 
   481 	THGM_MAKE_TEST1_vafmt_void(214, Object<133>);
   482 	THGM_SETUP_PI1(214, Object<133>, SourceHook::PassInfo::PassType_Object,
   483 		SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor |
   484 		SourceHook::PassInfo::PassFlag_CCtor | SourceHook::PassInfo::PassFlag_AssignOp);
   485 
   486 
   487 	MAKE_STATE(State_Hello_Func4_Called);
   488 	MAKE_STATE(State_Hello_Func79_Called);
   489 
   490 	MAKE_STATE(State_Hello_Func4_PreHook);
   491 	MAKE_STATE(State_Hello_Func79_PreHook);
   492 
   493 	// Test for larger vtable indices
   494 	class Hello
   495 	{
   496 	public:
   497 		virtual void Func0() {}
   498 		virtual void Func1() {}
   499 		virtual void Func2() {}
   500 		virtual void Func3() {}
   501 		virtual void Func4()
   502 		{
   503 			ADD_STATE(State_Hello_Func4_Called);
   504 		}
   505 		virtual void Func5() {}
   506 		virtual void Func6() {}
   507 		virtual void Func7() {}
   508 		virtual void Func8() {}
   509 		virtual void Func9() {}
   510 		virtual void Func10() {}
   511 		virtual void Func11() {}
   512 		virtual void Func12() {}
   513 		virtual void Func13() {}
   514 		virtual void Func14() {}
   515 		virtual void Func15() {}
   516 		virtual void Func16() {}
   517 		virtual void Func17() {}
   518 		virtual void Func18() {}
   519 		virtual void Func19() {}
   520 		virtual void Func20() {}
   521 		virtual void Func21() {}
   522 		virtual void Func22() {}
   523 		virtual void Func23() {}
   524 		virtual void Func24() {}
   525 		virtual void Func25() {}
   526 		virtual void Func26() {}
   527 		virtual void Func27() {}
   528 		virtual void Func28() {}
   529 		virtual void Func29() {}
   530 		virtual void Func30() {}
   531 		virtual void Func31() {}
   532 		virtual void Func32() {}
   533 		virtual void Func33() {}
   534 		virtual void Func34() {}
   535 		virtual void Func35() {}
   536 		virtual void Func36() {}
   537 		virtual void Func37() {}
   538 		virtual void Func38() {}
   539 		virtual void Func39() {}
   540 		virtual void Func40() {}
   541 		virtual void Func41() {}
   542 		virtual void Func42() {}
   543 		virtual void Func43() {}
   544 		virtual void Func44() {}
   545 		virtual void Func45() {}
   546 		virtual void Func46() {}
   547 		virtual void Func47() {}
   548 		virtual void Func48() {}
   549 		virtual void Func49() {}
   550 		virtual void Func50() {}
   551 		virtual void Func51() {}
   552 		virtual void Func52() {}
   553 		virtual void Func53() {}
   554 		virtual void Func54() {}
   555 		virtual void Func55() {}
   556 		virtual void Func56() {}
   557 		virtual void Func57() {}
   558 		virtual void Func58() {}
   559 		virtual void Func59() {}
   560 		virtual void Func60() {}
   561 		virtual void Func61() {}
   562 		virtual void Func62() {}
   563 		virtual void Func63() {}
   564 		virtual void Func64() {}
   565 		virtual void Func65() {}
   566 		virtual void Func66() {}
   567 		virtual void Func67() {}
   568 		virtual void Func68() {}
   569 		virtual void Func69() {}
   570 		virtual void Func70() {}
   571 		virtual void Func71() {}
   572 		virtual void Func72() {}
   573 		virtual void Func73() {}
   574 		virtual void Func74() {}
   575 		virtual void Func75() {}
   576 		virtual void Func76() {}
   577 		virtual void Func77() {}
   578 		virtual void Func78() {}
   579 		virtual void Func79()
   580 		{
   581 			ADD_STATE(State_Hello_Func79_Called);
   582 		}
   583 	};
   584 	class Hello_Func4_Deleg : public MyDelegate
   585 	{
   586 		virtual void Func()
   587 		{
   588 			ADD_STATE(State_Hello_Func4_PreHook);
   589 		}
   590 	};
   591 	class Hello_Func79_Deleg : public MyDelegate
   592 	{
   593 		int a;
   594 		virtual void Func()
   595 		{
   596 			ADD_STATE(State_Hello_Func79_PreHook);
   597 		}
   598 	};
   599 
   600 	bool Tests1(std::string &error)
   601 	{
   602 		THGM_DO_TEST_void(0, ());
   603 
   604 		THGM_DO_TEST_void(1, (100));
   605 		
   606 		THGM_DO_TEST_void(2, (0x1F00));
   607 
   608 		THGM_DO_TEST_void(3, (0x1F000000));
   609 
   610 		THGM_DO_TEST_void(4, (0.5f));
   611 
   612 		THGM_DO_TEST_void(5, (5.5));
   613 
   614 		THGM_DO_TEST_void(6, (100, 0x1f00, 0x1f000000, 0.5f, 5.5));
   615 
   616 		return true;
   617 	}
   618 
   619 	bool Tests2(std::string &error)
   620 	{
   621 		char a = 5;
   622 		double b = 233.33;
   623 		THGM_DO_TEST_void(7, (a, b));
   624 
   625 		POD<7> pod7 = MakeRet< POD<7> >::Do(78);
   626 		THGM_DO_TEST_void(8, (pod7));
   627 
   628 		POD<600> pod600 = MakeRet< POD<600> >::Do(34);
   629 		THGM_DO_TEST_void(9, (pod600));
   630 		
   631 		THGM_DO_TEST_void(10, (pod600));
   632 
   633 		// Test11: Special: constructors/destructors
   634 		PtrBuf_Clear();
   635 		Object<3> *obj3_real = new Object<3>(33);
   636 		Object<600> *obj600_real = new Object<600>(21);
   637 
   638 		Object<3> & obj3 = *obj3_real;
   639 		Object<600> & obj600 = *obj600_real;
   640 
   641 		CHECK_STATES((&g_States,
   642 			new State_ObjOCtor_Called(3),
   643 			new State_ObjOCtor_Called(600),
   644 			NULL), "Test11 Part0");
   645 
   646 		setuppi_11();
   647 		SourceHook::HookManagerPubFunc myhookman11 = g_HMAGPtr->MakeHookMan(protoinfo_11, 0, 0); \
   648 		CAutoReleaseHookMan arhm_11(myhookman11); \
   649 		int hook1_11, hook2_11, hook3_11, hook4_11;
   650 		
   651 		TestClass11 *pTest11 = new TestClass11;
   652 		CAutoPtrDestruction<TestClass11> apd11(pTest11);
   653 		
   654 		/* no hooks - no hooks */
   655 		PtrBuf_Clear();
   656 		pTest11->Func(obj3, obj600);
   657 
   658 		g_Inside_LeafFunc = true;
   659 		CHECK_STATES((&g_States,
   660 			new State_ObjCCtor_Called(3),
   661 			new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   662 			new State_ObjODtor_Called(3),
   663 			NULL), "Test" "11" " Part1");
   664 		g_Inside_LeafFunc = false;
   665 
   666 		/* hook1 - no hooks */
   667 		THGM_ADD_HOOK(11, 1);
   668 		
   669 		PtrBuf_Clear();
   670 		pTest11->Func(obj3, obj600);
   671 		g_Inside_LeafFunc = true;
   672 		CHECK_STATES((&g_States,
   673 			new State_ObjCCtor_Called(3),
   674 
   675 			new State_ObjCCtor_Called(3),
   676 			new State_Deleg_11(1, pTest11, 0, ParamState_m11 (obj3, obj600)),
   677 			new State_ObjODtor_Called(3),
   678 
   679 			new State_ObjCCtor_Called(3),
   680 			new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
   681 			new State_ObjODtor_Called(3),
   682 
   683 			new State_ObjODtor_Called(3),
   684 			NULL), "Test" "11" " Part2");
   685 		g_Inside_LeafFunc = false;
   686 
   687 		THGM_REMOVE_HOOK(11, 1);
   688 
   689 		/* hook1, hook2 - hook3, hook4 */
   690 		THGM_ADD_HOOK(11, 1);
   691 		THGM_ADD_HOOK(11, 2);
   692 		THGM_ADD_HOOK(11, 3);
   693 		THGM_ADD_HOOK(11, 4);
   694 		PtrBuf_Clear();
   695 		pTest11->Func(obj3, obj600);
   696 		g_Inside_LeafFunc = true;
   697 		CHECK_STATES((&g_States,
   698 			new State_ObjCCtor_Called(3),
   699 
   700 			new State_ObjCCtor_Called(3),
   701 			new	State_Deleg_11(1, pTest11, 0, ParamState_m11 (obj3, obj600)),
   702 			new State_ObjODtor_Called(3),
   703 
   704 			new State_ObjCCtor_Called(3),
   705 			new State_Deleg_11(2, pTest11, 1, ParamState_m11 (obj3, obj600)),
   706 			new State_ObjODtor_Called(3),
   707 
   708 			new State_ObjCCtor_Called(3),
   709 			new State_Deleg_11(3, pTest11, 2, ParamState_m11 (obj3, obj600)),
   710 			new State_ObjODtor_Called(3),
   711 
   712 			new State_ObjCCtor_Called(3),
   713 			new State_Deleg_11(4, pTest11, 3, ParamState_m11 (obj3, obj600)),
   714 			new State_ObjODtor_Called(3),
   715 
   716 			new State_ObjODtor_Called(3),
   717 		NULL), "Test" "11" " Part3");
   718 		g_Inside_LeafFunc = false;
   719 
   720 		/* hook1 - hook3, hook4 WITH RECALLS */
   721 		THGM_REMOVE_HOOK(11, 2);
   722 		PtrBuf_Clear();
   723 		TestClass11::ms_DoRecall = true;
   724 		pTest11->Func(obj3, obj600);
   725 		g_Inside_LeafFunc = true;
   726 		CHECK_STATES((&g_States,
   727 			new State_ObjCCtor_Called(3),
   728 
   729 			new State_ObjCCtor_Called(3),
   730 			new	State_Deleg_11(1, pTest11, 0 /* first deleg ptr */, ParamState_m11 (obj3, obj600)),
   731 
   732 			// recall !
   733 				// second hookfunc -> new copy
   734 				new State_ObjCCtor_Called(3),
   735 
   736 				// in second hookfunc now
   737 				//   -> calls orig func
   738 
   739 				new State_ObjCCtor_Called(3),
   740 				new State_Func11(pTest11, ParamState_m11 (obj3, obj600)(1)),
   741 				new State_ObjODtor_Called(3),
   742 
   743 				// calls first posthook
   744 				new State_ObjCCtor_Called(3),
   745 				new State_Deleg_11(3, pTest11, 1 /* second deleg ptr */, ParamState_m11 (obj3, obj600)(1)),
   746 
   747 				// recall!
   748 					// third hookfunc -> new copy
   749 					new State_ObjCCtor_Called(3),
   750 
   751 					// calls second posthook
   752 
   753 					new State_ObjCCtor_Called(3),
   754 					new State_Deleg_11(4, pTest11, 2 /* third deleg ptr */, ParamState_m11 (obj3, obj600)(2)),
   755 
   756 					// recall!
   757 						// fourth hookfunc -> new copy
   758 						new State_ObjCCtor_Called(3),
   759 
   760 						// has nothing to do though!
   761 
   762 						// fourth hookfunc done -> ret
   763 						new State_ObjODtor_Called(3),
   764 
   765 					// third hookfunc done -> ret
   766 					new State_ObjODtor_Called(3),
   767 					// ret from hookhandler which did the recall
   768 					new State_ObjODtor_Called(3),
   769 
   770 				// second hookfunc done -> ret
   771 				new State_ObjODtor_Called(3),
   772 				// ret from hookhandler which did the recall
   773 				new State_ObjODtor_Called(3),
   774 
   775 			// deleg1's instance
   776 			new State_ObjODtor_Called(3),
   777 			// first hookfunc done -> ret
   778 			new State_ObjODtor_Called(3),
   779 			NULL), "Test" "11" " Part4");
   780 		g_Inside_LeafFunc = false;
   781 
   782 		THGM_REMOVE_HOOK(11, 1);
   783 		THGM_REMOVE_HOOK(11, 3);
   784 		THGM_REMOVE_HOOK(11, 4);
   785 
   786 		delete obj3_real;
   787 		delete obj600_real;
   788 
   789 		CHECK_STATES((&g_States,
   790 			new State_ObjODtor_Called(3),
   791 			new State_ObjODtor_Called(600),
   792 			NULL), "Test11 Part100");
   793 
   794 		return true;
   795 	}
   796 
   797 	bool Tests3(std::string &error)
   798 	{
   799 		THGM_DO_TEST(101, ());
   800 
   801 		THGM_DO_TEST(102, ());
   802 
   803 		THGM_DO_TEST(103, ());
   804 
   805 		THGM_DO_TEST(104, ());
   806 
   807 		THGM_DO_TEST(105, ());
   808 
   809 		// pod returns
   810 		THGM_DO_TEST(106, (5));
   811 		THGM_DO_TEST(107, (5));
   812 		THGM_DO_TEST(108, (5));
   813 		THGM_DO_TEST(109, (5));
   814 
   815 		return true;
   816 	}
   817 
   818 	bool Tests4(std::string &error)
   819 	{
   820 		// Test110: Special: constructors/destructors on return
   821 		PtrBuf_Clear();
   822 		ObjRet13 *obj13_real = new ObjRet13;
   823 		ObjRet13 &obj13 = *obj13_real;
   824 
   825 		CHECK_STATES((&g_States,
   826 			new State_ObjOCtor_Called(13),
   827 			NULL), "Test110 Part0");
   828 
   829 		setuppi_110();
   830 		setupri_110();
   831 
   832 		SourceHook::HookManagerPubFunc myhookman110 = g_HMAGPtr->MakeHookMan(protoinfo_110, 0, 0); \
   833 		CAutoReleaseHookMan arhm_110(myhookman110); \
   834 		int hook1_110, hook2_110, hook3_110, hook4_110;
   835 
   836 		TestClass110 *pTest110 = new TestClass110;
   837 		CAutoPtrDestruction<TestClass110> apd110(pTest110);
   838 
   839 		/* no hooks - no hooks */
   840 		PtrBuf_Clear();
   841 		obj13 = pTest110->Func(5);
   842 
   843 		g_Inside_LeafFunc = true;
   844 		CHECK_STATES((&g_States,
   845 			new State_Func110(pTest110, ParamState_m110 (5)),
   846 			new State_ObjOCtor_Called(13),			// MakeRet: Construction of x
   847 			new State_ObjCCtor_Called(13),			// Return from MakeRet	-> construct temporary object in our stack
   848 			new State_ObjODtor_Called(13),			// MakeRet: Destruction of x
   849 
   850 			new State_ObjAssignOp_Called(13),		// assign: obj13 = temporary object in our stack
   851 			new State_ObjODtor_Called(13),			// Func110: destruction of temporary object
   852 			NULL), "Test" "110" " Part1");
   853 		g_Inside_LeafFunc = false;
   854 
   855 		/* hook1 - no hooks */
   856 		THGM_ADD_HOOK(110, 1);
   857 
   858 		PtrBuf_Clear();
   859 		obj13 = pTest110->Func(5);
   860 		g_Inside_LeafFunc = true;
   861 		CHECK_STATES((&g_States,
   862 			// HookFunc: construct orig_ret/override_ret/plugin_ret objects
   863 			new State_ObjOCtor_Called(13),
   864 			new State_ObjOCtor_Called(13),
   865 			new State_ObjOCtor_Called(13),
   866 
   867 			// Calling delegate
   868 				new State_Deleg_110(1, pTest110, 0, ParamState_m110 (5)),
   869 
   870 				new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   871 				new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   872 				new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   873 
   874 			// back in hookfunc
   875 			new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   876 			new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   877 
   878 			// Calling orig function Func110
   879 				new State_Func110(pTest110, ParamState_m110 (5)),
   880 				new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   881 				new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   882 				new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   883 
   884 			// back in hookfunc
   885 			new State_ObjAssignOp_Called(13),	// assign: orig_ret = temporary object in HookFunc's stack
   886 			new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   887 
   888 			// hookfunc is returning:
   889 			new State_ObjCCtor_Called(13),		// copy to temp object in our stack
   890 
   891 			// hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
   892 			new State_ObjODtor_Called(13),
   893 			new State_ObjODtor_Called(13),
   894 			new State_ObjODtor_Called(13),
   895 			
   896 			// we are in our function: assign 
   897 			new State_ObjAssignOp_Called(13),	// assign: obj13 = temporary object in our stack
   898 			new State_ObjODtor_Called(13),		// Func110: destruction of temporary object
   899 
   900 			NULL), "Test" "11" " Part2");
   901 		
   902 		CHECK_COND(obj13 == 0, "Test" "11" " Part 2.1");
   903 		g_Inside_LeafFunc = false;
   904 
   905 		THGM_REMOVE_HOOK(110, 1);
   906 
   907 		/* hook1, hook2 - hook3, hook4 */
   908 		THGM_ADD_HOOK(110, 1);
   909 		THGM_ADD_HOOK(110, 2);
   910 		THGM_ADD_HOOK(110, 3);
   911 		THGM_ADD_HOOK(110, 4);
   912 
   913 		PtrBuf_Clear();
   914 		obj13 = pTest110->Func(5);
   915 		g_Inside_LeafFunc = true;
   916 		CHECK_STATES((&g_States,
   917 			// HookFunc: construct orig_ret/override_ret/plugin_ret objects
   918 			new State_ObjOCtor_Called(13),
   919 			new State_ObjOCtor_Called(13),
   920 			new State_ObjOCtor_Called(13),
   921 
   922 			// Calling delegate1
   923 				new State_Deleg_110(1, pTest110, 0, ParamState_m110 (5)),
   924 
   925 				new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   926 				new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   927 				new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   928 
   929 			// back in hookfunc
   930 			new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   931 			new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   932 
   933 
   934 			// Calling delegate2
   935 				new State_Deleg_110(2, pTest110, 1, ParamState_m110 (5)),
   936 
   937 				new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   938 				new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   939 				new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   940 
   941 			// back in hookfunc
   942 			new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   943 			new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   944 
   945 			// hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret
   946 			new State_ObjAssignOp_Called(13),
   947 
   948 			// SUPERCEDE -> orig function is not called
   949 			// instead: orig_ret = override_ret
   950 			new State_ObjAssignOp_Called(13),
   951 
   952 			// Calling delegate3
   953 				new State_Deleg_110(3, pTest110, 2, ParamState_m110 (5)),
   954 
   955 				new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   956 				new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   957 				new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   958 
   959 			// back in hookfunc
   960 			new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   961 			new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   962 
   963 			// Calling delegate4
   964 				new State_Deleg_110(4, pTest110, 3, ParamState_m110 (5)),
   965 
   966 				new State_ObjOCtor_Called(13),	// MakeRet: Construction of x
   967 				new State_ObjCCtor_Called(13),	// Return from MakeRet	-> construct temporary object in HookFunc's stack
   968 				new State_ObjODtor_Called(13),	// MakeRet: Destruction of x
   969 
   970 			// back in hookfunc
   971 			new State_ObjAssignOp_Called(13),	// assign: plugin_ret = temporary object in HookFunc's stack
   972 			new State_ObjODtor_Called(13),		// destruction of temporary object in HookFunc's stack
   973 
   974 			// hookfunc finds out that the hook wanted to SUPERCEDE --> copy to override_Ret (yes really, we overwrite the old value!)
   975 			new State_ObjAssignOp_Called(13),
   976 
   977 			// hookfunc is returning:
   978 			new State_ObjCCtor_Called(13),		// copy to temp object in our stack
   979 
   980 			// hookfunc cleans up its stack -> destroys plugin_ret/override_ret/orig_ret
   981 			new State_ObjODtor_Called(13),
   982 			new State_ObjODtor_Called(13),
   983 			new State_ObjODtor_Called(13),
   984 
   985 			// we are in our function: assign 
   986 			new State_ObjAssignOp_Called(13),	// assign: obj13 = temporary object in our stack
   987 			new State_ObjODtor_Called(13),		// Func110: destruction of temporary object
   988 
   989 			NULL), "Test" "11" " Part3");
   990 
   991 		CHECK_COND(obj13 == 4, "Test" "11" " Part 3.1");
   992 		g_Inside_LeafFunc = false;
   993 
   994 		THGM_REMOVE_HOOK(110, 1);
   995 		THGM_REMOVE_HOOK(110, 2);
   996 		THGM_REMOVE_HOOK(110, 3);
   997 		THGM_REMOVE_HOOK(110, 4);
   998 
   999 		delete obj13_real;
  1000 
  1001 		CHECK_STATES((&g_States,
  1002 			new State_ObjODtor_Called(13),
  1003 			NULL), "Test110 Part100");
  1004 
  1005 
  1006 		THGM_DO_TEST_void(150, (5, 5.5, 6));
  1007 
  1008 		return true;
  1009 	}
  1010 
  1011 	bool Tests5(std::string &error)
  1012 	{
  1013 		// RefRet
  1014 		THGM_DO_TEST(111, ());
  1015 
  1016 		// Vafmt
  1017 
  1018 		THGM_DO_TEST_void(200, ("Hello %s%d%s", "BA", 1, "L!"));
  1019 
  1020 		THGM_DO_TEST_void(201, (100, "Hello %s%d%s", "BA", 1, "L!"));
  1021 		
  1022 		THGM_DO_TEST_void(203, (0x1F000000, "Hello %s%d%s", "BA", 1, "L!"));
  1023 
  1024 		THGM_DO_TEST_void(206, (100, 0x1f00, 0x1f000000, 0.5f, 5.5, "Hello %s%d%s", "BA", 1, "L!"));
  1025 
  1026 		char a = 5;
  1027 		double b = 233.33;
  1028 		THGM_DO_TEST_void(207, (a, b, "Hello %s%d%s", "BA", 1, "L!"));
  1029 
  1030 		POD<7> pod7 = MakeRet< POD<7> >::Do(78);
  1031 		THGM_DO_TEST_void(208, (pod7, "Hello %s%d%s", "BA", 1, "L!"));
  1032 	
  1033 		POD<600> pod600 = MakeRet< POD<600> >::Do(34);
  1034 		THGM_DO_TEST_void(210, (pod600, "Hello %s%d%s", "BA", 1, "L!"));
  1035 		
  1036 
  1037 		THGM_DO_TEST(211, (5, "Hello %s%d%s", "BA", 1, "L!"));
  1038 		THGM_DO_TEST(212, (5, "Hello %s%d%s", "BA", 1, "L!"));
  1039 		THGM_DO_TEST(213, (5, "Hello %s%d%s", "BA", 1, "L!"));
  1040 
  1041 		// Silence Object's ctors/dtors: we only want a forced byref param
  1042 		g_Silent_CtorDtor = true;
  1043 		Object<133> *myobj133 = new Object<133>;
  1044 		THGM_DO_TEST_void(214, (*myobj133, "Hello %s%d%s", "BA", 1, "L!"));
  1045 		delete myobj133;
  1046 		g_Silent_CtorDtor = false;
  1047 
  1048 		// Test for high vtable indices
  1049 		Hello *pHello = new Hello;
  1050 		CAutoPtrDestruction<Hello> apdHello(pHello);
  1051 		SourceHook::CProtoInfoBuilder helloPi(SourceHook::ProtoInfo::CallConv_ThisCall);
  1052 		SourceHook::HookManagerPubFunc helloHM_4 = g_HMAGPtr->MakeHookMan(helloPi, 0, 4);
  1053 		SourceHook::HookManagerPubFunc helloHM_79 = g_HMAGPtr->MakeHookMan(helloPi, 0, 79);
  1054 
  1055 		pHello->Func4();
  1056 		pHello->Func79();
  1057 		SH_CALL(pHello, &Hello::Func4)();
  1058 		SH_CALL(pHello, &Hello::Func79)();
  1059 		CHECK_STATES((&g_States,
  1060 			new State_Hello_Func4_Called,
  1061 			new State_Hello_Func79_Called,
  1062 			new State_Hello_Func4_Called,
  1063 			new State_Hello_Func79_Called,
  1064 			NULL), "Test" "Hello" " Part1");
  1065 
  1066 		int helloHook4 = g_SHPtr->AddHook(g_PLID, SourceHook::ISourceHook::Hook_Normal, reinterpret_cast<void*>(pHello),
  1067 			0, helloHM_4, new Hello_Func4_Deleg, false);
  1068 
  1069 		int helloHook79 = g_SHPtr->AddHook(g_PLID, SourceHook::ISourceHook::Hook_Normal, reinterpret_cast<void*>(pHello),
  1070 			0, helloHM_79, new Hello_Func79_Deleg, false);
  1071 
  1072 		pHello->Func4();
  1073 		pHello->Func79();
  1074 		SH_CALL(pHello, &Hello::Func4)();
  1075 		SH_CALL(pHello, &Hello::Func79)();
  1076 
  1077 		CHECK_STATES((&g_States,
  1078 			new State_Hello_Func4_PreHook,
  1079 			new State_Hello_Func4_Called,
  1080 			new State_Hello_Func79_PreHook,
  1081 			new State_Hello_Func79_Called,
  1082 			new State_Hello_Func4_Called,
  1083 			new State_Hello_Func79_Called,
  1084 			NULL), "Test" "Hello" " Part2");
  1085 
  1086 		g_SHPtr->RemoveHookByID(helloHook4);
  1087 		g_SHPtr->RemoveHookByID(helloHook79);
  1088 
  1089 		g_HMAGPtr->ReleaseHookMan(helloHM_4);
  1090 		g_HMAGPtr->ReleaseHookMan(helloHM_79);
  1091 
  1092 		return true;
  1093 	}
  1094 }
  1095 
  1096 
  1097 bool TestHookManGen(std::string &error)
  1098 {
  1099 	GET_SHPTR(g_SHPtr);
  1100 	GET_HMAG(g_HMAGPtr, g_SHPtr);
  1101 	g_PLID = 1337;
  1102 
  1103 	// 5 Global constructors (g_O111_*)
  1104 	CHECK_STATES((&g_States,
  1105 		new State_ObjOCtor_Called(111),
  1106 		new State_ObjOCtor_Called(111),
  1107 		new State_ObjOCtor_Called(111),
  1108 		new State_ObjOCtor_Called(111),
  1109 		new State_ObjOCtor_Called(111),
  1110 		NULL), "GlobCtors");
  1111 
  1112 	if (!Tests1(error))
  1113 		return false;
  1114 	if (!Tests2(error))
  1115 		return false;
  1116 	if (!Tests3(error))
  1117 		return false;
  1118 	if (!Tests4(error))
  1119 		return false;
  1120 	if (!Tests5(error))
  1121 		return false;
  1122 
  1123 	// Shutdown now!
  1124 	// If we don't SH will auto-shutdown _after_ genc's destructor is called
  1125 	//  -> crash
  1126 
  1127 	Test_CompleteShutdown(g_SHPtr);
  1128 
  1129 	CHECK_COND(sizeof(PodRet1) == 1, "WTF!");
  1130 	CHECK_COND(sizeof(PodRet4) == 4, "WTF!");
  1131 	CHECK_COND(sizeof(PodRet8) == 8, "WTF!");
  1132 	CHECK_COND(sizeof(PodRet13) == 13, "WTF!");
  1133 
  1134 	return true;
  1135 }
  1136 
  1137 bool TestCPageAlloc(std::string &error)
  1138 {
  1139 	using namespace SourceHook;
  1140 
  1141 	CPageAlloc alloc;
  1142 	int i;
  1143 	size_t ps = alloc.GetPageSize();
  1144 
  1145 	char *test1[8];
  1146 
  1147 	for (i = 0; i < 8; ++i)
  1148 		test1[i] = (char*) alloc.Alloc(ps / 4);
  1149 
  1150 	CHECK_COND(test1[1] == test1[0] + ps/4, "Part 1.1");
  1151 	CHECK_COND(test1[2] == test1[1] + ps/4, "Part 1.2");
  1152 	CHECK_COND(test1[3] == test1[2] + ps/4, "Part 1.3");
  1153 
  1154 	CHECK_COND(test1[5] == test1[4] + ps/4, "Part 1.4");
  1155 	CHECK_COND(test1[6] == test1[5] + ps/4, "Part 1.5");
  1156 	CHECK_COND(test1[7] == test1[6] + ps/4, "Part 1.6");
  1157 
  1158 	void *test2 = alloc.Alloc(ps * 3);
  1159 
  1160 	alloc.SetRW(test2);
  1161 	memset(test2, 0, ps * 3);			// should not crash :)
  1162 	alloc.SetRE(test2);
  1163 
  1164 	alloc.Free(test2);
  1165 
  1166 	// Dealloc a ps/4 block and place two ps/8 blocks into it
  1167 	alloc.Free(test1[2]);
  1168 
  1169 	char *test3[2];
  1170 	test3[0] = (char*) alloc.Alloc(ps / 8);
  1171 	test3[1] = (char*) alloc.Alloc(ps / 8);
  1172 
  1173 	CHECK_COND(test3[0] == test1[2], "Part 2.1");
  1174 	CHECK_COND(test3[1] == test1[2] + ps/8, "Part 2.2");
  1175 
  1176 	// Isolated
  1177 	char *test4[8];
  1178 	for (i = 0; i < 8; ++i)
  1179 		test4[i] = (char*) alloc.AllocIsolated(ps / 4);
  1180 
  1181 	// -> The difference is at least one page
  1182 	CHECK_COND(static_cast<size_t>(abs(test4[1] - test4[0])) >= ps, "Part 3.1");
  1183 	CHECK_COND(static_cast<size_t>(abs(test4[2] - test4[1])) >= ps, "Part 3.2");
  1184 	CHECK_COND(static_cast<size_t>(abs(test4[3] - test4[2])) >= ps, "Part 3.3");
  1185 
  1186 	CHECK_COND(static_cast<size_t>(abs(test4[5] - test4[4])) >= ps, "Part 3.4");
  1187 	CHECK_COND(static_cast<size_t>(abs(test4[6] - test4[5])) >= ps, "Part 3.5");
  1188 	CHECK_COND(static_cast<size_t>(abs(test4[7] - test4[6])) >= ps, "Part 3.6");
  1189 
  1190 	// Thus i can set everything except for test4[2] to RE and still write to test4[2]
  1191 
  1192 	alloc.SetRW(test4[2]);
  1193 
  1194 	alloc.SetRE(test4[0]);
  1195 	alloc.SetRE(test4[1]);
  1196 	alloc.SetRE(test4[3]);
  1197 	alloc.SetRE(test4[4]);
  1198 	alloc.SetRE(test4[5]);
  1199 	alloc.SetRE(test4[6]);
  1200 	alloc.SetRE(test4[7]);
  1201 
  1202 	memset((void*)test4[2], 0, ps / 4);
  1203 
  1204 	// alignement tests
  1205 	CPageAlloc alloc1(1);
  1206 	CPageAlloc alloc4(4);
  1207 	CPageAlloc alloc16(16);
  1208 
  1209 	void *last1 = alloc1.Alloc(1);
  1210 	void *last4 = alloc4.Alloc(4);
  1211 	void *last16 = alloc16.Alloc(16);
  1212 	void *cur;
  1213 	for (int i = 0; i < 20; ++i)
  1214 	{
  1215 		cur = alloc1.Alloc(1);
  1216 		CHECK_COND(reinterpret_cast<intptr_t>(cur) == reinterpret_cast<intptr_t>(last1) + 1, "Part 4.1");
  1217 		last1 = cur;
  1218 
  1219 		cur = alloc4.Alloc(1);
  1220 		CHECK_COND(reinterpret_cast<intptr_t>(cur) == reinterpret_cast<intptr_t>(last4) + 4, "Part 4.2");
  1221 		last4 = cur;
  1222 
  1223 		cur = alloc16.Alloc(1);
  1224 		CHECK_COND(reinterpret_cast<intptr_t>(cur) == reinterpret_cast<intptr_t>(last16) + 16, "Part 4.3");
  1225 		last16 = cur;
  1226 	}
  1227 
  1228 	alloc16.Free(alloc16.Alloc(1));
  1229 	return true;
  1230 }