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 "gtest/gtest.h"
28 #include "rtl/ustrbuf.hxx"
29 #include "rtl/ustring.h"
30 #include "rtl/ustring.hxx"
31 
32 namespace {
33 
appendString(rtl::OUStringBuffer & buffer,rtl::OUString const & string)34 void appendString(rtl::OUStringBuffer & buffer, rtl::OUString const & string) {
35     buffer.append(static_cast< sal_Unicode >('"'));
36     for (int i = 0; i < string.getLength(); ++i) {
37         buffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("\\u"));
38         sal_Unicode c = string[i];
39         if (c < 0x1000) {
40             buffer.append(static_cast< sal_Unicode >('0'));
41             if (c < 0x100) {
42                 buffer.append(static_cast< sal_Unicode >('0'));
43                 if (c < 0x10) {
44                     buffer.append(static_cast< sal_Unicode >('0'));
45                 }
46             }
47         }
48         buffer.append(
49             static_cast< sal_Int32 >(c), static_cast< sal_Int16 >(16));
50     }
51     buffer.append(static_cast< sal_Unicode >('"'));
52 }
53 
createMessage(rtl::OUStringBuffer & message,rtl::OUString const & string1,rtl::OUString const & string2)54 void createMessage(
55     rtl::OUStringBuffer & message, rtl::OUString const & string1,
56     rtl::OUString const & string2)
57 {
58     message.setLength(0);
59     appendString(message, string1);
60     message.appendAscii(RTL_CONSTASCII_STRINGPARAM(" vs. "));
61     appendString(message, string2);
62 }
63 
64 }
65 
66 
67 namespace test { namespace oustringbuffer {
68 
69 class Utf32: public ::testing::Test {
70 };
71 
TEST_F(Utf32,appendUtf32)72 TEST_F(Utf32, appendUtf32) {
73     int const str1Len = 3;
74     sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
75     int const str2Len = 4;
76     sal_Unicode const str2[str2Len] = { 'a', 'b', 'c', 'd' };
77     int const str3Len = 6;
78     sal_Unicode const str3[str3Len] = { 'a', 'b', 'c', 'd', 0xD800, 0xDC00 };
79     rtl::OUStringBuffer message;
80     rtl::OUStringBuffer buf1(rtl::OUString(str1, str1Len));
81     buf1.appendUtf32('d');
82     rtl::OUString res1(buf1.makeStringAndClear());
83     createMessage(message, res1, rtl::OUString(str2, str2Len));
84     ASSERT_TRUE(
85         res1 == rtl::OUString(str2, str2Len)) << message.getStr();
86     rtl::OUStringBuffer buf2(rtl::OUString(str2, str2Len));
87     buf2.appendUtf32(0x10000);
88     rtl::OUString res2(buf2.makeStringAndClear());
89     createMessage(message, res2, rtl::OUString(str3, str3Len));
90     ASSERT_TRUE(
91         res2 == rtl::OUString(str3, str3Len)) << message.getStr();
92 }
93 
TEST_F(Utf32,insertUtf32)94 TEST_F(Utf32, insertUtf32) {
95     int const str1Len = 3;
96     sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
97     int const str2Len = 4;
98     sal_Unicode const str2[str2Len] = { 'a', 'b', 'd', 'c' };
99     int const str3Len = 6;
100     sal_Unicode const str3[str3Len] = { 'a', 'b', 0xDBFF, 0xDFFF, 'd', 'c' };
101     rtl::OUStringBuffer message;
102     rtl::OUStringBuffer buf1(rtl::OUString(str1, str1Len));
103     buf1.insertUtf32(2, 'd');
104     rtl::OUString res1(buf1.makeStringAndClear());
105     createMessage(message, res1, rtl::OUString(str2, str2Len));
106     ASSERT_TRUE(
107         res1 == rtl::OUString(str2, str2Len)) << message.getStr();
108     rtl::OUStringBuffer buf2(rtl::OUString(str2, str2Len));
109     buf2.insertUtf32(2, 0x10FFFF);
110     rtl::OUString res2(buf2.makeStringAndClear());
111     createMessage(message, res2, rtl::OUString(str3, str3Len));
112     ASSERT_TRUE(
113         res2 == rtl::OUString(str3, str3Len)) << message.getStr();
114 }
115 
116 } }
117 
118 
119 
120