Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/types.h>
21 : #include <cppunit/TestFixture.h>
22 : #include <cppunit/TestAssert.h>
23 : #include <cppunit/extensions/HelperMacros.h>
24 : #include "rtl/strbuf.hxx"
25 : #include "rtl/ustrbuf.hxx"
26 : #include "rtl/ustring.h"
27 : #include "rtl/ustring.hxx"
28 :
29 : namespace test { namespace oustringbuffer {
30 :
31 6 : class Utf32: public CppUnit::TestFixture {
32 : private:
33 : void appendUtf32();
34 :
35 : void insertUtf32();
36 :
37 2 : CPPUNIT_TEST_SUITE(Utf32);
38 1 : CPPUNIT_TEST(appendUtf32);
39 1 : CPPUNIT_TEST(insertUtf32);
40 5 : CPPUNIT_TEST_SUITE_END();
41 : };
42 :
43 : } }
44 :
45 1 : CPPUNIT_TEST_SUITE_REGISTRATION(test::oustringbuffer::Utf32);
46 :
47 : namespace {
48 :
49 8 : void appendString(rtl::OStringBuffer & buffer, rtl::OUString const & string) {
50 8 : buffer.append('"');
51 48 : for (int i = 0; i < string.getLength(); ++i) {
52 40 : buffer.append(RTL_CONSTASCII_STRINGPARAM("\\u"));
53 40 : sal_Unicode c = string[i];
54 40 : if (c < 0x1000) {
55 32 : buffer.append('0');
56 32 : if (c < 0x100) {
57 32 : buffer.append('0');
58 32 : if (c < 0x10) {
59 0 : buffer.append('0');
60 : }
61 : }
62 : }
63 : buffer.append(
64 40 : static_cast< sal_Int32 >(c), static_cast< sal_Int16 >(16));
65 : }
66 8 : buffer.append('"');
67 8 : }
68 :
69 4 : void createMessage(
70 : rtl::OStringBuffer & message, rtl::OUString const & string1,
71 : rtl::OUString const & string2)
72 : {
73 4 : message.setLength(0);
74 4 : appendString(message, string1);
75 4 : message.append(RTL_CONSTASCII_STRINGPARAM(" vs. "));
76 4 : appendString(message, string2);
77 4 : }
78 :
79 : }
80 :
81 1 : void test::oustringbuffer::Utf32::appendUtf32() {
82 1 : int const str1Len = 3;
83 1 : sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
84 1 : int const str2Len = 4;
85 1 : sal_Unicode const str2[str2Len] = { 'a', 'b', 'c', 'd' };
86 1 : int const str3Len = 6;
87 1 : sal_Unicode const str3[str3Len] = { 'a', 'b', 'c', 'd', 0xD800, 0xDC00 };
88 1 : rtl::OStringBuffer message;
89 2 : rtl::OUStringBuffer buf1(rtl::OUString(str1, str1Len));
90 1 : buf1.appendUtf32('d');
91 2 : rtl::OUString res1(buf1.makeStringAndClear());
92 1 : createMessage(message, res1, rtl::OUString(str2, str2Len));
93 2 : CPPUNIT_ASSERT_MESSAGE(
94 1 : message.getStr(), res1 == rtl::OUString(str2, str2Len));
95 2 : rtl::OUStringBuffer buf2(rtl::OUString(str2, str2Len));
96 1 : buf2.appendUtf32(0x10000);
97 2 : rtl::OUString res2(buf2.makeStringAndClear());
98 1 : createMessage(message, res2, rtl::OUString(str3, str3Len));
99 2 : CPPUNIT_ASSERT_MESSAGE(
100 2 : message.getStr(), res2 == rtl::OUString(str3, str3Len));
101 1 : }
102 :
103 1 : void test::oustringbuffer::Utf32::insertUtf32() {
104 1 : int const str1Len = 3;
105 1 : sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
106 1 : int const str2Len = 4;
107 1 : sal_Unicode const str2[str2Len] = { 'a', 'b', 'd', 'c' };
108 1 : int const str3Len = 6;
109 1 : sal_Unicode const str3[str3Len] = { 'a', 'b', 0xDBFF, 0xDFFF, 'd', 'c' };
110 1 : rtl::OStringBuffer message;
111 2 : rtl::OUStringBuffer buf1(rtl::OUString(str1, str1Len));
112 1 : buf1.insertUtf32(2, 'd');
113 2 : rtl::OUString res1(buf1.makeStringAndClear());
114 1 : createMessage(message, res1, rtl::OUString(str2, str2Len));
115 2 : CPPUNIT_ASSERT_MESSAGE(
116 1 : message.getStr(), res1 == rtl::OUString(str2, str2Len));
117 2 : rtl::OUStringBuffer buf2(rtl::OUString(str2, str2Len));
118 1 : buf2.insertUtf32(2, 0x10FFFF);
119 2 : rtl::OUString res2(buf2.makeStringAndClear());
120 1 : createMessage(message, res2, rtl::OUString(str3, str3Len));
121 2 : CPPUNIT_ASSERT_MESSAGE(
122 2 : message.getStr(), res2 == rtl::OUString(str3, str3Len));
123 4 : }
124 :
125 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|