sourcehook/test/testhookmangen.cpp
branchhookman_autogen
changeset 435 35296642e147
parent 432 f26dd3e3030b
child 436 41f21d77c3c1
     1.1 --- a/sourcehook/test/testhookmangen.cpp	Sun Oct 21 21:55:17 2007 +0000
     1.2 +++ b/sourcehook/test/testhookmangen.cpp	Thu Nov 01 01:58:44 2007 +0000
     1.3 @@ -3,6 +3,7 @@
     1.4  #include "sourcehook_test.h"
     1.5  #include "testevents.h"
     1.6  #include "sourcehook_hookmangen.h"
     1.7 +#include "sh_memory.h"
     1.8  
     1.9  // TESTHOOKMANGEN
    1.10  // Test automatic hookman generation
    1.11 @@ -268,7 +269,6 @@
    1.12  	THGM_SETUP_RI(105, double, SourceHook::PassInfo::PassType_Float, SourceHook::PassInfo::PassFlag_ByVal);
    1.13  }
    1.14  
    1.15 -
    1.16  bool TestHookManGen(std::string &error)
    1.17  {
    1.18  	GET_SHPTR(g_SHPtr);
    1.19 @@ -468,3 +468,73 @@
    1.20  	Test_CompleteShutdown(g_SHPtr);
    1.21  	return true;
    1.22  }
    1.23 +
    1.24 +bool TestCPageAlloc(std::string &error)
    1.25 +{
    1.26 +	using namespace SourceHook;
    1.27 +
    1.28 +	CPageAlloc alloc;
    1.29 +	int i;
    1.30 +	size_t ps = alloc.GetPageSize();
    1.31 +
    1.32 +	char *test1[8];
    1.33 +
    1.34 +	for (i = 0; i < 8; ++i)
    1.35 +		test1[i] = (char*) alloc.Alloc(ps / 4);
    1.36 +
    1.37 +	CHECK_COND(test1[1] == test1[0] + ps/4, "Part 1.1");
    1.38 +	CHECK_COND(test1[2] == test1[1] + ps/4, "Part 1.2");
    1.39 +	CHECK_COND(test1[3] == test1[2] + ps/4, "Part 1.3");
    1.40 +
    1.41 +	CHECK_COND(test1[5] == test1[4] + ps/4, "Part 1.4");
    1.42 +	CHECK_COND(test1[6] == test1[5] + ps/4, "Part 1.5");
    1.43 +	CHECK_COND(test1[7] == test1[6] + ps/4, "Part 1.6");
    1.44 +
    1.45 +	void *test2 = alloc.Alloc(ps * 3);
    1.46 +
    1.47 +	alloc.SetRW(test2);
    1.48 +	memset(test2, 0, ps * 3);			// should not crash :)
    1.49 +	alloc.SetRE(test2);
    1.50 +
    1.51 +	alloc.Free(test2);
    1.52 +
    1.53 +	// Dealloc a ps/4 block and place two ps/8 blocks into it
    1.54 +	alloc.Free(test1[2]);
    1.55 +
    1.56 +	char *test3[2];
    1.57 +	test3[0] = (char*) alloc.Alloc(ps / 8);
    1.58 +	test3[1] = (char*) alloc.Alloc(ps / 8);
    1.59 +
    1.60 +	CHECK_COND(test3[0] == test1[2], "Part 2.1");
    1.61 +	CHECK_COND(test3[1] == test1[2] + ps/8, "Part 2.2");
    1.62 +
    1.63 +	// Isolated
    1.64 +	char *test4[8];
    1.65 +	for (i = 0; i < 8; ++i)
    1.66 +		test4[i] = (char*) alloc.AllocIsolated(ps / 4);
    1.67 +
    1.68 +	// -> The difference is at least one page
    1.69 +	CHECK_COND(static_cast<size_t>(abs(test4[1] - test4[0])) >= ps, "Part 3.1");
    1.70 +	CHECK_COND(static_cast<size_t>(abs(test4[2] - test4[1])) >= ps, "Part 3.2");
    1.71 +	CHECK_COND(static_cast<size_t>(abs(test4[3] - test4[2])) >= ps, "Part 3.3");
    1.72 +
    1.73 +	CHECK_COND(static_cast<size_t>(abs(test4[5] - test4[4])) >= ps, "Part 3.4");
    1.74 +	CHECK_COND(static_cast<size_t>(abs(test4[6] - test4[5])) >= ps, "Part 3.5");
    1.75 +	CHECK_COND(static_cast<size_t>(abs(test4[7] - test4[6])) >= ps, "Part 3.6");
    1.76 +
    1.77 +	// Thus i can set everything except for test4[2] to RE and still write to test4[2]
    1.78 +
    1.79 +	alloc.SetRW(test4[2]);
    1.80 +
    1.81 +	alloc.SetRE(test4[0]);
    1.82 +	alloc.SetRE(test4[1]);
    1.83 +	alloc.SetRE(test4[3]);
    1.84 +	alloc.SetRE(test4[4]);
    1.85 +	alloc.SetRE(test4[5]);
    1.86 +	alloc.SetRE(test4[6]);
    1.87 +	alloc.SetRE(test4[7]);
    1.88 +
    1.89 +	memset((void*)test4[2], 0, ps / 4);
    1.90 +
    1.91 +	return true;
    1.92 +}
    1.93 \ No newline at end of file