Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <sal/types.h>
30 : : #include <cppunit/TestFixture.h>
31 : : #include <cppunit/TestAssert.h>
32 : : #include <cppunit/extensions/HelperMacros.h>
33 : : #include "rtl/strbuf.hxx"
34 : : #include "rtl/ustrbuf.hxx"
35 : : #include "rtl/ustring.h"
36 : : #include "rtl/ustring.hxx"
37 : :
38 : : namespace test { namespace oustringbuffer {
39 : :
40 [ - + ]: 30 : class Utf32: public CppUnit::TestFixture {
41 : : private:
42 : : void appendUtf32();
43 : :
44 : : void insertUtf32();
45 : :
46 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(Utf32);
[ + - ][ + - ]
[ # # ]
47 [ + - ][ + - ]: 5 : CPPUNIT_TEST(appendUtf32);
[ + - ][ + - ]
[ + - ][ + - ]
48 [ + - ][ + - ]: 5 : CPPUNIT_TEST(insertUtf32);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
49 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
50 : : };
51 : :
52 : : } }
53 : :
54 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(test::oustringbuffer::Utf32);
55 : :
56 : : namespace {
57 : :
58 : 40 : void appendString(rtl::OStringBuffer & buffer, rtl::OUString const & string) {
59 : 40 : buffer.append('"');
60 [ + + ]: 240 : for (int i = 0; i < string.getLength(); ++i) {
61 : 200 : buffer.append(RTL_CONSTASCII_STRINGPARAM("\\u"));
62 : 200 : sal_Unicode c = string[i];
63 [ + + ]: 200 : if (c < 0x1000) {
64 : 160 : buffer.append('0');
65 [ + - ]: 160 : if (c < 0x100) {
66 : 160 : buffer.append('0');
67 [ - + ]: 160 : if (c < 0x10) {
68 : 0 : buffer.append('0');
69 : : }
70 : : }
71 : : }
72 : : buffer.append(
73 : 200 : static_cast< sal_Int32 >(c), static_cast< sal_Int16 >(16));
74 : : }
75 : 40 : buffer.append('"');
76 : 40 : }
77 : :
78 : 20 : void createMessage(
79 : : rtl::OStringBuffer & message, rtl::OUString const & string1,
80 : : rtl::OUString const & string2)
81 : : {
82 : 20 : message.setLength(0);
83 : 20 : appendString(message, string1);
84 : 20 : message.append(RTL_CONSTASCII_STRINGPARAM(" vs. "));
85 : 20 : appendString(message, string2);
86 : 20 : }
87 : :
88 : : }
89 : :
90 : 5 : void test::oustringbuffer::Utf32::appendUtf32() {
91 : 5 : int const str1Len = 3;
92 : 5 : sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
93 : 5 : int const str2Len = 4;
94 : 5 : sal_Unicode const str2[str2Len] = { 'a', 'b', 'c', 'd' };
95 : 5 : int const str3Len = 6;
96 : 5 : sal_Unicode const str3[str3Len] = { 'a', 'b', 'c', 'd', 0xD800, 0xDC00 };
97 : 5 : rtl::OStringBuffer message;
98 [ + - ]: 5 : rtl::OUStringBuffer buf1(rtl::OUString(str1, str1Len));
99 [ + - ]: 5 : buf1.appendUtf32('d');
100 [ + - ]: 5 : rtl::OUString res1(buf1.makeStringAndClear());
101 [ + - ]: 5 : createMessage(message, res1, rtl::OUString(str2, str2Len));
102 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
103 [ + - ]: 5 : (const char *) message.getStr(), res1 == rtl::OUString(str2, str2Len));
104 [ + - ]: 5 : rtl::OUStringBuffer buf2(rtl::OUString(str2, str2Len));
105 [ + - ]: 5 : buf2.appendUtf32(0x10000);
106 [ + - ]: 5 : rtl::OUString res2(buf2.makeStringAndClear());
107 [ + - ]: 5 : createMessage(message, res2, rtl::OUString(str3, str3Len));
108 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
109 [ + - ]: 10 : (const char *)message.getStr(), res2 == rtl::OUString(str3, str3Len));
110 : 5 : }
111 : :
112 : 5 : void test::oustringbuffer::Utf32::insertUtf32() {
113 : 5 : int const str1Len = 3;
114 : 5 : sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
115 : 5 : int const str2Len = 4;
116 : 5 : sal_Unicode const str2[str2Len] = { 'a', 'b', 'd', 'c' };
117 : 5 : int const str3Len = 6;
118 : 5 : sal_Unicode const str3[str3Len] = { 'a', 'b', 0xDBFF, 0xDFFF, 'd', 'c' };
119 : 5 : rtl::OStringBuffer message;
120 [ + - ]: 5 : rtl::OUStringBuffer buf1(rtl::OUString(str1, str1Len));
121 : 5 : buf1.insertUtf32(2, 'd');
122 [ + - ]: 5 : rtl::OUString res1(buf1.makeStringAndClear());
123 [ + - ]: 5 : createMessage(message, res1, rtl::OUString(str2, str2Len));
124 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
125 [ + - ]: 5 : (const char *) message.getStr(), res1 == rtl::OUString(str2, str2Len));
126 [ + - ]: 5 : rtl::OUStringBuffer buf2(rtl::OUString(str2, str2Len));
127 : 5 : buf2.insertUtf32(2, 0x10FFFF);
128 [ + - ]: 5 : rtl::OUString res2(buf2.makeStringAndClear());
129 [ + - ]: 5 : createMessage(message, res2, rtl::OUString(str3, str3Len));
130 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
131 [ + - ]: 10 : (const char *) message.getStr(), res2 == rtl::OUString(str3, str3Len));
132 [ + - ][ + - ]: 20 : }
133 : :
134 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|