From 0ceaa38aeb258b6cc055fa002f718a921d52c6ab Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Tue, 20 Oct 2009 21:39:43 +0000 Subject: tests: Added forgotten test files git-svn-id: http://pugixml.googlecode.com/svn/trunk@168 99668b35-9821-0410-8761-19e4c4f06640 --- tests/test_memory.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_unicode.cpp | 37 ++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 tests/test_memory.cpp create mode 100644 tests/test_unicode.cpp (limited to 'tests') 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("")); + + CHECK(allocate_count == 1); + CHECK(deallocate_count == 0); + CHECK_STRING(buffer, ""); + + // 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; + } +} diff --git a/tests/test_unicode.cpp b/tests/test_unicode.cpp new file mode 100644 index 0000000..b763355 --- /dev/null +++ b/tests/test_unicode.cpp @@ -0,0 +1,37 @@ +#include "common.hpp" + +// letters taken from http://www.utf8-chartable.de/ + +TEST(as_utf16) +{ + // valid 1-byte, 2-byte and 3-byte inputs + CHECK(as_utf16("?\xd0\x80\xe2\x80\xbd") == L"?\x0400\x203D"); + + // invalid 1-byte input + CHECK(as_utf16("\xb0") == L" "); + + // valid 4-byte input + std::wstring b4 = as_utf16("\xf2\x97\x98\xa4 \xf4\x80\x8f\xbf"); + CHECK(b4.size() == 3 && b4[0] == (wchar_t)0x97624 && b4[1] == L' ' && b4[2] == (wchar_t)0x1003ff); + + // invalid 5-byte input + std::wstring b5 = as_utf16("\xf8\nbcd"); + CHECK(b5 == L" \nbcd"); +} + +TEST(as_utf8) +{ + // valid 1-byte, 2-byte and 3-byte outputs + CHECK(as_utf8(L"?\x0400\x203D") == "?\xd0\x80\xe2\x80\xbd"); + + // valid 4-byte output +#if 0 + // requires 4-byte wchar_t :( + CHECK(as_utf8(L"\x97624 \x1003ff") == "\xf2\x97\x98\xa4 \xf4\x80\x8f\xbf"); +#endif +} + +TEST_XML(parse_bom_utf8, "\xef\xbb\xbf") +{ + CHECK_NODE(doc, ""); +} -- cgit v1.2.3