Beispiel #1
0
            public void WHEN_ComputeSimilarityOfTwoIdenticalStrings_THEN_ComputedString2IsOriginal()
            {
                testString1          = "abc";
                testString2          = "abc";
                expectedResultString = "abc";

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedResultString, StringSimilarity.GetComputedString2());
            }
Beispiel #2
0
            public void WHEN_ComputeSimilarityWhereString2Missing1CharBefore_THEN_ComputedString2Shows1Gap()
            {
                testString1          = "ab";
                testString2          = "b";
                expectedResultString = "-b";

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedResultString, StringSimilarity.GetComputedString2());
            }
Beispiel #3
0
            public void WHEN_ComputeSimilarityWhereString1Wrong2CharBetween_THEN_ComputedString1Shows2Gap()
            {
                testString1          = "aefd";
                testString2          = "abcd";
                expectedResultString = "aefd";

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedResultString, StringSimilarity.GetComputedString1());
            }
Beispiel #4
0
            public void WHEN_ComputeSimilarityWhereString2Wrong1CharBefore_THEN_ComputedString1ShowsSame()
            {
                testString1          = "ab";
                testString2          = "cb";
                expectedResultString = "ab";

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedResultString, StringSimilarity.GetComputedString1());
            }
Beispiel #5
0
            public void WHEN_ComputeSimilarityWhereString1Wrong1CharAfter_THEN_ComputedString1ShowsGap()
            {
                testString1          = "ac";
                testString2          = "ab";
                expectedResultString = "ac";

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedResultString, StringSimilarity.GetComputedString1());
            }
Beispiel #6
0
            public void WHEN_ComputeSimilarityOfTwoIdenticalStrings_THEN_ScoreIs0()
            {
                testString1   = "abc";
                testString2   = "abc";
                expectedScore = 0;

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedScore, StringSimilarity.GetTotalDiff());
            }
Beispiel #7
0
            public void WHEN_ComputeSimilarityWhereString2Missing2CharBetween_THEN_ComputedString2Shows1Gap1Mismatch()
            {
                testString1          = "abcd";
                testString2          = "ad";
                expectedResultString = "a--d";

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedResultString, StringSimilarity.GetComputedString2());
            }
Beispiel #8
0
            public void WHEN_ComputeSimilarityWhereString2Missing2CharBetween_THEN_ComputedScoreHas2Deduction()
            {
                testString1   = "abcd";
                testString2   = "ad";
                expectedScore = 2;

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedScore, StringSimilarity.GetTotalDiff());
            }
Beispiel #9
0
            public void WHEN_ComputeSimilarityWhereStringIsNumbers_THEN_ComputedScoreIsCorrect()
            {
                testString1   = "1234";
                testString2   = "1234";
                expectedScore = 0;

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedScore, StringSimilarity.GetTotalDiff());
            }
Beispiel #10
0
            public void WHEN_ComputeSimilarityWhereString1Wrong1CharBetween_THEN_ComputedString2ShowsSame()
            {
                testString1          = "adc";
                testString2          = "abc";
                expectedResultString = "abc";

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedResultString, StringSimilarity.GetComputedString2());
            }
Beispiel #11
0
            public void WHEN_ComputeSimilarityWhereString2Empty_THEN_ComputedString2ShowsGap()
            {
                testString1          = "1";
                testString2          = "";
                expectedResultString = "-";

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedResultString, StringSimilarity.GetComputedString2());
            }
Beispiel #12
0
            public void WHEN_ComputeSimilarityWhereString1Missing1CharAfter_THEN_ComputedString2ShowsSame()
            {
                testString1          = "a";
                testString2          = "ab";
                expectedResultString = "ab";

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedResultString, StringSimilarity.GetComputedString2());
            }
Beispiel #13
0
            public void WHEN_ComputeSimilarityWhereString2Empty_THEN_ComputedScoreHas1ScoreDeduction()
            {
                testString1   = "a";
                testString2   = "";
                expectedScore = 1;

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedScore, StringSimilarity.GetTotalDiff());
            }
Beispiel #14
0
            public void WHEN_ComputeSimilarityWhereStringIs1000Characters_THEN_ComputedScoreIsCorrect()
            {
                testString1   = new string('a', 1000);
                testString2   = "a";
                expectedScore = 999;

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedScore, StringSimilarity.GetTotalDiff());
            }
Beispiel #15
0
            public void WHEN_ComputeSimilarityWhereStringHasSymbolCharacters_THEN_ComputedScoreIsCorrect()
            {
                testString1   = "~`!@#$%^&*()_+-=[]\\{}|;':\",./<>?";
                testString2   = "a";
                expectedScore = 32;

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedScore, StringSimilarity.GetTotalDiff());
            }
Beispiel #16
0
            public void WHEN_ComputeSimilarityWhereString2Wrong1CharBefore_THEN_ComputedScoreHas1Deduction()
            {
                testString1   = "ab";
                testString2   = "cb";
                expectedScore = 1;

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedScore, StringSimilarity.GetTotalDiff());
            }
Beispiel #17
0
            public void WHEN_ComputeSimilarityOfTwoEmptyStrings_THEN_ComputedString2IsEmpty()
            {
                testString1          = "";
                testString2          = "";
                expectedResultString = "";

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedResultString, StringSimilarity.GetComputedString2());
            }
Beispiel #18
0
            public void WHEN_ComputeSimilarityWhereStringHasEscapeCharacters_THEN_ComputedScoreIsCorrect()
            {
                // \' single quote, \" double quote, \\ backslash, \0 null character \a alert character \b backspace \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab
                testString1   = "\'\"\\\0\a\b\f\n\r\t\v";
                testString2   = "a";
                expectedScore = 11;

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedScore, StringSimilarity.GetTotalDiff());
            }
Beispiel #19
0
            public void WHEN_ComputeSimilarityWhereStringHasUnicodeCharacters_THEN_ComputedScoreIsCorrect()
            {
                // Unicode character format: \uxxxx for a unicode character hex value such as \u0020
                // \x is the same as \u, but you dont need leading zeros \x20

                testString1   = "\u0020";
                testString2   = "a";
                expectedScore = 1;

                StringSimilarity.ComputeSimilarity(testString1, testString2);

                Assert.AreEqual(expectedScore, StringSimilarity.GetTotalDiff());
            }
        /// <summary>
        /// CompareAnswerAndRecall iterates through HighlightedSquares and RecalledSquares sequences,
        /// computes the gaps and mismatches and does the mark cut down
        /// then, returns the new score
        /// </summary>
        /// <param name="oldScore">Old score</param>
        /// <param name="highlightedSquares">A sequence of index and positions of highlited squares.</param>
        /// <param name="recalledSquares">A sequence of index and positions of recalled squares.</param>
        /// <returns>New computed score.</returns>
        private static int CompareAnswerAndRecall(
            int oldScore,
            List <IndexAndPosition> highlightedSquares,
            List <IndexAndPosition> recalledSquares)
        {
            int numberOfHighlightedSquares = highlightedSquares.Count;

            string answer = "";
            string recall = "";

            // Create a string of the char indices of highlighted squares
            foreach (IndexAndPosition squareIndexAndPos in highlightedSquares)
            {
                int  intIndex  = squareIndexAndPos.Index;
                char charIndex = MatchIntIndexToCharIndex(intIndex);
                answer = answer + charIndex;
            }

            // Create a string of the indices of recalled squares
            foreach (IndexAndPosition squareIndexAndPos in recalledSquares)
            {
                int  intIndex  = squareIndexAndPos.Index;
                char charIndex = MatchIntIndexToCharIndex(intIndex);
                recall = recall + charIndex;
            }

            // Compute string similarity on the above two strings
            StringSimilarity.ComputeSimilarity(answer, recall);

            // Derive the computed strings
            // Computed strings are in char indices
            string computedAnswer = StringSimilarity.GetComputedString1();
            string computedRecall = StringSimilarity.GetComputedString2();

            int newScore = oldScore;

            // Boolean for recording if the player correctly recalls in the current round
            bool recallCorrect = true;

            // Iterate through computed strings and cut down marks:
            int lengthOfComputedAnswer = computedAnswer.Length;

            for (int i = 0; i < lengthOfComputedAnswer; i++)
            {
                // If there is a missing square in the player's answer
                if (computedAnswer[i] == '-' || computedRecall[i] == '-')
                {
                    newScore     -= MatchGapToMarkCutDown(numberOfHighlightedSquares);
                    recallCorrect = false;
                }
                // If there is an incorrect square in the player's answer
                else if (computedAnswer[i] != computedRecall[i])
                {
                    //get the int index based on the char index
                    int answerIntIndex = MatchCharIndexToIntIndex(computedAnswer[i]);
                    int recallIntIndex = MatchCharIndexToIntIndex(computedRecall[i]);

                    int markCutDown = MatchMismatchToMarkCutDown(
                        answerIntIndex, recallIntIndex,
                        highlightedSquares, recalledSquares);

                    newScore     -= markCutDown;
                    recallCorrect = false;
                }
            }

            // Check to see if player is eligible to get a bonus mark
            int bonusMark = GetBonusMark(numberOfHighlightedSquares, recallCorrect);

            newScore += bonusMark;

            return(newScore);
        }