PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/enough-polish-j2me/source/test/de/enough/polish/util/TrieTest.java

https://github.com/ovidiuiliescu/j2mepolish
Java | 407 lines | 330 code | 66 blank | 11 comment | 9 complexity | 718a68084d1ce9c9cdc0e651fe73b199 MD5 | raw file
  1. package de.enough.polish.util;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.util.LinkedList;
  8. import de.enough.polish.util.Trie.Node;
  9. import junit.framework.TestCase;
  10. public class TrieTest extends TestCase {
  11. private static final String[] SMILIES = new String[] { ":)", ":))", "XOXO", ":)V", ":-D", "=)", ";)", ":-X", ":-*", ":-P", ":\'-(", "T_T", "(stop)", "u_u", ":-b", "%-}", "(@-))", "(exciting1)", "(grin2)", "(kiss2)", "(x_x2)", "(nerd2)", "(music2)", "(on_fire2)", "(sick2)", "(crying2)", "(boo2)", "(ninja2)" };
  12. private static final String LONGSEARCHTEXT = ":) kaslfkjsd fj ioasjfiose hfioashfsdiofh sio fhoiüg hgiodhgiodhg ag dighioghroighgoihfgoi hofhg oihjoighfd oigsdgfh afd :-P gfdsfg g (stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stopg gdf g fd gfd h lksdjf kld(stop)(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop(stop";
  13. protected LinkedList searchResults = new LinkedList();
  14. private final class SearchResultConsumer implements Trie.TrieSearchConsumer {
  15. public void onWordFound(String originalText, String matchedWord, int matchIndex) {
  16. addSearchResult(originalText, matchedWord, matchIndex);
  17. }
  18. }
  19. private class SearchResult {
  20. public SearchResult(String originalText, String matchedWord, int matchIndex) {
  21. this.originalText = originalText;
  22. this.matchedWord = matchedWord;
  23. this.matchIndex = matchIndex;
  24. }
  25. public String originalText;
  26. public String matchedWord;
  27. public int matchIndex;
  28. }
  29. public void testSmoketest() {
  30. String source = "Hallo:)Welt";
  31. Trie.TrieSearchConsumer trieSearchConsumer = new SearchResultConsumer();
  32. Trie trie = new Trie(SMILIES);
  33. trie.search(source, trieSearchConsumer);
  34. assertEquals(1, this.searchResults.size());
  35. SearchResult searchResult;
  36. searchResult = (SearchResult) this.searchResults.get(0);
  37. assertEquals(":)", searchResult.matchedWord);
  38. assertEquals(5, searchResult.matchIndex);
  39. }
  40. public void testSmoketestWithResult() {
  41. String source = "Hallo:)Welt";
  42. Trie.TrieSearchResult searchResult = new Trie.TrieSearchResult();
  43. int startIndex = 0;
  44. Trie trie = new Trie(SMILIES);
  45. boolean found = trie.search(source, startIndex, searchResult);
  46. assertTrue(found);
  47. assertEquals(":)", searchResult.matchedWord);
  48. assertEquals(5, searchResult.matchedWordIndex);
  49. startIndex = searchResult.matchedWordIndex + searchResult.matchedWord.length();
  50. found = trie.search(source, startIndex, searchResult);
  51. assertFalse(found);
  52. }
  53. public void testGreedyOption() {
  54. String source = "HalloWelt:))";
  55. Trie.TrieSearchConsumer trieSearchConsumer = new SearchResultConsumer();
  56. Trie trie = new Trie(SMILIES);
  57. trie.search(source, trieSearchConsumer);
  58. assertEquals(1, this.searchResults.size());
  59. SearchResult searchResult;
  60. searchResult = (SearchResult) this.searchResults.get(0);
  61. assertEquals(":))", searchResult.matchedWord);
  62. assertEquals(9, searchResult.matchIndex);
  63. }
  64. public void testGreedyOptionWithResult() {
  65. String source = "HalloWelt:))";
  66. Trie.TrieSearchResult searchResult = new Trie.TrieSearchResult();
  67. int startIndex = 0;
  68. Trie trie = new Trie(SMILIES);
  69. boolean found = trie.search(source, startIndex, searchResult);
  70. assertTrue(found);
  71. assertEquals(":))", searchResult.matchedWord);
  72. assertEquals(9, searchResult.matchedWordIndex);
  73. startIndex = searchResult.matchedWordIndex + searchResult.matchedWord.length();
  74. found = trie.search(source, startIndex, searchResult);
  75. assertFalse(found);
  76. }
  77. public void testShortestMatch() {
  78. String source = "HalloWelt:))";
  79. Trie.TrieSearchConsumer trieSearchConsumer = new SearchResultConsumer();
  80. Trie trie = new Trie(SMILIES);
  81. trie.setLongestMatchOption(false);
  82. trie.search(source, trieSearchConsumer);
  83. assertEquals(2, this.searchResults.size());
  84. SearchResult searchResult;
  85. searchResult = (SearchResult) this.searchResults.get(0);
  86. assertEquals(":)", searchResult.matchedWord);
  87. assertEquals(9, searchResult.matchIndex);
  88. searchResult = (SearchResult) this.searchResults.get(1);
  89. assertEquals(":))", searchResult.matchedWord);
  90. assertEquals(9, searchResult.matchIndex);
  91. }
  92. public void testShortestMatchWithResult() {
  93. String source = "HalloWelt:))";
  94. Trie.TrieSearchResult searchResult = new Trie.TrieSearchResult();
  95. int startIndex = 0;
  96. Trie trie = new Trie(SMILIES);
  97. trie.setLongestMatchOption(false);
  98. boolean found = trie.search(source, startIndex, searchResult);
  99. assertTrue(found);
  100. assertEquals(":)", searchResult.matchedWord);
  101. assertEquals(9, searchResult.matchedWordIndex);
  102. startIndex = searchResult.matchedWordIndex + searchResult.matchedWord.length();
  103. found = trie.search(source, startIndex, searchResult);
  104. assertFalse(found);
  105. }
  106. public void testNoMatchWithResult() {
  107. String source = "HelloWorld What is going on :-";
  108. Trie.TrieSearchResult searchResult = new Trie.TrieSearchResult();
  109. Trie trie = new Trie(SMILIES);
  110. trie.setLongestMatchOption(false);
  111. int startIndex = 0;
  112. boolean found = trie.search(source, startIndex, searchResult);
  113. assertFalse(found);
  114. source = ":- HelloWorld What is going on";
  115. startIndex = 0;
  116. found = trie.search(source, startIndex, searchResult);
  117. assertFalse(found);
  118. }
  119. public void testLongUnmatched() {
  120. Trie.TrieSearchConsumer trieSearchConsumer = new SearchResultConsumer();
  121. Trie trie = new Trie(SMILIES);
  122. trie.search(LONGSEARCHTEXT, trieSearchConsumer);
  123. assertEquals(3, this.searchResults.size());
  124. SearchResult searchResult;
  125. searchResult = (SearchResult) this.searchResults.get(0);
  126. assertEquals(":)", searchResult.matchedWord);
  127. assertEquals(0, searchResult.matchIndex);
  128. searchResult = (SearchResult) this.searchResults.get(1);
  129. assertEquals(":-P", searchResult.matchedWord);
  130. assertEquals(122, searchResult.matchIndex);
  131. searchResult = (SearchResult) this.searchResults.get(2);
  132. assertEquals("(stop)", searchResult.matchedWord);
  133. assertEquals(532, searchResult.matchIndex);
  134. }
  135. public void testLongUnmatchedWithResult() {
  136. Trie trie = new Trie(SMILIES);
  137. Trie.TrieSearchResult searchResult = new Trie.TrieSearchResult();
  138. int startIndex = 0;
  139. boolean found = trie.search(LONGSEARCHTEXT, startIndex, searchResult);
  140. assertTrue( found );
  141. assertEquals(":)", searchResult.matchedWord);
  142. assertEquals(0, searchResult.matchedWordIndex);
  143. startIndex = searchResult.matchedWordIndex + searchResult.matchedWord.length();
  144. found = trie.search(LONGSEARCHTEXT, startIndex, searchResult);
  145. assertTrue( found );
  146. assertEquals(":-P", searchResult.matchedWord);
  147. assertEquals(122, searchResult.matchedWordIndex);
  148. startIndex = searchResult.matchedWordIndex + searchResult.matchedWord.length();
  149. found = trie.search(LONGSEARCHTEXT, startIndex, searchResult);
  150. assertEquals("(stop)", searchResult.matchedWord);
  151. assertEquals(532, searchResult.matchedWordIndex);
  152. startIndex = searchResult.matchedWordIndex + searchResult.matchedWord.length();
  153. found = trie.search(LONGSEARCHTEXT, startIndex, searchResult);
  154. assertFalse( found );
  155. }
  156. public void testWrongConstructorParameters() {
  157. try {
  158. new Trie(null);
  159. fail();
  160. } catch (Throwable throwable) {
  161. // Good.
  162. }
  163. try {
  164. new Trie(new String[] { null });
  165. fail();
  166. } catch (Throwable throwable) {
  167. // Good.
  168. }
  169. try {
  170. new Trie(new String[] { "" });
  171. fail();
  172. } catch (Throwable throwable) {
  173. // Good.
  174. }
  175. }
  176. public void testWrongAddWordParameter() {
  177. Trie trie = new Trie();
  178. try {
  179. trie.addWord(null);
  180. } catch (Throwable throwable) {
  181. // Good.
  182. }
  183. try {
  184. trie.addWord("");
  185. } catch (Throwable throwable) {
  186. // Good.
  187. }
  188. }
  189. public void testRaceToTheTop() {
  190. Trie.TrieSearchConsumer trieSearchConsumer = new SearchResultConsumer();
  191. Trie trie = new Trie(SMILIES);
  192. long timeStartTrie = System.currentTimeMillis();
  193. for (int looper = 0; looper < 5000; looper++) {
  194. trie.search(LONGSEARCHTEXT, trieSearchConsumer);
  195. }
  196. long timeEndTrie = System.currentTimeMillis();
  197. long timeStartIndexOf = System.currentTimeMillis();
  198. for (int looper = 0; looper < 10000; looper++) {
  199. for (int smilieIndex = 0; smilieIndex < SMILIES.length; smilieIndex++) {
  200. LONGSEARCHTEXT.indexOf(SMILIES[smilieIndex]);
  201. }
  202. }
  203. long timeEndIndexOf = System.currentTimeMillis();
  204. long durationTrie = timeEndTrie - timeStartTrie;
  205. long durationIndexOf = timeEndIndexOf - timeStartIndexOf;
  206. assertTrue("Duration of trie run was '" + durationTrie + "' and of indexOf was '" + durationIndexOf + "'", durationTrie <= durationIndexOf);
  207. }
  208. public void testNodeEquality() {
  209. Trie.Node root1 = new Trie.Node((char) 0);
  210. populateNode(root1, true, true, 3, 'A');
  211. Trie.Node root2 = new Trie.Node((char) 0);
  212. populateNode(root2, true, true, 3, 'A');
  213. assertTrue(root1.equals(root2));
  214. }
  215. public void testTrieEquality() {
  216. Trie trie1 = new Trie(new String[] { ":-)", ":-(" });
  217. Trie trie2 = new Trie(new String[] { ":-)", ":-(" });
  218. assertTrue(trie1.equals(trie2));
  219. }
  220. public void testTrieInequality() {
  221. Trie trie1 = new Trie(new String[] { ":-)", ":-(" });
  222. Trie trie2 = new Trie(new String[] { ":-)", ":-" });
  223. assertFalse(trie1.equals(trie2));
  224. }
  225. public void testNodeStructure() {
  226. Node expectedRoot = new Node((char) 0);
  227. Node nodeA = new Node('A');
  228. Node nodeB = new Node('B');
  229. Node nodeC = new Node('C');
  230. Node nodeD = new Node('D');
  231. Node nodeE = new Node('E');
  232. Node nodeF = new Node('F');
  233. nodeA.word = "A";
  234. nodeC.word = "BC";
  235. nodeD.word = "BD";
  236. nodeE.word = "BE";
  237. nodeF.word = "F";
  238. expectedRoot.nextSibling = nodeA;
  239. nodeA.nextSibling = nodeB;
  240. nodeB.firstChild = nodeC;
  241. nodeB.nextSibling = nodeF;
  242. nodeC.nextSibling = nodeD;
  243. nodeD.nextSibling = nodeE;
  244. Trie trie = new Trie(new String[] { "A", "BC", "BD", "BE", "F" });
  245. Node actualRootNode = trie.getRootNode();
  246. assertEquals(expectedRoot, actualRootNode);
  247. assertEquals(expectedRoot.print(), actualRootNode.print());
  248. }
  249. public void testRead() {
  250. byte[] byteArray = new byte[] { 1, 1, 2, 0, 65, 0, 1, 65, 2, 0, 66, 0, 0, 1, 0, 67, 0, 2, 66, 67, 2, 0, 68, 0, 2, 66, 68, 2, 0, 69, 0, 2, 66, 69, 3, 2, 0, 70, 0, 1, 70, 3 };
  251. ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
  252. DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
  253. Trie actualTrie = new Trie();
  254. try {
  255. actualTrie.read(dataInputStream);
  256. } catch (IOException e) {
  257. e.printStackTrace();
  258. fail();
  259. return;
  260. }
  261. Trie expectedTrie = new Trie(new String[] { "A", "BC", "BD", "BE", "F" });
  262. System.out.println(actualTrie);
  263. System.out.println(expectedTrie);
  264. assertEquals(expectedTrie, actualTrie);
  265. }
  266. public void testWrite() {
  267. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  268. DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
  269. Trie trie = new Trie(new String[] { "A", "BC", "BD", "BE", "F" });
  270. try {
  271. trie.write(dataOutputStream);
  272. } catch (IOException e) {
  273. e.printStackTrace();
  274. fail();
  275. }
  276. byte[] actualByteArray = byteArrayOutputStream.toByteArray();
  277. byte[] expectedByteArray = new byte[] { 1, 1, 2, 0, 65, 0, 1, 65, 2, 0, 66, 0, 0, 1, 0, 67, 0, 2, 66, 67, 2, 0, 68, 0, 2, 66, 68, 2, 0, 69, 0, 2, 66, 69, 3, 2, 0, 70, 0, 1, 70, 3 };
  278. System.out.println(printByteArray(actualByteArray));
  279. assertEquals(expectedByteArray.length, actualByteArray.length);
  280. for (int i = 0; i < expectedByteArray.length; i++) {
  281. assertEquals(expectedByteArray[i], actualByteArray[i]);
  282. }
  283. }
  284. public void testReadWrite() {
  285. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  286. DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
  287. Trie trie1 = new Trie(new String[] { ":-)", ":-(" });
  288. try {
  289. trie1.write(dataOutputStream);
  290. } catch (IOException e) {
  291. e.printStackTrace();
  292. fail();
  293. return;
  294. }
  295. byte[] byteArray = byteArrayOutputStream.toByteArray();
  296. ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
  297. DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
  298. Trie trie2 = new Trie();
  299. try {
  300. trie2.read(dataInputStream);
  301. } catch (IOException e) {
  302. e.printStackTrace();
  303. fail();
  304. return;
  305. }
  306. assertTrue(trie1.equals(trie2));
  307. }
  308. protected String printByteArray(byte[] byteArray) {
  309. StringBuffer buffer = new StringBuffer();
  310. for (int i = 0; i < byteArray.length; i++) {
  311. buffer.append(Integer.toString(byteArray[i]));
  312. buffer.append(" ");
  313. }
  314. buffer.append("\n");
  315. return buffer.toString();
  316. }
  317. protected char populateNode(Trie.Node node, boolean addChild, boolean addSibling, int level, char character) {
  318. if (level == 0) {
  319. return character;
  320. }
  321. // if(node == null) {
  322. // return character;
  323. // }
  324. char nextCharacter = character;
  325. if (addChild) {
  326. node.firstChild = new Trie.Node(nextCharacter);
  327. nextCharacter = populateNode(node.firstChild, addChild, addSibling, level - 1, (char) (nextCharacter + 1));
  328. }
  329. if (addSibling) {
  330. // if(level%2 != 0) {
  331. // addSibling = false;
  332. // }
  333. node.nextSibling = new Trie.Node((char) (nextCharacter));
  334. nextCharacter = populateNode(node.nextSibling, addChild, addSibling, level - 1, (char) (nextCharacter + 1));
  335. }
  336. return nextCharacter;
  337. }
  338. protected void addSearchResult(String originalText, String matchedWord, int matchIndex) {
  339. this.searchResults.add(new SearchResult(originalText, matchedWord, matchIndex));
  340. }
  341. }