PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/searchcode/app/util/SpellingCorrectorTest.java

https://github.com/boyter/searchcode-server
Java | 123 lines | 91 code | 29 blank | 3 comment | 2 complexity | 2da8bfb04c4e570d78d8526aa83c9378 MD5 | raw file
  1. package com.searchcode.app.util;
  2. import junit.framework.TestCase;
  3. import org.apache.commons.lang3.RandomStringUtils;
  4. import java.util.Random;
  5. public class SpellingCorrectorTest extends TestCase {
  6. public ISpellingCorrector getSpellingCorrector() {
  7. return new SearchcodeSpellingCorrector();
  8. }
  9. public void testEmptyNullTerms() {
  10. ISpellingCorrector sc = this.getSpellingCorrector();
  11. assertNull(sc.correct(null));
  12. assertEquals("", sc.correct(""));
  13. assertEquals(" ", sc.correct(" "));
  14. }
  15. public void testSingleLetter() {
  16. ISpellingCorrector sc = getSpellingCorrector();
  17. sc.correct("a");
  18. }
  19. public void testTermsLowercased() {
  20. ISpellingCorrector sc = this.getSpellingCorrector();
  21. sc.putWord("UPPERCASE");
  22. assertTrue(sc.containsWord("uppercase"));
  23. assertFalse(sc.containsWord("UPPERCASE"));
  24. }
  25. public void testSpellingCorrectorWordExistsInDictionary() {
  26. ISpellingCorrector sc = this.getSpellingCorrector();
  27. sc.putWord("test");
  28. String actual = sc.correct("test");
  29. assertEquals("test", actual);
  30. }
  31. public void testSpellingCorrectorEmptyDictionary() {
  32. ISpellingCorrector sc = this.getSpellingCorrector();
  33. String actual = sc.correct("testz");
  34. assertEquals("testz", actual);
  35. }
  36. public void testSpellingCorrectorMissingLetter() {
  37. ISpellingCorrector sc = this.getSpellingCorrector();
  38. sc.putWord("tests");
  39. String actual = sc.correct("test");
  40. assertEquals("tests", actual);
  41. }
  42. public void testSpellingCorrectorIncorrectLetter() {
  43. ISpellingCorrector sc = this.getSpellingCorrector();
  44. sc.putWord("default");
  45. String test = sc.correct("defaulz");
  46. assertEquals("default", test);
  47. }
  48. public void testSpellingCorrectorExtraLetter() {
  49. ISpellingCorrector sc = this.getSpellingCorrector();
  50. sc.putWord("default");
  51. String test = sc.correct("defaults");
  52. assertEquals("default", test);
  53. }
  54. public void testSpellingCorrectorTwoMatchesSameLengthWins() {
  55. ISpellingCorrector sc = this.getSpellingCorrector();
  56. sc.putWord("test");
  57. sc.putWord("tests");
  58. String test = sc.correct("testz");
  59. assertEquals("tests", test);
  60. }
  61. public void testSpellingCorrectorTwoMatchesOfSameLengthMostCommonWins() {
  62. ISpellingCorrector sc = this.getSpellingCorrector();
  63. sc.putWord("testy");
  64. sc.putWord("testy");
  65. sc.putWord("testy");
  66. sc.putWord("tests");
  67. String test = sc.correct("testz");
  68. assertEquals("testy", test);
  69. }
  70. public void testSpellingCorrectorSecondCycle() {
  71. ISpellingCorrector sc = this.getSpellingCorrector();
  72. sc.putWord("test");
  73. String test = sc.correct("testss");
  74. assertEquals("test", test);
  75. }
  76. /**
  77. * If there is a performance issue this takes forever to run
  78. */
  79. public void testLongStringPerformance() {
  80. ISpellingCorrector sc = this.getSpellingCorrector();
  81. sc.correct("thisisareallylongstringthatshouldcalusethingstorunreallyslow");
  82. }
  83. public void testFuzzSpellingCorrector() {
  84. Random rand = new Random();
  85. ISpellingCorrector sc = getSpellingCorrector();
  86. for (int j = 0; j < 1000; j++) {
  87. sc.putWord(RandomStringUtils.randomAlphabetic(rand.nextInt(10) + 1));
  88. }
  89. for (int i = 0; i < 100; i++) {
  90. sc.correct(RandomStringUtils.randomAlphabetic(rand.nextInt(10) + 1));
  91. sc.getSampleWords(10);
  92. }
  93. }
  94. }