コード例 #1
0
        public void ExecutionTimeTest()
        {
            string fileName = "BigFileTest.txt";

            try
            {
                File.Delete(fileName);
            }
            catch
            { };

            int iterations = 1000000;

            using (StreamWriter sw = new StreamWriter(fileName))
            {
                for (int i = 0; i < iterations; i++)
                {
                    sw.WriteLine("abc, def - ghi. Abc. ");
                }
                sw.Close();
            }

            IDictionary <string, int> calculatedAnswer = new Dictionary <string, int>();
            IWordCounter wc        = CreateInstance();
            Stopwatch    stopwatch = new Stopwatch();

            //0 current
            //1 - next
            using (StreamReader sr = new StreamReader(fileName))
            {
                stopwatch.Start();

                bool  isLastChar = false;
                int[] readValues = new int[2] {
                    -1, -1
                };

                readValues[0] = sr.Read();
                readValues[1] = sr.Read();

                if (readValues[0] == -1)
                {
                    return;
                }

                if (readValues[0] != -1 && readValues[1] == -1)
                {
                    isLastChar = true;
                }

                wc.CalculateChar((char)readValues[0], isLastChar);

                while (isLastChar == false)
                {
                    readValues[0] = readValues[1];
                    readValues[1] = sr.Read();
                    if (readValues[0] != -1 && readValues[1] == -1)
                    {
                        isLastChar = true;
                    }
                    wc.CalculateChar((char)readValues[0], isLastChar);
                }

                calculatedAnswer = wc.GetCurrentFinishedWordCounter();
                stopwatch.Stop();
                sr.Close();
            }

            IDictionary <string, int> correctAnswer = new Dictionary <string, int>();

            correctAnswer["abc"] = 2 * iterations;
            correctAnswer["def"] = 1 * iterations;
            correctAnswer["ghi"] = 1 * iterations;
            AssertCorrectness(calculatedAnswer, correctAnswer);

            long elapsedTimeinMs     = stopwatch.ElapsedMilliseconds;
            long expectedElapsedTime = (long)(0.006 * iterations);

            Assert.IsFalse(elapsedTimeinMs > expectedElapsedTime, string.Format("Processing time too long. Actual: {0}, Expected {1}", elapsedTimeinMs, expectedElapsedTime));
        }