1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_oustring_convert.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 #include "cppunit/simpleheader.hxx"
35 #include "rtl/strbuf.hxx"
36 #include "rtl/string.hxx"
37 #include "rtl/ustring.hxx"
39 namespace test
{ namespace oustring
{
41 class Convert
: public CppUnit::TestFixture
44 void convertToString();
46 CPPUNIT_TEST_SUITE(Convert
);
47 CPPUNIT_TEST(convertToString
);
48 CPPUNIT_TEST_SUITE_END();
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test::oustring::Convert
, "alltest");
57 struct TestConvertToString
59 sal_Unicode aSource
[100];
61 rtl_TextEncoding nEncoding
;
64 char const * pRelaxed
;
67 void testConvertToString(TestConvertToString
const & rTest
)
69 const rtl::OUString
aSource(rTest
.aSource
, rTest
.nLength
);
70 rtl::OString
aStrict(RTL_CONSTASCII_STRINGPARAM("12345"));
71 bool bSuccess
= aSource
.convertToString(&aStrict
, rTest
.nEncoding
,
73 rtl::OString
aRelaxed(rtl::OUStringToOString(aSource
, rTest
.nEncoding
,
76 rtl::OStringBuffer aPrefix
;
77 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM("{"));
78 for (sal_Int32 i
= 0; i
< rTest
.nLength
; ++i
)
80 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM("U+"));
81 aPrefix
.append(static_cast< sal_Int32
>(rTest
.aSource
[i
]), 16);
82 if (i
+ 1 < rTest
.nLength
)
83 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM(","));
85 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM("}, "));
86 aPrefix
.append(static_cast< sal_Int32
>(rTest
.nEncoding
));
87 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM(", 0x"));
88 aPrefix
.append(static_cast< sal_Int32
>(rTest
.nFlags
), 16);
89 aPrefix
.append(RTL_CONSTASCII_STRINGPARAM(" -> "));
93 if (rTest
.pStrict
== 0 || !aStrict
.equals(rTest
.pStrict
))
95 rtl::OStringBuffer
aMessage(aPrefix
);
96 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("strict = \""));
97 aMessage
.append(aStrict
);
98 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("\""));
99 CPPUNIT_ASSERT_MESSAGE(aMessage
.getStr(), false);
104 if (!aStrict
.equals(rtl::OString(RTL_CONSTASCII_STRINGPARAM("12345"))))
106 rtl::OStringBuffer
aMessage(aPrefix
);
107 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("modified output"));
108 CPPUNIT_ASSERT_MESSAGE(aMessage
.getStr(), false);
110 if (rTest
.pStrict
!= 0)
112 rtl::OStringBuffer
aMessage(aPrefix
);
113 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("failed"));
114 CPPUNIT_ASSERT_MESSAGE(aMessage
.getStr(), false);
117 if (!aRelaxed
.equals(rTest
.pRelaxed
))
119 rtl::OStringBuffer
aMessage(aPrefix
);
120 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("relaxed = \""));
121 aMessage
.append(aRelaxed
);
122 aMessage
.append(RTL_CONSTASCII_STRINGPARAM("\""));
123 CPPUNIT_ASSERT_MESSAGE(aMessage
.getStr(), false);
129 void test::oustring::Convert::convertToString()
131 TestConvertToString
const aTests
[]
134 RTL_TEXTENCODING_ASCII_US
,
135 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
136 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
,
141 RTL_TEXTENCODING_ASCII_US
,
142 OUSTRING_TO_OSTRING_CVTFLAGS
,
145 { { 0x0041,0x0042,0x0043 },
147 RTL_TEXTENCODING_ASCII_US
,
148 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
149 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
,
152 { { 0x0041,0x0042,0x0043 },
154 RTL_TEXTENCODING_ASCII_US
,
155 OUSTRING_TO_OSTRING_CVTFLAGS
,
160 RTL_TEXTENCODING_ISO_2022_JP
,
161 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
162 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
,
165 // the next also tests that a short source produces a long target:
168 RTL_TEXTENCODING_ISO_2022_JP
,
169 OUSTRING_TO_OSTRING_CVTFLAGS
,
172 { { 0x0041,0x0100,0x0042 },
174 RTL_TEXTENCODING_ISO_8859_1
,
175 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
176 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
,
179 { { 0x0041,0x0100,0x0042 },
181 RTL_TEXTENCODING_ISO_8859_1
,
182 OUSTRING_TO_OSTRING_CVTFLAGS
,
185 for (unsigned int i
= 0; i
< sizeof aTests
/ sizeof aTests
[0]; ++i
)
186 testConvertToString(aTests
[i
]);