1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "testshl/simpleheader.hxx"
28cdf0e10cSrcweir #include "rtl/strbuf.hxx"
29cdf0e10cSrcweir #include "rtl/string.hxx"
30cdf0e10cSrcweir #include "rtl/ustring.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace test { namespace oustring {
33cdf0e10cSrcweir 
34cdf0e10cSrcweir class Convert: public CppUnit::TestFixture
35cdf0e10cSrcweir {
36cdf0e10cSrcweir private:
37cdf0e10cSrcweir     void convertToString();
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(Convert);
40cdf0e10cSrcweir     CPPUNIT_TEST(convertToString);
41cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
42cdf0e10cSrcweir };
43cdf0e10cSrcweir 
44cdf0e10cSrcweir } }
45cdf0e10cSrcweir 
46cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(test::oustring::Convert, "alltest");
47cdf0e10cSrcweir 
48cdf0e10cSrcweir namespace {
49cdf0e10cSrcweir 
50cdf0e10cSrcweir struct TestConvertToString
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     sal_Unicode aSource[100];
53cdf0e10cSrcweir     sal_Int32 nLength;
54cdf0e10cSrcweir     rtl_TextEncoding nEncoding;
55cdf0e10cSrcweir     sal_uInt32 nFlags;
56cdf0e10cSrcweir     char const * pStrict;
57cdf0e10cSrcweir     char const * pRelaxed;
58cdf0e10cSrcweir };
59cdf0e10cSrcweir 
testConvertToString(TestConvertToString const & rTest)60cdf0e10cSrcweir void testConvertToString(TestConvertToString const & rTest)
61cdf0e10cSrcweir {
62cdf0e10cSrcweir     const rtl::OUString aSource(rTest.aSource, rTest.nLength);
63cdf0e10cSrcweir     rtl::OString aStrict(RTL_CONSTASCII_STRINGPARAM("12345"));
64cdf0e10cSrcweir     bool bSuccess = aSource.convertToString(&aStrict, rTest.nEncoding,
65cdf0e10cSrcweir                                             rTest.nFlags);
66cdf0e10cSrcweir     rtl::OString aRelaxed(rtl::OUStringToOString(aSource, rTest.nEncoding,
67cdf0e10cSrcweir                                                  rTest.nFlags));
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     rtl::OStringBuffer aPrefix;
70cdf0e10cSrcweir     aPrefix.append(RTL_CONSTASCII_STRINGPARAM("{"));
71cdf0e10cSrcweir     for (sal_Int32 i = 0; i < rTest.nLength; ++i)
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         aPrefix.append(RTL_CONSTASCII_STRINGPARAM("U+"));
74cdf0e10cSrcweir         aPrefix.append(static_cast< sal_Int32 >(rTest.aSource[i]), 16);
75cdf0e10cSrcweir         if (i + 1 < rTest.nLength)
76cdf0e10cSrcweir             aPrefix.append(RTL_CONSTASCII_STRINGPARAM(","));
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir     aPrefix.append(RTL_CONSTASCII_STRINGPARAM("}, "));
79cdf0e10cSrcweir     aPrefix.append(static_cast< sal_Int32 >(rTest.nEncoding));
80cdf0e10cSrcweir     aPrefix.append(RTL_CONSTASCII_STRINGPARAM(", 0x"));
81cdf0e10cSrcweir     aPrefix.append(static_cast< sal_Int32 >(rTest.nFlags), 16);
82cdf0e10cSrcweir     aPrefix.append(RTL_CONSTASCII_STRINGPARAM(" -> "));
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     if (bSuccess)
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         if (rTest.pStrict == 0 || !aStrict.equals(rTest.pStrict))
87cdf0e10cSrcweir         {
88cdf0e10cSrcweir             rtl::OStringBuffer aMessage(aPrefix);
89cdf0e10cSrcweir             aMessage.append(RTL_CONSTASCII_STRINGPARAM("strict = \""));
90cdf0e10cSrcweir             aMessage.append(aStrict);
91cdf0e10cSrcweir             aMessage.append(RTL_CONSTASCII_STRINGPARAM("\""));
92cdf0e10cSrcweir             CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir     else
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         if (!aStrict.equals(rtl::OString(RTL_CONSTASCII_STRINGPARAM("12345"))))
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             rtl::OStringBuffer aMessage(aPrefix);
100cdf0e10cSrcweir             aMessage.append(RTL_CONSTASCII_STRINGPARAM("modified output"));
101cdf0e10cSrcweir             CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir         if (rTest.pStrict != 0)
104cdf0e10cSrcweir         {
105cdf0e10cSrcweir             rtl::OStringBuffer aMessage(aPrefix);
106cdf0e10cSrcweir             aMessage.append(RTL_CONSTASCII_STRINGPARAM("failed"));
107cdf0e10cSrcweir             CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir     if (!aRelaxed.equals(rTest.pRelaxed))
111cdf0e10cSrcweir     {
112cdf0e10cSrcweir         rtl::OStringBuffer aMessage(aPrefix);
113cdf0e10cSrcweir         aMessage.append(RTL_CONSTASCII_STRINGPARAM("relaxed = \""));
114cdf0e10cSrcweir         aMessage.append(aRelaxed);
115cdf0e10cSrcweir         aMessage.append(RTL_CONSTASCII_STRINGPARAM("\""));
116cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
convertToString()122cdf0e10cSrcweir void test::oustring::Convert::convertToString()
123cdf0e10cSrcweir {
124cdf0e10cSrcweir     TestConvertToString const aTests[]
125cdf0e10cSrcweir         = { { { 0 },
126cdf0e10cSrcweir               0,
127cdf0e10cSrcweir               RTL_TEXTENCODING_ASCII_US,
128cdf0e10cSrcweir               RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
129cdf0e10cSrcweir                   | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
130cdf0e10cSrcweir               "",
131cdf0e10cSrcweir               "" },
132cdf0e10cSrcweir             { { 0 },
133cdf0e10cSrcweir               0,
134cdf0e10cSrcweir               RTL_TEXTENCODING_ASCII_US,
135cdf0e10cSrcweir               OUSTRING_TO_OSTRING_CVTFLAGS,
136cdf0e10cSrcweir               "",
137cdf0e10cSrcweir               "" },
138cdf0e10cSrcweir             { { 0x0041,0x0042,0x0043 },
139cdf0e10cSrcweir               3,
140cdf0e10cSrcweir               RTL_TEXTENCODING_ASCII_US,
141cdf0e10cSrcweir               RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
142cdf0e10cSrcweir                   | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
143cdf0e10cSrcweir               "ABC",
144cdf0e10cSrcweir               "ABC" },
145cdf0e10cSrcweir             { { 0x0041,0x0042,0x0043 },
146cdf0e10cSrcweir               3,
147cdf0e10cSrcweir               RTL_TEXTENCODING_ASCII_US,
148cdf0e10cSrcweir               OUSTRING_TO_OSTRING_CVTFLAGS,
149cdf0e10cSrcweir               "ABC",
150cdf0e10cSrcweir               "ABC" },
151cdf0e10cSrcweir             { { 0xB800 },
152cdf0e10cSrcweir               1,
153cdf0e10cSrcweir               RTL_TEXTENCODING_ISO_2022_JP,
154cdf0e10cSrcweir               RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
155cdf0e10cSrcweir                   | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
156cdf0e10cSrcweir               0,
157cdf0e10cSrcweir               "" },
158cdf0e10cSrcweir             // the next also tests that a short source produces a long target:
159cdf0e10cSrcweir             { { 0xB800 },
160cdf0e10cSrcweir               1,
161cdf0e10cSrcweir               RTL_TEXTENCODING_ISO_2022_JP,
162cdf0e10cSrcweir               OUSTRING_TO_OSTRING_CVTFLAGS,
163cdf0e10cSrcweir               "\x1B(B?",
164cdf0e10cSrcweir               "\x1B(B?" },
165cdf0e10cSrcweir             { { 0x0041,0x0100,0x0042 },
166cdf0e10cSrcweir               3,
167cdf0e10cSrcweir               RTL_TEXTENCODING_ISO_8859_1,
168cdf0e10cSrcweir               RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
169cdf0e10cSrcweir                   | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
170cdf0e10cSrcweir               0,
171cdf0e10cSrcweir               "A" },
172cdf0e10cSrcweir             { { 0x0041,0x0100,0x0042 },
173cdf0e10cSrcweir               3,
174cdf0e10cSrcweir               RTL_TEXTENCODING_ISO_8859_1,
175cdf0e10cSrcweir               OUSTRING_TO_OSTRING_CVTFLAGS,
176cdf0e10cSrcweir               "A?B",
177cdf0e10cSrcweir               "A?B" } };
178cdf0e10cSrcweir     for (unsigned int i = 0; i < sizeof aTests / sizeof aTests[0]; ++i)
179cdf0e10cSrcweir         testConvertToString(aTests[i]);
180cdf0e10cSrcweir }
181