Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : : *
5 : : * The contents of this file are subject to the Mozilla Public License Version
6 : : * 1.1 (the "License"); you may not use this file except in compliance with
7 : : * the License. You may obtain a copy of the License at
8 : : * http://www.mozilla.org/MPL/
9 : : *
10 : : * Software distributed under the License is distributed on an "AS IS" basis,
11 : : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : : * for the specific language governing rights and limitations under the
13 : : * License.
14 : : *
15 : : * The Initial Developer of the Original Code is
16 : : * Caolán McNamara <caolanm@redhat.com> (Red Hat, Inc.)
17 : : * Portions created by the Initial Developer are Copyright (C) 2010 the
18 : : * Initial Developer. All Rights Reserved.
19 : : *
20 : : * Contributor(s): Caolán McNamara <caolanm@redhat.com>
21 : : *
22 : : * Alternatively, the contents of this file may be used under the terms of
23 : : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : : * instead of those above.
27 : : */
28 : :
29 : : #include <sal/types.h>
30 : : #include "cppunit/TestAssert.h"
31 : : #include "cppunit/TestFixture.h"
32 : : #include "cppunit/extensions/HelperMacros.h"
33 : : #include "cppunit/plugin/TestPlugIn.h"
34 : : #include "rtl/oustringostreaminserter.hxx"
35 : : #include <rtl/ustring.hxx>
36 : : #include <vector>
37 : :
38 : : #include "tools/tenccvt.hxx"
39 : :
40 : : //Tests for getBestMSEncodingByChar
41 : :
42 : : namespace
43 : : {
44 : :
45 [ - + ]: 150 : class Test: public CppUnit::TestFixture
46 : : {
47 : : public:
48 : : void testEncoding(rtl_TextEncoding eEncoding);
49 : :
50 : : void test1258();
51 : : void test1257();
52 : : void test1256();
53 : : void test1255();
54 : : void test1254();
55 : : void test1253();
56 : : void test1252();
57 : : void test1251();
58 : : void test1250();
59 : : void test874();
60 : :
61 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(Test);
[ + - ][ + - ]
[ # # ]
62 [ + - ][ + - ]: 5 : CPPUNIT_TEST(test1258);
[ + - ][ + - ]
[ + - ][ + - ]
63 [ + - ][ + - ]: 5 : CPPUNIT_TEST(test1257);
[ + - ][ + - ]
[ + - ][ + - ]
64 [ + - ][ + - ]: 5 : CPPUNIT_TEST(test1256);
[ + - ][ + - ]
[ + - ][ + - ]
65 [ + - ][ + - ]: 5 : CPPUNIT_TEST(test1255);
[ + - ][ + - ]
[ + - ][ + - ]
66 [ + - ][ + - ]: 5 : CPPUNIT_TEST(test1254);
[ + - ][ + - ]
[ + - ][ + - ]
67 [ + - ][ + - ]: 5 : CPPUNIT_TEST(test1253);
[ + - ][ + - ]
[ + - ][ + - ]
68 [ + - ][ + - ]: 5 : CPPUNIT_TEST(test1252);
[ + - ][ + - ]
[ + - ][ + - ]
69 [ + - ][ + - ]: 5 : CPPUNIT_TEST(test1251);
[ + - ][ + - ]
[ + - ][ + - ]
70 [ + - ][ + - ]: 5 : CPPUNIT_TEST(test1250);
[ + - ][ + - ]
[ + - ][ + - ]
71 [ + - ][ + - ]: 5 : CPPUNIT_TEST(test874);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
72 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
73 : : };
74 : :
75 : 50 : void Test::testEncoding(rtl_TextEncoding eEncoding)
76 : : {
77 : : //Taking the single byte legacy encodings, fill in all possible values
78 [ + - ]: 50 : std::vector<sal_Char> aAllChars(255);
79 [ + + ]: 12800 : for (int i = 1; i <= 255; ++i)
80 [ + - ]: 12750 : aAllChars[i-1] = static_cast<sal_Char>(i);
81 : :
82 : : //Some slots are unused, so don't map to private, just set them to 'X'
83 : 50 : sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS ^ RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE;
84 [ + - ][ + - ]: 50 : rtl::OUString sOrigText(&aAllChars[0], aAllChars.size(), eEncoding, convertFlags);
85 : 50 : sOrigText = sOrigText.replace( 0xfffd, 'X' );
86 : :
87 : : //Should clearly be equal
88 : 50 : sal_Int32 nLength = aAllChars.size();
89 [ + - ][ + - ]: 50 : CPPUNIT_ASSERT_EQUAL(sOrigText.getLength(), nLength);
[ + - ][ + - ]
[ + - ]
90 : :
91 : 50 : rtl::OUString sFinalText;
92 : :
93 : : //Split up in chunks of the same encoding returned by
94 : : //getBestMSEncodingByChar, convert to it, and back
95 : 50 : rtl_TextEncoding ePrevEncoding = RTL_TEXTENCODING_DONTKNOW;
96 : 50 : const sal_Unicode *pStr = sOrigText.getStr();
97 : 50 : sal_Int32 nChunkStart=0;
98 [ + + ]: 12800 : for (int i = 0; i < 255; ++i)
99 : : {
100 [ + - ]: 12750 : rtl_TextEncoding eCurrEncoding = getBestMSEncodingByChar(pStr[i]);
101 [ + + ]: 12750 : if (eCurrEncoding != ePrevEncoding)
102 : : {
103 [ + - ]: 1685 : rtl::OString aChunk(pStr+nChunkStart, i-nChunkStart, ePrevEncoding);
104 [ + - ]: 1685 : sFinalText += rtl::OStringToOUString(aChunk, ePrevEncoding);
105 : 1685 : nChunkStart = i;
106 : : }
107 : 12750 : ePrevEncoding = eCurrEncoding;
108 : : }
109 [ + - ]: 50 : if (nChunkStart < 255)
110 : : {
111 [ + - ]: 50 : rtl::OString aChunk(pStr+nChunkStart, 255-nChunkStart, ePrevEncoding);
112 [ + - ]: 50 : sFinalText += rtl::OStringToOUString(aChunk, ePrevEncoding);
113 : : }
114 : :
115 : : //Final text should be the same as original
116 [ + - ][ + - ]: 50 : CPPUNIT_ASSERT_EQUAL(sOrigText, sFinalText);
[ + - ][ + - ]
[ + - ]
117 : 50 : }
118 : :
119 : 5 : void Test::test1252()
120 : : {
121 : 5 : testEncoding(RTL_TEXTENCODING_MS_1252);
122 : 5 : }
123 : :
124 : 5 : void Test::test874()
125 : : {
126 : 5 : testEncoding(RTL_TEXTENCODING_MS_874);
127 : 5 : }
128 : :
129 : 5 : void Test::test1258()
130 : : {
131 : 5 : testEncoding(RTL_TEXTENCODING_MS_1258);
132 : 5 : }
133 : :
134 : 5 : void Test::test1257()
135 : : {
136 : 5 : testEncoding(RTL_TEXTENCODING_MS_1257);
137 : 5 : }
138 : :
139 : 5 : void Test::test1256()
140 : : {
141 : 5 : testEncoding(RTL_TEXTENCODING_MS_1256);
142 : 5 : }
143 : :
144 : 5 : void Test::test1255()
145 : : {
146 : 5 : testEncoding(RTL_TEXTENCODING_MS_1255);
147 : 5 : }
148 : :
149 : 5 : void Test::test1254()
150 : : {
151 : 5 : testEncoding(RTL_TEXTENCODING_MS_1254);
152 : 5 : }
153 : :
154 : 5 : void Test::test1253()
155 : : {
156 : 5 : testEncoding(RTL_TEXTENCODING_MS_1253);
157 : 5 : }
158 : :
159 : 5 : void Test::test1251()
160 : : {
161 : 5 : testEncoding(RTL_TEXTENCODING_MS_1251);
162 : 5 : }
163 : :
164 : 5 : void Test::test1250()
165 : : {
166 : 5 : testEncoding(RTL_TEXTENCODING_MS_1250);
167 : 5 : }
168 : :
169 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
170 : : }
171 : :
172 [ + - ][ + - ]: 20 : CPPUNIT_PLUGIN_IMPLEMENT();
[ + - ][ + - ]
[ + - ][ # # ]
173 : :
174 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|