public void ElementAt() { IParsedFile file = new ParsedFile(_sampleFolderPath + "Sample_file.txt"); IParsedLine firstLine = file.NextLine(); string lastElement = firstLine.ElementAt <string>(firstLine.Count - 1); Assert.Equal("food", lastElement); IParsedLine peekedSecondLine = file.PeekNextLine(); int number = peekedSecondLine.ElementAt <int>(1); Assert.Equal(100, number); const double modifiedNumber = 100 + Math.PI; peekedSecondLine.Append($" {modifiedNumber}"); IParsedLine secondLine = file.NextLine(); int numberAgain = secondLine.ElementAt <int>(1); Assert.Equal(number, numberAgain); double addedNumber = secondLine.ElementAt <double>(secondLine.Count - 1); Assert.Equal(modifiedNumber, addedNumber, 10); }
public void ExtractChar() { const string fileName = "ExtractChar.txt"; const string line1 = "+-*/!?#$%&"; const string line2 = "@()[]{}\""; StringBuilder parsedFile = new(string.Empty); using (StreamWriter writer = new(fileName)) { writer.WriteLine(Math.PI.ToString()); writer.WriteLine(line1); writer.WriteLine(line2); } IParsedFile file = new ParsedFile(fileName); IParsedLine firstParsedLine = file.NextLine(); int nLines = Convert.ToInt32(Math.Floor(firstParsedLine.NextElement <double>())) - 1; for (int iLine = 0; iLine < nLines; ++iLine) { IParsedLine parsedLine = file.NextLine(); while (!parsedLine.Empty) { parsedFile.Append(parsedLine.NextElement <char>()); } parsedFile.Append('\\'); Assert.True(parsedLine.Empty); } Assert.True(file.Empty); Assert.Equal(line1 + '\\' + line2 + '\\', parsedFile.ToString()); }
private IEnumerable <Input> ParseInputPrivate(string level) { var file = new ParsedFile($"Inputs/{level}"); var numberOfLines = file.NextLine().NextElement <int>(); for (int i = 0; i < numberOfLines; ++i) { var line = file.NextLine().ToSingleString(); var elements = line.Split(","); if (elements.Length != 7) { throw new Exception("Error parsing file"); } yield return(new Input( double.Parse(elements[0]), double.Parse(elements[1]), double.Parse(elements[2]), double.Parse(elements[3]), elements[4], elements[5], double.Parse(elements[6]))); } if (!file.Empty) { throw new Exception("Error parsing file"); } }
public void ParsingException() { IParsedFile file = new ParsedFile(_validPath + "Sample_file.txt"); IParsedLine line = file.NextLine(); while (!file.Empty) { line = file.NextLine(); } Assert.Throws <ParsingException>(() => file.NextLine()); while (!line.Empty) { line.NextElement <object>(); } Assert.Throws <ParsingException>(() => line.NextElement <object>()); Queue <string> testQueue = new(new string[] { string.Empty }); ParsedLine testLine = new(testQueue); Assert.Throws <ParsingException>(() => testLine.NextElement <char>()); }
public static void Main() { using var outputFile = new StreamWriter("results.out"); ParsedFile inputFile = new ParsedFile("Inputs/testInput.txt"); var lines = inputFile.NextLine().NextElement <int>(); for (int i = 0; i < lines; ++i) { var line = inputFile.NextLine(); var player1 = (int)Enum.Parse(typeof(Item), line.NextElement <string>()); var player2 = (int)Enum.Parse(typeof(Item), line.NextElement <string>()); var players = new[] { player1, player2 }; var result = Math.Abs(player2 - player1) switch { 0 => "-", 1 => ((Item)players.Max()).ToString(), 2 => ((Item)players.Min()).ToString(), _ => throw new Exception(@"¯\_(ツ)_/¯") }; outputFile.WriteLine($"Case #{i + 1}: {result}"); } } }
public void ParsedDoubleWithCommas() { IParsedFile parsedFile = new ParsedFile(Path.Combine(_sampleFolderPath, "DoubleWithCommas.txt")); ValidateSimpleFile(parsedFile.NextLine(), parsedFile.NextLine()); Assert.True(parsedFile.Empty); }
public void ParsedDoubleWithPointsAndCommasAsThousandSeparators() { IParsedFile parsedFile = new ParsedFile(Path.Combine(_sampleFolderPath, "DoubleWithPointsAndCommasAsThousandsSeparators.txt")); ValidateFileWithPointsAndCommas(parsedFile.NextLine(), parsedFile.NextLine(), parsedFile.NextLine(), parsedFile.NextLine()); Assert.True(parsedFile.Empty); }
private void LoadData() { ParsedFile _file = new ParsedFile(_inputPath); IParsedLine firstLine = _file.NextLine(); /*long gridRow = */ firstLine.NextElement <long>(); /*long gridCol = */ firstLine.NextElement <long>(); //Grid = new Grid(gridRow, gridCol); long n_vehicles = firstLine.NextElement <long>(); for (int i = 0; i < n_vehicles; ++i) { VehicleList.Add(new Vehicle(i)); } long n_rides = firstLine.NextElement <long>(); long bonus = firstLine.NextElement <long>(); TotalSimulationSteps = firstLine.NextElement <long>(); long rideId = 0; while (!_file.Empty) { IParsedLine line = _file.NextLine(); long row = line.NextElement <long>(); long col = line.NextElement <long>(); long endrow = line.NextElement <long>(); long endcol = line.NextElement <long>(); long start = line.NextElement <long>(); long end = line.NextElement <long>(); RideList.Add(new Ride( rideId, bonus, new Position(row, col), new Position(endrow, endcol), start, end, TotalSimulationSteps)); ++rideId; } if (n_rides != RideList.Count) { throw new ParsingException(); } }
public void LineAt() { IParsedFile file = new ParsedFile(_sampleFolderPath + "Sample_file.txt"); IParsedLine lineAt0 = file.LineAt(0); int n = lineAt0.PeekNextElement <int>(); Assert.Equal(23, n); int nPlusOne = n + 1; lineAt0.Append($" {nPlusOne}"); file.Append(new ParsedLine(new[] { nPlusOne.ToString() })); int totalNumberOfLines = file.Count; IParsedLine firstLine = file.NextLine(); int nAgain = firstLine.NextElement <int>(); string str = firstLine.NextElement <string>(); int incrementedN = firstLine.NextElement <int>(); Assert.Equal(n, nAgain); Assert.Equal(nPlusOne, incrementedN); Assert.Equal("food", str); IParsedLine lastLine = file.LastLine(); Assert.Equal(incrementedN, lastLine.PeekNextElement <int>()); for (int lineIndex = 1; lineIndex < totalNumberOfLines - 1; ++lineIndex) { IParsedLine line = file.NextLine(); int counter = line.NextElement <int>(); for (int j = 0; j < counter; ++j) { line.NextElement <int>(); } while (!line.Empty) { line.NextElement <string>(); } } IParsedLine extraLine = file.NextLine(); Assert.Equal(incrementedN, extraLine.NextElement <int>()); Assert.True(extraLine.Empty); Assert.Throws <ArgumentOutOfRangeException>(() => extraLine.ElementAt <string>(1)); Assert.Throws <InvalidOperationException>(() => extraLine.LastElement <string>()); Assert.True(file.Empty); Assert.Throws <ArgumentOutOfRangeException>(() => file.LineAt(1)); Assert.Throws <InvalidOperationException>(() => file.LastLine()); }
private IEnumerable <ulong> ParseInput() { ParsedFile file = new ParsedFile(InputFilePath); var cases = file.NextLine().NextElement <int>(); for (int i = 0; i < cases; ++i) { yield return(ulong.Parse(file.NextLine().ToSingleString())); } }
public void ToSingleStringWithCustomSeparator() { IParsedFile parsedFile = new ParsedFile(Path.Combine(_sampleFolderPath, "ToSingleString.txt")); parsedFile.NextLine(); IParsedLine secondLine = parsedFile.NextLine(); Assert.Equal( "Second^~line^~1^~2^~34", secondLine.ToSingleString("^~")); Assert.True(secondLine.Empty); }
public void WriteMultipleElements() { string path = "WriteMultipleElements.txt"; Writer.Clear(path); bool _bool = false; Writer.Write(path, _bool); int _int = 1; Writer.Write(path, _int); double _double = 3.14159265; Writer.Write(path, _double); string _string = " Vishy "; Writer.Write(path, _string); DateTime _dateTime = new DateTime(1999, 12, 31, 23, 59, 59); Writer.WriteLine(path, _dateTime); Writer.NextLine(path); // Extra line Writer.WriteLine(path, _bool); Writer.WriteLine(path, _int); Writer.WriteLine(path, _double); Writer.WriteLine(path, _string); IParsedFile parsedFile = new ParsedFile(path); IParsedLine firstLine = parsedFile.NextLine(); Assert.Equal(_bool, firstLine.NextElement <bool>()); Assert.Equal(_int, firstLine.NextElement <int>()); Assert.Equal(_double, firstLine.NextElement <double>()); Assert.Equal(_string.Trim(), firstLine.NextElement <string>()); Assert.Equal(_bool, parsedFile.NextLine().NextElement <bool>()); Assert.Equal(_int, parsedFile.NextLine().NextElement <int>()); Assert.Equal(_double, parsedFile.NextLine().NextElement <double>()); Assert.Equal(_string.Trim(), parsedFile.NextLine().NextElement <string>()); Assert.True(parsedFile.Empty); }
public void WriteListString() { string path = "WriteListString.txt"; Writer.Clear(path); ICollection <string> vectorToWrite = new List <string>() { "string1", "string2", "string3", "string4" }; string textInFile = string.Empty; string separator = "$"; Writer.WriteLine <ICollection <string>, string>(path, vectorToWrite, separator); IParsedFile file = new ParsedFile(path, separator); while (!file.Empty) { IParsedLine line = file.NextLine(); while (!line.Empty) { textInFile += line.NextElement <string>(); if (!line.Empty) { textInFile += "@"; } } } List <string> vectorInFile = textInFile.Split('@').ToList(); Assert.Equal(vectorToWrite, vectorInFile); }
static void Main(string[] args) { var cultureInfo = new CultureInfo("en-US"); CultureInfo.DefaultThreadCurrentCulture = cultureInfo; List <double> listDouble = new List <double>(); string str; // Input start IParsedFile file = new ParsedFile("SimpleInput.txt"); IParsedLine firstLine = file.NextLine(); int _integer = firstLine.NextElement <int>(); for (int i = 0; i < _integer; ++i) { listDouble.Add(firstLine.NextElement <double>()); } str = firstLine.NextElement <string>(); // Input end // Data Processing // Output start StreamWriter writer = new StreamWriter("..\\C#SimpleOutput.txt"); using (writer) { writer.WriteLine(_integer + " " + string.Join(null, listDouble)); } // Output end }
private ICollection <Event> ParseInput() { ICollection <Event> events = new List <Event>(); IParsedFile parsedFile = new ParsedFile(FilePath); while (!parsedFile.Empty) { IParsedLine parsedLine = parsedFile.NextLine(); string aux = string.Empty; while (!parsedLine.Empty) { aux += $"{parsedLine.NextElement<string>()} "; } string line = aux .Trim() .Replace(" ", " "); Event evnt = ParseEvent(line); evnt.DateTime = ParseDateTime(line); events.Add(evnt); } return(events); }
private Lvl5Input ParseInput(string level) { var file = new ParsedFile($"Inputs/{level}"); var result = new Lvl5Input(double.Parse(file.NextLine().ToSingleString())); var numberOfLines = file.NextLine().NextElement <int>(); for (int i = 0; i < numberOfLines; ++i) { result.FlightIds.Add(file.NextLine().ToSingleString()); } if (!file.Empty) { throw new Exception("Error parsing file"); } return(result); }
private IEnumerable <string> ReadInputs() { IParsedFile inputFile = new ParsedFile(_inputFilePath); while (!inputFile.Empty) { yield return(inputFile.NextLine().ToSingleString()); } }
private IEnumerable <Lvl4Input> ParseInputPrivate(string level) { var file = new ParsedFile($"Inputs/{level}"); var numberOfLines = file.NextLine().NextElement <int>(); for (int i = 0; i < numberOfLines; ++i) { var line = file.NextLine(); yield return(new Lvl4Input( line.NextElement <string>(), line.NextElement <double>())); } if (!file.Empty) { throw new Exception("Error parsing file"); } }
public void BasicTest() { // Sample file: // First line has a random number and a category of aliments. // Following lines firstly indicate how many numeric elements are following. // After those numeric elements, they include an unknown number of items. List <int> numberList = new(); List <string> stringList = new(); IParsedFile file = new ParsedFile(_sampleFolderPath + "Sample_file.txt"); IParsedLine firstLine = file.NextLine(); int n = firstLine.NextElement <int>(); string str = firstLine.NextElement <string>(); while (!file.Empty) { IParsedLine line = file.NextLine(); int counter = line.NextElement <int>(); for (int j = 0; j < counter; ++j) { numberList.Add(line.NextElement <int>()); } while (!line.Empty) { stringList.Add(line.NextElement <string>()); } } Assert.Equal(23, n); Assert.Equal("food", str); Assert.Equal(new List <int>() { 100, 200, 300, 400, 500, 600, 700 }, numberList); Assert.Equal(new List <string>() { "apple", "peer", "banana", "meat", "fish" }, stringList); }
public void ToSingleStringWithEmptySeparator() { IParsedFile parsedFile = new ParsedFile(Path.Combine(_sampleFolderPath, "ToSingleString.txt")); IParsedLine firstLine = parsedFile.NextLine(); Assert.Equal( "0This1234isalinewithsomelinesoftext404", firstLine.ToSingleString(string.Empty)); Assert.True(firstLine.Empty); }
private IEnumerable <string> Play() { ParsedFile file = new ParsedFile(InputFilePath); var lines = file.NextLine().NextElement <int>(); for (int i = 0; i < lines; ++i) { var line = file.NextLine(); var player1 = (int)Enum.Parse(typeof(Item), line.NextElement <string>()); var player2 = (int)Enum.Parse(typeof(Item), line.NextElement <string>()); var players = new[] { player1, player2 }; yield return(Math.Abs(player2 - player1) switch { 0 => "-", 1 => ((Item)players.Max()).ToString(), 2 => ((Item)players.Min()).ToString(), _ => throw new ArgumentException(@"¯\_(ツ)_/¯") });
private IEnumerable <Rectangle> ParseInput() { ICollection <Rectangle> rectangles = new List <Rectangle>(); IParsedFile parsedFile = new ParsedFile(FilePath); while (!parsedFile.Empty) { IParsedLine parsedLine = parsedFile.NextLine(); string idString = parsedLine.NextElement <string>(); if (!idString.StartsWith("#")) { throw new Exception($"{idString} is not #n"); } int.TryParse(idString.Trim('#'), out int id); if (parsedLine.NextElement <string>() != "@") { throw new Exception($"Exception parsing @"); } string[] x0y0 = parsedLine.NextElement <string>() .Trim(':') .Split(','); if (x0y0.Length != 2) { throw new Exception($"Length of {x0y0} isn't 2"); } int.TryParse(x0y0.First(), out int x0); int.TryParse(x0y0.Last(), out int y0); string[] xy = parsedLine.NextElement <string>() .Split('x'); if (xy.Length != 2) { throw new Exception($"Length of {xy} isn't 2"); } int.TryParse(xy.First(), out int x); int.TryParse(xy.Last(), out int y); if (!parsedLine.Empty) { throw new Exception($"Error parsing line, missing at least {parsedLine.PeekNextElement<string>()}"); } rectangles.Add(new Rectangle(x: x, y: y, x0: x0, y0: y0) { Id = id }); } return(rectangles); }
private IEnumerable <long> ParseInput() { IParsedFile parsedFile = new ParsedFile(FilePath); while (!parsedFile.Empty) { IParsedLine parsedLine = parsedFile.NextLine(); while (!parsedLine.Empty) { yield return(parsedLine.NextElement <long>()); } } }
public void NotSupportedException() { Assert.Throws <NotSupportedException>(() => new ParsedFile(_validPath + "Sample_file.txt").ToList <uint>()); Assert.Throws <NotSupportedException>(() => new ParsedFile(_validPath + "Sample_file.txt").ToList <DateTime>()); var line = new ParsedLine(new Queue <string>(new string[] { "1234" })); Assert.Throws <NotSupportedException>(() => line.NextElement <ulong>()); IParsedFile parsedFile = new ParsedFile(_validPath + "Sample_file.txt"); IParsedLine firstParsedLine = parsedFile.NextLine(); Assert.Throws <NotSupportedException>(() => firstParsedLine.NextElement <char>()); }
public void PeekTestEmptyingFile() { IParsedFile file = new ParsedFile(_sampleFolderPath + "Sample_file.txt"); IParsedLine peekedFirstLine = file.PeekNextLine(); // Allows its modification without extracting it int peekedN = peekedFirstLine.PeekNextElement <int>(); Assert.Equal(peekedN, peekedFirstLine.NextElement <int>()); // Extracting the element string peekedStr = peekedFirstLine.PeekNextElement <string>(); Assert.Equal(peekedStr, peekedFirstLine.NextElement <string>()); Assert.True(file.NextLine().Empty); while (!file.Empty) { List <int> peekedNumberList = new(); List <string> peekedStringList = new(); IParsedLine peekedLine = file.PeekNextLine(); int peekedCounter = peekedLine.PeekNextElement <int>(); Assert.Equal(peekedCounter, peekedLine.NextElement <int>()); for (int j = 0; j < peekedCounter; ++j) { peekedNumberList.Add(peekedLine.PeekNextElement <int>()); Assert.Equal(peekedNumberList.Last(), peekedLine.NextElement <int>()); // Extracting the element } while (!peekedLine.Empty) { peekedStringList.Add(peekedLine.PeekNextElement <string>()); Assert.Equal(peekedStringList.Last(), peekedLine.NextElement <string>()); // Extracting the element } IParsedLine line = file.NextLine(); // Extracting the line, already emptied, to allow the test to finish Assert.True(line.Empty); } }
private IEnumerable <Point> ParseInput() { IParsedFile parsedFile = new ParsedFile(FilePath); while (!parsedFile.Empty) { IParsedLine parsedLine = parsedFile.NextLine(); int.TryParse(parsedLine.NextElement <string>().Trim(','), out int x); yield return(new Point(x, parsedLine.NextElement <int>())); } }
private FlightInput ParseFlightInfo(string flightId) { var file = new ParsedFile($"Inputs/usedFlights/{flightId}.csv"); var result = new FlightInput() { Start = file.NextLine().ToSingleString(), End = file.NextLine().ToSingleString(), Takeoff = double.Parse(file.NextLine().ToSingleString()) }; var numberOfLines = file.NextLine().NextElement <int>(); for (int i = 0; i < numberOfLines; ++i) { var line = file.NextLine().ToSingleString(); var elements = line.Split(","); if (elements.Length != 4) { throw new Exception("Error parsing file"); } result.FlightInfo.Add(new FlightInfo( double.Parse(elements[0]), double.Parse(elements[1]), double.Parse(elements[2]), double.Parse(elements[3]))); } result.FlightInfo = result.FlightInfo.OrderBy(f => f.TimestampOffset).ToList(); if (!file.Empty) { throw new Exception("Error parsing file"); } return(result); }
public void ToSingleStringWithDefaultSeparator() { IParsedFile parsedFile = new ParsedFile(Path.Combine(_sampleFolderPath, "ToSingleString.txt")); IParsedLine firstLine = parsedFile.NextLine(); Assert.Equal(0, firstLine.NextElement <int>()); Assert.Equal("This", firstLine.NextElement <string>()); Assert.Equal( "1234 is a line with some lines of text 404", firstLine.ToSingleString()); Assert.True(firstLine.Empty); }
public Tuple <int, int> ParseInput() { IParsedFile parsedFile = new ParsedFile(FilePath); IParsedLine parsedLine = parsedFile.NextLine(); int nPlayers = parsedLine.NextElement <int>(); string str = string.Empty; do { str = parsedLine.NextElement <string>(); } while (str != "worth"); int maxPoints = parsedLine.NextElement <int>(); return(Tuple.Create(nPlayers, maxPoints)); }
private HashSet <Step> ParseInput() { HashSet <Step> steps = new HashSet <Step>(); IParsedFile parsedFile = new ParsedFile(FilePath); while (!parsedFile.Empty) { IParsedLine parsedLine = parsedFile.NextLine(); parsedLine.NextElement <string>(); string dependencyName = parsedLine.NextElement <string>(); Step stepDependency = new Step(char.ToUpperInvariant(dependencyName.Single())); steps.Add(stepDependency); while (!parsedLine.Empty) { string word = parsedLine.NextElement <string>(); if (word.ToUpperInvariant() == "STEP") { string mainStepName = parsedLine.NextElement <string>(); if (steps.TryGetValue(new Step(char.ToUpperInvariant(mainStepName.Single())), out Step existingStep)) { existingStep.NonResolvedDependencies.Add(stepDependency); } else { steps.Add(new Step(mainStepName.Single(), stepDependency)); } continue; } } } return(steps); }