tests: Fix all tests for compact mode

Memory allocation behavior is different in compact mode so tests that rely
on current behavior have to be adjusted.
This commit is contained in:
Arseny Kapoulkine 2014-11-06 09:59:07 +01:00
parent 393cc28481
commit 50bfdb1856
2 changed files with 31 additions and 19 deletions

View File

@ -1310,6 +1310,11 @@ TEST(dom_node_copy_copyless)
// the document is parsed in-place so there should only be 1 page worth of allocations // the document is parsed in-place so there should only be 1 page worth of allocations
test_runner::_memory_fail_threshold = 32768 + 128; test_runner::_memory_fail_threshold = 32768 + 128;
#ifdef PUGIXML_COMPACT
// ... and some space for hash table
test_runner::_memory_fail_threshold += 2048;
#endif
xml_document doc; xml_document doc;
CHECK(doc.load_buffer_inplace(&datacopy[0], datacopy.size() * sizeof(char_t), parse_full)); CHECK(doc.load_buffer_inplace(&datacopy[0], datacopy.size() * sizeof(char_t), parse_full));

View File

@ -1,30 +1,37 @@
#include "common.hpp" #include "common.hpp"
#include "writer_string.hpp" #include "writer_string.hpp"
#include "allocator.hpp"
#include <string> #include <string>
namespace namespace
{ {
int allocate_count = 0; int page_allocs = 0;
int deallocate_count = 0; int page_deallocs = 0;
bool is_page(size_t size)
{
return size >= 8192;
}
void* allocate(size_t size) void* allocate(size_t size)
{ {
++allocate_count; void* ptr = memory_allocate(size);
return new char[size]; page_allocs += is_page(memory_size(ptr));
return ptr;
} }
void deallocate(void* ptr) void deallocate(void* ptr)
{ {
++deallocate_count; page_deallocs += is_page(memory_size(ptr));
delete[] reinterpret_cast<char*>(ptr); memory_deallocate(ptr);
} }
} }
TEST(memory_custom_memory_management) TEST(memory_custom_memory_management)
{ {
allocate_count = deallocate_count = 0; page_allocs = page_deallocs = 0;
// remember old functions // remember old functions
allocation_function old_allocate = get_memory_allocation_function(); allocation_function old_allocate = get_memory_allocation_function();
@ -37,30 +44,30 @@ TEST(memory_custom_memory_management)
// parse document // parse document
xml_document doc; xml_document doc;
CHECK(allocate_count == 0 && deallocate_count == 0); CHECK(page_allocs == 0 && page_deallocs == 0);
CHECK(doc.load(STR("<node />"))); CHECK(doc.load(STR("<node />")));
CHECK(allocate_count == 2 && deallocate_count == 0); CHECK(page_allocs == 1 && page_deallocs == 0);
// modify document (no new page) // modify document (no new page)
CHECK(doc.first_child().set_name(STR("foobars"))); CHECK(doc.first_child().set_name(STR("foobars")));
CHECK(allocate_count == 2 && deallocate_count == 0); CHECK(page_allocs == 1 && page_deallocs == 0);
// modify document (new page) // modify document (new page)
std::basic_string<pugi::char_t> s(65536, 'x'); std::basic_string<pugi::char_t> s(65536, 'x');
CHECK(doc.first_child().set_name(s.c_str())); CHECK(doc.first_child().set_name(s.c_str()));
CHECK(allocate_count == 3 && deallocate_count == 0); CHECK(page_allocs == 2 && page_deallocs == 0);
// modify document (new page, old one should die) // modify document (new page, old one should die)
s += s; s += s;
CHECK(doc.first_child().set_name(s.c_str())); CHECK(doc.first_child().set_name(s.c_str()));
CHECK(allocate_count == 4 && deallocate_count == 1); CHECK(page_allocs == 3 && page_deallocs == 1);
} }
CHECK(allocate_count == 4 && deallocate_count == 4); CHECK(page_allocs == 3 && page_deallocs == 3);
// restore old functions // restore old functions
set_memory_management_functions(old_allocate, old_deallocate); set_memory_management_functions(old_allocate, old_deallocate);
@ -68,7 +75,7 @@ TEST(memory_custom_memory_management)
TEST(memory_large_allocations) TEST(memory_large_allocations)
{ {
allocate_count = deallocate_count = 0; page_allocs = page_deallocs = 0;
// remember old functions // remember old functions
allocation_function old_allocate = get_memory_allocation_function(); allocation_function old_allocate = get_memory_allocation_function();
@ -80,7 +87,7 @@ TEST(memory_large_allocations)
{ {
xml_document doc; xml_document doc;
CHECK(allocate_count == 0 && deallocate_count == 0); CHECK(page_allocs == 0 && page_deallocs == 0);
// initial fill // initial fill
for (size_t i = 0; i < 128; ++i) for (size_t i = 0; i < 128; ++i)
@ -90,7 +97,7 @@ TEST(memory_large_allocations)
CHECK(doc.append_child(node_pcdata).set_value(s.c_str())); CHECK(doc.append_child(node_pcdata).set_value(s.c_str()));
} }
CHECK(allocate_count > 0 && deallocate_count == 0); CHECK(page_allocs > 0 && page_deallocs == 0);
// grow-prune loop // grow-prune loop
while (doc.first_child()) while (doc.first_child())
@ -116,15 +123,15 @@ TEST(memory_large_allocations)
} }
} }
CHECK(allocate_count == deallocate_count + 1); // only one live page left (it waits for new allocations) CHECK(page_allocs == page_deallocs + 1); // only one live page left (it waits for new allocations)
char buffer; char buffer;
CHECK(doc.load_buffer_inplace(&buffer, 0, parse_fragment, get_native_encoding())); CHECK(doc.load_buffer_inplace(&buffer, 0, parse_fragment, get_native_encoding()));
CHECK(allocate_count == deallocate_count); // no live pages left CHECK(page_allocs == page_deallocs); // no live pages left
} }
CHECK(allocate_count == deallocate_count); // everything is freed CHECK(page_allocs == page_deallocs); // everything is freed
// restore old functions // restore old functions
set_memory_management_functions(old_allocate, old_deallocate); set_memory_management_functions(old_allocate, old_deallocate);