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