diff options
| author | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2009-10-20 21:36:29 +0000 | 
|---|---|---|
| committer | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2009-10-20 21:36:29 +0000 | 
| commit | c4fbc83cd021b46fb84ab2e55d9da0caaa8a4bb5 (patch) | |
| tree | 8d1fca26d353472f9006e24e5ebdd5a42fa032f4 | |
| parent | 327096ae0df60cf1ad6f59f6dafe474e58b2dbab (diff) | |
tests: Added memory leak detection
git-svn-id: http://pugixml.googlecode.com/svn/trunk@165 99668b35-9821-0410-8761-19e4c4f06640
| -rw-r--r-- | tests/main.cpp | 50 | ||||
| -rw-r--r-- | tests/test.hpp | 1 | 
2 files changed, 51 insertions, 0 deletions
diff --git a/tests/main.cpp b/tests/main.cpp index 55903b9..664f171 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -3,10 +3,53 @@  #include <exception>
  #include <stdio.h>
 +#include <malloc.h>
 +
  test_runner* test_runner::_tests = 0;
 +size_t test_runner::_memory_fail_threshold = 0;
 +
 +size_t g_memory_total_size = 0;
 +
 +void* custom_allocate(size_t size)
 +{
 +	if (test_runner::_memory_fail_threshold > 0 && test_runner::_memory_fail_threshold < size)
 +		return 0;
 +	else
 +	{
 +		void* ptr = malloc(size);
 +
 +		g_memory_total_size += _msize(ptr);
 +		
 +		return ptr;
 +	}
 +}
 +
 +void custom_deallocate(void* ptr)
 +{
 +	if (ptr)
 +	{
 +		g_memory_total_size -= _msize(ptr);
 +		
 +		free(ptr);
 +	}
 +}
 +
 +void replace_memory_management()
 +{
 +	// create some document to touch original functions
 +	{
 +		pugi::xml_document doc;
 +		doc.append_child().set_name("node");
 +	}
 +
 +	// replace functions
 +	pugi::set_memory_management_functions(custom_allocate, custom_deallocate);
 +}
  int main()
  {
 +	replace_memory_management();
 +
  	unsigned int total = 0;
  	unsigned int passed = 0;
 @@ -15,7 +58,14 @@ int main()  		try
  		{
  			total++;
 +
 +			g_memory_total_size = 0;
 +			test_runner::_memory_fail_threshold = 0;
 +
  			test->run();
 +
 +			if (g_memory_total_size != 0) throw "Memory leaks found";
 +
  			passed++;
  		}
  		catch (const std::exception& e)
 diff --git a/tests/test.hpp b/tests/test.hpp index af3c63b..023c452 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -42,6 +42,7 @@ struct test_runner  	test_runner* _next;
  	static test_runner* _tests;
 +	static size_t _memory_fail_threshold;
  };
  struct dummy_fixture {};
  | 
