예제 #1
0
        public void LetterALongestWordsTest()
        {
            var letterA = DictionaryLetter.InitializeDictionaryLetter('a', new MockDictionary());

            var longestWords = letterA.GetLongestWords();

            Assert.AreEqual(2, longestWords.Count);
            Assert.AreEqual("abbreviations", longestWords.First());
            Assert.AreEqual("abductions", longestWords.Last());
        }
예제 #2
0
        public void LetterSWordsInWordListTest()
        {
            var letterS = DictionaryLetter.InitializeDictionaryLetter('s', new MockDictionary());

            string[] letterSWords = new string[]
            {
                "stammer",
                "stamp",
                "stance",
                "standard",
                "standstill"
            };

            foreach (string s in letterSWords)
            {
                Assert.IsTrue(letterS.WordsBeginningWith.Contains(s));
            }
        }
예제 #3
0
        public void LetterAWordsInWordListTest()
        {
            var letterA = DictionaryLetter.InitializeDictionaryLetter('a', new MockDictionary());

            string[] letterAWords = new string[]
            {
                "abbreviations",
                "abdicates",
                "abductions",
                "abductors",
                "abilities"
            };

            foreach (string s in letterAWords)
            {
                Assert.IsTrue(letterA.WordsBeginningWith.Contains(s));
            }
        }
예제 #4
0
        public void WordListLengthAndPropertyEqualTest(char letter)
        {
            var letterWhat = DictionaryLetter.InitializeDictionaryLetter(letter, new MockDictionary());

            Assert.AreEqual(letterWhat.NumberWordsBeginningWith, letterWhat.WordsBeginningWith.Count);
        }
예제 #5
0
        public void AverageCharacterCountTest(char letter, int avgCount)
        {
            var letterWhat = DictionaryLetter.InitializeDictionaryLetter(letter, new MockDictionary());

            Assert.AreEqual(avgCount, letterWhat.AverageCharacterCount);
        }
예제 #6
0
        public void NumberEndingWithLetterTest(char letter, int numberEnding)
        {
            var letterWhat = DictionaryLetter.InitializeDictionaryLetter(letter, new MockDictionary());

            Assert.AreEqual(numberEnding, letterWhat.NumberWordsEndingWith);
        }
예제 #7
0
        public void AverageCharacterCountEmptyDictionaryTest(char alphabetLetter)
        {
            var eachLetter = DictionaryLetter.InitializeDictionaryLetter(alphabetLetter, new MockDictionary(new string[0]));

            Assert.AreEqual(0, eachLetter.AverageCharacterCount);
        }
예제 #8
0
        public async Task <int> AddWordsToDictionary(Stream wordStream)
        {
            int wordsAdded = 0;

            // Dictionary corresponding to the to-be-added wordlist
            ActiveDictionary addDictionary;

            try
            {
                addDictionary = Loader.LoadDictionary(wordStream);
            }
            catch (FileLoadException)
            {
                // Empty, Abort
                return(0);
            }

            // Dictionary corresponding to the in-database wordlist
            var dbDictionary = context.GetWordDictionaryForContext();

            var lettersToUpdate = addDictionary.GetAllDictionaryLetters().Select(dl => dl.Letter).ToList();

            // Get the actual entities from the DB
            List <Letter> letters =
                await context.Letters.Where(letter => lettersToUpdate.Contains(letter.Character))
                .OrderBy(letter => letter.Character)
                .ToListAsync();

            try
            {
                foreach (var letter in letters)
                {
                    var addTheseWords = addDictionary.GetDictionaryWordsStartingWith(letter.Character)
                                        .Select(word => new Word()
                    {
                        TheWord = word, Letter = letter
                    });

                    wordsAdded += context.AddWordEntities(addTheseWords);
                }
            }
            finally
            {
                await context.SaveChangesAsync();
            }

            // Changes must be saved prior to getting updated states. fml -.-
            try
            {
                foreach (var letter in letters)
                {
                    // Update Letter Statistics
                    var updatedLetterStats = DictionaryLetter.InitializeDictionaryLetter(letter.Character, dbDictionary);
                    letter.AverageCharacters  = updatedLetterStats.AverageCharacterCount;
                    letter.CountBeginningWith = updatedLetterStats.NumberWordsBeginningWith;
                    letter.CountEndingWith    = updatedLetterStats.NumberWordsEndingWith;
                    context.Letters.Update(letter);
                }
            }
            finally
            {
                await context.SaveChangesAsync();
            }

            return(wordsAdded);
        }