sourcehook/test/test4.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Aug 10 23:43:16 2007 +0000 (2007-08-10)
changeset 346 d0868271663a
parent 324 257f0837a885
child 380 f9e21a2ac558
permissions -rw-r--r--
Reformatted output of meta list command
     1 #include <string>
     2 #include "sourcehook_test.h"
     3 #include "testevents.h"
     4 
     5 // TEST4
     6 // Tests of plugin management system
     7 
     8 namespace
     9 {
    10 	StateList g_States;
    11 	SourceHook::ISourceHook *g_SHPtr;
    12 	SourceHook::Plugin g_PLID;
    13 
    14 	MAKE_STATE(State_Func1_Called);
    15 	MAKE_STATE(State_Func2_Called);
    16 	MAKE_STATE(State_Func3_Called);
    17 
    18 	MAKE_STATE(State_Func1H_Called);
    19 	MAKE_STATE(State_Func2H_Called);
    20 	MAKE_STATE(State_Func3H_Called);
    21 
    22 	MAKE_STATE_2(State_PluginInUse, int, bool);
    23 
    24 	class Test
    25 	{
    26 	public:
    27 		virtual void Func1()
    28 		{
    29 			ADD_STATE(State_Func1_Called);
    30 		}
    31 
    32 		virtual void Func2()
    33 		{
    34 			ADD_STATE(State_Func2_Called);
    35 		}
    36 
    37 		virtual void Func3()
    38 		{
    39 			ADD_STATE(State_Func3_Called);
    40 		}
    41 	};
    42 
    43 	// GCC's optimizer is too good. I had to add this in order to make it execute a virtual table lookup!
    44 	class Whatever : public Test
    45 	{
    46 	};
    47 
    48 	SH_DECL_HOOK0_void(Test, Func1, SH_NOATTRIB, 0);
    49 	SH_DECL_HOOK0_void(Test, Func2, SH_NOATTRIB, 0);
    50 	SH_DECL_HOOK0_void(Test, Func3, SH_NOATTRIB, 0);
    51 
    52 	void Handler_Func1()
    53 	{
    54 		ADD_STATE(State_Func1H_Called);
    55 	}
    56 	void Handler_Func2()
    57 	{
    58 		ADD_STATE(State_Func2H_Called);
    59 	}
    60 	void Handler_Func3()
    61 	{
    62 		ADD_STATE(State_Func3H_Called);
    63 	}
    64 }
    65 
    66 bool TestPlugSys(std::string &error)
    67 {
    68 	GET_SHPTR(g_SHPtr);
    69 	g_PLID = 1;
    70 
    71 	Whatever inst;
    72 	Test *pInst = &inst;
    73 
    74 	// 1)
    75 	//   Add hooks, then issue a complete shutdown
    76 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
    77 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
    78 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
    79 
    80 	pInst->Func1();
    81 	pInst->Func2();
    82 	pInst->Func3();
    83 
    84 	CHECK_STATES((&g_States,
    85 		new State_Func1H_Called,
    86 		new State_Func1_Called,
    87 		new State_Func2_Called,
    88 		new State_Func2H_Called,
    89 		new State_Func3H_Called,
    90 		new State_Func3_Called,
    91 		NULL), "Part 1.1");
    92 
    93 	Test_CompleteShutdown(g_SHPtr);
    94 
    95 	pInst->Func1();
    96 	pInst->Func2();
    97 	pInst->Func3();
    98 
    99 	CHECK_STATES((&g_States,
   100 		new State_Func1_Called,
   101 		new State_Func2_Called,
   102 		new State_Func3_Called,
   103 		NULL), "Part 1.2");
   104 
   105 	// 2)
   106 	//   Add hooks from "different plugins", then shutdown the plugins
   107 
   108 	g_PLID = 1;
   109 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   110 	g_PLID = 2;
   111 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   112 	g_PLID = 3;
   113 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   114 
   115 	g_PLID = 1;
   116 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   117 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   118 
   119 	g_PLID = 2;
   120 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   121 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   122 
   123 	g_PLID = 3;
   124 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   125 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   126 
   127 	pInst->Func1();
   128 	pInst->Func2();
   129 	pInst->Func3();
   130 
   131 	CHECK_STATES((&g_States,
   132 		new State_Func1H_Called,
   133 		new State_Func1H_Called,
   134 		new State_Func1H_Called,
   135 		new State_Func1_Called,
   136 		new State_Func2_Called,
   137 		new State_Func2H_Called,
   138 		new State_Func2H_Called,
   139 		new State_Func2H_Called,
   140 		new State_Func3H_Called,
   141 		new State_Func3H_Called,
   142 		new State_Func3H_Called,
   143 		new State_Func3_Called,
   144 		NULL), "Part 2.1");
   145 
   146 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   147 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   148 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   149 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   150 
   151 	CHECK_STATES((&g_States,
   152 		new State_PluginInUse(1, true),
   153 		new State_PluginInUse(2, true),
   154 		new State_PluginInUse(3, true),
   155 		new State_PluginInUse(4, false),
   156 		NULL), "Part 2.2");
   157 
   158 	// Unload plugins one by one
   159 	Test_UnloadPlugin(g_SHPtr, 3);
   160 
   161 	pInst->Func1();
   162 	pInst->Func2();
   163 	pInst->Func3();
   164 
   165 	CHECK_STATES((&g_States,
   166 		new State_Func1H_Called,
   167 		new State_Func1H_Called,
   168 		new State_Func1_Called,
   169 		new State_Func2_Called,
   170 		new State_Func2H_Called,
   171 		new State_Func2H_Called,
   172 		new State_Func3H_Called,
   173 		new State_Func3H_Called,
   174 		new State_Func3_Called,
   175 		NULL), "Part 2.3.1");
   176 
   177 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   178 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   179 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   180 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   181 
   182 	CHECK_STATES((&g_States,
   183 		new State_PluginInUse(1, true),
   184 		new State_PluginInUse(2, true),
   185 		new State_PluginInUse(3, false),
   186 		new State_PluginInUse(4, false),
   187 		NULL), "Part 2.3.2");
   188 
   189 	Test_UnloadPlugin(g_SHPtr, 2);
   190 
   191 	pInst->Func1();
   192 	pInst->Func2();
   193 	pInst->Func3();
   194 
   195 	CHECK_STATES((&g_States,
   196 		new State_Func1H_Called,
   197 		new State_Func1_Called,
   198 		new State_Func2_Called,
   199 		new State_Func2H_Called,
   200 		new State_Func3H_Called,
   201 		new State_Func3_Called,
   202 		NULL), "Part 2.4.1");
   203 
   204 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   205 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   206 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   207 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   208 
   209 	CHECK_STATES((&g_States,
   210 		new State_PluginInUse(1, true),
   211 		new State_PluginInUse(2, false),
   212 		new State_PluginInUse(3, false),
   213 		new State_PluginInUse(4, false),
   214 		NULL), "Part 2.4.2");
   215 
   216 	Test_UnloadPlugin(g_SHPtr, 1);
   217 
   218 	pInst->Func1();
   219 	pInst->Func2();
   220 	pInst->Func3();
   221 
   222 	CHECK_STATES((&g_States,
   223 		new State_Func1_Called,
   224 		new State_Func2_Called,
   225 		new State_Func3_Called,
   226 		NULL), "Part 2.5.1");
   227 
   228 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   229 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   230 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   231 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   232 
   233 	CHECK_STATES((&g_States,
   234 		new State_PluginInUse(1, false),
   235 		new State_PluginInUse(2, false),
   236 		new State_PluginInUse(3, false),
   237 		new State_PluginInUse(4, false),
   238 		NULL), "Part 2.5.2");
   239 
   240 
   241 	// 3)
   242 	//   Add hooks from "different plugins", then pause the plugins
   243 
   244 	g_PLID = 1;
   245 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   246 	g_PLID = 2;
   247 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   248 	g_PLID = 3;
   249 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   250 
   251 	g_PLID = 1;
   252 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   253 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   254 
   255 	g_PLID = 2;
   256 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   257 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   258 
   259 	g_PLID = 3;
   260 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   261 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   262 
   263 	pInst->Func1();
   264 	pInst->Func2();
   265 	pInst->Func3();
   266 
   267 	CHECK_STATES((&g_States,
   268 		new State_Func1H_Called,
   269 		new State_Func1H_Called,
   270 		new State_Func1H_Called,
   271 		new State_Func1_Called,
   272 		new State_Func2_Called,
   273 		new State_Func2H_Called,
   274 		new State_Func2H_Called,
   275 		new State_Func2H_Called,
   276 		new State_Func3H_Called,
   277 		new State_Func3H_Called,
   278 		new State_Func3H_Called,
   279 		new State_Func3_Called,
   280 		NULL), "Part 3.1");
   281 
   282 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   283 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   284 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   285 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   286 
   287 	CHECK_STATES((&g_States,
   288 		new State_PluginInUse(1, true),
   289 		new State_PluginInUse(2, true),
   290 		new State_PluginInUse(3, true),
   291 		new State_PluginInUse(4, false),
   292 		NULL), "Part 3.2");
   293 
   294 	// Unload plugins one by one
   295 	Test_PausePlugin(g_SHPtr, 3);
   296 
   297 	pInst->Func1();
   298 	pInst->Func2();
   299 	pInst->Func3();
   300 
   301 	CHECK_STATES((&g_States,
   302 		new State_Func1H_Called,
   303 		new State_Func1H_Called,
   304 		new State_Func1_Called,
   305 		new State_Func2_Called,
   306 		new State_Func2H_Called,
   307 		new State_Func2H_Called,
   308 		new State_Func3H_Called,
   309 		new State_Func3H_Called,
   310 		new State_Func3_Called,
   311 		NULL), "Part 3.3.1");
   312 
   313 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   314 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   315 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   316 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   317 
   318 	CHECK_STATES((&g_States,
   319 		new State_PluginInUse(1, true),
   320 		new State_PluginInUse(2, true),
   321 		new State_PluginInUse(3, true),
   322 		new State_PluginInUse(4, false),
   323 		NULL), "Part 3.3.2");
   324 
   325 	Test_PausePlugin(g_SHPtr, 2);
   326 
   327 	pInst->Func1();
   328 	pInst->Func2();
   329 	pInst->Func3();
   330 
   331 	CHECK_STATES((&g_States,
   332 		new State_Func1H_Called,
   333 		new State_Func1_Called,
   334 		new State_Func2_Called,
   335 		new State_Func2H_Called,
   336 		new State_Func3H_Called,
   337 		new State_Func3_Called,
   338 		NULL), "Part 3.4.1");
   339 
   340 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   341 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   342 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   343 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   344 
   345 	CHECK_STATES((&g_States,
   346 		new State_PluginInUse(1, true),
   347 		new State_PluginInUse(2, true),
   348 		new State_PluginInUse(3, true),
   349 		new State_PluginInUse(4, false),
   350 		NULL), "Part 3.4.2");
   351 
   352 	Test_PausePlugin(g_SHPtr, 1);
   353 
   354 	pInst->Func1();
   355 	pInst->Func2();
   356 	pInst->Func3();
   357 
   358 	CHECK_STATES((&g_States,
   359 		new State_Func1_Called,
   360 		new State_Func2_Called,
   361 		new State_Func3_Called,
   362 		NULL), "Part 3.5.1");
   363 
   364 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   365 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   366 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   367 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   368 
   369 	CHECK_STATES((&g_States,
   370 		new State_PluginInUse(1, true),
   371 		new State_PluginInUse(2, true),
   372 		new State_PluginInUse(3, true),
   373 		new State_PluginInUse(4, false),
   374 		NULL), "Part 3.5.2");
   375 
   376 	Test_UnpausePlugin(g_SHPtr, 1);
   377 	Test_UnpausePlugin(g_SHPtr, 2);
   378 	Test_UnpausePlugin(g_SHPtr, 3);
   379 
   380 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   381 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   382 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   383 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   384 
   385 	CHECK_STATES((&g_States,
   386 		new State_PluginInUse(1, true),
   387 		new State_PluginInUse(2, true),
   388 		new State_PluginInUse(3, true),
   389 		new State_PluginInUse(4, false),
   390 		NULL), "Part 3.6");
   391 
   392 	pInst->Func1();
   393 	pInst->Func2();
   394 	pInst->Func3();
   395 
   396 	CHECK_STATES((&g_States,
   397 		new State_Func1H_Called,
   398 		new State_Func1H_Called,
   399 		new State_Func1H_Called,
   400 		new State_Func1_Called,
   401 		new State_Func2_Called,
   402 		new State_Func2H_Called,
   403 		new State_Func2H_Called,
   404 		new State_Func2H_Called,
   405 		new State_Func3H_Called,
   406 		new State_Func3H_Called,
   407 		new State_Func3H_Called,
   408 		new State_Func3_Called,
   409 		NULL), "Part 3.7");
   410 
   411 	// 4) Shutdown :)
   412 	Test_CompleteShutdown(g_SHPtr);
   413 
   414 	pInst->Func1();
   415 	pInst->Func2();
   416 	pInst->Func3();
   417 
   418 	CHECK_STATES((&g_States,
   419 		new State_Func1_Called,
   420 		new State_Func2_Called,
   421 		new State_Func3_Called,
   422 		NULL), "Part 4");
   423 
   424 	return true;
   425 }