Exemplo n.º 1
0
        public void ToNFA_WithStar_ReturnsExpectedTransitions()
        {
            // Arrange
            var id      = 0;
            var regex   = new Regex("a*");
            var builder = new RegexNFABuilder();

            // Assert
            var nfa = builder.NFAFromRegex(regex);

            // Assert
            Assert.That(nfa.States.Count(), Is.EqualTo(4));
            Assert.That(nfa.Transitions.Count(), Is.EqualTo(5));

            var aTransition = nfa.ForInput('a').Single();

            Assert.That(nfa.Between(nfa.Initial, aTransition.Source).Single(), Is.EqualTo(nfa.Epsilon));
            Assert.That(nfa.Between(nfa.Initial, nfa.Terminal).Single(), Is.EqualTo(nfa.Epsilon));
            Assert.That(nfa.Between(aTransition.Target, aTransition.Source).Single(), Is.EqualTo(nfa.Epsilon));
            Assert.That(nfa.Between(aTransition.Target, nfa.Terminal).Single(), Is.EqualTo(nfa.Epsilon));

            // This would be a cool syntax
            //Assert.That(nfa.FromInitial().ToTerminal(), Is.EqualTo(nfa.Epsilon));
            //Assert.That(nfa.FromInitial().ViaEpsilon().Contains(aTransition.Initial));
        }
Exemplo n.º 2
0
        public void ToNFA_WithAlt_ReturnsExpectedTransitions()
        {
            // Arrange
            var id      = 0;
            var regex   = new Regex("a|b");
            var builder = new RegexNFABuilder();

            // Assert
            var nfa = builder.NFAFromRegex(regex);

            // Assert
            Assert.That(nfa.States.Count(), Is.EqualTo(6));
            Assert.That(nfa.Transitions.Count(), Is.EqualTo(6));

            var aTransition = nfa.ForInput('a').Single();

            Assert.That(nfa.SourceOf(out var initialFromA, nfa.Epsilon, aTransition.Source), Is.True);
            Assert.That(initialFromA, Is.EqualTo(nfa.Initial));
            Assert.That(nfa.TargetOf(aTransition.Target, nfa.Epsilon, out var terminalFromA), Is.True);
            Assert.That(terminalFromA, Is.EqualTo(nfa.Terminal));

            var bTransition = nfa.ForInput('b').Single();

            Assert.That(nfa.SourceOf(out var initialFromB, nfa.Epsilon, bTransition.Source), Is.True);
            Assert.That(initialFromB, Is.EqualTo(nfa.Initial));
            Assert.That(nfa.TargetOf(bTransition.Target, nfa.Epsilon, out var terminalFromB), Is.True);
            Assert.That(terminalFromB, Is.EqualTo(nfa.Terminal));
        }
Exemplo n.º 3
0
        private bool Matches(string pattern, string input)
        {
            var id         = 0;
            var nfaBuilder = new RegexNFABuilder();
            var dfaBuilder = new RegexDFABuilder();

            var regex = new Regex(pattern);
            var nfa   = nfaBuilder.NFAFromRegex(regex);
            var dfa   = dfaBuilder.DFAFromNFA(nfa);

            return(dfa.Matches(input));
        }
Exemplo n.º 4
0
        public void ToNFA_WithPlus_ReturnsExpectedTransitions()
        {
            // Arrange
            var id      = 0;
            var regex   = new Regex("a+");
            var builder = new RegexNFABuilder();

            // Assert
            var nfa = builder.NFAFromRegex(regex);

            // Assert
            Assert.That(nfa.States.Count(), Is.EqualTo(6));
            Assert.That(nfa.Transitions.Count(), Is.EqualTo(7));
        }
Exemplo n.º 5
0
        public void ToNFA_WithConcat_ReturnsExpectedTransitions()
        {
            // Arrange
            var id      = 0;
            var regex   = new Regex("ab");
            var builder = new RegexNFABuilder();

            // Assert
            var nfa = builder.NFAFromRegex(regex);

            // Assert
            Assert.That(nfa.States.Count(), Is.EqualTo(4));
            Assert.That(nfa.Transitions.Count(), Is.EqualTo(3));

            Assert.True(nfa.TargetOf(nfa.Initial, 'a', out var aTerminal));
            Assert.True(nfa.TargetOf(aTerminal, nfa.Epsilon, out var bInitial));
            Assert.True(nfa.TargetOf(bInitial, 'b', out var bTerminal));
            Assert.That(bTerminal, Is.EqualTo(nfa.Terminal));
        }
Exemplo n.º 6
0
        public void ToNFA_WithQuestion_ReturnsExpectedTransitions()
        {
            // Arrange
            var id      = 0;
            var regex   = new Regex("a?");
            var builder = new RegexNFABuilder();

            // Assert
            var nfa = builder.NFAFromRegex(regex);

            // Assert
            Assert.That(nfa.States.Count(), Is.EqualTo(6));
            Assert.That(nfa.Transitions.Count(), Is.EqualTo(6));

            Assert.That(nfa.ForInput('a').Count(), Is.EqualTo(1));
            Assert.That(nfa.ForInput(nfa.Epsilon).Where(t => !t.Source.Equals(nfa.Initial) && !t.Target.Equals(nfa.Terminal)).Count(), Is.EqualTo(1));

            Assert.That(nfa.FromSource(nfa.Initial).Where(t => t.Input.Equals(nfa.Epsilon)).Count(), Is.EqualTo(2));
            Assert.That(nfa.ToTarget(nfa.Terminal).Where(t => t.Input.Equals(nfa.Epsilon)).Count(), Is.EqualTo(2));
        }