Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <sal/types.h>
30 : : #include <cppunit/TestFixture.h>
31 : : #include <cppunit/extensions/HelperMacros.h>
32 : : #include "rtl/strbuf.hxx"
33 : : #include "rtl/string.hxx"
34 : : #include "rtl/ustring.hxx"
35 : : #include <sal/macros.h>
36 : :
37 : : namespace test { namespace oustring {
38 : :
39 [ - + ]: 15 : class Convert: public CppUnit::TestFixture
40 : : {
41 : : private:
42 : : void convertToString();
43 : :
44 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(Convert);
[ + - ][ + - ]
[ # # ]
45 [ + - ][ + - ]: 5 : CPPUNIT_TEST(convertToString);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
46 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
47 : : };
48 : :
49 : : } }
50 : :
51 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::Convert);
52 : :
53 : : namespace {
54 : :
55 : : struct TestConvertToString
56 : : {
57 : : sal_Unicode aSource[100];
58 : : sal_Int32 nLength;
59 : : rtl_TextEncoding nEncoding;
60 : : sal_uInt32 nFlags;
61 : : char const * pStrict;
62 : : char const * pRelaxed;
63 : : };
64 : :
65 : 40 : void testConvertToString(TestConvertToString const & rTest)
66 : : {
67 : 40 : const rtl::OUString aSource(rTest.aSource, rTest.nLength);
68 : 40 : rtl::OString aStrict(RTL_CONSTASCII_STRINGPARAM("12345"));
69 : : bool bSuccess = aSource.convertToString(&aStrict, rTest.nEncoding,
70 : 40 : rTest.nFlags);
71 : : rtl::OString aRelaxed(rtl::OUStringToOString(aSource, rTest.nEncoding,
72 [ + - ]: 40 : rTest.nFlags));
73 : :
74 : 40 : rtl::OStringBuffer aPrefix;
75 [ + - ]: 40 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM("{"));
76 [ + + ]: 115 : for (sal_Int32 i = 0; i < rTest.nLength; ++i)
77 : : {
78 [ + - ]: 75 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM("U+"));
79 [ + - ]: 75 : aPrefix.append(static_cast< sal_Int32 >(rTest.aSource[i]), 16);
80 [ + + ]: 75 : if (i + 1 < rTest.nLength)
81 [ + - ]: 45 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM(","));
82 : : }
83 [ + - ]: 40 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM("}, "));
84 [ + - ]: 40 : aPrefix.append(static_cast< sal_Int32 >(rTest.nEncoding));
85 [ + - ]: 40 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM(", 0x"));
86 [ + - ]: 40 : aPrefix.append(static_cast< sal_Int32 >(rTest.nFlags), 16);
87 [ + - ]: 40 : aPrefix.append(RTL_CONSTASCII_STRINGPARAM(" -> "));
88 : :
89 [ + + ]: 40 : if (bSuccess)
90 : : {
91 [ + - ][ - + ]: 30 : if (rTest.pStrict == 0 || !aStrict.equals(rTest.pStrict))
[ + - ][ + - ]
[ - + ]
92 : : {
93 [ # # ]: 0 : rtl::OStringBuffer aMessage(aPrefix);
94 [ # # ]: 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("strict = \""));
95 [ # # ]: 0 : aMessage.append(aStrict);
96 [ # # ]: 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("\""));
97 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
98 : : }
99 : : }
100 : : else
101 : : {
102 [ - + ]: 10 : if (!aStrict.equals(rtl::OString(RTL_CONSTASCII_STRINGPARAM("12345"))))
103 : : {
104 [ # # ]: 0 : rtl::OStringBuffer aMessage(aPrefix);
105 [ # # ]: 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("modified output"));
106 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
107 : : }
108 [ - + ]: 10 : if (rTest.pStrict != 0)
109 : : {
110 [ # # ]: 0 : rtl::OStringBuffer aMessage(aPrefix);
111 [ # # ]: 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("failed"));
112 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
113 : : }
114 : : }
115 [ - + ]: 40 : if (!aRelaxed.equals(rTest.pRelaxed))
116 : : {
117 [ # # ]: 0 : rtl::OStringBuffer aMessage(aPrefix);
118 [ # # ]: 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("relaxed = \""));
119 [ # # ]: 0 : aMessage.append(aRelaxed);
120 [ # # ]: 0 : aMessage.append(RTL_CONSTASCII_STRINGPARAM("\""));
121 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(aMessage.getStr(), false);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
122 : 40 : }
123 : 40 : }
124 : :
125 : : }
126 : :
127 : 5 : void test::oustring::Convert::convertToString()
128 : : {
129 : : TestConvertToString const aTests[]
130 : : = { { { 0 },
131 : : 0,
132 : : RTL_TEXTENCODING_ASCII_US,
133 : : RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
134 : : | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
135 : : "",
136 : : "" },
137 : : { { 0 },
138 : : 0,
139 : : RTL_TEXTENCODING_ASCII_US,
140 : : OUSTRING_TO_OSTRING_CVTFLAGS,
141 : : "",
142 : : "" },
143 : : { { 0x0041,0x0042,0x0043 },
144 : : 3,
145 : : RTL_TEXTENCODING_ASCII_US,
146 : : RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
147 : : | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
148 : : "ABC",
149 : : "ABC" },
150 : : { { 0x0041,0x0042,0x0043 },
151 : : 3,
152 : : RTL_TEXTENCODING_ASCII_US,
153 : : OUSTRING_TO_OSTRING_CVTFLAGS,
154 : : "ABC",
155 : : "ABC" },
156 : : { { 0xB800 },
157 : : 1,
158 : : RTL_TEXTENCODING_ISO_2022_JP,
159 : : RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
160 : : | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
161 : : 0,
162 : : "" },
163 : : { { 0x3001, 0xB800 },
164 : : 2,
165 : : RTL_TEXTENCODING_ISO_2022_JP,
166 : : OUSTRING_TO_OSTRING_CVTFLAGS,
167 : : "\x1b\x24\x42\x21\x22\x1b\x28\x42\x3f",
168 : : "\x1b\x24\x42\x21\x22\x1b\x28\x42\x3f" },
169 : : { { 0x0041,0x0100,0x0042 },
170 : : 3,
171 : : RTL_TEXTENCODING_ISO_8859_1,
172 : : RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
173 : : | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
174 : : 0,
175 : : "A" },
176 : : { { 0x0041,0x0100,0x0042 },
177 : : 3,
178 : : RTL_TEXTENCODING_ISO_8859_1,
179 : : OUSTRING_TO_OSTRING_CVTFLAGS,
180 : : "A?B",
181 : 5 : "A?B" } };
182 [ + + ]: 45 : for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
183 [ + - ]: 40 : testConvertToString(aTests[i]);
184 [ + - ][ + - ]: 20 : }
185 : :
186 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|