protected override void AfterGenerateValues(Project project, Column column, int count, int nullCount) { if (_generator != null) { _generator.Dispose(); _generator = null; } _engine = null; }
public static void GenerateMembers_WithWhiteSpaceAndDigitsForAsciiEncoding_ExecutesRoundTrip() { const int seed = 123; const string regex = @"^\s{3}\d{4} -\s+\d{3}$"; var engine = new RexEngine(Encoding.ASCII, seed); var result = engine.GenerateMembers(regex, 1).Single(); var matches = Regex.IsMatch(result, regex); Assert.That(matches, Is.True, $"Generated result '{ result }' does not match regular expression '{ regex }'"); }
public static void GenerateMembers_WithDigitsForUnicodeEncoding_ExecutesRoundTrip() { const int seed = 123; const string regex = @"^\d\d\d - \d\d\d\d$"; var engine = new RexEngine(Encoding.Unicode, seed); var result = engine.GenerateMembers(regex, 1).Single(); var matches = Regex.IsMatch(result, regex); Assert.That(matches, Is.True, $"Generated result '{ result }' does not match regular expression '{ regex }'"); }
protected override void BeforeGenerateValues(Project project, Column column, int count, int nullCount) { CharacterEncoding encoding = Encoding; if (!IsUnicode(column)) { encoding = CharacterEncoding.ASCII; } _engine = new RexEngine(encoding, -1); _generator = _engine.GenerateMembers(RegexOptions, count, RegexPattern).GetEnumerator(); }
public static void GenerateMembers_WithWhiteSpaceAndDigitsForAsciiEncodingWithLargeSuite_ExecutesRoundTrip() { const int seed = 123; const string regex = @"^\s{3}\d{4} -\s+\d{3}$"; const int suiteSize = 100; var engine = new RexEngine(Encoding.ASCII, seed); var results = engine.GenerateMembers(regex, suiteSize); Assert.Multiple(() => { foreach (var result in results) { var matches = Regex.IsMatch(result, regex); Assert.That(matches, Is.True, $"Generated result '{ result }' does not match regular expression '{ regex }'"); } }); }
public void TestRegexCompile() { Regex[] regexes = Array.ConvertAll(File.ReadAllLines(regexesFile), x => new Regex(x)); for (int i = 0; i < regexes.Length && i < 50; i++) { var regex = regexes[i]; var rex = new RexEngine(BitWidth.BV16); var aut = rex.CreateFromRegexes(regex.ToString()); var comp = regex.Compile(100000); if (comp == null) { continue; } foreach (var s in rex.GenerateMembers(aut, 10)) { Assert.IsTrue(regex.IsMatch(s)); Assert.IsTrue(comp.IsMatch(s)); } } }
public void gen_chsarp_TestSampleRegexes2csharp() { var solver = new CharSetSolver(BitWidth.BV16); List <string> regexesAll = new List <string>(File.ReadAllLines(regexesFile)); List <int> timedout = new List <int>(); List <int> excluded = new List <int>(new int[] { 36, 50, 64, 65, 162, 166, 210, 238, 334, 355, 392, 455, 471, 490, 594, 611, 612, 671, 725, 731, 741, 760, 775, 800, 852, 870, 873, 880, 882, 893, 923, 991, 997, 1053, 1062, 1164, 1220, 1228, 1273, 1318, 1339, 1352, 1386, 1404, 1413, 1414, 1423, 1424, 1429, 1431, 1434, 1482, 1487, 1516, 1517, 1518, 1519, 1520, 1537, 1565, 1566, 1635, 1744, 1749, 1829, 1868 }); List <string> regexes = new List <string>(); for (int i = 1; i < regexesAll.Count; i++) { if (!excluded.Contains(i)) { regexes.Add(regexesAll[i]); } } int K = 10; //number of pos/neg strings to be generated for each regex for (int i = 1; i < 100; i++) { try { var regex = regexes[i]; var aut = solver.Convert(regex, RegexOptions.Singleline); var autDet = aut.Determinize(2000); var autMin = autDet.Minimize(); var autMinC = aut.Complement(); if (autMin.IsEmpty || autMinC.IsEmpty || autMinC.IsEpsilon) { continue; } CheckIsClean(autMin); //var autMinExpr = z3.ConvertAutomatonGuardsToExpr(autMin); //var sfa = new SFA<FuncDecl, Expr, Sort>(z3, z3.CharacterSort, autMinExpr); //var stbb = new STBuilder<FuncDecl, Expr, Sort>(z3); //var st = ST<FuncDecl, Expr, Sort>.SFAtoST(sfa); //var stb = st.ToSTb(); ////var csAcceptor = stb.Compile("RegexTransfomer", "SampleAcceptor", false, true); var csAcceptor = solver.ToCS(autMin); HashSet <string> posSamples = new HashSet <string>(); HashSet <string> negSamples = new HashSet <string>(); int k = autMin.FindShortestFinalPath(autMin.InitialState).Item1.Length; var maxLengthAut = solver.Convert("^.{0," + (3 * k) + "}$").Determinize().Minimize(); int tries = 0; var aut1 = autMin.Intersect(maxLengthAut); while (posSamples.Count < K && tries < 10 * K) { var s = solver.GenerateMemberUniformly(aut1); if (!s.EndsWith("\n")) { if (!posSamples.Add(s)) { tries++; } } } tries = 0; int k2 = autMinC.FindShortestFinalPath(autMinC.InitialState).Item1.Length; var maxLengthAut2 = solver.Convert("^.{0," + (3 * k2) + "}$").Determinize().Minimize(); var autMinCprefix = autMinC.Intersect(maxLengthAut2); while (negSamples.Count < K && tries < 10 * K) { var s = solver.GenerateMemberUniformly(autMinCprefix); if (!s.EndsWith("\n")) { if (!negSamples.Add(s)) { tries++; } } } foreach (string s in posSamples) { if (!RexEngine.IsMatch(s, regex, RegexOptions.Singleline)) { Console.WriteLine("match expected regex:" + i); break; } if (!csAcceptor.IsMatch(s)) { Console.WriteLine("match expected regex:" + i); break; } } foreach (string s in negSamples) { if (RexEngine.IsMatch(s, regex, RegexOptions.Singleline)) { Console.WriteLine("mismatch expected regex:" + i); break; } if (csAcceptor.IsMatch(s)) { Console.WriteLine("mismatch expected regex:" + i); break; } } } catch (TimeoutException) { Console.WriteLine("timeout regex:" + i); timedout.Add(i); continue; } } }