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