CanonSharp/CanonSharp.Tests/LexicalAnalyzerTests/RegularExpressionTests.cs
jackfiled 3c0d51cec5 feat: 正则词法识别器 (#1)
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/1
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
2024-07-29 16:59:29 +08:00

105 lines
3.4 KiB
C#

using CanonSharp.Common.LexicalAnalyzer;
namespace CanonSharp.Tests.LexicalAnalyzerTests;
public class RegularExpressionTests
{
[Fact]
public void KleeneTest()
{
RegularExpression expression = RegularExpression.Concatenate(
RegularExpression.Kleene(RegularExpression.Single('a')),
RegularExpression.Single('b'));
NondeterministicFiniteAutomation automation = expression.Convert2Nfa();
automation.Start.Transactions.TryGetValue(EmptyChar.Empty, out HashSet<NondeterministicState>? next);
Assert.NotNull(next);
Assert.Contains(next, s => s.Transactions.ContainsKey(new EmptyChar('a')));
Assert.Contains(next, s => s.Transactions.ContainsKey(new EmptyChar('b')));
}
[Fact]
public void AlternateTest()
{
RegularExpression expression = RegularExpression.Alternate(
RegularExpression.Kleene(RegularExpression.Single('a')),
RegularExpression.Single('b'));
NondeterministicFiniteAutomation automation = expression.Convert2Nfa();
automation.Start.Transactions.TryGetValue(EmptyChar.Empty, out HashSet<NondeterministicState>? next);
Assert.NotNull(next);
Assert.Contains(next, s => s.Transactions.ContainsKey(new EmptyChar('b')));
NondeterministicState? state = (from item in next
where item.Transactions[EmptyChar.Empty].Count == 2
select item).FirstOrDefault();
Assert.NotNull(state);
Assert.Contains(state.Transactions[EmptyChar.Empty],
s => s.Transactions.ContainsKey(new EmptyChar('a')));
}
[Fact]
public void RangeTest()
{
RegularExpression expression = RegularExpression.Range('a', 'z');
NondeterministicFiniteAutomation nfa = expression.Convert2Nfa();
Assert.Equal(26, nfa.Start.Transactions.Count);
}
[Fact]
public void ConvertTest()
{
RegularExpression expression = RegularExpression.Alternate(
RegularExpression.Kleene(RegularExpression.Single('a')),
RegularExpression.Single('b'));
NondeterministicFiniteAutomation automation = expression.Convert2Nfa();
DeterministicFiniteAutomation dfa = DeterministicFiniteAutomation.Create(automation);
DeterministicState state2 = dfa.Start.Transaction['a'];
Assert.Equal(state2, state2.Transaction['a']);
DeterministicState state3 = dfa.Start.Transaction['b'];
Assert.Empty(state3.Transaction);
Assert.Equal(3, dfa.FinalStates.Count);
}
[Fact]
public void NondeterministicStateSetTest()
{
Dictionary<NondeterministicStateSet, char> map = [];
NondeterministicState key1 = new();
NondeterministicState key2 = new();
map.Add(new NondeterministicStateSet([key1, key2]), 'a');
Assert.Equal('a', map[new NondeterministicStateSet([key2, key1])]);
}
[Fact]
public void PrefixConvertTest()
{
RegularExpression expression = RegularExpression.Alternate(
RegularExpression.String("string"),
RegularExpression.String("string1"));
NondeterministicFiniteAutomation nfa = expression.Convert2Nfa();
DeterministicFiniteAutomation.Create(nfa);
}
[Fact]
public void WhiteSpaceConvertTest()
{
NondeterministicFiniteAutomation nfa = LexicalToken.WhiteSpace.Expression.Convert2Nfa();
DeterministicFiniteAutomation.Create(nfa);
}
}