Branch libreoffice-5-0-4
[LibreOffice.git] / tools / qa / cppunit / test_reversemap.cxx
blob1f1f7c3b9fb0a5cb6e0664982b9a6da30c6d796c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
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>
18 #include <tools/tenccvt.hxx>
20 //Tests for getBestMSEncodingByChar
22 namespace
25 class Test: public CppUnit::TestFixture
27 public:
28 void testEncoding(rtl_TextEncoding eEncoding);
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();
41 CPPUNIT_TEST_SUITE(Test);
42 CPPUNIT_TEST(test1258);
43 CPPUNIT_TEST(test1257);
44 CPPUNIT_TEST(test1256);
45 CPPUNIT_TEST(test1255);
46 CPPUNIT_TEST(test1254);
47 CPPUNIT_TEST(test1253);
48 CPPUNIT_TEST(test1252);
49 CPPUNIT_TEST(test1251);
50 CPPUNIT_TEST(test1250);
51 CPPUNIT_TEST(test874);
52 CPPUNIT_TEST_SUITE_END();
55 void Test::testEncoding(rtl_TextEncoding eEncoding)
57 //Taking the single byte legacy encodings, fill in all possible values
58 std::vector<sal_Char> aAllChars(255);
59 for (int i = 1; i <= 255; ++i)
60 aAllChars[i-1] = static_cast<sal_Char>(i);
62 //Some slots are unused, so don't map to private, just set them to 'X'
63 sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS ^ RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE;
64 OUString sOrigText(&aAllChars[0], aAllChars.size(), eEncoding, convertFlags);
65 sOrigText = sOrigText.replace( 0xfffd, 'X' );
67 //Should clearly be equal
68 sal_Int32 nLength = aAllChars.size();
69 CPPUNIT_ASSERT_EQUAL(sOrigText.getLength(), nLength);
71 OUString sFinalText;
73 //Split up in chunks of the same encoding returned by
74 //getBestMSEncodingByChar, convert to it, and back
75 rtl_TextEncoding ePrevEncoding = RTL_TEXTENCODING_DONTKNOW;
76 const sal_Unicode *pStr = sOrigText.getStr();
77 sal_Int32 nChunkStart=0;
78 for (int i = 0; i < 255; ++i)
80 rtl_TextEncoding eCurrEncoding = getBestMSEncodingByChar(pStr[i]);
81 if (eCurrEncoding != ePrevEncoding)
83 OString aChunk(pStr+nChunkStart, i-nChunkStart, ePrevEncoding);
84 sFinalText += OStringToOUString(aChunk, ePrevEncoding);
85 nChunkStart = i;
87 ePrevEncoding = eCurrEncoding;
89 if (nChunkStart < 255)
91 OString aChunk(pStr+nChunkStart, 255-nChunkStart, ePrevEncoding);
92 sFinalText += OStringToOUString(aChunk, ePrevEncoding);
95 //Final text should be the same as original
96 CPPUNIT_ASSERT_EQUAL(sOrigText, sFinalText);
99 void Test::test1252()
101 testEncoding(RTL_TEXTENCODING_MS_1252);
104 void Test::test874()
106 testEncoding(RTL_TEXTENCODING_MS_874);
109 void Test::test1258()
111 testEncoding(RTL_TEXTENCODING_MS_1258);
114 void Test::test1257()
116 testEncoding(RTL_TEXTENCODING_MS_1257);
119 void Test::test1256()
121 testEncoding(RTL_TEXTENCODING_MS_1256);
124 void Test::test1255()
126 testEncoding(RTL_TEXTENCODING_MS_1255);
129 void Test::test1254()
131 testEncoding(RTL_TEXTENCODING_MS_1254);
134 void Test::test1253()
136 testEncoding(RTL_TEXTENCODING_MS_1253);
139 void Test::test1251()
141 testEncoding(RTL_TEXTENCODING_MS_1251);
144 void Test::test1250()
146 testEncoding(RTL_TEXTENCODING_MS_1250);
149 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
152 CPPUNIT_PLUGIN_IMPLEMENT();
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */