Пример #1
0
        public void PeekTestNotModifiyngFile()
        {
            IParsedFile file = new ParsedFile(_sampleFolderPath + "Sample_file.txt");

            for (int i = 0; i < 10; ++i)
            {
                IParsedLine peekedFirstLine = file.PeekNextLine();       // Allows its modification without extracting it

                int peekedN = peekedFirstLine.PeekNextElement <int>();
                Assert.Equal(peekedN, peekedFirstLine.PeekNextElement <int>());

                IParsedLine peekedFirstLine2 = file.PeekNextLine();

                int peekedN2 = peekedFirstLine.PeekNextElement <int>();
                Assert.Equal(peekedN2, peekedFirstLine2.PeekNextElement <int>());

                Assert.Equal(peekedN, peekedN2);
            }
        }
Пример #2
0
        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());
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
            }
        }