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