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 <sal/types.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 #include <cppunit/plugin/TestPlugIn.h>
25 #include <editeng/LookupTree.hxx>
26 #include <editeng/LatinLookupTree.hxx>
28 #include <editeng/Trie.hxx>
32 class LookupTreeTest
: public CppUnit::TestFixture
35 void testLookupTree();
38 CPPUNIT_TEST_SUITE(LookupTreeTest
);
39 CPPUNIT_TEST(testLookupTree
);
40 CPPUNIT_TEST(testTrie
);
41 CPPUNIT_TEST_SUITE_END();
44 CPPUNIT_TEST_SUITE_REGISTRATION(LookupTreeTest
);
46 void LookupTreeTest::testLookupTree()
48 LookupTree
* a
= new LatinLookupTree( "a" );
50 a
->insert( OUString("vorschlagnummer1"), 2 );
51 a
->insert( OUString("vorschlagnummer12") );
52 a
->insert( OUString("vorschlagnummer2") );
54 CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer1"), a
->suggestAutoCompletion() );
56 a
->insert( OUString("vorschlagnummer12") );
57 CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer12"), a
->suggestAutoCompletion() );
59 a
->insert( OUString("vorschlagnummer2") );
60 a
->insert( OUString("vorschlagnummer2") );
61 CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer2"), a
->suggestAutoCompletion() );
63 a
->insert( OUString("vorschlag"), 15 );
64 CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a
->suggestAutoCompletion() );
66 a
->insert( OUString("vorschlagnummer2"), 16 );
67 CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer2"), a
->suggestAutoCompletion() );
69 a
->remove( OUString("vorschlagnummer2") );
70 CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a
->suggestAutoCompletion() );
72 a
->insert( OUString("vorschlag20"), 20 );
73 CPPUNIT_ASSERT_EQUAL( OUString("vorschlag20"), a
->suggestAutoCompletion() );
75 a
->remove( OUString("vorschlag20") );
76 CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a
->suggestAutoCompletion() );
78 a
->insert( OUString("vorschlagn"), 14 );
79 CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a
->suggestAutoCompletion() );
81 a
->remove( OUString("vorschlag") );
82 CPPUNIT_ASSERT_EQUAL( OUString("vorschlagn"), a
->suggestAutoCompletion() );
84 a
->remove( OUString("vorschlagn") );
85 CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer12"), a
->suggestAutoCompletion() );
87 a
->insert( OUString("aber"), 1 );
88 CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer12"), a
->suggestAutoCompletion() );
91 CPPUNIT_ASSERT_EQUAL( OUString("ber"), a
->suggestAutoCompletion() );
94 CPPUNIT_ASSERT_EQUAL( OUString("vorschlagnummer12"), a
->suggestAutoCompletion() );
96 a
->insert( OUString("vorschlag"), 15 );
97 CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a
->suggestAutoCompletion() );
99 a
->insert( OUString("vorschlag13"), 13 );
100 CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a
->suggestAutoCompletion() );
102 a
->gotoNode( "vorsch" );
103 CPPUNIT_ASSERT_EQUAL( OUString("lag"), a
->suggestAutoCompletion() );
106 CPPUNIT_ASSERT_EQUAL( OUString("ag"), a
->suggestAutoCompletion() );
109 CPPUNIT_ASSERT_EQUAL( OUString("g13"), a
->suggestAutoCompletion() );
112 CPPUNIT_ASSERT_EQUAL( OUString("13"), a
->suggestAutoCompletion() );
115 CPPUNIT_ASSERT_EQUAL( OUString("3"), a
->suggestAutoCompletion() );
118 CPPUNIT_ASSERT ( a
->suggestAutoCompletion().isEmpty() );
122 CPPUNIT_ASSERT ( a
->suggestAutoCompletion().isEmpty() );
124 /*a->gotoNode( "vorschlag13" );
125 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
130 a->remove( "vorschlag13" );
131 CPPUNIT_ASSERT_EQUAL( OUString(""), a->suggestAutoCompletion() );*/
133 a
->insert( "VeraHatMichL1eb.", 1000000 );
135 CPPUNIT_ASSERT_EQUAL( OUString("VeraHatMichL1eb."), a
->suggestAutoCompletion() );
137 a
->remove( "VeraHatMichL1eb." );
138 a
->gotoNode( "VeraHatMich" );
139 CPPUNIT_ASSERT_EQUAL( OUString(""), a
->suggestAutoCompletion() );
142 CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a
->suggestAutoCompletion() );
144 a
->gotoNode( "VeraLiebtMich" );
147 CPPUNIT_ASSERT_EQUAL( OUString("VeraLiebtMich"), a
->suggestAutoCompletion() );
149 a
->insert( "VeraHatMichL1eb.", 1000000 );
151 CPPUNIT_ASSERT_EQUAL( OUString("VeraHatMichL1eb."), a
->suggestAutoCompletion() );
153 a
->remove( "VeraHatMichL1eb." );
154 a
->gotoNode( "VeraHatMich" );
155 CPPUNIT_ASSERT ( a
->suggestAutoCompletion().isEmpty() );
158 CPPUNIT_ASSERT ( a
->suggestAutoCompletion().isEmpty() );
160 a
->insert( "VeraHatMichL1eb.", 1000000 );
162 a
->remove( "VeraHatMichL1eb." );
163 a
->gotoNode( "VeraHatMich" );
164 CPPUNIT_ASSERT ( a
->suggestAutoCompletion().isEmpty() );
167 CPPUNIT_ASSERT ( a
->suggestAutoCompletion().isEmpty() );
169 a
->insert( "VeraHatMichL1eb.", 1000000 );
171 a
->remove( "VeraHatMichL1eb." );
172 a
->gotoNode( "VeraHatMich" );
173 CPPUNIT_ASSERT ( a
->suggestAutoCompletion().isEmpty() );
176 CPPUNIT_ASSERT ( a
->suggestAutoCompletion().isEmpty() );
178 a
->insert( "neu", 2000 );
180 CPPUNIT_ASSERT_EQUAL( OUString("neu"), a
->suggestAutoCompletion() );
183 CPPUNIT_ASSERT_EQUAL( OUString("u"), a
->suggestAutoCompletion() );
185 a
->advance( sal_Unicode('u') );
186 a
->advance( sal_Unicode('e') );
187 a
->advance( sal_Unicode('r') );
189 CPPUNIT_ASSERT ( a
->suggestAutoCompletion().isEmpty() );
192 CPPUNIT_ASSERT_EQUAL( OUString("neu"), a
->suggestAutoCompletion() );
195 CPPUNIT_ASSERT_EQUAL( OUString("eu"), a
->suggestAutoCompletion() );
198 CPPUNIT_ASSERT_EQUAL( OUString("uer"), a
->suggestAutoCompletion() );
201 OUString aQueryString
= OStringToOUString( "H\xC3\xA4llo", RTL_TEXTENCODING_UTF8
);
202 a
->insert( aQueryString
);
204 a
->advance( sal_Unicode('H') );
206 OUString aAutocompletedString
= a
->suggestAutoCompletion();
207 OUString aExpectedString
= OStringToOUString( "\xC3\xA4llo", RTL_TEXTENCODING_UTF8
);
209 CPPUNIT_ASSERT_EQUAL( aExpectedString
, aAutocompletedString
);
211 OString
aUtf8String( "\xe3\x81\x82\xe3\x81\x97\xe3\x81\x9f" );
212 aQueryString
= OStringToOUString( aUtf8String
, RTL_TEXTENCODING_UTF8
);
213 a
->insert( aQueryString
);
215 OUString aGotoString
= OStringToOUString( "\xe3\x81\x82", RTL_TEXTENCODING_UTF8
);
216 a
->gotoNode( aGotoString
);
218 aAutocompletedString
= a
->suggestAutoCompletion();
219 aExpectedString
= OStringToOUString( "\xe3\x81\x97\xe3\x81\x9f", RTL_TEXTENCODING_UTF8
);
220 CPPUNIT_ASSERT_EQUAL( aExpectedString
, aAutocompletedString
);
225 void LookupTreeTest::testTrie()
228 std::vector
<OUString
> suggestions
;
230 trie
.findSuggestions( OUString(""), suggestions
);
231 CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions
.size() );
233 trie
.insert( OUString("") );
234 trie
.findSuggestions( OUString(""), suggestions
);
235 CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions
.size() );
237 trie
.findSuggestions( OUString("a"), suggestions
);
238 CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions
.size() );
240 trie
.insert( OUString("abc") );
241 trie
.insert( OUString("abcdefghijklmnopqrstuvwxyz") );
242 trie
.findSuggestions( OUString("a"), suggestions
);
243 CPPUNIT_ASSERT_EQUAL( (size_t) 2, suggestions
.size() );
244 CPPUNIT_ASSERT_EQUAL( OUString("abc"), suggestions
[0] );
245 CPPUNIT_ASSERT_EQUAL( OUString("abcdefghijklmnopqrstuvwxyz"), suggestions
[1] );
248 trie
.findSuggestions( OUString("abc"), suggestions
);
249 CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions
.size() );
250 CPPUNIT_ASSERT_EQUAL( OUString("abcdefghijklmnopqrstuvwxyz"), suggestions
[0] );
253 trie
.findSuggestions( OUString("abe"), suggestions
);
254 CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions
.size() );
257 trie
.insert( OUString("abe") );
258 trie
.findSuggestions( OUString(""), suggestions
);
259 CPPUNIT_ASSERT_EQUAL( (size_t) 3, suggestions
.size() );
260 CPPUNIT_ASSERT_EQUAL( OUString("abc"), suggestions
[0] );
261 CPPUNIT_ASSERT_EQUAL( OUString("abcdefghijklmnopqrstuvwxyz"), suggestions
[1] );
262 CPPUNIT_ASSERT_EQUAL( OUString("abe"), suggestions
[2] );
265 trie
.insert( OUString("H31l0") );
266 trie
.findSuggestions( OUString("H"), suggestions
);
268 CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions
.size() );
269 CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions
[0] );
272 trie
.insert( OUString("H1") );
273 trie
.findSuggestions( OUString("H"), suggestions
);
274 CPPUNIT_ASSERT_EQUAL( (size_t) 2, suggestions
.size() );
275 CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions
[0] );
276 CPPUNIT_ASSERT_EQUAL( OUString("H1"), suggestions
[1] );
279 trie
.findSuggestions( OUString("H3"), suggestions
);
280 CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions
.size() );
281 CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions
[0] );
284 trie
.insert( OStringToOUString( "H\xC3\xA4llo", RTL_TEXTENCODING_UTF8
) );
285 trie
.findSuggestions( OUString("H"), suggestions
);
286 CPPUNIT_ASSERT_EQUAL( (size_t) 3, suggestions
.size() );
287 CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions
[0] );
288 CPPUNIT_ASSERT_EQUAL( OUString("H1"), suggestions
[1] );
289 CPPUNIT_ASSERT_EQUAL( OStringToOUString( "H\xC3\xA4llo", RTL_TEXTENCODING_UTF8
), suggestions
[2] );
292 trie
.findSuggestions( OUString("H3"), suggestions
);
293 CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions
.size() );
294 CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions
[0] );
297 trie
.findSuggestions( OStringToOUString("H\xC3\xA4", RTL_TEXTENCODING_UTF8
), suggestions
);
298 CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions
.size() );
299 CPPUNIT_ASSERT_EQUAL( OStringToOUString("H\xC3\xA4llo", RTL_TEXTENCODING_UTF8
), suggestions
[0] );
302 trie
.findSuggestions( OUString(""), suggestions
);
303 CPPUNIT_ASSERT_EQUAL( (size_t) 6, suggestions
.size() );
310 CPPUNIT_PLUGIN_IMPLEMENT();
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */