1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcehook/test/testhookmangen.cpp Sun Oct 21 21:47:53 2007 +0000
1.3 @@ -0,0 +1,470 @@
1.4 +#include <string>
1.5 +#include "sourcehook.h"
1.6 +#include "sourcehook_test.h"
1.7 +#include "testevents.h"
1.8 +#include "sourcehook_hookmangen.h"
1.9 +
1.10 +// TESTHOOKMANGEN
1.11 +// Test automatic hookman generation
1.12 +// Tests void and non-void functions
1.13 +// 0 to 6 params:
1.14 +// integer-type, float-type, plain-old-data struct and objects with ctors/dtors
1.15 +// both byval and byref
1.16 +// also tests ignore/supercede
1.17 +// also tests recalls
1.18 +
1.19 +// :TODO: test override as well
1.20 +
1.21 +namespace
1.22 +{
1.23 + #include "testhookmangen.h"
1.24 +
1.25 + StateList g_States;
1.26 + SourceHook::ISourceHook *g_SHPtr;
1.27 + SourceHook::Plugin g_PLID;
1.28 +
1.29 + // PtrBuf(ptr) gives ptrs unique numbers
1.30 + // in the order they appear
1.31 + SourceHook::List<const void*> g_PtrHash;
1.32 +
1.33 + bool g_Inside_LeafFunc = false; // inside a hook or a func
1.34 +
1.35 + template <class T>
1.36 + int PtrBuf(T ptr)
1.37 + {
1.38 + int a = 0;
1.39 +
1.40 + const void *vptr = reinterpret_cast<const void*>(ptr);
1.41 + for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end(); ++iter)
1.42 + {
1.43 + if (*iter == vptr)
1.44 + return a;
1.45 + else
1.46 + ++a;
1.47 + }
1.48 + g_PtrHash.push_back(vptr);
1.49 + return static_cast<int>(g_PtrHash.size()) - 1;
1.50 + }
1.51 +
1.52 + template <class T>
1.53 + T PtrBufPtr(T ptr)
1.54 + {
1.55 + PtrBuf(ptr);
1.56 + return ptr;
1.57 + }
1.58 +
1.59 + void PtrBuf_Clear(int leave_in = 0)
1.60 + {
1.61 + for (SourceHook::List<const void*>::iterator iter = g_PtrHash.begin(); iter != g_PtrHash.end();)
1.62 + {
1.63 + if (--leave_in < 0)
1.64 + iter = g_PtrHash.erase(iter);
1.65 + else
1.66 + ++iter;
1.67 + }
1.68 + }
1.69 +
1.70 + // POD / Object types
1.71 + template <int MYSIZE>
1.72 + struct POD
1.73 + {
1.74 + char x[MYSIZE];
1.75 +
1.76 + bool operator==(const POD<MYSIZE> &other)
1.77 + {
1.78 + return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
1.79 + }
1.80 + };
1.81 +
1.82 + template <int MYSIZE>
1.83 + std::ostream& operator <<(std::ostream &os, const POD<MYSIZE> &obj)
1.84 + {
1.85 + os << "Some POD!";
1.86 + return os;
1.87 + }
1.88 +
1.89 + MAKE_STATE_1(State_ObjOCtor_Called, int /*MYSIZE*/);
1.90 + MAKE_STATE_1(State_ObjCCtor_Called, int /*MYSIZE*/);
1.91 + MAKE_STATE_1(State_ObjODtor_Called, int /*MYSIZE*/);
1.92 +
1.93 + template <int MYSIZE>
1.94 + struct Object
1.95 + {
1.96 + char x[MYSIZE];
1.97 +
1.98 + Object(char initch)
1.99 + {
1.100 + memset(reinterpret_cast<void*>(x), initch, MYSIZE);
1.101 + if (!g_Inside_LeafFunc)
1.102 + ADD_STATE(State_ObjOCtor_Called(MYSIZE));
1.103 + }
1.104 +
1.105 + Object()
1.106 + {
1.107 + if (!g_Inside_LeafFunc)
1.108 + ADD_STATE(State_ObjOCtor_Called(MYSIZE));
1.109 + }
1.110 +
1.111 + Object(const Object<MYSIZE> & other)
1.112 + {
1.113 + memcpy(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE);
1.114 + if (!g_Inside_LeafFunc)
1.115 + ADD_STATE(State_ObjCCtor_Called(MYSIZE));
1.116 + }
1.117 +
1.118 + ~Object()
1.119 + {
1.120 + if (!g_Inside_LeafFunc)
1.121 + ADD_STATE(State_ObjODtor_Called(MYSIZE));
1.122 + }
1.123 +
1.124 + bool operator==(const Object<MYSIZE> &other)
1.125 + {
1.126 + return memcmp(reinterpret_cast<void*>(x), reinterpret_cast<const void*>(other.x), MYSIZE) == 0;
1.127 + }
1.128 + };
1.129 +
1.130 + template <int MYSIZE>
1.131 + std::ostream& operator <<(std::ostream &os, const Object<MYSIZE> &obj)
1.132 + {
1.133 + os << "Some Obj!";
1.134 + return os;
1.135 + }
1.136 +
1.137 +
1.138 + // "Increment" something: (reproducible change for recall tests)
1.139 + // integer: ++
1.140 + // float: += 1.3
1.141 + // pod/object: x[i]++, 0 <= i < MYSIZE
1.142 + template <class T>
1.143 + struct Increment
1.144 + {
1.145 + static void Incr(T &what)
1.146 + {
1.147 + ++what;
1.148 + }
1.149 + };
1.150 +
1.151 + template<>
1.152 + struct Increment<float>
1.153 + {
1.154 + static void Incr(float &what)
1.155 + {
1.156 + what += 1.3f;
1.157 + }
1.158 + };
1.159 +
1.160 + template<>
1.161 + struct Increment<double>
1.162 + {
1.163 + static void Incr(double &what)
1.164 + {
1.165 + what += 1.3;
1.166 + }
1.167 + };
1.168 +
1.169 + template<int MYSIZE>
1.170 + struct Increment< POD<MYSIZE> >
1.171 + {
1.172 + static void Incr(POD<MYSIZE> &what)
1.173 + {
1.174 + for (int i = 0; i < MYSIZE; ++i)
1.175 + ++ what.x[i];
1.176 + }
1.177 + };
1.178 +
1.179 + template<int MYSIZE>
1.180 + struct Increment< Object<MYSIZE> >
1.181 + {
1.182 + static void Incr(Object<MYSIZE> &what)
1.183 + {
1.184 + for (int i = 0; i < MYSIZE; ++i)
1.185 + ++ what.x[i];
1.186 + }
1.187 + };
1.188 +
1.189 + // MyDelegate base class for other delegates
1.190 + class MyDelegate : public SourceHook::ISHDelegate
1.191 + {
1.192 + // Unneeded
1.193 + // because we don't use old SH_REMOVE_HOOK syntax
1.194 + virtual bool IsEqual(SourceHook::ISHDelegate *pOtherDeleg)
1.195 + {
1.196 + return false;
1.197 + }
1.198 +
1.199 + virtual void DeleteThis()
1.200 + {
1.201 + delete this;
1.202 + }
1.203 + };
1.204 +
1.205 + THGM_MAKE_TEST0_void(0);
1.206 + THGM_SETUP_PI0(0);
1.207 +
1.208 + THGM_MAKE_TEST1_void(1, char);
1.209 + THGM_SETUP_PI1(1, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
1.210 +
1.211 + THGM_MAKE_TEST1_void(2, short);
1.212 + THGM_SETUP_PI1(2, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
1.213 +
1.214 + THGM_MAKE_TEST1_void(3, int);
1.215 + THGM_SETUP_PI1(3, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
1.216 +
1.217 + THGM_MAKE_TEST1_void(4, float);
1.218 + THGM_SETUP_PI1(4, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
1.219 +
1.220 + THGM_MAKE_TEST1_void(5, double);
1.221 + THGM_SETUP_PI1(5, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
1.222 +
1.223 + THGM_MAKE_TEST5_void(6, char, short, int, float, double);
1.224 + THGM_SETUP_PI5(6,
1.225 + char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
1.226 + short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
1.227 + int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal,
1.228 + float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal,
1.229 + double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal
1.230 + );
1.231 +
1.232 + THGM_MAKE_TEST2_void(7, char&, double&);
1.233 + THGM_SETUP_PI2(7,
1.234 + char&, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByRef,
1.235 + double&, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByRef
1.236 + );
1.237 +
1.238 + THGM_MAKE_TEST1_void(8, POD<7>);
1.239 + THGM_SETUP_PI1(8, POD<7>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
1.240 +
1.241 + THGM_MAKE_TEST1_void(9, POD<600>);
1.242 + THGM_SETUP_PI1(9, POD<600>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal);
1.243 +
1.244 + THGM_MAKE_TEST1_void(10, POD<600> &);
1.245 + THGM_SETUP_PI1(10, POD<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef);
1.246 +
1.247 + THGM_MAKE_TEST2_void(11, Object<3>, Object<600>&);
1.248 + THGM_SETUP_PI2(11,
1.249 + Object<3>, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByVal | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor,
1.250 + Object<600> &, SourceHook::PassInfo::PassType_Object, SourceHook::PassInfo::PassFlag_ByRef | SourceHook::PassInfo::PassFlag_OCtor | SourceHook::PassInfo::PassFlag_ODtor | SourceHook::PassInfo::PassFlag_CCtor
1.251 + );
1.252 +
1.253 + THGM_MAKE_TEST0(101, char);
1.254 + THGM_SETUP_PI0(101);
1.255 + THGM_SETUP_RI(101, char, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
1.256 +
1.257 + THGM_MAKE_TEST0(102, short);
1.258 + THGM_SETUP_PI0(102);
1.259 + THGM_SETUP_RI(102, short, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
1.260 +
1.261 + THGM_MAKE_TEST0(103, int);
1.262 + THGM_SETUP_PI0(103);
1.263 + THGM_SETUP_RI(103, int, SourceHook::PassInfo::PassType_Basic, SourceHook::PassInfo::PassFlag_ByVal);
1.264 +
1.265 + THGM_MAKE_TEST0(104, float);
1.266 + THGM_SETUP_PI0(104);
1.267 + THGM_SETUP_RI(104, float, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
1.268 +
1.269 + THGM_MAKE_TEST0(105, double);
1.270 + THGM_SETUP_PI0(105);
1.271 + THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
1.272 +}
1.273 +
1.274 +
1.275 +bool TestHookManGen(std::string &error)
1.276 +{
1.277 + GET_SHPTR(g_SHPtr);
1.278 + g_PLID = 1337;
1.279 +
1.280 + THGM_DO_TEST_void(0, ());
1.281 +
1.282 + THGM_DO_TEST_void(1, (100));
1.283 +
1.284 + THGM_DO_TEST_void(2, (0x1F00));
1.285 +
1.286 + THGM_DO_TEST_void(3, (0x1F000000));
1.287 +
1.288 + THGM_DO_TEST_void(4, (0.5f));
1.289 +
1.290 + THGM_DO_TEST_void(5, (5.5));
1.291 +
1.292 + THGM_DO_TEST_void(6, (100, 0x1f00, 0x1f000000, 0.5f, 5.5));
1.293 +
1.294 + char a = 5;
1.295 + double b = 233.33;
1.296 + THGM_DO_TEST_void(7, (a, b));
1.297 +
1.298 + POD<7> pod7 = {78};
1.299 + THGM_DO_TEST_void(8, (pod7));
1.300 +
1.301 + POD<600> pod600 = {34};
1.302 + THGM_DO_TEST_void(9, (pod600));
1.303 +
1.304 + THGM_DO_TEST_void(10, (pod600));
1.305 +
1.306 + // Test11: Special: constructors/destructors
1.307 + PtrBuf_Clear();
1.308 + Object<3> obj3(33);
1.309 + Object<600> obj600(21);
1.310 +
1.311 + CHECK_STATES((&g_States,
1.312 + new State_ObjOCtor_Called(3),
1.313 + new State_ObjOCtor_Called(600),
1.314 + NULL), "Test11 Part0");
1.315 +
1.316 + setuppi_11();
1.317 + g_Genc11 = new SourceHook::Impl::GenContext(&protoinfo_11, 0, 0, g_SHPtr);
1.318 + g_Genc_ad11.set(g_Genc11);
1.319 + SourceHook::HookManagerPubFunc myhookman11 = g_Genc11->Generate();
1.320 + int hook1_11, hook2_11, hook3_11, hook4_11;
1.321 +
1.322 + TestClass11 *pTest11 = new TestClass11;
1.323 + CAutoPtrDestruction<TestClass11> apd11(pTest11);
1.324 +
1.325 + /* no hooks - no hooks */
1.326 + PtrBuf_Clear();
1.327 + pTest11->Func(obj3, obj600);
1.328 +
1.329 + g_Inside_LeafFunc = true;
1.330 + CHECK_STATES((&g_States,
1.331 + new State_ObjCCtor_Called(3),
1.332 + new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
1.333 + new State_ObjODtor_Called(3),
1.334 + NULL), "Test" "11" " Part1");
1.335 + g_Inside_LeafFunc = false;
1.336 +
1.337 + /* hook1 - no hooks */
1.338 + THGM_ADD_HOOK(11, 1);
1.339 +
1.340 + PtrBuf_Clear();
1.341 + pTest11->Func(obj3, obj600);
1.342 + g_Inside_LeafFunc = true;
1.343 + CHECK_STATES((&g_States,
1.344 + new State_ObjCCtor_Called(3),
1.345 +
1.346 + new State_ObjCCtor_Called(3),
1.347 + new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
1.348 + new State_ObjODtor_Called(3),
1.349 +
1.350 + new State_ObjCCtor_Called(3),
1.351 + new State_Func11(pTest11, ParamState_m11 (obj3, obj600)),
1.352 + new State_ObjODtor_Called(3),
1.353 +
1.354 + new State_ObjODtor_Called(3),
1.355 + NULL), "Test" "11" " Part2");
1.356 + g_Inside_LeafFunc = false;
1.357 +
1.358 + THGM_REMOVE_HOOK(11, 1);
1.359 +
1.360 + /* hook1, hook2 - hook3, hook4 */
1.361 + THGM_ADD_HOOK(11, 1);
1.362 + THGM_ADD_HOOK(11, 2);
1.363 + THGM_ADD_HOOK(11, 3);
1.364 + THGM_ADD_HOOK(11, 4);
1.365 + PtrBuf_Clear();
1.366 + pTest11->Func(obj3, obj600);
1.367 + g_Inside_LeafFunc = true;
1.368 + CHECK_STATES((&g_States,
1.369 + new State_ObjCCtor_Called(3),
1.370 +
1.371 + new State_ObjCCtor_Called(3),
1.372 + new State_Deleg1_11(pTest11, 0, ParamState_m11 (obj3, obj600)),
1.373 + new State_ObjODtor_Called(3),
1.374 +
1.375 + new State_ObjCCtor_Called(3),
1.376 + new State_Deleg2_11(pTest11, 1, ParamState_m11 (obj3, obj600)),
1.377 + new State_ObjODtor_Called(3),
1.378 +
1.379 + new State_ObjCCtor_Called(3),
1.380 + new State_Deleg3_11(pTest11, 2, ParamState_m11 (obj3, obj600)),
1.381 + new State_ObjODtor_Called(3),
1.382 +
1.383 + new State_ObjCCtor_Called(3),
1.384 + new State_Deleg4_11(pTest11, 3, ParamState_m11 (obj3, obj600)),
1.385 + new State_ObjODtor_Called(3),
1.386 +
1.387 + new State_ObjODtor_Called(3),
1.388 + NULL), "Test" "11" " Part3");
1.389 + g_Inside_LeafFunc = false;
1.390 +
1.391 + /* hook1 - hook3, hook4 WITH RECALLS */
1.392 + THGM_REMOVE_HOOK(11, 2);
1.393 + PtrBuf_Clear();
1.394 + TestClass11::ms_DoRecall = true;
1.395 + pTest11->Func(obj3, obj600);
1.396 + g_Inside_LeafFunc = true;
1.397 + CHECK_STATES((&g_States,
1.398 + new State_ObjCCtor_Called(3),
1.399 +
1.400 + new State_ObjCCtor_Called(3),
1.401 + new State_Deleg1_11(pTest11, 0 /* first deleg ptr */, ParamState_m11 (obj3, obj600)),
1.402 +
1.403 + // recall !
1.404 + // second hookfunc -> new copy
1.405 + new State_ObjCCtor_Called(3),
1.406 +
1.407 + // in second hookfunc now
1.408 + // -> calls orig func
1.409 +
1.410 + new State_ObjCCtor_Called(3),
1.411 + new State_Func11(pTest11, ParamState_m11 (obj3, obj600)(1)),
1.412 + new State_ObjODtor_Called(3),
1.413 +
1.414 + // calls first posthook
1.415 + new State_ObjCCtor_Called(3),
1.416 + new State_Deleg3_11(pTest11, 1 /* second deleg ptr */, ParamState_m11 (obj3, obj600)(1)),
1.417 +
1.418 + // recall!
1.419 + // third hookfunc -> new copy
1.420 + new State_ObjCCtor_Called(3),
1.421 +
1.422 + // calls second posthook
1.423 +
1.424 + new State_ObjCCtor_Called(3),
1.425 + new State_Deleg4_11(pTest11, 2 /* third deleg ptr */, ParamState_m11 (obj3, obj600)(2)),
1.426 +
1.427 + // recall!
1.428 + // fourth hookfunc -> new copy
1.429 + new State_ObjCCtor_Called(3),
1.430 +
1.431 + // has nothing to do though!
1.432 +
1.433 + // fourth hookfunc done -> ret
1.434 + new State_ObjODtor_Called(3),
1.435 +
1.436 + // third hookfunc done -> ret
1.437 + new State_ObjODtor_Called(3),
1.438 + // ret from hookhandler which did the recall
1.439 + new State_ObjODtor_Called(3),
1.440 +
1.441 + // second hookfunc done -> ret
1.442 + new State_ObjODtor_Called(3),
1.443 + // ret from hookhandler which did the recall
1.444 + new State_ObjODtor_Called(3),
1.445 +
1.446 + // deleg1's instance
1.447 + new State_ObjODtor_Called(3),
1.448 + // first hookfunc done -> ret
1.449 + new State_ObjODtor_Called(3),
1.450 + NULL), "Test" "11" " Part4");
1.451 + g_Inside_LeafFunc = false;
1.452 +
1.453 + THGM_REMOVE_HOOK(11, 1);
1.454 + THGM_REMOVE_HOOK(11, 3);
1.455 + THGM_REMOVE_HOOK(11, 4);
1.456 +
1.457 + THGM_DO_TEST(101, ());
1.458 +
1.459 + THGM_DO_TEST(102, ());
1.460 +
1.461 + THGM_DO_TEST(103, ());
1.462 +
1.463 + THGM_DO_TEST(104, ());
1.464 +
1.465 + THGM_DO_TEST(105, ());
1.466 +
1.467 + // Shutdown now!
1.468 + // If we don't SH will auto-shutdown _after_ genc's destructor is called
1.469 + // -> crash
1.470 +
1.471 + Test_CompleteShutdown(g_SHPtr);
1.472 + return true;
1.473 +}