Esempio n. 1
0
        public void TestWithBackTrack()
        {
            var          sb      = new StringBuilder("abacab");
            const string pattern = "aba";
            var          algo    = new StringMatchingAlgorithm(sb, pattern, 0);

            for (var i = 0; i < sb.Length; i++)
            {
                algo.CheckForMatch(i + 1);
            }
            var abacabState = algo.State;

            sb.Append('a');
            Assert.That(algo.CheckForMatch(sb.Length), Is.True);
            sb.Remove(sb.Length - 1, 1);

            algo.State = abacabState;
            sb.Append('a');
            Assert.That(algo.CheckForMatch(sb.Length), Is.True);
            sb.Remove(sb.Length - 1, 1);

            algo.State = abacabState;
            sb.Append('c');
            Assert.That(algo.CheckForMatch(sb.Length), Is.False);
        }
        public void RandomTest(int alphabet, string pattern)
        {
            var occurences   = new List <int>();
            var milliseconds = new List <long>();
            var rnd          = new Random();

            foreach (var textLength in TextLengths)
            {
                var sb   = new StringBuilder();
                var algo = new StringMatchingAlgorithm(sb, pattern, 0);
                var sw   = Stopwatch.StartNew();
                var occ  = 0;
                for (var i = 0; i < textLength; ++i)
                {
                    sb.Append((char)('a' + rnd.Next() % alphabet));
                    if (algo.CheckForMatch(i + 1))
                    {
                        occ++;
                    }
                }
                sw.Stop();
                occurences.Add(occ);
                milliseconds.Add(sw.ElapsedMilliseconds);
            }
            for (var i = 0; i < TextLengths.Length; ++i)
            {
                Console.WriteLine("Text length: {0}, Occurences count: {1}, Elapsed ms: {2}",
                                  TextLengths[i], occurences[i], milliseconds[i]);
            }
        }
        private void DoTest(string inputFile, string outputFile)
        {
            var outputData = File.ReadAllLines(outputFile);
            var result     = outputData[0] == "0"
                ? new int[] {}
                : outputData[1]
            .Split(null as char[], StringSplitOptions.RemoveEmptyEntries)
            .Select(int.Parse)
            .ToArray();

            Console.WriteLine("Testing file: {0}", inputFile);
            var inputData  = File.ReadAllLines(inputFile);
            var sb         = new StringBuilder();
            var algo       = new StringMatchingAlgorithm(sb, inputData[1], 0);
            var occurences = new List <int>();
            var sw         = Stopwatch.StartNew();

            for (var i = 0; i < inputData[0].Length; ++i)
            {
                sb.Append(inputData[0][i]);
                if (algo.CheckForMatch(i + 1))
                {
                    occurences.Add(i - inputData[1].Length + 2);
                }
            }
            sw.Stop();
            Assert.That(occurences, Is.EquivalentTo(result));
            Console.WriteLine("\tText length: {0}\n\tPattern length: {1}\n\tOccurences found: {2}\n\tElapsed milliseconds: {3}\n", inputData[0].Length, inputData[1].Length, occurences.Count, sw.ElapsedMilliseconds);
        }
Esempio n. 4
0
        public void Test(string text, string pattern, int[] expectedOccurences)
        {
            var sb        = new StringBuilder();
            var algorithm = new StringMatchingAlgorithm(sb, pattern, 0);

            var occurences = new List <int>();

            for (var i = 0; i < text.Length; ++i)
            {
                sb.Append(text[i]);
                if (algorithm.CheckForMatch(i + 1))
                {
                    occurences.Add(i);
                }
            }
            Assert.That(occurences, Is.EquivalentTo(expectedOccurences));
        }