1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sal.hxx"
26 
27 #include "testshl/simpleheader.hxx"
28 #include "rtl/ustrbuf.hxx"
29 #include "rtl/ustring.h"
30 #include "rtl/ustring.hxx"
31 
32 namespace test { namespace oustringbuffer {
33 
34 class Utf32: public CppUnit::TestFixture {
35 private:
36     void appendUtf32();
37 
38     void insertUtf32();
39 
40     CPPUNIT_TEST_SUITE(Utf32);
41     CPPUNIT_TEST(appendUtf32);
42     CPPUNIT_TEST(insertUtf32);
43     CPPUNIT_TEST_SUITE_END();
44 };
45 
46 } }
47 
48 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test::oustringbuffer::Utf32, "alltest");
49 
50 namespace {
51 
52 void appendString(rtl::OUStringBuffer & buffer, rtl::OUString const & string) {
53     buffer.append(static_cast< sal_Unicode >('"'));
54     for (int i = 0; i < string.getLength(); ++i) {
55         buffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("\\u"));
56         sal_Unicode c = string[i];
57         if (c < 0x1000) {
58             buffer.append(static_cast< sal_Unicode >('0'));
59             if (c < 0x100) {
60                 buffer.append(static_cast< sal_Unicode >('0'));
61                 if (c < 0x10) {
62                     buffer.append(static_cast< sal_Unicode >('0'));
63                 }
64             }
65         }
66         buffer.append(
67             static_cast< sal_Int32 >(c), static_cast< sal_Int16 >(16));
68     }
69     buffer.append(static_cast< sal_Unicode >('"'));
70 }
71 
72 void createMessage(
73     rtl::OUStringBuffer & message, rtl::OUString const & string1,
74     rtl::OUString const & string2)
75 {
76     message.setLength(0);
77     appendString(message, string1);
78     message.appendAscii(RTL_CONSTASCII_STRINGPARAM(" vs. "));
79     appendString(message, string2);
80 }
81 
82 }
83 
84 void test::oustringbuffer::Utf32::appendUtf32() {
85     int const str1Len = 3;
86     sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
87     int const str2Len = 4;
88     sal_Unicode const str2[str2Len] = { 'a', 'b', 'c', 'd' };
89     int const str3Len = 6;
90     sal_Unicode const str3[str3Len] = { 'a', 'b', 'c', 'd', 0xD800, 0xDC00 };
91     rtl::OUStringBuffer message;
92     rtl::OUStringBuffer buf1(rtl::OUString(str1, str1Len));
93     buf1.appendUtf32('d');
94     rtl::OUString res1(buf1.makeStringAndClear());
95     createMessage(message, res1, rtl::OUString(str2, str2Len));
96     CPPUNIT_ASSERT_MESSAGE(
97         message.getStr(), res1 == rtl::OUString(str2, str2Len));
98     rtl::OUStringBuffer buf2(rtl::OUString(str2, str2Len));
99     buf2.appendUtf32(0x10000);
100     rtl::OUString res2(buf2.makeStringAndClear());
101     createMessage(message, res2, rtl::OUString(str3, str3Len));
102     CPPUNIT_ASSERT_MESSAGE(
103         message.getStr(), res2 == rtl::OUString(str3, str3Len));
104 }
105 
106 void test::oustringbuffer::Utf32::insertUtf32() {
107     int const str1Len = 3;
108     sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
109     int const str2Len = 4;
110     sal_Unicode const str2[str2Len] = { 'a', 'b', 'd', 'c' };
111     int const str3Len = 6;
112     sal_Unicode const str3[str3Len] = { 'a', 'b', 0xDBFF, 0xDFFF, 'd', 'c' };
113     rtl::OUStringBuffer message;
114     rtl::OUStringBuffer buf1(rtl::OUString(str1, str1Len));
115     buf1.insertUtf32(2, 'd');
116     rtl::OUString res1(buf1.makeStringAndClear());
117     createMessage(message, res1, rtl::OUString(str2, str2Len));
118     CPPUNIT_ASSERT_MESSAGE(
119         message.getStr(), res1 == rtl::OUString(str2, str2Len));
120     rtl::OUStringBuffer buf2(rtl::OUString(str2, str2Len));
121     buf2.insertUtf32(2, 0x10FFFF);
122     rtl::OUString res2(buf2.makeStringAndClear());
123     createMessage(message, res2, rtl::OUString(str3, str3Len));
124     CPPUNIT_ASSERT_MESSAGE(
125         message.getStr(), res2 == rtl::OUString(str3, str3Len));
126 }
127