update dev300-m57
[ooovba.git] / sal / qa / rtl / strings / test_oustring_convert.cxx
blobe868f303e7d09099d6af38065e5652d4527093b4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_oustring_convert.cxx,v $
10 * $Revision: 1.6 $
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
43 private:
44 void convertToString();
46 CPPUNIT_TEST_SUITE(Convert);
47 CPPUNIT_TEST(convertToString);
48 CPPUNIT_TEST_SUITE_END();
51 } }
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test::oustring::Convert, "alltest");
55 namespace {
57 struct TestConvertToString
59 sal_Unicode aSource[100];
60 sal_Int32 nLength;
61 rtl_TextEncoding nEncoding;
62 sal_uInt32 nFlags;
63 char const * pStrict;
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,
72 rTest.nFlags);
73 rtl::OString aRelaxed(rtl::OUStringToOString(aSource, rTest.nEncoding,
74 rTest.nFlags));
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(" -> "));
91 if (bSuccess)
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);
102 else
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[]
132 = { { { 0 },
134 RTL_TEXTENCODING_ASCII_US,
135 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
136 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
138 "" },
139 { { 0 },
141 RTL_TEXTENCODING_ASCII_US,
142 OUSTRING_TO_OSTRING_CVTFLAGS,
144 "" },
145 { { 0x0041,0x0042,0x0043 },
147 RTL_TEXTENCODING_ASCII_US,
148 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
149 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
150 "ABC",
151 "ABC" },
152 { { 0x0041,0x0042,0x0043 },
154 RTL_TEXTENCODING_ASCII_US,
155 OUSTRING_TO_OSTRING_CVTFLAGS,
156 "ABC",
157 "ABC" },
158 { { 0xB800 },
160 RTL_TEXTENCODING_ISO_2022_JP,
161 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
162 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
164 "" },
165 // the next also tests that a short source produces a long target:
166 { { 0xB800 },
168 RTL_TEXTENCODING_ISO_2022_JP,
169 OUSTRING_TO_OSTRING_CVTFLAGS,
170 "\x1B(B?",
171 "\x1B(B?" },
172 { { 0x0041,0x0100,0x0042 },
174 RTL_TEXTENCODING_ISO_8859_1,
175 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
176 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
178 "A" },
179 { { 0x0041,0x0100,0x0042 },
181 RTL_TEXTENCODING_ISO_8859_1,
182 OUSTRING_TO_OSTRING_CVTFLAGS,
183 "A?B",
184 "A?B" } };
185 for (unsigned int i = 0; i < sizeof aTests / sizeof aTests[0]; ++i)
186 testConvertToString(aTests[i]);