Ejemplo n.º 1
0
        public void TestABString()
        {
            string inputString = "abaabb";

            var tokenCategories = new List <KeyValuePair <SimpleTokenCategory, string> >
            {
                new KeyValuePair <SimpleTokenCategory, string>(SimpleTokenCategory.A, "a"),
                new KeyValuePair <SimpleTokenCategory, string>(SimpleTokenCategory.B, "b"),
            };

            IInputReader inputReader      = new StringInputReader(inputString);
            var          input            = inputReader.Read();
            var          conflictResolver = new ConflictResolver <SimpleTokenCategory>(SimpleTokenCategory.None);
            var          lexer            = new Lexer <SimpleTokenCategory>(
                tokenCategories,
                SimpleTokenCategory.Eof,
                SimpleTokenCategory.None,
                conflictResolver.ResolveWithMaxValue);
            var actual   = lexer.Scan(input, null).Select(x => x.Category).ToList();
            var expected = new List <SimpleTokenCategory>
            {
                SimpleTokenCategory.A, SimpleTokenCategory.B, SimpleTokenCategory.A, SimpleTokenCategory.A,
                SimpleTokenCategory.B, SimpleTokenCategory.B, SimpleTokenCategory.Eof
            };
            var expectedText = string.Join(", ", expected);
            var actualText   = string.Join(", ", actual);

            CollectionAssert.AreEqual(expected, actual, $"Expected: {expectedText}, actual: {actualText}");
        }
Ejemplo n.º 2
0
        protected void WhenParseAll <T>(IParser <T> parser)
        {
            var inp = new StringInputReader(_text);

            _parsedList = parser.TryParseAllText(_text)
                          .Select(x => x.CastToParseResult())
                          .ToArray();
        }
Ejemplo n.º 3
0
        public void TestValues()
        {
            var inputReader = new StringInputReader("test");
            var data        = inputReader.Read();
            var expected    = "test" + Constants.EndOfInput;
            var actual      = string.Concat(data.Select(kvp => kvp.Value));

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 4
0
        protected void WhenParse <T>(IParser <T> parser)
        {
            var inp = new StringInputReader(_text);

            _parsedList = new[]
            {
                parser.TryParse(inp).CastToParseResult()
            };
        }
Ejemplo n.º 5
0
        public void String_NoMatch_ConsumesNoInput()
        {
            var parser = Chars.String("xyz");
            var input  = new StringInputReader("---");
            var result = parser.Parse(input);

            Position position = input.GetPosition();

            Assert.AreEqual(0, position.Offset);
        }
        public void CalculateDistancesBetweenTiles()
        {
            StringInputReader reader = new StringInputReader();
            HexWalker         walker = new HexWalker(reader);

            Assert.Equal(3, walker.ShortestDistanceFromStart("ne,ne,ne"));
            Assert.Equal(0, walker.ShortestDistanceFromStart("ne,ne,sw,sw"));
            Assert.Equal(2, walker.ShortestDistanceFromStart("ne,ne,s,s"));
            Assert.Equal(3, walker.ShortestDistanceFromStart("se,sw,se,sw,sw"));
        }
Ejemplo n.º 7
0
        public void TestKeys()
        {
            var inputReader = new StringInputReader("test");
            var data        = inputReader.Read();
            var expected    = new List <int> {
                0, 1, 2, 3, 4
            };
            var actual = data.Select(kvp => ((StringLocation)kvp.Key).Position).ToList();

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 8
0
        public void String_PartialMatch_SetsCorrectPosition()
        {
            var parser = Chars.String("xyz");
            var input  = new StringInputReader("xy-");
            var result = parser.Parse(input);

            Position position = input.GetPosition();

            Assert.AreEqual(2, position.Offset, "Offset");
            Assert.AreEqual(1, position.Line, "Line");
            Assert.AreEqual(3, position.Column, "Column");
        }
        public void ProcessInstructions()
        {
            CPU cpu = new CPU();
            StringInputReader    reader    = new StringInputReader();
            InstructionProcessor processor = new InstructionProcessor(cpu, reader);

            string input = @"b inc 5 if a > 1
                a inc 1 if b < 5
                c dec -10 if a >= 1
                c inc -20 if c == 10";

            processor.ProcessInstructions(input);

            Assert.Equal(1, processor.GetCurrentLargestValue());
            Assert.Equal(10, processor.GetHistoricalLargestValue());
        }
Ejemplo n.º 10
0
        public void TestWithStringInput()
        {
            string inputString = "(a +40)*num   +\n[(g] * -45x";

            var tokenCategories = new List <KeyValuePair <StringTestCategory, string> >
            {
                new KeyValuePair <StringTestCategory, string>(StringTestCategory.Paren, "[\\(\\)\\[\\]]"),
                new KeyValuePair <StringTestCategory, string>(StringTestCategory.Operator, "[\\*+\\-]"),
                new KeyValuePair <StringTestCategory, string>(StringTestCategory.Number, "0|[1-9][0-9]*"),
                new KeyValuePair <StringTestCategory, string>(StringTestCategory.Variable, "[a-z][a-z]*"),
                new KeyValuePair <StringTestCategory, string>(StringTestCategory.Whitespace, "[ \n][ \n]*")
            };

            var expectedTokens = new List <Token <StringTestCategory> >
            {
                CreateToken(StringTestCategory.Paren, "(", new StringLocation(0), new StringLocation(1)),
                CreateToken(StringTestCategory.Variable, "a", new StringLocation(1), new StringLocation(2)),
                CreateToken(StringTestCategory.Whitespace, " ", new StringLocation(2), new StringLocation(3)),
                CreateToken(StringTestCategory.Operator, "+", new StringLocation(3), new StringLocation(4)),
                CreateToken(StringTestCategory.Number, "40", new StringLocation(4), new StringLocation(6)),
                CreateToken(StringTestCategory.Paren, ")", new StringLocation(6), new StringLocation(7)),
                CreateToken(StringTestCategory.Operator, "*", new StringLocation(7), new StringLocation(8)),
                CreateToken(StringTestCategory.Variable, "num", new StringLocation(8), new StringLocation(11)),
                CreateToken(StringTestCategory.Whitespace, "   ", new StringLocation(11), new StringLocation(14)),
                CreateToken(StringTestCategory.Operator, "+", new StringLocation(14), new StringLocation(15)),
                CreateToken(StringTestCategory.Whitespace, "\n", new StringLocation(15), new StringLocation(16)),
                CreateToken(StringTestCategory.Paren, "[", new StringLocation(16), new StringLocation(17)),
                CreateToken(StringTestCategory.Paren, "(", new StringLocation(17), new StringLocation(18)),
                CreateToken(StringTestCategory.Variable, "g", new StringLocation(18), new StringLocation(19)),
                CreateToken(StringTestCategory.Paren, "]", new StringLocation(19), new StringLocation(20)),
                CreateToken(StringTestCategory.Whitespace, " ", new StringLocation(20), new StringLocation(21)),
                CreateToken(StringTestCategory.Operator, "*", new StringLocation(21), new StringLocation(22)),
                CreateToken(StringTestCategory.Whitespace, " ", new StringLocation(22), new StringLocation(23)),
                CreateToken(StringTestCategory.Operator, "-", new StringLocation(23), new StringLocation(24)),
                CreateToken(StringTestCategory.Number, "45", new StringLocation(24), new StringLocation(26)),
                CreateToken(StringTestCategory.Variable, "x", new StringLocation(26), new StringLocation(27)),
                CreateToken(StringTestCategory.Eof, null, null, null)
            };

            IInputReader inputReader  = new StringInputReader(inputString);
            var          resolver     = new ConflictResolver <StringTestCategory>(StringTestCategory.None);
            var          lexer        = new Lexer <StringTestCategory>(tokenCategories, StringTestCategory.Eof, StringTestCategory.None, resolver.ResolveWithMaxValue);
            var          outputTokens = lexer.Scan(inputReader.Read(), null);

            Assert.IsTrue(outputTokens.SequenceEqual(expectedTokens, new TokenComparer <StringTestCategory>()));
        }
        public void CreateDiscTowersFromInput()
        {
            string input = @"pbga (66)
                             xhth (57)
                             ebii (61)
                             havc (66)
                             ktlj (57)
                             fwft (72) -> ktlj, cntj, xhth
                             qoyq (66)
                             padx (45) -> pbga, havc, qoyq
                             tknk (41) -> ugml, padx, fwft
                             jptl (61)
                             ugml (68) -> gyxo, ebii, jptl
                             gyxo (61)
                             cntj (57)";

            StringInputReader reader  = new StringInputReader();
            DiscTowerFactory  factory = new DiscTowerFactory(reader);
            DiscTower         tower   = factory.CreateFromInput(input);

            Assert.Equal("tknk", tower.GetRootDisc().Name);
        }
Ejemplo n.º 12
0
        public void TestWithRemovedComments()
        {
            string inputString = "Velit qui eu cillum anim /*labore. Mollit nulla " +
                                 "consequat fugiat ut - dolor nost/*rud veniam fugiat adipisicing " +
                                 "irure est cupi*/datat, minim. Incid*/idunt occaecat ipsum officia. " +
                                 "Consectetur labore volup/*tate voluptate inci*/didunt, eu occaecat.";

            var tokenCategories = new List <KeyValuePair <CommentsTestCategory, string> >
            {
                new KeyValuePair <CommentsTestCategory, string>(CommentsTestCategory.UppercaseWord, "[A-Z][a-z]*"),
                new KeyValuePair <CommentsTestCategory, string>(CommentsTestCategory.LowercaseWord, "[a-z][a-z]*"),
                new KeyValuePair <CommentsTestCategory, string>(CommentsTestCategory.Punctuation, ".|[,\\-]"),
                new KeyValuePair <CommentsTestCategory, string>(CommentsTestCategory.Space, "  *")
            };

            var expectedTokens = new List <Token <CommentsTestCategory> >
            {
                CreateToken(CommentsTestCategory.UppercaseWord, "Velit", new StringLocation(0), new StringLocation(5)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(5), new StringLocation(6)),
                CreateToken(CommentsTestCategory.LowercaseWord, "qui", new StringLocation(6), new StringLocation(9)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(9), new StringLocation(10)),
                CreateToken(CommentsTestCategory.LowercaseWord, "eu", new StringLocation(10), new StringLocation(12)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(12), new StringLocation(13)),
                CreateToken(CommentsTestCategory.LowercaseWord, "cillum", new StringLocation(13), new StringLocation(19)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(19), new StringLocation(20)),
                CreateToken(CommentsTestCategory.LowercaseWord, "anim", new StringLocation(20), new StringLocation(24)),
                CreateToken(CommentsTestCategory.Space, "  ", new StringLocation(24), new StringLocation(149)),
                CreateToken(CommentsTestCategory.LowercaseWord, "idunt", new StringLocation(149), new StringLocation(154)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(154), new StringLocation(155)),
                CreateToken(CommentsTestCategory.LowercaseWord, "occaecat", new StringLocation(155), new StringLocation(163)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(163), new StringLocation(164)),
                CreateToken(CommentsTestCategory.LowercaseWord, "ipsum", new StringLocation(164), new StringLocation(169)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(169), new StringLocation(170)),
                CreateToken(CommentsTestCategory.LowercaseWord, "officia", new StringLocation(170), new StringLocation(177)),
                CreateToken(CommentsTestCategory.Punctuation, ".", new StringLocation(177), new StringLocation(178)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(178), new StringLocation(179)),
                CreateToken(CommentsTestCategory.UppercaseWord, "Consectetur", new StringLocation(179), new StringLocation(190)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(190), new StringLocation(191)),
                CreateToken(CommentsTestCategory.LowercaseWord, "labore", new StringLocation(191), new StringLocation(197)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(197), new StringLocation(198)),
                CreateToken(CommentsTestCategory.LowercaseWord, "volup", new StringLocation(198), new StringLocation(203)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(203), new StringLocation(226)),
                CreateToken(CommentsTestCategory.LowercaseWord, "didunt", new StringLocation(226), new StringLocation(232)),
                CreateToken(CommentsTestCategory.Punctuation, ",", new StringLocation(232), new StringLocation(233)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(233), new StringLocation(234)),
                CreateToken(CommentsTestCategory.LowercaseWord, "eu", new StringLocation(234), new StringLocation(236)),
                CreateToken(CommentsTestCategory.Space, " ", new StringLocation(236), new StringLocation(237)),
                CreateToken(CommentsTestCategory.LowercaseWord, "occaecat", new StringLocation(237), new StringLocation(245)),
                CreateToken(CommentsTestCategory.Punctuation, ".", new StringLocation(245), new StringLocation(246)),
                CreateToken(CommentsTestCategory.Eof, null, null, null),
            };

            StringInputReader inputReader  = new StringInputReader(inputString);
            Preprocessor      preprocessor = new Preprocessor();
            var input        = preprocessor.PreprocessInput(inputReader.ReadGenerator(), null);
            var resolver     = new ConflictResolver <CommentsTestCategory>(CommentsTestCategory.None);
            var lexer        = new Lexer <CommentsTestCategory>(tokenCategories, CommentsTestCategory.Eof, CommentsTestCategory.None, resolver.ResolveWithMaxValue);
            var outputTokens = lexer.Scan(input, null);
            var expectedText = string.Join(",\n", expectedTokens);
            var actualText   = string.Join(",\n", outputTokens);

            Assert.IsTrue(outputTokens.SequenceEqual(expectedTokens, new TokenComparer <CommentsTestCategory>()), $"Expected:\n{expectedText}\nactual:\n{actualText}");
        }
Ejemplo n.º 13
0
        public static Artifacts RunOnString(this ICompiler compiler, string text, IDiagnostics diag)
        {
            var fileInputReader = new StringInputReader(text);

            return(compiler.RunOnInputReader(fileInputReader, diag));
        }