Exemplo n.º 1
0
        public void TestSymbolicRegex_Reverse()
        {
            CharSetSolver css = new CharSetSolver();
            //-----
            var R1       = new Regex(@"abc");
            var sr1      = css.RegexConverter.ConvertToSymbolicRegex(R1, true);
            var rev1     = sr1.Reverse();
            var matcher1 = new SymbolicRegex <BDD>(rev1, css, rev1.ComputeMinterms());

            Assert.IsTrue(matcher1.IsMatch("cba"));
            //-----
            var R2       = new Regex(@"^(foo|ab+d)+$");
            var sr2      = css.RegexConverter.ConvertToSymbolicRegex(R2, true);
            var rev2     = sr2.Reverse();
            var matcher2 = new SymbolicRegex <BDD>(rev2, css, rev2.ComputeMinterms());

            Assert.IsTrue(sr2.Equals(rev2.Reverse()));
            Assert.IsTrue(matcher2.IsMatch("oof"));
            Assert.IsTrue(matcher2.IsMatch("oofdbbaoofoofdbbadba"));
            var sampler = new SymbolicRegexSampler <BDD>(css.RegexConverter.srBuilder, rev2, 10);
            var samples = sampler.GetPositiveDataset(100);

            foreach (var sample in samples)
            {
                Assert.IsTrue(matcher2.IsMatch(sample));
            }
        }
Exemplo n.º 2
0
        public void TestSymbolicRegexBDD_IsMatch()
        {
            var css     = new CharSetSolver();
            var R       = new Regex(@"^abc[\0-\xFF]+$");
            var sr      = R.ConvertToSymbolicRegexBDD(css);
            var matcher = new SymbolicRegex <BDD>(sr, css, sr.ComputeMinterms());
            var str     = "abc" + CreateRandomString(1000);

            Assert.IsTrue(matcher.IsMatch(str));
            Assert.IsFalse(matcher.IsMatch(str + "\uFFFD\uFFFD\uFFFD"));
        }
Exemplo n.º 3
0
        public void TestDerivative_IsMatch2()
        {
            var                  regex   = @"^(abc|bbd|add|dde|ddd){1,2000}$";
            CharSetSolver        css     = new CharSetSolver();
            var                  sr      = css.RegexConverter.ConvertToSymbolicRegex(regex, RegexOptions.None, true);
            Func <string, BDD[]> F       = s => Array.ConvertAll <char, BDD>(s.ToCharArray(), c => css.MkCharConstraint(c));
            var                  matcher = new SymbolicRegex <BDD>(sr, css, sr.ComputeMinterms());

            Assert.IsTrue(matcher.IsMatch("addddd"));
            Assert.IsFalse(matcher.IsMatch("adddddd"));
        }
Exemplo n.º 4
0
        public void TestDerivative_IsMatch3()
        {
            var           R       = new Regex(@".*(ab|ba)+$", RegexOptions.Singleline);
            var           R1      = new Regex(@"(ab|ba)+", RegexOptions.Singleline);
            CharSetSolver css     = new CharSetSolver();
            var           sr      = css.RegexConverter.ConvertToSymbolicRegex(R, true);
            var           matcher = new SymbolicRegex <BDD>(sr, css, sr.ComputeMinterms());

            Assert.IsTrue(matcher.IsMatch("xxabbabbaba"));
            Assert.IsTrue(matcher.IsMatch("abba"));
            Assert.IsTrue(R1.IsMatch("baba"));
            Assert.IsFalse(R1.IsMatch("bb"));
            var matches = R1.Matches("xxabbabbaba");

            Assert.IsTrue(matches.Count == 2);
            Assert.IsTrue(matches[0].Index == 2);
            Assert.IsTrue(matches[0].Value == "abba");
            Assert.IsTrue(matches[1].Value == "baba");
            Assert.IsTrue(matches[1].Index == 7);
        }
        private static void ValidateMatches(Regex re, SymbolicRegex <BV> sr, string str, Tuple <int, int>[] sr_res, Tuple <int, int>[] re_res)
        {
            //--- correctness check of different matches ---
            //Assert.IsTrue(re_matches.Count == sr_matches.Count);
            var sr_matches_minus_re_matches = new HashSet <Tuple <int, int> >(sr_res);

            sr_matches_minus_re_matches.ExceptWith(re_res);
            var re_matches_minus_sr_matches = new HashSet <Tuple <int, int> >(re_res);

            re_matches_minus_sr_matches.ExceptWith(sr_res);
            foreach (var pair in sr_matches_minus_re_matches)
            {
                Assert.IsTrue(re.IsMatch(str.Substring(pair.Item1, pair.Item2)));
                Assert.IsTrue(sr.IsMatch(str.Substring(pair.Item1, pair.Item2)));
            }
            foreach (var pair in re_matches_minus_sr_matches)
            {
                Assert.IsTrue(re.IsMatch(str.Substring(pair.Item1, pair.Item2)));
                Assert.IsTrue(sr.IsMatch(str.Substring(pair.Item1, pair.Item2)));
            }
        }
Exemplo n.º 6
0
        public void TestDerivative_IsMatch4()
        {
            var           R       = new Regex(@"(ab|ba)+|ababbba", RegexOptions.Singleline);
            CharSetSolver css     = new CharSetSolver();
            var           sr      = css.RegexConverter.ConvertToSymbolicRegex(R, true);
            var           matcher = new SymbolicRegex <BDD>(sr, css, sr.ComputeMinterms());

            Assert.IsTrue(matcher.IsMatch("ababba"));
            var matches = R.Matches("xaababbba");

            Assert.IsTrue(matches.Count == 2);
            Assert.IsTrue(matches[0].Value == "abab");
            Assert.IsTrue(matches[1].Value == "ba");
            var R2 = new Regex(@"ababbba|(ab|ba)+", RegexOptions.Singleline);

            Assert.IsTrue(R2.Matches("ababba").Count == 1);
        }
Exemplo n.º 7
0
        public void TestDerivative_IsMatch1()
        {
            var                  regex   = @"^\w\d\w{1,8}$";
            CharSetSolver        css     = new CharSetSolver();
            var                  sr      = css.RegexConverter.ConvertToSymbolicRegex(regex, RegexOptions.None, true);
            Func <string, BDD[]> F       = s => Array.ConvertAll <char, BDD>(s.ToCharArray(), c => css.MkCharConstraint(c));
            var                  matcher = new SymbolicRegex <BDD>(sr, css, sr.ComputeMinterms());

            Assert.IsTrue(matcher.IsMatch("a0d"));
            Assert.IsFalse(matcher.IsMatch("a0"));
            Assert.IsTrue(matcher.IsMatch("a5def"));
            Assert.IsFalse(matcher.IsMatch("aa"));
            Assert.IsTrue(matcher.IsMatch("a3abcdefg"));
            Assert.IsTrue(matcher.IsMatch("a3abcdefgh"));
            Assert.IsFalse(matcher.IsMatch("a3abcdefghi"));
        }
Exemplo n.º 8
0
        public void TestDerivative_IsMatch5()
        {
            var           R   = new Regex(@"^(ab*a|bbba*)$", RegexOptions.Singleline);
            CharSetSolver css = new CharSetSolver();
            var           A   = css.Convert(R.ToString(), R.Options).Determinize().Minimize().Normalize();
            //A.ShowGraph("A");
            var R1 = new Regex(@"^.*(ab*a|bbba*)$", RegexOptions.Singleline);
            var A1 = css.Convert(R1.ToString(), R1.Options).Determinize().Minimize().Normalize();
            //A1.ShowGraph("A1");
            var sr       = css.RegexConverter.ConvertToSymbolicRegex(R, true);
            var sr1      = css.RegexConverter.ConvertToSymbolicRegex(R1, true);
            var matcher  = new SymbolicRegex <BDD>(sr, css, sr.ComputeMinterms());
            var matcher1 = new SymbolicRegex <BDD>(sr1, css, sr1.ComputeMinterms());

            Assert.IsTrue(matcher.IsMatch("aa"));
            Assert.IsTrue(matcher.IsMatch("abbbbbbbbbba"));
            Assert.IsTrue(matcher.IsMatch("bbb"));
            Assert.IsTrue(matcher.IsMatch("bbbaaaaaaaaa"));
            Assert.IsFalse(matcher.IsMatch("baba"));
            Assert.IsFalse(matcher.IsMatch("abab"));
            //--------------
            Assert.IsTrue(matcher1.IsMatch("xxxxaa"));
            Assert.IsTrue(matcher1.IsMatch("xxabbbbbbbbbba"));
            Assert.IsTrue(matcher1.IsMatch("xxbbb"));
            Assert.IsTrue(matcher1.IsMatch("xxxbbbaaaaaaaaa"));
            Assert.IsFalse(matcher1.IsMatch("babab"));
            Assert.IsFalse(matcher1.IsMatch("ababx"));
            //---
            var R2      = new Regex(@"bbba*|ab*a", RegexOptions.Singleline);
            var matches = R2.Matches("xxabbba");

            Assert.AreEqual <int>(1, matches.Count);
            Assert.AreEqual <int>(2, matches[0].Index);
            Assert.AreEqual <string>("abbba", matches[0].Value);
            var matches2 = R2.Matches("xxabbbbaa");

            Assert.AreEqual <int>(1, matches2.Count);
            Assert.AreEqual <int>(2, matches2[0].Index);
            Assert.AreEqual <string>("abbbba", matches2[0].Value);
            var matches3 = R2.Matches("xxabbbbbbbbbaa");

            Assert.AreEqual <int>(1, matches3.Count);
            var matches4 = R2.Matches("xxxbbbbbbbbbaa");

            Assert.AreEqual <int>(3, matches4.Count);
        }