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 "gtest/gtest.h"
28 #include "rtl/strbuf.hxx"
29 #include "rtl/string.hxx"
30 #include "rtl/ustring.hxx"
31
32
33 namespace {
34
35 struct TestConvertToString
36 {
37 sal_Unicode aSource[100];
38 sal_Int32 nLength;
39 rtl_TextEncoding nEncoding;
40 sal_uInt32 nFlags;
41 char const * pStrict;
42 char const * pRelaxed;
43 };
44
testConvertToString(TestConvertToString const & rTest)45 void testConvertToString(TestConvertToString const & rTest)
46 {
47 const rtl::OUString aSource(rTest.aSource, rTest.nLength);
48 rtl::OString aStrict(RTL_CONSTASCII_STRINGPARAM("12345"));
49 bool bSuccess = aSource.convertToString(&aStrict, rTest.nEncoding,
50 rTest.nFlags);
51 rtl::OString aRelaxed(rtl::OUStringToOString(aSource, rTest.nEncoding,
52 rTest.nFlags));
53
54 rtl::OStringBuffer aPrefix;
55 aPrefix.append(RTL_CONSTASCII_STRINGPARAM("{"));
56 for (sal_Int32 i = 0; i < rTest.nLength; ++i)
57 {
58 aPrefix.append(RTL_CONSTASCII_STRINGPARAM("U+"));
59 aPrefix.append(static_cast< sal_Int32 >(rTest.aSource[i]), 16);
60 if (i + 1 < rTest.nLength)
61 aPrefix.append(RTL_CONSTASCII_STRINGPARAM(","));
62 }
63 aPrefix.append(RTL_CONSTASCII_STRINGPARAM("}, "));
64 aPrefix.append(static_cast< sal_Int32 >(rTest.nEncoding));
65 aPrefix.append(RTL_CONSTASCII_STRINGPARAM(", 0x"));
66 aPrefix.append(static_cast< sal_Int32 >(rTest.nFlags), 16);
67 aPrefix.append(RTL_CONSTASCII_STRINGPARAM(" -> "));
68
69 if (bSuccess)
70 {
71 if (rTest.pStrict == 0 || !aStrict.equals(rTest.pStrict))
72 {
73 rtl::OStringBuffer aMessage(aPrefix);
74 aMessage.append(RTL_CONSTASCII_STRINGPARAM("strict = \""));
75 aMessage.append(aStrict);
76 aMessage.append(RTL_CONSTASCII_STRINGPARAM("\""));
77 FAIL() << aMessage.getStr();
78 }
79 }
80 else
81 {
82 if (!aStrict.equals(rtl::OString(RTL_CONSTASCII_STRINGPARAM("12345"))))
83 {
84 rtl::OStringBuffer aMessage(aPrefix);
85 aMessage.append(RTL_CONSTASCII_STRINGPARAM("modified output"));
86 FAIL() << aMessage.getStr();
87 }
88 if (rTest.pStrict != 0)
89 {
90 rtl::OStringBuffer aMessage(aPrefix);
91 aMessage.append(RTL_CONSTASCII_STRINGPARAM("failed"));
92 FAIL() << aMessage.getStr();
93 }
94 }
95 if (!aRelaxed.equals(rTest.pRelaxed))
96 {
97 rtl::OStringBuffer aMessage(aPrefix);
98 aMessage.append(RTL_CONSTASCII_STRINGPARAM("relaxed = \""));
99 aMessage.append(aRelaxed);
100 aMessage.append(RTL_CONSTASCII_STRINGPARAM("\""));
101 FAIL() << aMessage.getStr();
102 }
103 }
104
105 }
106
107
108 namespace test { namespace oustring {
109
110 class Convert: public ::testing::Test
111 {
112 };
113
TEST_F(Convert,convertToString)114 TEST_F(Convert, convertToString)
115 {
116 TestConvertToString const aTests[]
117 = { { { 0 },
118 0,
119 RTL_TEXTENCODING_ASCII_US,
120 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
121 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
122 "",
123 "" },
124 { { 0 },
125 0,
126 RTL_TEXTENCODING_ASCII_US,
127 OUSTRING_TO_OSTRING_CVTFLAGS,
128 "",
129 "" },
130 { { 0x0041,0x0042,0x0043 },
131 3,
132 RTL_TEXTENCODING_ASCII_US,
133 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
134 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
135 "ABC",
136 "ABC" },
137 { { 0x0041,0x0042,0x0043 },
138 3,
139 RTL_TEXTENCODING_ASCII_US,
140 OUSTRING_TO_OSTRING_CVTFLAGS,
141 "ABC",
142 "ABC" },
143 { { 0xB800 },
144 1,
145 RTL_TEXTENCODING_ISO_2022_JP,
146 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
147 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
148 0,
149 "" },
150 // the next also tests that a short source produces a long target:
151 { { 0xB800 },
152 1,
153 RTL_TEXTENCODING_ISO_2022_JP,
154 OUSTRING_TO_OSTRING_CVTFLAGS,
155 "\x1B(B?",
156 "\x1B(B?" },
157 { { 0x0041,0x0100,0x0042 },
158 3,
159 RTL_TEXTENCODING_ISO_8859_1,
160 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
161 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
162 0,
163 "A" },
164 { { 0x0041,0x0100,0x0042 },
165 3,
166 RTL_TEXTENCODING_ISO_8859_1,
167 OUSTRING_TO_OSTRING_CVTFLAGS,
168 "A?B",
169 "A?B" } };
170 for (unsigned int i = 0; i < sizeof aTests / sizeof aTests[0]; ++i)
171 testConvertToString(aTests[i]);
172 }
173
174 } }
175
176