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