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 <unotest/bootstrapfixturebase.hxx>
29 #include <unicode/regex.h>
31 #include <rtl/strbuf.hxx>
32 #include <rtl/ustrbuf.hxx>
34 using namespace ::com::sun::star
;
35 using namespace U_ICU_NAMESPACE
;
36 typedef U_ICU_NAMESPACE::UnicodeString IcuUniString
;
38 class TestTextSearch
: public test::BootstrapFixtureBase
42 virtual void tearDown();
47 CPPUNIT_TEST_SUITE(TestTextSearch
);
48 CPPUNIT_TEST(testICU
);
49 CPPUNIT_TEST(testSearches
);
50 CPPUNIT_TEST_SUITE_END();
52 uno::Reference
<util::XTextSearch
> m_xSearch
;
55 // Sanity check our ICU first ...
56 void TestTextSearch::testICU()
58 UErrorCode nErr
= U_ZERO_ERROR
;
59 RegexMatcher
* pRegexMatcher
;
60 sal_uInt32 nSearchFlags
= UREGEX_UWORD
| UREGEX_CASE_INSENSITIVE
;
62 OUString
aString( "abcdefgh" );
63 OUString
aPattern( "e" );
64 IcuUniString
aSearchPat( (const UChar
*)aPattern
.getStr(), aPattern
.getLength() );
66 pRegexMatcher
= new RegexMatcher( aSearchPat
, nSearchFlags
, nErr
);
68 IcuUniString
aSource( (const UChar
*)aString
.getStr(), aString
.getLength() );
69 pRegexMatcher
->reset( aSource
);
71 CPPUNIT_ASSERT( pRegexMatcher
->find( 0, nErr
) );
72 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
73 CPPUNIT_ASSERT( pRegexMatcher
->start( nErr
) == 4 );
74 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
75 CPPUNIT_ASSERT( pRegexMatcher
->end( nErr
) == 5 );
76 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
80 OUString
aString2( "acababaabcababadcdaa" );
81 OUString
aPattern2( "a" );
83 IcuUniString
aSearchPat2( (const UChar
*)aPattern2
.getStr(), aPattern2
.getLength() );
84 pRegexMatcher
= new RegexMatcher( aSearchPat2
, nSearchFlags
, nErr
);
86 IcuUniString
aSource2( (const UChar
*)aString2
.getStr(), aString2
.getLength() );
87 pRegexMatcher
->reset( aSource2
);
89 CPPUNIT_ASSERT( pRegexMatcher
->find( 0, nErr
) );
90 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
91 CPPUNIT_ASSERT( pRegexMatcher
->start( nErr
) == 0 );
92 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
93 CPPUNIT_ASSERT( pRegexMatcher
->end( nErr
) == 1 );
94 CPPUNIT_ASSERT( nErr
== U_ZERO_ERROR
);
98 void TestTextSearch::testSearches()
100 OUString
str( "acababaabcababadcdaa" );
101 sal_Int32 startPos
= 2, endPos
= 20 ;
102 OUString
searchStr( "(ab)*a(c|d)+" );
103 sal_Int32 fStartRes
= 10, fEndRes
= 18 ;
104 sal_Int32 bStartRes
= 18, bEndRes
= 10 ;
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
;
116 aRes
= m_xSearch
->searchForward( str
, startPos
, endPos
);
117 CPPUNIT_ASSERT( aRes
.subRegExpressions
> 0 );
118 CPPUNIT_ASSERT( aRes
.startOffset
[0] == fStartRes
);
119 CPPUNIT_ASSERT( aRes
.endOffset
[0] == fEndRes
);
122 aRes
= m_xSearch
->searchBackward( str
, endPos
, startPos
);
123 CPPUNIT_ASSERT( aRes
.subRegExpressions
> 0 );
124 CPPUNIT_ASSERT( aRes
.startOffset
[0] == bStartRes
);
125 CPPUNIT_ASSERT( aRes
.endOffset
[0] == bEndRes
);
128 void TestTextSearch::setUp()
130 BootstrapFixtureBase::setUp();
131 m_xSearch
= uno::Reference
< util::XTextSearch
>(m_xSFactory
->createInstance(
132 "com.sun.star.util.TextSearch"), uno::UNO_QUERY_THROW
);
135 void TestTextSearch::tearDown()
138 BootstrapFixtureBase::tearDown();
141 CPPUNIT_TEST_SUITE_REGISTRATION(TestTextSearch
);
143 CPPUNIT_PLUGIN_IMPLEMENT();
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */