Debian Codegraph @

sal/qa/rtl/strings/test_oustring_convert.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <sal/types.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 #include <rtl/strbuf.hxx>
24 #include <rtl/string.hxx>
25 #include <rtl/ustring.hxx>
26 #include <sal/macros.h>
27
28 namespace test::oustring {
29
30 class Convert: public CppUnit::TestFixture
31 {
32 private:
33 void convertToString();
34
35 CPPUNIT_TEST_SUITE(Convert);
36 CPPUNIT_TEST(convertToString);
37 CPPUNIT_TEST_SUITE_END();
38 };
39
40 }
41
42 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::Convert);
43
44 namespace {
45
46 struct TestConvertToString
47 {
48 sal_Unicode aSource[100];
49 sal_Int32 nLength;
50 rtl_TextEncoding nEncoding;
51 sal_uInt32 nFlags;
52 char const * pStrict;
53 char const * pRelaxed;
54 };
55
56 void testConvertToString(TestConvertToString const & rTest)
57 {
58 const OUString aSource(rTest.aSource, rTest.nLength);
59 OString aStrict(RTL_CONSTASCII_STRINGPARAM("12345"));
60 bool bSuccess = aSource.convertToString(&aStrict, rTest.nEncoding,
61 rTest.nFlags);
62 OString aRelaxed(OUStringToOString(aSource, rTest.nEncoding,
63 rTest.nFlags));
64
65 OStringBuffer aPrefix;
66 aPrefix.append("{");
67 for (sal_Int32 i = 0; i < rTest.nLength; ++i)
68 {
69 aPrefix.append("U+");
70 aPrefix.append(static_cast< sal_Int32 >(rTest.aSource[i]), 16);
71 if (i + 1 < rTest.nLength)
72 aPrefix.append(",");
73 }
74 aPrefix.append("}, ");
75 aPrefix.append(static_cast< sal_Int32 >(rTest.nEncoding));
76 aPrefix.append(", 0x");
77 aPrefix.append(static_cast< sal_Int32 >(rTest.nFlags), 16);
78 aPrefix.append(" -> ");
79
80 if (bSuccess)
81 {
82 if (rTest.pStrict == nullptr || aStrict != rTest.pStrict)
83 {
84 OStringBuffer aMessage(aPrefix);
85 aMessage.append("strict = \"");
86 aMessage.append(aStrict);
87 aMessage.append("\"");
88 CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
89 }
90 }
91 else
92 {
93 if (aStrict != "12345")
94 {
95 OStringBuffer aMessage(aPrefix);
96 aMessage.append("modified output");
97 CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
98 }
99 if (rTest.pStrict != nullptr)
100 {
101 OStringBuffer aMessage(aPrefix);
102 aMessage.append("failed");
103 CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
104 }
105 }
106 if (aRelaxed != rTest.pRelaxed)
107 {
108 OStringBuffer aMessage(aPrefix);
109 aMessage.append("relaxed = \"");
110 aMessage.append(aRelaxed);
111 aMessage.append("\"");
112 CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
113 }
114 }
115
116 }
117
118 void test::oustring::Convert::convertToString()
119 {
120 TestConvertToString const aTests[]
121 = { { { 0 },
122 0,
123 RTL_TEXTENCODING_ASCII_US,
124 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
125 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
126 "",
127 "" },
128 { { 0 },
129 0,
130 RTL_TEXTENCODING_ASCII_US,
131 OUSTRING_TO_OSTRING_CVTFLAGS,
132 "",
133 "" },
134 { { 0x0041,0x0042,0x0043 },
135 3,
136 RTL_TEXTENCODING_ASCII_US,
137 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
138 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
139 "ABC",
140 "ABC" },
141 { { 0x0041,0x0042,0x0043 },
142 3,
143 RTL_TEXTENCODING_ASCII_US,
144 OUSTRING_TO_OSTRING_CVTFLAGS,
145 "ABC",
146 "ABC" },
147 { { 0xB800 },
148 1,
149 RTL_TEXTENCODING_ISO_2022_JP,
150 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
151 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
152 nullptr,
153 "" },
154 { { 0x3001, 0xB800 },
155 2,
156 RTL_TEXTENCODING_ISO_2022_JP,
157 OUSTRING_TO_OSTRING_CVTFLAGS,
158 "\x1b\x24\x42\x21\x22\x1b\x28\x42\x3f",
159 "\x1b\x24\x42\x21\x22\x1b\x28\x42\x3f" },
160 { { 0x0041,0x0100,0x0042 },
161 3,
162 RTL_TEXTENCODING_ISO_8859_1,
163 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
164 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
165 nullptr,
166 "A" },
167 { { 0x0041,0x0100,0x0042 },
168 3,
169 RTL_TEXTENCODING_ISO_8859_1,
170 OUSTRING_TO_OSTRING_CVTFLAGS,
171 "A?B",
172 "A?B" } };
173 for (auto const& aTest : aTests)
174 testConvertToString(aTest);
175 }
176
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */