Пример #1
0
        public void SuggestsDartford()
        {
            Suggestions          suggestions = stationFinder.GetSuggestions("Dartford");
            IEnumerable <string> stations    = suggestions.Stations;

            Assert.AreEqual(1, stations.Count());
            Assert.AreEqual("Dartford", stations.First());
        }
            public void ReturnSuggestionsFromStationFinderStrategy()
            {
                var expected = new Suggestions()
                {
                    Stations    = new [] { "DARTFORD", "DARTON " },
                    NextLetters = new [] { 'F', 'O' }
                };
                var stationFinderStrategyMock = new Mock <IStationFinderStrategy>();

                stationFinderStrategyMock.Setup(x => x.GetSuggestions("DART"))
                .Returns(expected);
                var sut = new StationFinder(stationFinderStrategyMock.Object);

                var actual = sut.GetSuggestions("DART");

                actual.Should().Be(expected);
            }
Пример #3
0
        static void Main(string[] args)
        {
            var rawDataGetter = new RawDataGetter();
            var dataStore     = new DataStore(rawDataGetter);

            var stationsTrie = new Trie();

            foreach (var station in dataStore.GetStations())
            {
                stationsTrie.AddWord(station);
            }

            IVisitor visitor = new DepthFirstVisitor();
            IStationFinderStrategy finderStrategy = new TrieBasedStationFinderStrategy(stationsTrie, visitor);

            if (args.Length > 0)
            {
                if (args.First() == "perf")
                {
                    var basicStrategy = new BasicStationFinderStrategy(dataStore.GetStations());

                    var timedTest = new TimedTest();
                    timedTest.Execute("TrieBased", dataStore, finderStrategy);

                    timedTest = new TimedTest();
                    timedTest.Execute("Basic", dataStore, basicStrategy);
                    return;
                }

                if (args.First() == "basic")
                {
                    Console.WriteLine("[Strategy: Basic]");
                    finderStrategy = new BasicStationFinderStrategy(dataStore.GetStations());
                }
            }

            var finder = new StationFinder(finderStrategy);

            string userInput = "";

            while (true)
            {
                Console.WriteLine("Enter a letter:");
                Console.WriteLine("(or Escape to quit, backspace to delete)");

                var read = Console.ReadKey();

                if (read.Key == ConsoleKey.Escape)
                {
                    break;
                }

                if (read.Key == ConsoleKey.Backspace)
                {
                    if (userInput.Length > 0)
                    {
                        userInput = userInput.Substring(0, userInput.Length - 1);
                    }
                }
                else
                {
                    userInput += read.KeyChar.ToString().ToUpperInvariant();
                }

                var suggestions = finder.GetSuggestions(userInput);

                Console.WriteLine($"\n\nUserInput: {userInput}");
                Console.WriteLine($"NextLetters: {string.Join(" | ", suggestions.NextLetters)}");
                Console.WriteLine($"Stations: {string.Join(" | ", suggestions.Stations)}\n");
            }
        }