1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
29 namespace test::oustringbuffer
{
31 class Utf32
: public CppUnit::TestFixture
{
37 CPPUNIT_TEST_SUITE(Utf32
);
38 CPPUNIT_TEST(appendUtf32
);
39 CPPUNIT_TEST(insertUtf32
);
40 CPPUNIT_TEST_SUITE_END();
45 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustringbuffer::Utf32
);
49 void appendString(OStringBuffer
& buffer
, OUString
const & string
) {
51 for (int i
= 0; i
< string
.getLength(); ++i
) {
53 sal_Unicode c
= string
[i
];
64 static_cast< sal_Int32
>(c
), static_cast< sal_Int16
>(16));
70 OStringBuffer
& message
, OUString
const & string1
,
71 OUString
const & string2
)
74 appendString(message
, string1
);
75 message
.append(" vs. ");
76 appendString(message
, string2
);
81 void test::oustringbuffer::Utf32::appendUtf32() {
82 int const str1Len
= 3;
83 sal_Unicode
const str1
[str1Len
] = { 'a', 'b', 'c' };
84 static constexpr OUStringLiteral str2
= u
"abcd";
85 static constexpr OUStringLiteral str3
= u
"abcd\U00010000";
86 OStringBuffer message
;
87 OUStringBuffer
buf1(std::u16string_view(str1
, str1Len
));
88 buf1
.appendUtf32('d');
89 OUString
res1(buf1
.makeStringAndClear());
90 createMessage(message
, res1
, str2
);
91 CPPUNIT_ASSERT_EQUAL_MESSAGE(
92 message
.getStr(), OUString(str2
), res1
);
93 OUStringBuffer
buf2(str2
);
94 buf2
.appendUtf32(0x10000);
95 OUString
res2(buf2
.makeStringAndClear());
96 createMessage(message
, res2
, str3
);
97 CPPUNIT_ASSERT_EQUAL_MESSAGE(
98 message
.getStr(), OUString(str3
), res2
);
101 void test::oustringbuffer::Utf32::insertUtf32() {
102 int const str1Len
= 3;
103 sal_Unicode
const str1
[str1Len
] = { 'a', 'b', 'c' };
104 static constexpr OUStringLiteral str2
= u
"abdc";
105 static constexpr OUStringLiteral str3
= u
"ab\U0010FFFFdc";
106 OStringBuffer message
;
107 OUStringBuffer
buf1(std::u16string_view(str1
, str1Len
));
108 buf1
.insertUtf32(2, 'd');
109 OUString
res1(buf1
.makeStringAndClear());
110 createMessage(message
, res1
, str2
);
111 CPPUNIT_ASSERT_EQUAL_MESSAGE(
112 message
.getStr(), OUString(str2
), res1
);
113 OUStringBuffer
buf2(str2
);
114 buf2
.insertUtf32(2, 0x10FFFF);
115 OUString
res2(buf2
.makeStringAndClear());
116 createMessage(message
, res2
, str3
);
117 CPPUNIT_ASSERT_EQUAL_MESSAGE(
118 message
.getStr(), OUString(str3
), res2
);
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */