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 12 : class LookupTreeTest : public CppUnit::TestFixture
29 : {
30 : public:
31 : void testTrie();
32 : void testTrieGetAllEntries();
33 :
34 4 : CPPUNIT_TEST_SUITE(LookupTreeTest);
35 2 : CPPUNIT_TEST(testTrie);
36 2 : CPPUNIT_TEST(testTrieGetAllEntries);
37 4 : CPPUNIT_TEST_SUITE_END();
38 : };
39 :
40 2 : CPPUNIT_TEST_SUITE_REGISTRATION(LookupTreeTest);
41 :
42 2 : void LookupTreeTest::testTrie()
43 : {
44 2 : editeng::Trie trie;
45 4 : std::vector<OUString> suggestions;
46 :
47 2 : trie.findSuggestions( OUString(), suggestions);
48 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions.size() );
49 :
50 2 : trie.insert( OUString() );
51 2 : trie.findSuggestions( OUString(), suggestions);
52 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions.size() );
53 :
54 2 : trie.findSuggestions( OUString("a"), suggestions);
55 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions.size() );
56 :
57 2 : trie.insert( OUString("abc") );
58 2 : trie.insert( OUString("abcdefghijklmnopqrstuvwxyz") );
59 2 : trie.findSuggestions( OUString("a"), suggestions);
60 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 2, suggestions.size() );
61 2 : CPPUNIT_ASSERT_EQUAL( OUString("abc"), suggestions[0] );
62 2 : CPPUNIT_ASSERT_EQUAL( OUString("abcdefghijklmnopqrstuvwxyz"), suggestions[1] );
63 2 : suggestions.clear();
64 :
65 2 : trie.findSuggestions( OUString("abc"), suggestions);
66 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
67 2 : CPPUNIT_ASSERT_EQUAL( OUString("abcdefghijklmnopqrstuvwxyz"), suggestions[0] );
68 2 : suggestions.clear();
69 :
70 2 : trie.findSuggestions( OUString("abe"), suggestions);
71 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 0, suggestions.size() );
72 2 : suggestions.clear();
73 :
74 2 : trie.insert( OUString("abe") );
75 2 : trie.findSuggestions( OUString(""), suggestions);
76 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 3, suggestions.size() );
77 2 : CPPUNIT_ASSERT_EQUAL( OUString("abc"), suggestions[0] );
78 2 : CPPUNIT_ASSERT_EQUAL( OUString("abcdefghijklmnopqrstuvwxyz"), suggestions[1] );
79 2 : CPPUNIT_ASSERT_EQUAL( OUString("abe"), suggestions[2] );
80 2 : suggestions.clear();
81 :
82 2 : trie.insert( OUString("H31l0") );
83 2 : trie.findSuggestions( OUString("H"), suggestions);
84 :
85 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
86 2 : CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
87 2 : suggestions.clear();
88 :
89 2 : trie.insert( OUString("H1") );
90 2 : trie.findSuggestions( OUString("H"), suggestions);
91 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 2, suggestions.size() );
92 2 : CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
93 2 : CPPUNIT_ASSERT_EQUAL( OUString("H1"), suggestions[1] );
94 2 : suggestions.clear();
95 :
96 2 : trie.findSuggestions( OUString("H3"), suggestions);
97 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
98 2 : CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
99 2 : suggestions.clear();
100 :
101 2 : trie.insert( OStringToOUString( "H\xC3\xA4llo", RTL_TEXTENCODING_UTF8 ) );
102 2 : trie.findSuggestions( OUString("H"), suggestions );
103 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 3, suggestions.size() );
104 2 : CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
105 2 : CPPUNIT_ASSERT_EQUAL( OUString("H1"), suggestions[1] );
106 2 : CPPUNIT_ASSERT_EQUAL( OStringToOUString( "H\xC3\xA4llo", RTL_TEXTENCODING_UTF8 ), suggestions[2] );
107 2 : suggestions.clear();
108 :
109 2 : trie.findSuggestions( OUString("H3"), suggestions );
110 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
111 2 : CPPUNIT_ASSERT_EQUAL( OUString("H31l0"), suggestions[0] );
112 2 : suggestions.clear();
113 :
114 2 : trie.findSuggestions( OStringToOUString("H\xC3\xA4", RTL_TEXTENCODING_UTF8), suggestions );
115 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, suggestions.size() );
116 2 : CPPUNIT_ASSERT_EQUAL( OStringToOUString("H\xC3\xA4llo", RTL_TEXTENCODING_UTF8), suggestions[0] );
117 2 : suggestions.clear();
118 :
119 2 : trie.findSuggestions( OUString(""), suggestions);
120 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 6, suggestions.size() );
121 4 : suggestions.clear();
122 2 : }
123 :
124 2 : void LookupTreeTest::testTrieGetAllEntries()
125 : {
126 2 : editeng::Trie trie;
127 :
128 4 : std::vector<OUString> entries;
129 :
130 2 : trie.getAllEntries(entries);
131 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 0, entries.size() );
132 :
133 2 : trie.insert("A");
134 2 : trie.getAllEntries(entries);
135 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 1, entries.size() );
136 2 : entries.clear();
137 :
138 2 : trie.insert("B");
139 2 : trie.insert("C");
140 2 : trie.getAllEntries(entries);
141 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 3, entries.size() );
142 2 : entries.clear();
143 :
144 2 : trie.insert("AA");
145 2 : trie.insert("AAA");
146 2 : trie.getAllEntries(entries);
147 2 : CPPUNIT_ASSERT_EQUAL( (size_t) 5, entries.size() );
148 4 : entries.clear();
149 2 : }
150 :
151 : } // namespace end
152 :
153 8 : CPPUNIT_PLUGIN_IMPLEMENT();
154 :
155 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|