public void Char_Int_AddConflict() { var target = new InsertOnlyTrie <char, int>(); target.Add("abc", 1); Action act = () => target.Add("abc", 2); act.Should().Throw <TrieInsertException>(); }
public void Char_Int_DuplicateAddGet() { var target = new InsertOnlyTrie <char, int>(); target.Add("abc", 1); target.Add("abc", 1); target.Add("abc", 1); target.Add("abc", 1); target.Get("abc").Value.Should().Be(1); }
public void Char_Int_DoesntExist() { var target = new InsertOnlyTrie <char, int>(); target.Add("abc", 1); target.Add("abd", 2); target.Add("aef", 3); target.Add("aeg", 4); target.Add("hij", 5); target.Get("XYZ").Success.Should().BeFalse(); }
public void Char_Int_AddGetBacktrack() { var target = new InsertOnlyTrie <char, int>(); target.Add("abc", 1); target.Add("abcde", 2); // looks for "abcd", has a node but no value. Then backtracks to "abc" and finds the value var input = new StringCharacterSequence("abcd"); target.Get(input).Value.Should().Be(1); }
public void Char_Int_GetAllPatterns_Duplicate() { var target = new InsertOnlyTrie <char, int>(); target.Add("abc", 1); target.Add("abc", 1); target.Add("abc", 1); var result = target.GetAllPatterns().ToList(); result.Count.Should().Be(1); result[0].Should().BeEquivalentTo(new[] { 'a', 'b', 'c' }); }
public void Char_Int_AddGetPrefixes() { var target = new InsertOnlyTrie <char, int>(); target.Add("a", 1); target.Add("ab", 2); target.Add("abc", 3); target.Add("abcd", 4); target.Get("a").Value.Should().Be(1); target.Get("ab").Value.Should().Be(2); target.Get("abc").Value.Should().Be(3); target.Get("abcd").Value.Should().Be(4); }
public void Char_Int_AddGet() { var target = new InsertOnlyTrie <char, int>(); target.Add("abc", 1); target.Add("abd", 2); target.Add("aef", 3); target.Add("aeg", 4); target.Add("hij", 5); target.Get("abc").Value.Should().Be(1); target.Get("abd").Value.Should().Be(2); target.Get("aef").Value.Should().Be(3); target.Get("aeg").Value.Should().Be(4); target.Get("hij").Value.Should().Be(5); }
public void Multi_Action_Continue() { var trie = new InsertOnlyTrie <char, string>(); trie.Add("=", "="); trie.Add("==", "=="); trie.Add("===", "==="); trie.Add(">=", ">="); trie.Add("<=", "<="); trie.Add("<", "<"); trie.Add(">", ">"); var multiTrieParser = TrieMulti(trie); var singleTrieParser = Trie(trie); var target = multiTrieParser.ContinueWith(left => Rule( left, singleTrieParser, (l, r) => $"{l} {r}" )); var input = new StringCharacterSequence("===>=<=><<=="); var result = target.Parse(input); result.Success.Should().BeTrue(); result.Results.Count.Should().Be(3); result.Results[0].Value.Should().Be("= =="); result.Results[1].Value.Should().Be("== ="); result.Results[2].Value.Should().Be("=== >="); }
/// <summary> /// Optimized implementation of First() which returns an input which matches any of the given pattern /// strings. Uses a Trie internally to greedily match the longest matching input sequence. /// </summary> /// <param name="patterns"></param> /// <returns></returns> public static IParser <char, string> MatchAny(IEnumerable <string> patterns) { var trie = new InsertOnlyTrie <char, string>(); foreach (var pattern in patterns) { trie.Add(pattern, pattern); } return(new TrieParser <char, string>(trie)); }
public void Char_Int_GetAllPatterns() { var target = new InsertOnlyTrie <char, int>(); target.Add("abc", 1); target.Add("abd", 2); target.Add("aef", 3); target.Add("aeg", 4); target.Add("hij", 5); var result = target.GetAllPatterns().ToList(); result.Count.Should().Be(5); result[0].Should().BeEquivalentTo(new[] { 'a', 'b', 'c' }); result[1].Should().BeEquivalentTo(new[] { 'a', 'b', 'd' }); result[2].Should().BeEquivalentTo(new[] { 'a', 'e', 'f' }); result[3].Should().BeEquivalentTo(new[] { 'a', 'e', 'g' }); result[4].Should().BeEquivalentTo(new[] { 'h', 'i', 'j' }); }
public void Char_Int_AddGetSequence() { var target = new InsertOnlyTrie <char, int>(); target.Add("abc", 1); target.Add("abd", 2); target.Add("aef", 3); target.Add("aeg", 4); target.Add("hij", 5); target.Get(new StringCharacterSequence("abc")).Value.Should().Be(1); target.Get(new StringCharacterSequence("abd")).Value.Should().Be(2); target.Get(new StringCharacterSequence("aef")).Value.Should().Be(3); target.Get(new StringCharacterSequence("aeg")).Value.Should().Be(4); target.Get(new StringCharacterSequence("hij")).Value.Should().Be(5); target.Get(new StringCharacterSequence("abX")).Success.Should().BeFalse(); target.Get(new StringCharacterSequence("aXc")).Success.Should().BeFalse(); target.Get(new StringCharacterSequence("Xbc")).Success.Should().BeFalse(); }
public void Parse_Operators_Fail() { var trie = new InsertOnlyTrie <char, string>(); trie.Add("=", "="); trie.Add("==", "=="); trie.Add(">=", ">="); trie.Add("<=", "<="); trie.Add("<", "<"); trie.Add(">", ">"); IParser <char, string> target = new TrieParser <char, string>(trie, ""); var input = new StringCharacterSequence("X===>=<=><<=="); target.Parse(input).Success.Should().BeFalse(); }
public void Parse_Operators_Method() { var trie = new InsertOnlyTrie <char, string>(); trie.Add("=", "="); trie.Add("==", "=="); trie.Add(">=", ">="); trie.Add("<=", "<="); trie.Add("<", "<"); trie.Add(">", ">"); var target = Trie(trie); var input = new StringCharacterSequence("===>=<=><<=="); target.Parse(input).Value.Should().Be("=="); target.Parse(input).Value.Should().Be("="); target.Parse(input).Value.Should().Be(">="); target.Parse(input).Value.Should().Be("<="); target.Parse(input).Value.Should().Be(">"); target.Parse(input).Value.Should().Be("<"); target.Parse(input).Value.Should().Be("<="); target.Parse(input).Value.Should().Be("="); }