1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
30 
31 #include "testshl/simpleheader.hxx"
32 #include "rtl/ustrbuf.hxx"
33 #include "rtl/ustring.h"
34 #include "rtl/ustring.hxx"
35 
36 namespace test { namespace oustringbuffer {
37 
38 class Utf32: public CppUnit::TestFixture {
39 private:
40     void appendUtf32();
41 
42     void insertUtf32();
43 
44     CPPUNIT_TEST_SUITE(Utf32);
45     CPPUNIT_TEST(appendUtf32);
46     CPPUNIT_TEST(insertUtf32);
47     CPPUNIT_TEST_SUITE_END();
48 };
49 
50 } }
51 
52 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test::oustringbuffer::Utf32, "alltest");
53 
54 namespace {
55 
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'));
67                 }
68             }
69         }
70         buffer.append(
71             static_cast< sal_Int32 >(c), static_cast< sal_Int16 >(16));
72     }
73     buffer.append(static_cast< sal_Unicode >('"'));
74 }
75 
76 void createMessage(
77     rtl::OUStringBuffer & message, rtl::OUString const & string1,
78     rtl::OUString const & string2)
79 {
80     message.setLength(0);
81     appendString(message, string1);
82     message.appendAscii(RTL_CONSTASCII_STRINGPARAM(" vs. "));
83     appendString(message, string2);
84 }
85 
86 }
87 
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));
108 }
109 
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));
130 }
131