Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/types.h>
21 : #include <cppunit/TestFixture.h>
22 : #include <cppunit/extensions/HelperMacros.h>
23 : #include <cppunit/plugin/TestPlugIn.h>
24 : #include <editeng/Trie.hxx>
25 :
26 : namespace {
27 :
28 6 : class LookupTreeTest : public CppUnit::TestFixture
29 : {
30 : public:
31 : void testTrie();
32 : void testTrieGetAllEntries();
33 :
34 2 : CPPUNIT_TEST_SUITE(LookupTreeTest);
35 1 : CPPUNIT_TEST(testTrie);
36 1 : CPPUNIT_TEST(testTrieGetAllEntries);
37 5 : CPPUNIT_TEST_SUITE_END();
38 : };
39 :
40 1 : CPPUNIT_TEST_SUITE_REGISTRATION(LookupTreeTest);
41 :
42 1 : void LookupTreeTest::testTrie()
43 : {
44 1 : editeng::Trie trie;
45 2 : std::vector<OUString> suggestions;
46 :
47 1 : trie.findSuggestions( OUString(), suggestions);
48 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions.size() );
49 :
50 1 : trie.insert( OUString() );
51 1 : trie.findSuggestions( OUString(), suggestions);
52 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions.size() );
53 :
54 1 : trie.findSuggestions( OUString("a"), suggestions);
55 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions.size() );
56 :
57 1 : trie.insert( OUString("abc") );
58 1 : trie.insert( OUString("abcdefghijklmnopqrstuvwxyz") );
59 1 : trie.findSuggestions( OUString("a"), suggestions);
60 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 2, suggestions.size() );
61 1 : CPPUNIT_ASSERT_EQUAL( OUString("abc"), suggestions[0] );
62 1 : CPPUNIT_ASSERT_EQUAL( OUString("abcdefghijklmnopqrstuvwxyz"), suggestions[1] );
63 1 : suggestions.clear();
64 :
65 1 : trie.findSuggestions( OUString("abc"), suggestions);
66 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
67 1 : CPPUNIT_ASSERT_EQUAL( OUString("abcdefghijklmnopqrstuvwxyz"), suggestions[0] );
68 1 : suggestions.clear();
69 :
70 1 : trie.findSuggestions( OUString("abe"), suggestions);
71 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions.size() );
72 1 : suggestions.clear();
73 :
74 1 : trie.insert( OUString("abe") );
75 1 : trie.findSuggestions( OUString(""), suggestions);
76 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 3, suggestions.size() );
77 1 : CPPUNIT_ASSERT_EQUAL( OUString("abc"), suggestions[0] );
78 1 : CPPUNIT_ASSERT_EQUAL( OUString("abcdefghijklmnopqrstuvwxyz"), suggestions[1] );
79 1 : CPPUNIT_ASSERT_EQUAL( OUString("abe"), suggestions[2] );
80 1 : suggestions.clear();
81 :
82 1 : trie.insert( OUString("H31l0") );
83 1 : trie.findSuggestions( OUString("H"), suggestions);
84 :
85 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
86 1 : CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
87 1 : suggestions.clear();
88 :
89 1 : trie.insert( OUString("H1") );
90 1 : trie.findSuggestions( OUString("H"), suggestions);
91 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 2, suggestions.size() );
92 1 : CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
93 1 : CPPUNIT_ASSERT_EQUAL( OUString("H1"), suggestions[1] );
94 1 : suggestions.clear();
95 :
96 1 : trie.findSuggestions( OUString("H3"), suggestions);
97 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
98 1 : CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
99 1 : suggestions.clear();
100 :
101 1 : trie.insert( OStringToOUString( "H\xC3\xA4llo", RTL_TEXTENCODING_UTF8 ) );
102 1 : trie.findSuggestions( OUString("H"), suggestions );
103 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 3, suggestions.size() );
104 1 : CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
105 1 : CPPUNIT_ASSERT_EQUAL( OUString("H1"), suggestions[1] );
106 1 : CPPUNIT_ASSERT_EQUAL( OStringToOUString( "H\xC3\xA4llo", RTL_TEXTENCODING_UTF8 ), suggestions[2] );
107 1 : suggestions.clear();
108 :
109 1 : trie.findSuggestions( OUString("H3"), suggestions );
110 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
111 1 : CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
112 1 : suggestions.clear();
113 :
114 1 : trie.findSuggestions( OStringToOUString("H\xC3\xA4", RTL_TEXTENCODING_UTF8), suggestions );
115 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
116 1 : CPPUNIT_ASSERT_EQUAL( OStringToOUString("H\xC3\xA4llo", RTL_TEXTENCODING_UTF8), suggestions[0] );
117 1 : suggestions.clear();
118 :
119 1 : trie.findSuggestions( OUString(""), suggestions);
120 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 6, suggestions.size() );
121 2 : suggestions.clear();
122 1 : }
123 :
124 1 : void LookupTreeTest::testTrieGetAllEntries()
125 : {
126 1 : editeng::Trie trie;
127 :
128 2 : std::vector<OUString> entries;
129 :
130 1 : trie.getAllEntries(entries);
131 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 0, entries.size() );
132 :
133 1 : trie.insert("A");
134 1 : trie.getAllEntries(entries);
135 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, entries.size() );
136 1 : entries.clear();
137 :
138 1 : trie.insert("B");
139 1 : trie.insert("C");
140 1 : trie.getAllEntries(entries);
141 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 3, entries.size() );
142 1 : entries.clear();
143 :
144 1 : trie.insert("AA");
145 1 : trie.insert("AAA");
146 1 : trie.getAllEntries(entries);
147 1 : CPPUNIT_ASSERT_EQUAL( (size_t) 5, entries.size() );
148 2 : entries.clear();
149 1 : }
150 :
151 : } // namespace end
152 :
153 4 : CPPUNIT_PLUGIN_IMPLEMENT();
154 :
155 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|