bump product version to 5.0.4.1
[LibreOffice.git] / svl / qa / unit / test_lngmisc.cxx
blob4b5596cbdebac7bf1c771d33a3cb509a393e52ed
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"
16 #include <svl/lngmisc.hxx>
18 #include <rtl/ustrbuf.hxx>
20 namespace
22 class LngMiscTest : public CppUnit::TestFixture
24 private:
25 void testRemoveHyphens();
26 void testRemoveControlChars();
27 void testReplaceControlChars();
28 void testGetThesaurusReplaceText();
30 CPPUNIT_TEST_SUITE(LngMiscTest);
32 CPPUNIT_TEST(testRemoveHyphens);
33 CPPUNIT_TEST(testRemoveControlChars);
34 CPPUNIT_TEST(testReplaceControlChars);
35 CPPUNIT_TEST(testGetThesaurusReplaceText);
37 CPPUNIT_TEST_SUITE_END();
40 void LngMiscTest::testRemoveHyphens()
42 OUString str1("");
43 OUString str2("a-b--c---");
45 OUStringBuffer str3Buf;
46 str3Buf.append(SVT_SOFT_HYPHEN);
47 str3Buf.append(SVT_HARD_HYPHEN);
48 str3Buf.append(SVT_HARD_HYPHEN);
49 OUString str3(str3Buf.makeStringAndClear());
51 OUString str4("asdf");
53 bool bModified = linguistic::RemoveHyphens(str1);
54 CPPUNIT_ASSERT(!bModified);
55 CPPUNIT_ASSERT(str1.isEmpty());
57 // Note that '-' isn't a hyphen to RemoveHyphens.
58 bModified = linguistic::RemoveHyphens(str2);
59 CPPUNIT_ASSERT(!bModified);
60 CPPUNIT_ASSERT( str2 == "a-b--c---" );
62 bModified = linguistic::RemoveHyphens(str3);
63 CPPUNIT_ASSERT(bModified);
64 CPPUNIT_ASSERT(str3.isEmpty());
66 bModified = linguistic::RemoveHyphens(str4);
67 CPPUNIT_ASSERT(!bModified);
68 CPPUNIT_ASSERT( str4 == "asdf" );
71 void LngMiscTest::testRemoveControlChars()
73 OUString str1("");
74 OUString str2("asdf");
75 OUString str3("asdf\nasdf");
77 OUStringBuffer str4Buf(33);
78 str4Buf.setLength(33);
79 for(int i = 0; i < 33; i++)
80 str4Buf[i] = static_cast<sal_Unicode>(i);
81 // TODO: is this a bug? shouldn't RemoveControlChars remove this?
82 // str4Buf[33] = static_cast<sal_Unicode>(0x7F);
83 OUString str4(str4Buf.makeStringAndClear());
85 bool bModified = linguistic::RemoveControlChars(str1);
86 CPPUNIT_ASSERT(!bModified);
87 CPPUNIT_ASSERT(str1.isEmpty());
89 bModified = linguistic::RemoveControlChars(str2);
90 CPPUNIT_ASSERT(!bModified);
91 CPPUNIT_ASSERT( str2 == "asdf" );
93 bModified = linguistic::RemoveControlChars(str3);
94 CPPUNIT_ASSERT(bModified);
95 CPPUNIT_ASSERT( str3 == "asdfasdf" );
97 bModified = linguistic::RemoveControlChars(str4);
98 CPPUNIT_ASSERT(bModified);
99 CPPUNIT_ASSERT( str4 == " " );
102 void LngMiscTest::testReplaceControlChars()
104 OUString str1("");
105 OUString str2("asdf");
106 OUString str3("asdf\nasdf");
108 OUStringBuffer str4Buf(33);
109 str4Buf.setLength(33);
110 for(int i = 0; i < 33; i++)
111 str4Buf[i] = static_cast<sal_Unicode>(i);
112 // TODO: is this a bug? shouldn't RemoveControlChars remove this?
113 // str4Buf[33] = static_cast<sal_Unicode>(0x7F);
114 OUString str4(str4Buf.makeStringAndClear());
116 bool bModified = linguistic::ReplaceControlChars(str1);
117 CPPUNIT_ASSERT(!bModified);
118 CPPUNIT_ASSERT(str1.isEmpty());
120 bModified = linguistic::ReplaceControlChars(str2);
121 CPPUNIT_ASSERT(!bModified);
122 CPPUNIT_ASSERT( str2 == "asdf" );
124 bModified = linguistic::ReplaceControlChars(str3);
125 CPPUNIT_ASSERT(bModified);
126 CPPUNIT_ASSERT( str3 == "asdf asdf" );
128 bModified = linguistic::ReplaceControlChars(str4);
129 CPPUNIT_ASSERT(bModified);
130 CPPUNIT_ASSERT(str4.getLength() == 32);
131 for(int i = 0; i < 32; i++)
132 CPPUNIT_ASSERT(str4[i] == ' ');
135 void LngMiscTest::testGetThesaurusReplaceText()
137 const OUString str1("");
138 const OUString str2("asdf");
139 const OUString str3("asdf (abc)");
140 const OUString str4("asdf*");
141 const OUString str5("asdf * ");
142 const OUString str6("asdf (abc) *");
143 const OUString str7("asdf asdf * (abc)");
144 const OUString str8(" * (abc) asdf *");
146 OUString r = linguistic::GetThesaurusReplaceText(str1);
147 CPPUNIT_ASSERT(r.isEmpty());
149 r = linguistic::GetThesaurusReplaceText(str2);
150 CPPUNIT_ASSERT(r == str2);
152 r = linguistic::GetThesaurusReplaceText(str3);
153 CPPUNIT_ASSERT(r == str2);
155 r = linguistic::GetThesaurusReplaceText(str4);
156 CPPUNIT_ASSERT(r == str2);
158 r = linguistic::GetThesaurusReplaceText(str5);
159 CPPUNIT_ASSERT(r == str2);
161 r = linguistic::GetThesaurusReplaceText(str6);
162 CPPUNIT_ASSERT(r == str2);
164 r = linguistic::GetThesaurusReplaceText(str7);
165 CPPUNIT_ASSERT(r == "asdf asdf");
167 r = linguistic::GetThesaurusReplaceText(str8);
168 CPPUNIT_ASSERT(r.isEmpty());
171 CPPUNIT_TEST_SUITE_REGISTRATION(LngMiscTest);
173 CPPUNIT_PLUGIN_IMPLEMENT();
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */