public static TestSettings Deserialize(string path)
        {
            object returned = null;

            if (File.Exists(path))
            {
                using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    using (var sw = new StreamReader(fileStream))
                    {
                        var ser = new DataContractSerializer(typeof(TestSettings));
                        try
                        {
                            returned = ser.ReadObject(fileStream);
                        }
                        catch (Exception)
                        {
                            TestMenuConsolePages.PrintRed("Settings file is corrupted, creating default settings file");
                            Console.ReadKey();
                            return((TestSettings)returned);
                        }
                        finally
                        {
                            sw.Close();
                        }
                    }
                }
                TestMenuConsolePages.PrintGreen("Configuration file succefuly loaded");
                Console.WriteLine("Press any key to continue");
                Console.ReadKey();
                Console.Clear();
                return((TestSettings)returned);
            }
            else
            {
                var previousColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Couldn't open settings file, creating default");
                Console.ForegroundColor = previousColor;
                Console.WriteLine("Press any key to continue");
                Console.ReadKey();
                Console.Clear();
                return(new TestSettings(1));
            }
        }
        public static void GenerateTests(TestSettings currentSettings, string path)
        {
            using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (var sw = new StreamWriter(fs))
                {
                    var rand = new Random();
                    sw.WriteLine($"{currentSettings.CommandCount}");
                    for (var i = 0; i < currentSettings.CommandCount; i++)
                    {
                        var commandInt           = rand.Next(currentSettings.GetSumOfChances());
                        var sumOfPreviousChances = 0;

                        foreach (var option in currentSettings.AvailableOptionsTuples)
                        {
                            if (commandInt < currentSettings.GetChance(option.Key) + sumOfPreviousChances)
                            {
                                var appendedLineBuilder = new StringBuilder();
                                appendedLineBuilder.Append(option.Key);
                                for (var j = 0; j < option.Value; j++)
                                {
                                    appendedLineBuilder.Append($" {(j == 0 ? rand.Next() % 1000: rand.Next())}");
                                }
                                sw.WriteLine(appendedLineBuilder.ToString());
                                break;
                            }
                            else
                            {
                                sumOfPreviousChances += currentSettings.GetChance(option.Key);
                            }
                        }
                    }
                    fs.Flush();
                }
                TestMenuConsolePages.PrintGreen("Succeeded making test.txt");
                Console.ReadKey();
            }
        }
Beispiel #3
0
        public void TestGeneratingProcedure()
        {
            _currentSettings = TestSettings.Deserialize("settings.xml") ?? new TestSettings(1);

            while (true)
            {
                Console.Clear();
                TestMenuConsolePages.ViewMainMenu();
                var input = Console.ReadLine();
                switch (input)
                {
                case "1":
                    _menu.ViewChanceMenu(_currentSettings);
                    Tuple <bool, char, int> change = TestMenuConsolePages.ChangeOption();
                    if (change.Item1)
                    {
                        _currentSettings.SetChance(change.Item2, change.Item3);
                        continue;
                    }
                    else
                    {
                        Console.Clear();
                        continue;
                    }

                case "2":
                    _currentSettings.CommandCount = TestMenuConsolePages.AskForCommandAmount();
                    GenerateTests("test.txt");
                    break;

                default:
                    _currentSettings.Serialize("settings.xml");
                    return;
                }
            }
        }