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/ustring.hxx>
35 : #include <vector>
36 :
37 : #include "tools/tenccvt.hxx"
38 :
39 : //Tests for getBestMSEncodingByChar
40 :
41 : namespace
42 : {
43 :
44 60 : class Test: public CppUnit::TestFixture
45 : {
46 : public:
47 : void testEncoding(rtl_TextEncoding eEncoding);
48 :
49 : void test1258();
50 : void test1257();
51 : void test1256();
52 : void test1255();
53 : void test1254();
54 : void test1253();
55 : void test1252();
56 : void test1251();
57 : void test1250();
58 : void test874();
59 :
60 4 : CPPUNIT_TEST_SUITE(Test);
61 2 : CPPUNIT_TEST(test1258);
62 2 : CPPUNIT_TEST(test1257);
63 2 : CPPUNIT_TEST(test1256);
64 2 : CPPUNIT_TEST(test1255);
65 2 : CPPUNIT_TEST(test1254);
66 2 : CPPUNIT_TEST(test1253);
67 2 : CPPUNIT_TEST(test1252);
68 2 : CPPUNIT_TEST(test1251);
69 2 : CPPUNIT_TEST(test1250);
70 2 : CPPUNIT_TEST(test874);
71 4 : CPPUNIT_TEST_SUITE_END();
72 : };
73 :
74 20 : void Test::testEncoding(rtl_TextEncoding eEncoding)
75 : {
76 : //Taking the single byte legacy encodings, fill in all possible values
77 20 : std::vector<sal_Char> aAllChars(255);
78 5120 : for (int i = 1; i <= 255; ++i)
79 5100 : aAllChars[i-1] = static_cast<sal_Char>(i);
80 :
81 : //Some slots are unused, so don't map to private, just set them to 'X'
82 20 : sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS ^ RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE;
83 20 : rtl::OUString sOrigText(&aAllChars[0], aAllChars.size(), eEncoding, convertFlags);
84 20 : sOrigText = sOrigText.replace( 0xfffd, 'X' );
85 :
86 : //Should clearly be equal
87 20 : sal_Int32 nLength = aAllChars.size();
88 20 : CPPUNIT_ASSERT_EQUAL(sOrigText.getLength(), nLength);
89 :
90 20 : rtl::OUString sFinalText;
91 :
92 : //Split up in chunks of the same encoding returned by
93 : //getBestMSEncodingByChar, convert to it, and back
94 20 : rtl_TextEncoding ePrevEncoding = RTL_TEXTENCODING_DONTKNOW;
95 20 : const sal_Unicode *pStr = sOrigText.getStr();
96 20 : sal_Int32 nChunkStart=0;
97 5120 : for (int i = 0; i < 255; ++i)
98 : {
99 5100 : rtl_TextEncoding eCurrEncoding = getBestMSEncodingByChar(pStr[i]);
100 5100 : if (eCurrEncoding != ePrevEncoding)
101 : {
102 674 : rtl::OString aChunk(pStr+nChunkStart, i-nChunkStart, ePrevEncoding);
103 674 : sFinalText += rtl::OStringToOUString(aChunk, ePrevEncoding);
104 674 : nChunkStart = i;
105 : }
106 5100 : ePrevEncoding = eCurrEncoding;
107 : }
108 20 : if (nChunkStart < 255)
109 : {
110 20 : rtl::OString aChunk(pStr+nChunkStart, 255-nChunkStart, ePrevEncoding);
111 20 : sFinalText += rtl::OStringToOUString(aChunk, ePrevEncoding);
112 : }
113 :
114 : //Final text should be the same as original
115 20 : CPPUNIT_ASSERT_EQUAL(sOrigText, sFinalText);
116 20 : }
117 :
118 2 : void Test::test1252()
119 : {
120 2 : testEncoding(RTL_TEXTENCODING_MS_1252);
121 2 : }
122 :
123 2 : void Test::test874()
124 : {
125 2 : testEncoding(RTL_TEXTENCODING_MS_874);
126 2 : }
127 :
128 2 : void Test::test1258()
129 : {
130 2 : testEncoding(RTL_TEXTENCODING_MS_1258);
131 2 : }
132 :
133 2 : void Test::test1257()
134 : {
135 2 : testEncoding(RTL_TEXTENCODING_MS_1257);
136 2 : }
137 :
138 2 : void Test::test1256()
139 : {
140 2 : testEncoding(RTL_TEXTENCODING_MS_1256);
141 2 : }
142 :
143 2 : void Test::test1255()
144 : {
145 2 : testEncoding(RTL_TEXTENCODING_MS_1255);
146 2 : }
147 :
148 2 : void Test::test1254()
149 : {
150 2 : testEncoding(RTL_TEXTENCODING_MS_1254);
151 2 : }
152 :
153 2 : void Test::test1253()
154 : {
155 2 : testEncoding(RTL_TEXTENCODING_MS_1253);
156 2 : }
157 :
158 2 : void Test::test1251()
159 : {
160 2 : testEncoding(RTL_TEXTENCODING_MS_1251);
161 2 : }
162 :
163 2 : void Test::test1250()
164 : {
165 2 : testEncoding(RTL_TEXTENCODING_MS_1250);
166 2 : }
167 :
168 2 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
169 : }
170 :
171 8 : CPPUNIT_PLUGIN_IMPLEMENT();
172 :
173 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|