From 47c23efe6215e24e390d820b0ef0412655b455e3 Mon Sep 17 00:00:00 2001
From: "arseny.kapoulkine"
 <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>
Date: Mon, 10 May 2010 08:59:48 +0000
Subject: Reworked DOM memory allocation scheme (name/value allocations use the
 same pages as node/attribute structures, pages are now deallocated when
 completely free)

git-svn-id: http://pugixml.googlecode.com/svn/trunk@401 99668b35-9821-0410-8761-19e4c4f06640
---
 tests/test_dom_modify.cpp |  2 +-
 tests/test_memory.cpp     | 50 +++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 43 insertions(+), 9 deletions(-)

(limited to 'tests')

diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp
index c80e0e9..8b869f4 100644
--- a/tests/test_dom_modify.cpp
+++ b/tests/test_dom_modify.cpp
@@ -494,7 +494,7 @@ TEST_XML_FLAGS(dom_node_copy_types, "<root><?xml version='1.0'?><?pi value?><!--
 	CHECK_NODE(doc, STR("<root><?xml version=\"1.0\"?><?pi value?><!--comment--><node id=\"1\">pcdata<![CDATA[cdata]]></node></root><root><?xml version=\"1.0\"?><?pi value?><!--comment--><node id=\"1\">pcdata<![CDATA[cdata]]></node></root>"));
 }
 
-TEST_XML(dom_attr_assign_large, "<node attr1='' attr2='' />")
+TEST_XML(dom_attr_assign_large_number, "<node attr1='' attr2='' />")
 {
 	xml_node node = doc.child(STR("node"));
 
diff --git a/tests/test_memory.cpp b/tests/test_memory.cpp
index 7ca87d6..474cf45 100644
--- a/tests/test_memory.cpp
+++ b/tests/test_memory.cpp
@@ -1,22 +1,22 @@
 #include "common.hpp"
 
+#include <string>
+
 namespace
 {
-	pugi::char_t buffer[8];
 	int allocate_count = 0;
 	int deallocate_count = 0;
 
 	void* allocate(size_t size)
 	{
-		CHECK(size == sizeof(pugi::char_t) * 8);
 		++allocate_count;
-		return buffer;
+		return new char[size];
 	}
 
 	void deallocate(void* ptr)
 	{
-		CHECK(ptr == buffer);
 		++deallocate_count;
+		delete[] reinterpret_cast<char*>(ptr);
 	}
 }
 
@@ -34,22 +34,56 @@ TEST(custom_memory_management)
 		xml_document doc;
 		CHECK(doc.load(STR("<node />")));
 	
-		CHECK(allocate_count == 1);
+		CHECK(allocate_count == 2);
 		CHECK(deallocate_count == 0);
-		CHECK_STRING(buffer, STR("<node"));
 
 		// modify document
 		doc.child(STR("node")).set_name(STR("foobars"));
 
 		CHECK(allocate_count == 2);
 		CHECK(deallocate_count == 0);
-		CHECK_STRING(buffer, STR("foobars"));
 	}
 
 	CHECK(allocate_count == 2);
 	CHECK(deallocate_count == 2);
-	CHECK_STRING(buffer, STR("foobars"));
 
 	// restore old functions
 	set_memory_management_functions(old_allocate, old_deallocate);
 }
+
+TEST(large_allocations)
+{
+	xml_document doc;
+
+	// initial fill
+	for (size_t i = 0; i < 128; ++i)
+	{
+		std::basic_string<pugi::char_t> s(i * 128, 'x');
+
+		CHECK(doc.append_child(node_pcdata).set_value(s.c_str()));
+	}
+
+	// grow-prune loop
+	while (doc.first_child())
+	{
+		pugi::xml_node node;
+
+		// grow
+		for (node = doc.first_child(); node; node = node.next_sibling())
+		{
+			std::basic_string<pugi::char_t> s = node.value();
+
+			CHECK(node.set_value((s + s).c_str()));
+		}
+
+		// prune
+		for (node = doc.first_child(); node; )
+		{
+			pugi::xml_node next = node.next_sibling().next_sibling();
+
+			node.parent().remove_child(node);
+
+			node = next;
+		}
+	}
+}
-- 
cgit v1.2.3