PageRenderTime 1ms CodeModel.GetById 20ms app.highlight 5ms RepoModel.GetById 1ms 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
  1package com.searchcode.app.util;
  2
  3import junit.framework.TestCase;
  4import org.apache.commons.lang3.RandomStringUtils;
  5
  6import java.util.Random;
  7
  8public class SpellingCorrectorTest extends TestCase {
  9
 10    public ISpellingCorrector getSpellingCorrector() {
 11        return new SearchcodeSpellingCorrector();
 12    }
 13
 14    public void testEmptyNullTerms() {
 15        ISpellingCorrector sc = this.getSpellingCorrector();
 16        assertNull(sc.correct(null));
 17        assertEquals("", sc.correct(""));
 18        assertEquals(" ", sc.correct(" "));
 19    }
 20
 21    public void testSingleLetter() {
 22        ISpellingCorrector sc = getSpellingCorrector();
 23        sc.correct("a");
 24    }
 25
 26    public void testTermsLowercased() {
 27        ISpellingCorrector sc = this.getSpellingCorrector();
 28        sc.putWord("UPPERCASE");
 29        assertTrue(sc.containsWord("uppercase"));
 30        assertFalse(sc.containsWord("UPPERCASE"));
 31    }
 32
 33    public void testSpellingCorrectorWordExistsInDictionary() {
 34        ISpellingCorrector sc = this.getSpellingCorrector();
 35        sc.putWord("test");
 36
 37        String actual = sc.correct("test");
 38        assertEquals("test", actual);
 39    }
 40
 41    public void testSpellingCorrectorEmptyDictionary() {
 42        ISpellingCorrector sc = this.getSpellingCorrector();
 43        String actual = sc.correct("testz");
 44        assertEquals("testz", actual);
 45    }
 46
 47
 48    public void testSpellingCorrectorMissingLetter() {
 49        ISpellingCorrector sc = this.getSpellingCorrector();
 50        sc.putWord("tests");
 51
 52        String actual = sc.correct("test");
 53        assertEquals("tests", actual);
 54    }
 55
 56    public void testSpellingCorrectorIncorrectLetter() {
 57        ISpellingCorrector sc = this.getSpellingCorrector();
 58
 59        sc.putWord("default");
 60
 61        String test = sc.correct("defaulz");
 62        assertEquals("default", test);
 63    }
 64
 65    public void testSpellingCorrectorExtraLetter() {
 66        ISpellingCorrector sc = this.getSpellingCorrector();
 67        sc.putWord("default");
 68
 69        String test = sc.correct("defaults");
 70        assertEquals("default", test);
 71    }
 72
 73    public void testSpellingCorrectorTwoMatchesSameLengthWins() {
 74        ISpellingCorrector sc = this.getSpellingCorrector();
 75        sc.putWord("test");
 76        sc.putWord("tests");
 77
 78        String test = sc.correct("testz");
 79        assertEquals("tests", test);
 80    }
 81
 82    public void testSpellingCorrectorTwoMatchesOfSameLengthMostCommonWins() {
 83        ISpellingCorrector sc = this.getSpellingCorrector();
 84        sc.putWord("testy");
 85        sc.putWord("testy");
 86        sc.putWord("testy");
 87        sc.putWord("tests");
 88
 89        String test = sc.correct("testz");
 90        assertEquals("testy", test);
 91    }
 92
 93    public void testSpellingCorrectorSecondCycle() {
 94        ISpellingCorrector sc = this.getSpellingCorrector();
 95
 96        sc.putWord("test");
 97
 98        String test = sc.correct("testss");
 99        assertEquals("test", test);
100    }
101
102    /**
103     * If there is a performance issue this takes forever to run
104     */
105    public void testLongStringPerformance() {
106        ISpellingCorrector sc = this.getSpellingCorrector();
107        sc.correct("thisisareallylongstringthatshouldcalusethingstorunreallyslow");
108    }
109
110    public void testFuzzSpellingCorrector() {
111        Random rand = new Random();
112        ISpellingCorrector sc = getSpellingCorrector();
113
114        for (int j = 0; j < 1000; j++) {
115            sc.putWord(RandomStringUtils.randomAlphabetic(rand.nextInt(10) + 1));
116        }
117
118        for (int i = 0; i < 100; i++) {
119            sc.correct(RandomStringUtils.randomAlphabetic(rand.nextInt(10) + 1));
120            sc.getSampleWords(10);
121        }
122    }
123}