Example #1
0
        public void TrieSuggestorService_WhenEmptyStringPassed_ShouldReturnAllStations()
        {
            // Arrange
            var content = new List <string> {
                "A", "AA", "AB", "AC", "AD"
            };

            fileHandler.Setup(fh => fh.ReadTextFileLines()).Returns(content);
            this.service = new TrieSuggestorService(fileHandler.Object);

            List <string> expectedStations = new List <string> {
                "A", "AA", "AB", "AC", "AD"
            };


            // Act
            var result = this.service.GetSuggestions("");

            // Assert
            Assert.AreEqual(result.Stations.Count, 5);
            foreach (string expectedStation in expectedStations)
            {
                Assert.Contains(expectedStation, result.Stations);
            }
        }
Example #2
0
        public void TrieSuggestorService_WhenDataFileExists_ShouldReturnSuggestions(string input)
        {
            // Arrange
            var content = new List <string> {
                "AQ",
            };

            fileHandler.Setup(fh => fh.ReadTextFileLines()).Returns(content);
            this.service = new TrieSuggestorService(fileHandler.Object);

            // Act
            var result = this.service.GetSuggestions(input);

            // Assert
            Assert.IsNotNull(result);
        }
Example #3
0
        public void TrieSuggestorService_WhenFurtherStationsWithUserInputPrefixDoesNotExist_ShouldNotReturnNextLetters()
        {
            // Arrange
            var content = new List <string> {
                "A", "AA", "AB", "AC", "AAA", "AAC", "ABB", "B", "BA"
            };

            fileHandler.Setup(fh => fh.ReadTextFileLines()).Returns(content);
            this.service = new TrieSuggestorService(fileHandler.Object);

            List <char> expectedNextLetters = new List <char> {
            };

            // Act
            var result = this.service.GetSuggestions("aaa");

            // Assert
            Assert.AreEqual(expectedNextLetters.Count, result.NextLetters.Count);
            Assert.AreEqual(result.NextLetters.Count, 0);
        }
Example #4
0
        public void TrieSuggestorService_WhenStationsWithUserInputPrefixExist_ShouldReturnTheCorrectStations(string input, string[] expected)
        {
            // Arrange
            var content = new List <string> {
                "A", "AA", "AB", "AC", "AAA", "AAC", "ABB", "B", "BA"
            };

            fileHandler.Setup(fh => fh.ReadTextFileLines()).Returns(content);
            this.service = new TrieSuggestorService(fileHandler.Object);

            List <string> expectedStations = expected.ToList();

            // Act
            var result = this.service.GetSuggestions(input);

            // Assert
            Assert.AreEqual(expectedStations.Count, result.Stations.Count);
            foreach (string expectedStation in expectedStations)
            {
                Assert.Contains(expectedStation, result.Stations);
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            ITrainStationSuggestorService suggestorWithTrie;
            IFileHandler fileHAndler = new FileHandler("Data\\TrainStations.txt");

            try
            {
                // Init suggestors
                suggestorWithTrie = new TrieSuggestorService(fileHAndler);

                // Tests the suggestor implementation with user input
                TestSuggestor(ref suggestorWithTrie);

                // Meassures and displays performance difference between Trie and List implementation
                // ListSuggestorService uses List and Linq, Trie uses Trie search tree data structure
                CheckPerformance();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
Example #6
0
        /// <summary>
        /// Page faults, static initializers, JIT, CPU cache, branch predictors, and context switches affect run time
        /// therefore performance is checked in initiaising multiple times, and seach performance checked by searching
        /// for multiple (randomly generated) user inputs
        /// </summary>
        public static void CheckPerformance()
        {
            Console.WriteLine("");
            Console.WriteLine("#############################################");
            Console.WriteLine("-- Trainstation suggester performance test --");
            Console.WriteLine("#############################################");
            Console.WriteLine("");

            Stopwatch stopwatch = new Stopwatch();

            IFileHandler fileHandler = new FileHandler("Data\\TrainStations.txt");
            ITrainStationSuggestorService trieService;
            ITrainStationSuggestorService listService;

            trieService = new TrieSuggestorService(fileHandler);
            listService = new ListSuggestorService(fileHandler);

            // Performance check for initialisation (building the stations lists
            int numOfInit = 100;

            stopwatch.Start();
            for (int i = 0; i < numOfInit; ++i)
            {
                trieService = new TrieSuggestorService(fileHandler);
            }
            stopwatch.Start();
            var trieInitPerf = stopwatch.ElapsedTicks / numOfInit;

            stopwatch.Reset();

            stopwatch.Start();
            for (int i = 0; i < 100; ++i)
            {
                listService = new ListSuggestorService(fileHandler);
            }
            stopwatch.Start();
            var listInitPerf = stopwatch.ElapsedTicks / numOfInit;

            stopwatch.Reset();

            Console.WriteLine("---------------------------------------------------");
            Console.WriteLine($"Trie initialisation average performance: { trieInitPerf } ticks.");
            Console.WriteLine($"List initialisation average performance: { listInitPerf } ticks.");
            Console.WriteLine("---------------------------------------------------");
            Console.WriteLine("Generating test user inputs for search performance test...");

            // Performance check for search
            Suggestions result;
            Random      random         = new Random();
            int         numOfTestInput = 50;
            var         userInputs     = fileHandler.GetRandomStationPrefixes(numOfTestInput);

            Console.Write("Generated inputs: ");
            foreach (string input in userInputs)
            {
                Console.Write($"{input} ");
            }
            Console.WriteLine("");

            stopwatch.Start();
            foreach (string input in userInputs)
            {
                result = trieService.GetSuggestions(input);
            }
            stopwatch.Stop();
            var trieSearchPerf = stopwatch.ElapsedTicks / numOfTestInput;

            stopwatch.Reset();

            stopwatch.Start();
            foreach (string input in userInputs)
            {
                result = listService.GetSuggestions(input);
            }
            stopwatch.Stop();
            var listSearchPerf = stopwatch.ElapsedTicks / numOfTestInput;

            stopwatch.Reset();

            Console.WriteLine("---------------------------------------------------");
            Console.WriteLine($"Trie search average performance: { trieSearchPerf } ticks.");
            Console.WriteLine($"List search average performance: { listSearchPerf } ticks.");
        }