bump product version to 4.1.6.2
[LibreOffice.git] / editeng / qa / lookuptree / lookuptree_test.cxx
blob4722bb16d27898298f66b832a5ff6e7c45659a43
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 <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>
30 namespace {
32 class LookupTreeTest : public CppUnit::TestFixture
34 public:
35 void testLookupTree();
36 void testTrie();
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() );
90 a->advance( 'a' );
91 CPPUNIT_ASSERT_EQUAL( OUString("ber"), a->suggestAutoCompletion() );
93 a->goBack();
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() );
105 a->advance( 'l' );
106 CPPUNIT_ASSERT_EQUAL( OUString("ag"), a->suggestAutoCompletion() );
108 a->advance( 'a' );
109 CPPUNIT_ASSERT_EQUAL( OUString("g13"), a->suggestAutoCompletion() );
111 a->advance( 'g' );
112 CPPUNIT_ASSERT_EQUAL( OUString("13"), a->suggestAutoCompletion() );
114 a->advance( '1' );
115 CPPUNIT_ASSERT_EQUAL( OUString("3"), a->suggestAutoCompletion() );
117 a->advance( '3' );
118 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
120 a->goBack();
121 a->advance( 'z' );
122 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
124 /*a->gotoNode( "vorschlag13" );
125 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
127 a->advance( 'g' );
128 a->advance( '1' );
129 a->advance( '3' );
130 a->remove( "vorschlag13" );
131 CPPUNIT_ASSERT_EQUAL( OUString(""), a->suggestAutoCompletion() );*/
133 a->insert( "VeraHatMichL1eb.", 1000000 );
134 a->returnToRoot();
135 CPPUNIT_ASSERT_EQUAL( OUString("VeraHatMichL1eb."), a->suggestAutoCompletion() );
137 a->remove( "VeraHatMichL1eb." );
138 a->gotoNode( "VeraHatMich" );
139 CPPUNIT_ASSERT_EQUAL( OUString(""), a->suggestAutoCompletion() );
141 a->returnToRoot();
142 CPPUNIT_ASSERT_EQUAL( OUString("vorschlag"), a->suggestAutoCompletion() );
144 a->gotoNode( "VeraLiebtMich" );
145 a->insert( 600 );
146 a->returnToRoot();
147 CPPUNIT_ASSERT_EQUAL( OUString("VeraLiebtMich"), a->suggestAutoCompletion() );
149 a->insert( "VeraHatMichL1eb.", 1000000 );
150 a->returnToRoot();
151 CPPUNIT_ASSERT_EQUAL( OUString("VeraHatMichL1eb."), a->suggestAutoCompletion() );
153 a->remove( "VeraHatMichL1eb." );
154 a->gotoNode( "VeraHatMich" );
155 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
157 a->advance( 'L' );
158 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
160 a->insert( "VeraHatMichL1eb.", 1000000 );
161 a->returnToRoot();
162 a->remove( "VeraHatMichL1eb." );
163 a->gotoNode( "VeraHatMich" );
164 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
166 a->goBack();
167 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
169 a->insert( "VeraHatMichL1eb.", 1000000 );
170 a->returnToRoot();
171 a->remove( "VeraHatMichL1eb." );
172 a->gotoNode( "VeraHatMich" );
173 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
175 a->goBack();
176 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
178 a->insert( "neu", 2000 );
179 a->returnToRoot();
180 CPPUNIT_ASSERT_EQUAL( OUString("neu"), a->suggestAutoCompletion() );
182 a->gotoNode( "ne" );
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') );
188 a->insert();
189 CPPUNIT_ASSERT ( a->suggestAutoCompletion().isEmpty() );
191 a->returnToRoot();
192 CPPUNIT_ASSERT_EQUAL( OUString("neu"), a->suggestAutoCompletion() );
194 a->advance( 'n' );
195 CPPUNIT_ASSERT_EQUAL( OUString("eu"), a->suggestAutoCompletion() );
197 a->advance( 'e' );
198 CPPUNIT_ASSERT_EQUAL( OUString("uer"), a->suggestAutoCompletion() );
200 // Test unicode
201 OUString aQueryString = OStringToOUString( "H\xC3\xA4llo", RTL_TEXTENCODING_UTF8 );
202 a->insert( aQueryString );
203 a->returnToRoot();
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 );
222 delete a;
225 void LookupTreeTest::testTrie()
227 editeng::Trie trie;
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] );
246 suggestions.clear();
248 trie.findSuggestions( OUString("abc"), suggestions);
249 CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
250 CPPUNIT_ASSERT_EQUAL( OUString("abcdefghijklmnopqrstuvwxyz"), suggestions[0] );
251 suggestions.clear();
253 trie.findSuggestions( OUString("abe"), suggestions);
254 CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions.size() );
255 suggestions.clear();
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] );
263 suggestions.clear();
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] );
270 suggestions.clear();
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] );
277 suggestions.clear();
279 trie.findSuggestions( OUString("H3"), suggestions);
280 CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
281 CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
282 suggestions.clear();
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] );
290 suggestions.clear();
292 trie.findSuggestions( OUString("H3"), suggestions );
293 CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
294 CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
295 suggestions.clear();
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] );
300 suggestions.clear();
302 trie.findSuggestions( OUString(""), suggestions);
303 CPPUNIT_ASSERT_EQUAL( (size_t) 6, suggestions.size() );
304 suggestions.clear();
308 } // namespace end
310 CPPUNIT_PLUGIN_IMPLEMENT();
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */