Line data Source code
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 { namespace oustring {
29 :
30 3 : class Convert: public CppUnit::TestFixture
31 : {
32 : private:
33 : void convertToString();
34 :
35 2 : CPPUNIT_TEST_SUITE(Convert);
36 1 : CPPUNIT_TEST(convertToString);
37 5 : CPPUNIT_TEST_SUITE_END();
38 : };
39 :
40 : } }
41 :
42 1 : 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 8 : void testConvertToString(TestConvertToString const & rTest)
57 : {
58 8 : const rtl::OUString aSource(rTest.aSource, rTest.nLength);
59 16 : rtl::OString aStrict(RTL_CONSTASCII_STRINGPARAM("12345"));
60 : bool bSuccess = aSource.convertToString(&aStrict, rTest.nEncoding,
61 8 : rTest.nFlags);
62 : rtl::OString aRelaxed(rtl::OUStringToOString(aSource, rTest.nEncoding,
63 16 : rTest.nFlags));
64 :
65 16 : rtl::OStringBuffer aPrefix;
66 8 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM("{"));
67 23 : for (sal_Int32 i = 0; i < rTest.nLength; ++i)
68 : {
69 15 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM("U+"));
70 15 : aPrefix.append(static_cast< sal_Int32 >(rTest.aSource[i]), 16);
71 15 : if (i + 1 < rTest.nLength)
72 9 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM(","));
73 : }
74 8 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM("}, "));
75 8 : aPrefix.append(static_cast< sal_Int32 >(rTest.nEncoding));
76 8 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM(", 0x"));
77 8 : aPrefix.append(static_cast< sal_Int32 >(rTest.nFlags), 16);
78 8 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM(" -> "));
79 :
80 8 : if (bSuccess)
81 : {
82 6 : if (rTest.pStrict == 0 || !aStrict.equals(rTest.pStrict))
83 : {
84 0 : rtl::OStringBuffer aMessage(aPrefix);
85 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("strict = \""));
86 0 : aMessage.append(aStrict);
87 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("\""));
88 0 : CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
89 : }
90 : }
91 : else
92 : {
93 2 : if (!aStrict.equals(rtl::OString(RTL_CONSTASCII_STRINGPARAM("12345"))))
94 : {
95 0 : rtl::OStringBuffer aMessage(aPrefix);
96 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("modified output"));
97 0 : CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
98 : }
99 2 : if (rTest.pStrict != 0)
100 : {
101 0 : rtl::OStringBuffer aMessage(aPrefix);
102 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("failed"));
103 0 : CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
104 : }
105 : }
106 8 : if (!aRelaxed.equals(rTest.pRelaxed))
107 : {
108 0 : rtl::OStringBuffer aMessage(aPrefix);
109 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("relaxed = \""));
110 0 : aMessage.append(aRelaxed);
111 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("\""));
112 0 : CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
113 8 : }
114 8 : }
115 :
116 : }
117 :
118 1 : 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 : 0,
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 : 0,
166 : "A" },
167 : { { 0x0041,0x0100,0x0042 },
168 : 3,
169 : RTL_TEXTENCODING_ISO_8859_1,
170 : OUSTRING_TO_OSTRING_CVTFLAGS,
171 : "A?B",
172 1 : "A?B" } };
173 9 : for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
174 8 : testConvertToString(aTests[i]);
175 4 : }
176 :
177 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|