merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / qa / rtl / strings / test_oustringbuffer_utf32.cxx
blob4ec593f54eddb90c7bb2f69b0f84aaeab84fbdca
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
31 #include "testshl/simpleheader.hxx"
32 #include "rtl/ustrbuf.hxx"
33 #include "rtl/ustring.h"
34 #include "rtl/ustring.hxx"
36 namespace test { namespace oustringbuffer {
38 class Utf32: public CppUnit::TestFixture {
39 private:
40 void appendUtf32();
42 void insertUtf32();
44 CPPUNIT_TEST_SUITE(Utf32);
45 CPPUNIT_TEST(appendUtf32);
46 CPPUNIT_TEST(insertUtf32);
47 CPPUNIT_TEST_SUITE_END();
50 } }
52 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test::oustringbuffer::Utf32, "alltest");
54 namespace {
56 void appendString(rtl::OUStringBuffer & buffer, rtl::OUString const & string) {
57 buffer.append(static_cast< sal_Unicode >('"'));
58 for (int i = 0; i < string.getLength(); ++i) {
59 buffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("\\u"));
60 sal_Unicode c = string[i];
61 if (c < 0x1000) {
62 buffer.append(static_cast< sal_Unicode >('0'));
63 if (c < 0x100) {
64 buffer.append(static_cast< sal_Unicode >('0'));
65 if (c < 0x10) {
66 buffer.append(static_cast< sal_Unicode >('0'));
70 buffer.append(
71 static_cast< sal_Int32 >(c), static_cast< sal_Int16 >(16));
73 buffer.append(static_cast< sal_Unicode >('"'));
76 void createMessage(
77 rtl::OUStringBuffer & message, rtl::OUString const & string1,
78 rtl::OUString const & string2)
80 message.setLength(0);
81 appendString(message, string1);
82 message.appendAscii(RTL_CONSTASCII_STRINGPARAM(" vs. "));
83 appendString(message, string2);
88 void test::oustringbuffer::Utf32::appendUtf32() {
89 int const str1Len = 3;
90 sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
91 int const str2Len = 4;
92 sal_Unicode const str2[str2Len] = { 'a', 'b', 'c', 'd' };
93 int const str3Len = 6;
94 sal_Unicode const str3[str3Len] = { 'a', 'b', 'c', 'd', 0xD800, 0xDC00 };
95 rtl::OUStringBuffer message;
96 rtl::OUStringBuffer buf1(rtl::OUString(str1, str1Len));
97 buf1.appendUtf32('d');
98 rtl::OUString res1(buf1.makeStringAndClear());
99 createMessage(message, res1, rtl::OUString(str2, str2Len));
100 CPPUNIT_ASSERT_MESSAGE(
101 message.getStr(), res1 == rtl::OUString(str2, str2Len));
102 rtl::OUStringBuffer buf2(rtl::OUString(str2, str2Len));
103 buf2.appendUtf32(0x10000);
104 rtl::OUString res2(buf2.makeStringAndClear());
105 createMessage(message, res2, rtl::OUString(str3, str3Len));
106 CPPUNIT_ASSERT_MESSAGE(
107 message.getStr(), res2 == rtl::OUString(str3, str3Len));
110 void test::oustringbuffer::Utf32::insertUtf32() {
111 int const str1Len = 3;
112 sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
113 int const str2Len = 4;
114 sal_Unicode const str2[str2Len] = { 'a', 'b', 'd', 'c' };
115 int const str3Len = 6;
116 sal_Unicode const str3[str3Len] = { 'a', 'b', 0xDBFF, 0xDFFF, 'd', 'c' };
117 rtl::OUStringBuffer message;
118 rtl::OUStringBuffer buf1(rtl::OUString(str1, str1Len));
119 buf1.insertUtf32(2, 'd');
120 rtl::OUString res1(buf1.makeStringAndClear());
121 createMessage(message, res1, rtl::OUString(str2, str2Len));
122 CPPUNIT_ASSERT_MESSAGE(
123 message.getStr(), res1 == rtl::OUString(str2, str2Len));
124 rtl::OUStringBuffer buf2(rtl::OUString(str2, str2Len));
125 buf2.insertUtf32(2, 0x10FFFF);
126 rtl::OUString res2(buf2.makeStringAndClear());
127 createMessage(message, res2, rtl::OUString(str3, str3Len));
128 CPPUNIT_ASSERT_MESSAGE(
129 message.getStr(), res2 == rtl::OUString(str3, str3Len));