1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * TrieTest.java |
---|
19 | * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand |
---|
20 | */ |
---|
21 | |
---|
22 | package weka.core; |
---|
23 | |
---|
24 | import java.util.HashSet; |
---|
25 | import java.util.Iterator; |
---|
26 | import java.util.Vector; |
---|
27 | |
---|
28 | import junit.framework.Test; |
---|
29 | import junit.framework.TestCase; |
---|
30 | import junit.framework.TestSuite; |
---|
31 | |
---|
32 | /** |
---|
33 | * Tests Trie. Run from the command line with:<p/> |
---|
34 | * java weka.core.TrieTest |
---|
35 | * |
---|
36 | * @author fracpete (fracpete at waikato dot ac dot nz) |
---|
37 | * @version $Revision: 1.1 $ |
---|
38 | */ |
---|
39 | public class TrieTest |
---|
40 | extends TestCase { |
---|
41 | |
---|
42 | /** holds the data for testing the trie */ |
---|
43 | protected String[] m_Data; |
---|
44 | |
---|
45 | /** the default trie built from m_Data |
---|
46 | * @see #m_Data */ |
---|
47 | protected Trie m_Trie; |
---|
48 | |
---|
49 | /** |
---|
50 | * Constructs the <code>TrieTest</code>. |
---|
51 | * |
---|
52 | * @param name the name of the test class |
---|
53 | */ |
---|
54 | public TrieTest(String name) { |
---|
55 | super(name); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Called by JUnit before each test method. |
---|
60 | * |
---|
61 | * @throws Exception if an error occurs |
---|
62 | */ |
---|
63 | protected void setUp() throws Exception { |
---|
64 | super.setUp(); |
---|
65 | |
---|
66 | m_Data = new String[]{ |
---|
67 | "this is a test", |
---|
68 | "this is another test", |
---|
69 | "and something else"}; |
---|
70 | m_Trie = buildTrie(m_Data); |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Called by JUnit after each test method |
---|
75 | */ |
---|
76 | protected void tearDown() throws Exception { |
---|
77 | super.tearDown(); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * builds a new trie from the given data and returns it |
---|
82 | * |
---|
83 | * @param data the data to use for initializing the Trie |
---|
84 | * @return the built Trie |
---|
85 | */ |
---|
86 | public Trie buildTrie(String data) { |
---|
87 | return buildTrie(new String[]{data}); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * builds a new trie from the given data and returns it |
---|
92 | * |
---|
93 | * @param data the data to use for initializing the Trie |
---|
94 | * @return the built Trie |
---|
95 | */ |
---|
96 | public Trie buildTrie(String[] data) { |
---|
97 | Trie result; |
---|
98 | int i; |
---|
99 | |
---|
100 | result = new Trie(); |
---|
101 | for (i = 0; i < data.length; i++) |
---|
102 | result.add(data[i]); |
---|
103 | |
---|
104 | return result; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * tests whether all the string the Trie got built with can be retrieved |
---|
109 | * again (tests building and retrieval via iterator). |
---|
110 | */ |
---|
111 | public void testCorrectBuild() { |
---|
112 | // retrieve data |
---|
113 | Iterator<String> iter = m_Trie.iterator(); |
---|
114 | HashSet<String> set = new HashSet<String>(); |
---|
115 | while (iter.hasNext()) |
---|
116 | set.add(iter.next()); |
---|
117 | |
---|
118 | // correct size? |
---|
119 | assertEquals( |
---|
120 | "size() does not reflect number of added strings", |
---|
121 | m_Data.length, m_Trie.size()); |
---|
122 | |
---|
123 | // different size? |
---|
124 | assertEquals( |
---|
125 | "Iterator returns different number of strings", |
---|
126 | m_Data.length, set.size()); |
---|
127 | |
---|
128 | // different elements? |
---|
129 | for (int i = 0; i < m_Data.length; i++) { |
---|
130 | if (!set.contains(m_Data[i])) |
---|
131 | fail("Cannot find string '" + m_Data[i] + "'"); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * tests whether a different order of strings presented to the Trie will |
---|
137 | * result in a different Trie (= error). |
---|
138 | */ |
---|
139 | public void testDifferentBuildOrder() { |
---|
140 | // build 2. trie |
---|
141 | String[] newData = new String[m_Data.length]; |
---|
142 | for (int i = 0; i < m_Data.length; i++) |
---|
143 | newData[i] = m_Data[m_Data.length - i - 1]; |
---|
144 | Trie t2 = buildTrie(m_Data); |
---|
145 | |
---|
146 | if (!m_Trie.equals(t2)) |
---|
147 | fail("Tries differ"); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * tests the cloning of a trie |
---|
152 | */ |
---|
153 | public void testClone() { |
---|
154 | // clone trie |
---|
155 | Trie clone = (Trie) m_Trie.clone(); |
---|
156 | |
---|
157 | if (!m_Trie.equals(clone)) |
---|
158 | fail("Tries differ"); |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * tests the remove all method (only a few elements get removed) |
---|
163 | */ |
---|
164 | public void testRemoveAllPartial() { |
---|
165 | Trie remove = buildTrie(m_Data[0]); |
---|
166 | Trie clone = (Trie) m_Trie.clone(); |
---|
167 | m_Trie.removeAll(remove); |
---|
168 | assertEquals("Removing of 1 string", clone.size(), m_Trie.size() + 1); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * tests the remove all method (all elements get removed) |
---|
173 | */ |
---|
174 | public void testRemoveAllFull() { |
---|
175 | Trie remove = buildTrie(m_Data); |
---|
176 | Trie clone = (Trie) m_Trie.clone(); |
---|
177 | m_Trie.removeAll(remove); |
---|
178 | assertEquals("Removing all strings", clone.size(), m_Trie.size() + m_Data.length); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * tests the retain all method (retains a few elements) |
---|
183 | */ |
---|
184 | public void testRetainAllPartial() { |
---|
185 | Trie retain = buildTrie(m_Data[0]); |
---|
186 | m_Trie.retainAll(retain); |
---|
187 | assertEquals("Retaining of 1 string", 1, m_Trie.size()); |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * tests the retain all method (retains all elements) |
---|
192 | */ |
---|
193 | public void testRetainAllFull() { |
---|
194 | Trie retain = buildTrie(m_Data); |
---|
195 | Trie clone = (Trie) m_Trie.clone(); |
---|
196 | m_Trie.retainAll(retain); |
---|
197 | assertEquals("Retaining all strings", clone.size(), m_Trie.size()); |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * tests whether the common prefix is determined correctly |
---|
202 | */ |
---|
203 | public void testCommonPrefix() { |
---|
204 | String returned = m_Trie.getCommonPrefix(); |
---|
205 | assertEquals("Common prefixes differ", 0, returned.length()); |
---|
206 | |
---|
207 | String expected = "this is a"; |
---|
208 | Trie t = buildTrie(new String[]{m_Data[0], m_Data[1]}); |
---|
209 | returned = t.getCommonPrefix(); |
---|
210 | assertEquals("Common prefixes differ", expected.length(), returned.length()); |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * tests the finding of prefixes |
---|
215 | */ |
---|
216 | public void testFindPrefixes() { |
---|
217 | Vector<String> prefixes = m_Trie.getWithPrefix("this"); |
---|
218 | assertEquals("Different number of prefixes returned", 2, prefixes.size()); |
---|
219 | |
---|
220 | prefixes = m_Trie.getWithPrefix("blah"); |
---|
221 | assertEquals("Different number of prefixes returned", 0, prefixes.size()); |
---|
222 | } |
---|
223 | |
---|
224 | public static Test suite() { |
---|
225 | return new TestSuite(TrieTest.class); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Runs the test. |
---|
230 | * |
---|
231 | * @param args ignored |
---|
232 | */ |
---|
233 | public static void main(String[] args) { |
---|
234 | junit.textui.TestRunner.run(suite()); |
---|
235 | } |
---|
236 | } |
---|