0
0
mirror of https://github.com/zeux/pugixml.git synced 2024-12-26 21:04:25 +08:00

tests: Add more out of memory tests

Also add tests that verify save_file for absence of FILE leaks.
This commit is contained in:
Arseny Kapoulkine 2015-04-12 21:27:12 -07:00
parent a0d065cd22
commit 5edeaf6765
3 changed files with 62 additions and 4 deletions

View File

@ -143,9 +143,9 @@ struct dummy_fixture {};
#endif
#ifdef PUGIXML_NO_EXCEPTIONS
#define CHECK_ALLOC_FAIL(code) CHECK(!test_runner::_memory_fail_triggered); code; CHECK(test_runner::_memory_fail_triggered); test_runner::_memory_fail_triggered = false
#define CHECK_ALLOC_FAIL(code) do { CHECK(!test_runner::_memory_fail_triggered); code; CHECK(test_runner::_memory_fail_triggered); test_runner::_memory_fail_triggered = false; } while (test_runner::_memory_fail_triggered)
#else
#define CHECK_ALLOC_FAIL(code) CHECK(!test_runner::_memory_fail_triggered); try { code; } catch (std::bad_alloc&) {} CHECK(test_runner::_memory_fail_triggered); test_runner::_memory_fail_triggered = false
#define CHECK_ALLOC_FAIL(code) do { CHECK(!test_runner::_memory_fail_triggered); try { code; } catch (std::bad_alloc&) {} CHECK(test_runner::_memory_fail_triggered); test_runner::_memory_fail_triggered = false; } while (test_runner::_memory_fail_triggered)
#endif
#define STR(text) PUGIXML_TEXT(text)

View File

@ -319,9 +319,7 @@ TEST(document_load_file_out_of_memory_file_leak)
pugi::xml_document doc;
for (int i = 0; i < 256; ++i)
{
CHECK_ALLOC_FAIL(CHECK(doc.load_file("tests/data/small.xml").status == status_out_of_memory));
}
test_runner::_memory_fail_threshold = 0;
@ -329,6 +327,21 @@ TEST(document_load_file_out_of_memory_file_leak)
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_load_file_wide_out_of_memory_file_leak)
{
test_runner::_memory_fail_threshold = 256;
pugi::xml_document doc;
for (int i = 0; i < 256; ++i)
CHECK_ALLOC_FAIL(CHECK(doc.load_file(L"tests/data/small.xml").status == status_out_of_memory));
test_runner::_memory_fail_threshold = 0;
CHECK(doc.load_file(L"tests/data/small.xml"));
CHECK_NODE(doc, STR("<node />"));
}
TEST(document_load_file_error_previous)
{
pugi::xml_document doc;
@ -556,6 +569,26 @@ TEST_XML(document_save_file_wide_text, "<node/>")
CHECK(test_file_contents(f.path, "<node />\n", 9));
}
TEST_XML(document_save_file_leak, "<node/>")
{
temp_file f;
for (int i = 0; i < 256; ++i)
CHECK(doc.save_file(f.path));
}
TEST_XML(document_save_file_wide_leak, "<node/>")
{
temp_file f;
// widen the path
wchar_t wpath[sizeof(f.path)];
std::copy(f.path, f.path + strlen(f.path) + 1, wpath + 0);
for (int i = 0; i < 256; ++i)
CHECK(doc.save_file(wpath));
}
TEST(document_load_buffer)
{
const pugi::char_t text[] = STR("<?xml?><node/>");

View File

@ -935,6 +935,31 @@ TEST(parse_out_of_memory_conversion)
CHECK(!doc.first_child());
}
TEST(parse_out_of_memory_allocator_state_sync)
{
const unsigned int count = 10000;
static char_t text[count * 4];
for (unsigned int i = 0; i < count; ++i)
{
text[4*i + 0] = '<';
text[4*i + 1] = 'n';
text[4*i + 2] = '/';
text[4*i + 3] = '>';
}
test_runner::_memory_fail_threshold = 65536;
xml_document doc;
CHECK_ALLOC_FAIL(CHECK(doc.load_buffer_inplace(text, count * 4).status == status_out_of_memory));
CHECK_NODE(doc.first_child(), STR("<n />"));
test_runner::_memory_fail_threshold = 0;
for (unsigned int i = 0; i < count; ++i)
CHECK(doc.append_child(STR("n")));
}
static bool test_offset(const char_t* contents, unsigned int options, pugi::xml_parse_status status, ptrdiff_t offset)
{
xml_document doc;