2010-07-19 09:57:32 +00:00
|
|
|
#include "test.hpp"
|
|
|
|
#include "allocator.hpp"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-02-19 09:05:23 +00:00
|
|
|
#include <stdlib.h>
|
2010-08-29 16:28:10 +00:00
|
|
|
#include <float.h>
|
2010-08-29 15:20:27 +00:00
|
|
|
#include <assert.h>
|
2010-07-19 09:57:32 +00:00
|
|
|
|
2014-02-19 06:21:51 +00:00
|
|
|
#include <string>
|
|
|
|
|
2012-03-06 08:49:58 +00:00
|
|
|
#ifndef PUGIXML_NO_EXCEPTIONS
|
|
|
|
# include <exception>
|
|
|
|
#endif
|
|
|
|
|
2012-02-19 09:05:23 +00:00
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
# undef DebugBreak
|
2012-03-06 16:56:45 +00:00
|
|
|
# pragma warning(disable: 4201) // nonstandard extension used: nameless struct/union
|
2012-02-19 09:05:23 +00:00
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
test_runner* test_runner::_tests = 0;
|
|
|
|
size_t test_runner::_memory_fail_threshold = 0;
|
2014-10-26 03:46:28 +00:00
|
|
|
bool test_runner::_memory_fail_triggered = false;
|
2010-07-19 09:57:32 +00:00
|
|
|
jmp_buf test_runner::_failure_buffer;
|
|
|
|
const char* test_runner::_failure_message;
|
2014-02-19 06:21:51 +00:00
|
|
|
const char* test_runner::_temp_path;
|
2010-07-19 09:57:32 +00:00
|
|
|
|
|
|
|
static size_t g_memory_total_size = 0;
|
|
|
|
static size_t g_memory_total_count = 0;
|
2015-04-11 22:46:08 -07:00
|
|
|
static size_t g_memory_fail_triggered = false;
|
2010-07-19 09:57:32 +00:00
|
|
|
|
|
|
|
static void* custom_allocate(size_t size)
|
|
|
|
{
|
|
|
|
if (test_runner::_memory_fail_threshold > 0 && test_runner::_memory_fail_threshold < g_memory_total_size + size)
|
2014-10-26 03:46:28 +00:00
|
|
|
{
|
2015-04-11 22:46:08 -07:00
|
|
|
g_memory_fail_triggered = true;
|
2014-10-26 03:46:28 +00:00
|
|
|
test_runner::_memory_fail_triggered = true;
|
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
return 0;
|
2014-10-26 03:46:28 +00:00
|
|
|
}
|
2010-07-19 09:57:32 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
void* ptr = memory_allocate(size);
|
2014-10-21 03:33:47 +00:00
|
|
|
assert(ptr);
|
2010-07-19 09:57:32 +00:00
|
|
|
|
|
|
|
g_memory_total_size += memory_size(ptr);
|
|
|
|
g_memory_total_count++;
|
2016-01-24 14:03:02 +01:00
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 21:46:48 -07:00
|
|
|
#ifndef PUGIXML_NO_EXCEPTIONS
|
|
|
|
static void* custom_allocate_throw(size_t size)
|
|
|
|
{
|
|
|
|
void* result = custom_allocate(size);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
throw std::bad_alloc();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
static void custom_deallocate(void* ptr)
|
|
|
|
{
|
2010-08-29 15:20:27 +00:00
|
|
|
assert(ptr);
|
|
|
|
|
|
|
|
g_memory_total_size -= memory_size(ptr);
|
|
|
|
g_memory_total_count--;
|
2016-01-24 14:03:02 +01:00
|
|
|
|
2010-08-29 15:20:27 +00:00
|
|
|
memory_deallocate(ptr);
|
2010-07-19 09:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void replace_memory_management()
|
|
|
|
{
|
|
|
|
// create some document to touch original functions
|
|
|
|
{
|
|
|
|
pugi::xml_document doc;
|
|
|
|
doc.append_child().set_name(STR("node"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace functions
|
|
|
|
pugi::set_memory_management_functions(custom_allocate, custom_deallocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) && _MSC_VER > 1200 && _MSC_VER < 1400 && !defined(__INTEL_COMPILER) && !defined(__DMC__)
|
2012-03-06 16:56:45 +00:00
|
|
|
#include <exception>
|
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
_CRTIMP2 _Prhand _Raise_handler;
|
|
|
|
_CRTIMP2 void __cdecl _Throw(const exception&) {}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-12 21:46:48 -07:00
|
|
|
static bool run_test(test_runner* test, const char* test_name, pugi::allocation_function allocate)
|
2010-07-19 09:57:32 +00:00
|
|
|
{
|
|
|
|
#ifndef PUGIXML_NO_EXCEPTIONS
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
g_memory_total_size = 0;
|
|
|
|
g_memory_total_count = 0;
|
2015-04-11 22:46:08 -07:00
|
|
|
g_memory_fail_triggered = false;
|
2010-07-19 09:57:32 +00:00
|
|
|
test_runner::_memory_fail_threshold = 0;
|
2014-10-26 03:46:28 +00:00
|
|
|
test_runner::_memory_fail_triggered = false;
|
2016-01-24 14:03:02 +01:00
|
|
|
|
2015-04-12 21:46:48 -07:00
|
|
|
pugi::set_memory_management_functions(allocate, custom_deallocate);
|
2016-01-24 14:03:02 +01:00
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma warning(push)
|
|
|
|
# pragma warning(disable: 4611) // interaction between _setjmp and C++ object destruction is non-portable
|
2011-02-16 20:00:54 +00:00
|
|
|
# pragma warning(disable: 4793) // function compiled as native: presence of '_setjmp' makes a function unmanaged
|
2010-07-19 09:57:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
volatile int result = setjmp(test_runner::_failure_buffer);
|
2016-01-24 14:03:02 +01:00
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
2015-04-12 21:46:48 -07:00
|
|
|
printf("Test %s failed: %s\n", test_name, test_runner::_failure_message);
|
2010-07-19 09:57:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
test->run();
|
|
|
|
|
2015-04-11 22:46:08 -07:00
|
|
|
if (test_runner::_memory_fail_triggered)
|
|
|
|
{
|
2015-04-12 21:46:48 -07:00
|
|
|
printf("Test %s failed: unguarded memory fail triggered\n", test_name);
|
2015-04-11 22:46:08 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
if (g_memory_total_size != 0 || g_memory_total_count != 0)
|
|
|
|
{
|
2015-04-12 21:46:48 -07:00
|
|
|
printf("Test %s failed: memory leaks found (%u bytes in %u allocations)\n", test_name, static_cast<unsigned int>(g_memory_total_size), static_cast<unsigned int>(g_memory_total_count));
|
2010-07-19 09:57:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#ifndef PUGIXML_NO_EXCEPTIONS
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2015-04-12 21:46:48 -07:00
|
|
|
printf("Test %s failed: exception %s\n", test_name, e.what());
|
2010-07-19 09:57:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2015-04-12 21:46:48 -07:00
|
|
|
printf("Test %s failed for unknown reason\n", test_name);
|
2010-07-19 09:57:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__CELLOS_LV2__) && defined(PUGIXML_NO_EXCEPTIONS) && !defined(__SNC__)
|
2012-03-06 16:56:45 +00:00
|
|
|
#include <exception>
|
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
void std::exception::_Raise() const
|
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-19 06:21:51 +00:00
|
|
|
int main(int, char** argv)
|
2010-07-19 09:57:32 +00:00
|
|
|
{
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
_control87(MCW_EM | PC_53, MCW_EM | MCW_PC);
|
|
|
|
#endif
|
2014-02-19 06:21:51 +00:00
|
|
|
|
|
|
|
// setup temp path as the executable folder
|
|
|
|
std::string temp = argv[0];
|
|
|
|
std::string::size_type slash = temp.find_last_of("\\/");
|
|
|
|
temp.erase((slash != std::string::npos) ? slash + 1 : 0);
|
|
|
|
|
|
|
|
test_runner::_temp_path = temp.c_str();
|
2016-01-24 14:03:02 +01:00
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
replace_memory_management();
|
|
|
|
|
|
|
|
unsigned int total = 0;
|
|
|
|
unsigned int passed = 0;
|
|
|
|
|
|
|
|
test_runner* test = 0; // gcc3 "variable might be used uninitialized in this function" bug workaround
|
|
|
|
|
|
|
|
for (test = test_runner::_tests; test; test = test->_next)
|
|
|
|
{
|
|
|
|
total++;
|
2015-04-12 21:46:48 -07:00
|
|
|
passed += run_test(test, test->_name, custom_allocate);
|
|
|
|
|
|
|
|
#ifndef PUGIXML_NO_EXCEPTIONS
|
|
|
|
if (g_memory_fail_triggered)
|
|
|
|
{
|
|
|
|
total++;
|
|
|
|
passed += run_test(test, (test->_name + std::string(" (throw)")).c_str(), custom_allocate_throw);
|
|
|
|
}
|
|
|
|
#endif
|
2010-07-19 09:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int failed = total - passed;
|
|
|
|
|
|
|
|
if (failed != 0)
|
|
|
|
printf("FAILURE: %u out of %u tests failed.\n", failed, total);
|
|
|
|
else
|
|
|
|
printf("Success: %u tests passed.\n", total);
|
|
|
|
|
|
|
|
return failed;
|
|
|
|
}
|
2014-02-19 06:21:51 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
|
|
|
|
{
|
2014-02-26 22:37:40 +00:00
|
|
|
return main(0, NULL);
|
2014-02-19 06:21:51 +00:00
|
|
|
}
|
|
|
|
#endif
|