summaryrefslogtreecommitdiff
path: root/tests/test_memory.cpp
diff options
context:
space:
mode:
authorarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2009-10-20 21:39:43 +0000
committerarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2009-10-20 21:39:43 +0000
commit0ceaa38aeb258b6cc055fa002f718a921d52c6ab (patch)
tree868027b15390ee6fea0c395dc522b531d35ea03f /tests/test_memory.cpp
parent009f3d50d2736b0b7d2457d9aa67cd967f39e316 (diff)
tests: Added forgotten test files
git-svn-id: http://pugixml.googlecode.com/svn/trunk@168 99668b35-9821-0410-8761-19e4c4f06640
Diffstat (limited to 'tests/test_memory.cpp')
-rw-r--r--tests/test_memory.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/test_memory.cpp b/tests/test_memory.cpp
new file mode 100644
index 0000000..8f2d41b
--- /dev/null
+++ b/tests/test_memory.cpp
@@ -0,0 +1,66 @@
+#include "common.hpp"
+
+namespace
+{
+ char buffer[8];
+ int allocate_count = 0;
+ int deallocate_count = 0;
+
+ void* allocate(size_t size)
+ {
+ CHECK(size == 8);
+ ++allocate_count;
+ return buffer;
+ }
+
+ void deallocate(void* ptr)
+ {
+ CHECK(ptr == buffer);
+ ++deallocate_count;
+ }
+}
+
+TEST(custom_memory_management)
+{
+ // remember old functions
+ allocation_function old_allocate = get_memory_allocation_function();
+ deallocation_function old_deallocate = get_memory_deallocation_function();
+
+ // replace functions
+ set_memory_management_functions(allocate, deallocate);
+
+ try
+ {
+ {
+ // parse document
+ xml_document doc;
+ CHECK(doc.load("<node/>"));
+
+ CHECK(allocate_count == 1);
+ CHECK(deallocate_count == 0);
+ CHECK_STRING(buffer, "<node\0>");
+
+ // modify document
+ doc.child("node").set_name("foobars");
+
+ CHECK(allocate_count == 2);
+ CHECK(deallocate_count == 0);
+ CHECK_STRING(buffer, "foobars");
+ }
+
+ CHECK(allocate_count == 2);
+ CHECK(deallocate_count == 2);
+ CHECK_STRING(buffer, "foobars");
+
+ // restore old functions
+ set_memory_management_functions(old_allocate, old_deallocate);
+ }
+ catch (const char* error)
+ {
+ // restore old functions
+ set_memory_management_functions(old_allocate, old_deallocate);
+
+ // rethrow
+ throw error;
+ }
+}