lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / i18npool / qa / cppunit / test_textsearch.cxx
blob012bbeb3eddd7f280c06332e11c10ae5b9f7dcdd
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cppuhelper/bootstrap.hxx>
21 #include <cppuhelper/basemutex.hxx>
22 #include <com/sun/star/util/SearchFlags.hpp>
23 #include <com/sun/star/util/SearchOptions.hpp>
24 #include <com/sun/star/util/SearchAlgorithms2.hpp>
25 #include <com/sun/star/util/XTextSearch2.hpp>
26 #include <com/sun/star/i18n/Transliteration.hpp>
27 #include <unotest/bootstrapfixturebase.hxx>
28 #include <i18nutil/transliteration.hxx>
30 #include <unicode/regex.h>
32 #include <rtl/strbuf.hxx>
33 #include <rtl/ustrbuf.hxx>
35 using namespace ::com::sun::star;
36 using namespace U_ICU_NAMESPACE;
37 typedef U_ICU_NAMESPACE::UnicodeString IcuUniString;
39 class TestTextSearch : public test::BootstrapFixtureBase
41 public:
42 virtual void setUp() override;
43 virtual void tearDown() override;
45 void testICU();
46 void testSearches();
47 void testWildcardSearch();
49 CPPUNIT_TEST_SUITE(TestTextSearch);
50 CPPUNIT_TEST(testICU);
51 CPPUNIT_TEST(testSearches);
52 CPPUNIT_TEST(testWildcardSearch);
53 CPPUNIT_TEST_SUITE_END();
54 private:
55 uno::Reference<util::XTextSearch> m_xSearch;
56 uno::Reference<util::XTextSearch2> m_xSearch2;
59 // Sanity check our ICU first ...
60 void TestTextSearch::testICU()
62 UErrorCode nErr = U_ZERO_ERROR;
63 sal_uInt32 nSearchFlags = UREGEX_UWORD | UREGEX_CASE_INSENSITIVE;
65 OUString aString( "abcdefgh" );
66 OUString aPattern( "e" );
67 IcuUniString aSearchPat( reinterpret_cast<const UChar*>(aPattern.getStr()), aPattern.getLength() );
69 std::unique_ptr<RegexMatcher> pRegexMatcher(new RegexMatcher( aSearchPat, nSearchFlags, nErr ));
71 IcuUniString aSource( reinterpret_cast<const UChar*>(aString.getStr()), aString.getLength() );
72 pRegexMatcher->reset( aSource );
74 CPPUNIT_ASSERT( pRegexMatcher->find( 0, nErr ) );
75 CPPUNIT_ASSERT_EQUAL( U_ZERO_ERROR, nErr );
76 CPPUNIT_ASSERT_EQUAL( static_cast<int32_t>(4), pRegexMatcher->start( nErr ) );
77 CPPUNIT_ASSERT_EQUAL( U_ZERO_ERROR, nErr );
78 CPPUNIT_ASSERT_EQUAL( static_cast<int32_t>(5), pRegexMatcher->end( nErr ) );
79 CPPUNIT_ASSERT_EQUAL( U_ZERO_ERROR, nErr );
81 OUString aString2( "acababaabcababadcdaa" );
82 OUString aPattern2( "a" );
84 IcuUniString aSearchPat2( reinterpret_cast<const UChar*>(aPattern2.getStr()), aPattern2.getLength() );
85 pRegexMatcher.reset(new RegexMatcher( aSearchPat2, nSearchFlags, nErr ));
87 IcuUniString aSource2( reinterpret_cast<const UChar*>(aString2.getStr()), aString2.getLength() );
88 pRegexMatcher->reset( aSource2 );
90 CPPUNIT_ASSERT( pRegexMatcher->find( 0, nErr ) );
91 CPPUNIT_ASSERT_EQUAL( U_ZERO_ERROR, nErr );
92 CPPUNIT_ASSERT_EQUAL( static_cast<int32_t>(0), pRegexMatcher->start( nErr ) );
93 CPPUNIT_ASSERT_EQUAL( U_ZERO_ERROR, nErr );
94 CPPUNIT_ASSERT_EQUAL( static_cast<int32_t>(1), pRegexMatcher->end( nErr ) );
95 CPPUNIT_ASSERT_EQUAL( U_ZERO_ERROR, nErr );
98 void TestTextSearch::testSearches()
100 OUString str( "acababaabcababadcdaa" );
101 sal_Int32 startPos = 2, endPos = 20 ;
102 OUString const searchStr( "(ab)*a(c|d)+" );
103 sal_Int32 const fStartRes = 10, fEndRes = 18 ;
104 sal_Int32 const bStartRes = 18, bEndRes = 10 ;
106 // set options
107 util::SearchOptions aOptions;
108 aOptions.algorithmType = util::SearchAlgorithms_REGEXP ;
109 aOptions.searchFlag = util::SearchFlags::ALL_IGNORE_CASE;
110 aOptions.searchString = searchStr;
111 m_xSearch->setOptions( aOptions );
113 util::SearchResult aRes;
115 // search forward
116 aRes = m_xSearch->searchForward( str, startPos, endPos );
117 CPPUNIT_ASSERT( aRes.subRegExpressions > 0 );
118 CPPUNIT_ASSERT_EQUAL( fStartRes, aRes.startOffset[0] );
119 CPPUNIT_ASSERT_EQUAL( fEndRes, aRes.endOffset[0] );
121 // search backwards
122 aRes = m_xSearch->searchBackward( str, endPos, startPos );
123 CPPUNIT_ASSERT( aRes.subRegExpressions > 0 );
124 CPPUNIT_ASSERT_EQUAL( bStartRes, aRes.startOffset[0] );
125 CPPUNIT_ASSERT_EQUAL( bEndRes, aRes.endOffset[0] );
127 aOptions.transliterateFlags = static_cast<int>(TransliterationFlags::IGNORE_CASE
128 | TransliterationFlags::IGNORE_WIDTH);
129 aOptions.searchString = "([^ ]*)[ ]*([^ ]*)";
130 m_xSearch->setOptions(aOptions);
131 aRes = m_xSearch->searchForward("11 22 33", 2, 7);
132 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), aRes.subRegExpressions);
133 CPPUNIT_ASSERT((aRes.startOffset[0] == 2) && (aRes.endOffset[0] == 5));
134 CPPUNIT_ASSERT((aRes.startOffset[1] == 2) && (aRes.endOffset[1] == 2));
135 CPPUNIT_ASSERT((aRes.startOffset[2] == 3) && (aRes.endOffset[2] == 5));
138 void TestTextSearch::testWildcardSearch()
140 util::SearchOptions2 aOptions;
141 OUString aText;
142 util::SearchResult aRes;
144 aOptions.AlgorithmType2 = util::SearchAlgorithms2::WILDCARD ;
145 aOptions.WildcardEscapeCharacter = '~';
146 // aOptions.searchFlag = ::css::util::SearchFlags::WILD_MATCH_SELECTION;
147 // is not set, so substring match is allowed.
148 aOptions.transliterateFlags = sal_Int32(::css::i18n::TransliterationModules::TransliterationModules_IGNORE_CASE);
149 aText = "abAca";
151 aOptions.searchString = "a";
152 m_xSearch2->setOptions2( aOptions );
153 // match first "a", [0,1)
154 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
155 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
156 CPPUNIT_ASSERT((aRes.startOffset[0] == 0) && (aRes.endOffset[0] == 1));
157 // match last "a", (5,4]
158 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
159 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
160 CPPUNIT_ASSERT((aRes.startOffset[0] == 5) && (aRes.endOffset[0] == 4));
162 aOptions.searchString = "a?";
163 m_xSearch2->setOptions2( aOptions );
164 // match "ab", [0,2)
165 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
166 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
167 CPPUNIT_ASSERT((aRes.startOffset[0] == 0) && (aRes.endOffset[0] == 2));
168 // match "ac", (4,2]
169 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
170 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
171 CPPUNIT_ASSERT((aRes.startOffset[0] == 4) && (aRes.endOffset[0] == 2));
173 aOptions.searchString = "a*c";
174 m_xSearch2->setOptions2( aOptions );
175 // match "abac", [0,4) XXX NOTE: first match forward
176 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
177 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
178 CPPUNIT_ASSERT((aRes.startOffset[0] == 0) && (aRes.endOffset[0] == 4));
179 // match "ac", (4,2] XXX NOTE: first match backward, not greedy
180 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
181 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
182 CPPUNIT_ASSERT((aRes.startOffset[0] == 4) && (aRes.endOffset[0] == 2));
184 aOptions.searchString = "b*a";
185 m_xSearch2->setOptions2( aOptions );
186 // match "ba", [1,3) XXX NOTE: first match forward, not greedy
187 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
188 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
189 CPPUNIT_ASSERT((aRes.startOffset[0] == 1) && (aRes.endOffset[0] == 3));
190 // match "baca", (5,1] XXX NOTE: first match backward
191 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
192 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
193 CPPUNIT_ASSERT((aRes.startOffset[0] == 5) && (aRes.endOffset[0] == 1));
195 aText = "ab?ca";
197 aOptions.searchString = "?~??";
198 m_xSearch2->setOptions2( aOptions );
199 // match "b?c", [1,4)
200 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
201 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
202 CPPUNIT_ASSERT((aRes.startOffset[0] == 1) && (aRes.endOffset[0] == 4));
203 // match "b?c", (4,1]
204 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
205 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
206 CPPUNIT_ASSERT((aRes.startOffset[0] == 4) && (aRes.endOffset[0] == 1));
208 aText = "ab*ca";
210 aOptions.searchString = "?~*?";
211 m_xSearch2->setOptions2( aOptions );
212 // match "b?c", [1,4)
213 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
214 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
215 CPPUNIT_ASSERT((aRes.startOffset[0] == 1) && (aRes.endOffset[0] == 4));
216 // match "b?c", (4,1]
217 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
218 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
219 CPPUNIT_ASSERT((aRes.startOffset[0] == 4) && (aRes.endOffset[0] == 1));
221 aOptions.searchString = "ca?";
222 m_xSearch2->setOptions2( aOptions );
223 // no match
224 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
225 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), aRes.subRegExpressions);
226 // no match
227 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
228 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), aRes.subRegExpressions);
230 aOptions.searchString = "ca*";
231 m_xSearch2->setOptions2( aOptions );
232 // match "ca", [3,5)
233 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
234 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
235 CPPUNIT_ASSERT((aRes.startOffset[0] == 3) && (aRes.endOffset[0] == 5));
236 // match "ca", (5,3]
237 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
238 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
239 CPPUNIT_ASSERT((aRes.startOffset[0] == 5) && (aRes.endOffset[0] == 3));
241 aOptions.searchString = "*ca*";
242 m_xSearch2->setOptions2( aOptions );
243 // match "abaca", [0,5)
244 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
245 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
246 CPPUNIT_ASSERT((aRes.startOffset[0] == 0) && (aRes.endOffset[0] == 5));
247 // match "abaca", (5,0]
248 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
249 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
250 CPPUNIT_ASSERT((aRes.startOffset[0] == 5) && (aRes.endOffset[0] == 0));
252 aText = "123123";
253 aOptions.searchString = "*2?";
254 m_xSearch2->setOptions2( aOptions );
255 // match first "123", [0,3)
256 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
257 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
258 CPPUNIT_ASSERT((aRes.startOffset[0] == 0) && (aRes.endOffset[0] == 3));
259 // match "123123", (6,0] Yes this looks odd, but it is as searching "?2*" forward.
260 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
261 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
262 CPPUNIT_ASSERT((aRes.startOffset[0] == 6) && (aRes.endOffset[0] == 0));
264 aOptions.searchFlag |= util::SearchFlags::WILD_MATCH_SELECTION;
265 m_xSearch2->setOptions2( aOptions );
266 // match "123123", [0,6) with greedy '*'
267 aRes = m_xSearch2->searchForward( aText, 0, aText.getLength());
268 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
269 CPPUNIT_ASSERT((aRes.startOffset[0] == 0) && (aRes.endOffset[0] == 6));
270 // match "123123", (6,0]
271 aRes = m_xSearch2->searchBackward( aText, aText.getLength(), 0);
272 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aRes.subRegExpressions);
273 CPPUNIT_ASSERT((aRes.startOffset[0] == 6) && (aRes.endOffset[0] == 0));
276 void TestTextSearch::setUp()
278 BootstrapFixtureBase::setUp();
279 m_xSearch.set(m_xSFactory->createInstance("com.sun.star.util.TextSearch"), uno::UNO_QUERY_THROW);
280 m_xSearch2.set(m_xSFactory->createInstance("com.sun.star.util.TextSearch2"), uno::UNO_QUERY_THROW);
283 void TestTextSearch::tearDown()
285 m_xSearch.clear();
286 m_xSearch2.clear();
287 BootstrapFixtureBase::tearDown();
290 CPPUNIT_TEST_SUITE_REGISTRATION(TestTextSearch);
292 CPPUNIT_PLUGIN_IMPLEMENT();
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */