1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/compbase1.hxx>
21 #include <cppuhelper/bootstrap.hxx>
22 #include <cppuhelper/basemutex.hxx>
23 #include <com/sun/star/util/SearchFlags.hpp>
24 #include <com/sun/star/util/SearchOptions.hpp>
25 #include <com/sun/star/util/SearchAlgorithms.hpp>
26 #include <com/sun/star/util/XTextSearch.hpp>
27 #include <com/sun/star/i18n/Transliteration.hpp>
28 #include <unotest/bootstrapfixturebase.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
42 virtual void setUp() SAL_OVERRIDE
;
43 virtual void tearDown() SAL_OVERRIDE
;
48 CPPUNIT_TEST_SUITE(TestTextSearch
);
49 CPPUNIT_TEST(testICU
);
50 CPPUNIT_TEST(testSearches
);
51 CPPUNIT_TEST_SUITE_END();
53 uno::Reference
<util::XTextSearch
> m_xSearch
;
56 // Sanity check our ICU first ...
57 void TestTextSearch::testICU()
59 UErrorCode nErr
= U_ZERO_ERROR
;
60 RegexMatcher
* pRegexMatcher
;
61 sal_uInt32 nSearchFlags
= UREGEX_UWORD
| UREGEX_CASE_INSENSITIVE
;
63 OUString
aString( "abcdefgh" );
64 OUString
aPattern( "e" );
65 IcuUniString
aSearchPat( (const UChar
*)aPattern
.getStr(), aPattern
.getLength() );
67 pRegexMatcher
= new RegexMatcher( aSearchPat
, nSearchFlags
, nErr
);
69 IcuUniString
aSource( (const UChar
*)aString
.getStr(), aString
.getLength() );
70 pRegexMatcher
->reset( aSource
);
72 CPPUNIT_ASSERT( pRegexMatcher
->find( 0, nErr
) );
73 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
74 CPPUNIT_ASSERT( pRegexMatcher
->start( nErr
) == 4 );
75 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
76 CPPUNIT_ASSERT( pRegexMatcher
->end( nErr
) == 5 );
77 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
81 OUString
aString2( "acababaabcababadcdaa" );
82 OUString
aPattern2( "a" );
84 IcuUniString
aSearchPat2( (const UChar
*)aPattern2
.getStr(), aPattern2
.getLength() );
85 pRegexMatcher
= new RegexMatcher( aSearchPat2
, nSearchFlags
, nErr
);
87 IcuUniString
aSource2( (const UChar
*)aString2
.getStr(), aString2
.getLength() );
88 pRegexMatcher
->reset( aSource2
);
90 CPPUNIT_ASSERT( pRegexMatcher
->find( 0, nErr
) );
91 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
92 CPPUNIT_ASSERT( pRegexMatcher
->start( nErr
) == 0 );
93 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
94 CPPUNIT_ASSERT( pRegexMatcher
->end( nErr
) == 1 );
95 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
99 void TestTextSearch::testSearches()
101 OUString
str( "acababaabcababadcdaa" );
102 sal_Int32 startPos
= 2, endPos
= 20 ;
103 OUString
searchStr( "(ab)*a(c|d)+" );
104 sal_Int32 fStartRes
= 10, fEndRes
= 18 ;
105 sal_Int32 bStartRes
= 18, bEndRes
= 10 ;
108 util::SearchOptions aOptions
;
109 aOptions
.algorithmType
= util::SearchAlgorithms_REGEXP
;
110 aOptions
.searchFlag
= util::SearchFlags::ALL_IGNORE_CASE
;
111 aOptions
.searchString
= searchStr
;
112 m_xSearch
->setOptions( aOptions
);
114 util::SearchResult aRes
;
117 aRes
= m_xSearch
->searchForward( str
, startPos
, endPos
);
118 CPPUNIT_ASSERT( aRes
.subRegExpressions
> 0 );
119 CPPUNIT_ASSERT( aRes
.startOffset
[0] == fStartRes
);
120 CPPUNIT_ASSERT( aRes
.endOffset
[0] == fEndRes
);
123 aRes
= m_xSearch
->searchBackward( str
, endPos
, startPos
);
124 CPPUNIT_ASSERT( aRes
.subRegExpressions
> 0 );
125 CPPUNIT_ASSERT( aRes
.startOffset
[0] == bStartRes
);
126 CPPUNIT_ASSERT( aRes
.endOffset
[0] == bEndRes
);
128 aOptions
.transliterateFlags
= ::css::i18n::TransliterationModules::TransliterationModules_IGNORE_CASE
129 | ::css::i18n::TransliterationModules::TransliterationModules_IGNORE_WIDTH
;
130 aOptions
.searchString
= "([^ ]*)[ ]*([^ ]*)";
131 m_xSearch
->setOptions(aOptions
);
132 aRes
= m_xSearch
->searchForward("11 22 33", 2, 7);
133 CPPUNIT_ASSERT(aRes
.subRegExpressions
== 3);
134 CPPUNIT_ASSERT((aRes
.startOffset
[0] == 2) && (aRes
.endOffset
[0] == 5));
135 CPPUNIT_ASSERT((aRes
.startOffset
[1] == 2) && (aRes
.endOffset
[1] == 2));
136 CPPUNIT_ASSERT((aRes
.startOffset
[2] == 3) && (aRes
.endOffset
[2] == 5));
139 void TestTextSearch::setUp()
141 BootstrapFixtureBase::setUp();
142 m_xSearch
= uno::Reference
< util::XTextSearch
>(m_xSFactory
->createInstance(
143 "com.sun.star.util.TextSearch"), uno::UNO_QUERY_THROW
);
146 void TestTextSearch::tearDown()
149 BootstrapFixtureBase::tearDown();
152 CPPUNIT_TEST_SUITE_REGISTRATION(TestTextSearch
);
154 CPPUNIT_PLUGIN_IMPLEMENT();
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */