public void Setup() { _gameFactory = new GameFactory(); var generator = new GenerateKeyRangesService(); _serviceUnderTests = new VilleSolverService(generator); }
public static void TestOnRange(ISolveMastermindService serviceUnderTests, IGameSettings settings, string algoLabel) { // Arrange var gameFactory = new GameFactory(); var keys = GetKeysRange(settings); var fails = new Dictionary <string, int>(); long allRoundsCount = 0; var maxRounds = 0; var maxExample = ""; Stopwatch stopwatch = new Stopwatch(); TimeSpan elapsedTotal = TimeSpan.FromMilliseconds(0); TimeSpan longestElapsed = TimeSpan.FromMilliseconds(0); string longestExecutionExample = keys.First(); int i = 0; Console.WriteLine($"\rFor {algoLabel} algo on Mastermind({settings.Digits}, {settings.Colors}):"); foreach (var answer in keys) { ++i; var mastermindGame = gameFactory.PrepareGame(answer, settings); Console.Write($"\r[{i}/{keys.Count()}] '{answer}'"); stopwatch.Start(); // Act var result = serviceUnderTests.SolveGame(mastermindGame); // Assert stopwatch.Stop(); allRoundsCount += result.Rounds; elapsedTotal = elapsedTotal.Add(stopwatch.Elapsed); if (answer != result.Answer || result.Rounds > settings.RoundLimit) { Console.WriteLine($"[ERROR] Got {result.Answer} instead in {result.Rounds} rounds! {new string(' ', 110)}"); fails[answer] = result.Rounds; } else { var meanExexSoFarMs = (double)elapsedTotal.TotalMilliseconds / i; var ETA = TimeSpan.FromMilliseconds(meanExexSoFarMs * (keys.Count() - i)); Console.Write($"| Mean rounds {((double)allRoundsCount / i):0.00} max {maxRounds} for {maxExample}, mean time {meanExexSoFarMs:0.00} ms, max {longestElapsed.TotalMilliseconds:0.00} ms for {longestExecutionExample} [ETA {ETA}]"); if (result.Rounds > maxRounds) { maxRounds = result.Rounds; maxExample = answer; } if (stopwatch.Elapsed > longestElapsed) { longestElapsed = stopwatch.Elapsed; longestExecutionExample = answer; } } stopwatch.Reset(); } // display results if (fails.Keys.Count() > 0) { Console.WriteLine($"Failed to find solution in {fails.Count()} {new string(' ', 110)}"); var worstRoundCount = fails.Values.Max(); var worstCases = fails.Where((k, v) => v == worstRoundCount).Select((k, v) => k); string worstCaseExample = worstCases.First().Key; Console.WriteLine($"{worstCases.Count()} pessimistic cases - in {worstRoundCount} rounds, example: {worstCaseExample} "); } double mean = (double)allRoundsCount / keys.Count(); Console.WriteLine($"\rMean rounds per solution is {mean}{new string(' ', 110)}"); Console.WriteLine($"Example with most rounds - {maxRounds} - is {maxExample}"); double meanExecMs = (double)elapsedTotal.TotalMilliseconds / keys.Count(); Console.WriteLine($"\rMean execution time is {meanExecMs} ms"); Console.WriteLine($"Longest execution time {longestElapsed.TotalMilliseconds} ms found for {longestExecutionExample}"); }