本文整理汇总了C#中CharSetSolver.GenerateMemberUniformly方法的典型用法代码示例。如果您正苦于以下问题:C# CharSetSolver.GenerateMemberUniformly方法的具体用法?C# CharSetSolver.GenerateMemberUniformly怎么用?C# CharSetSolver.GenerateMemberUniformly使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CharSetSolver
的用法示例。
在下文中一共展示了CharSetSolver.GenerateMemberUniformly方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: gen_chsarp_TestSampleRegexes2csharp
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 = 50; //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)
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(autMin.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;
}
}
//.........这里部分代码省略.........
示例2: TestCppCodeGen
//private static void TestIgnoreCase()
//{
// Microsoft.Automata.Internal.Utilities.IgnoreCaseRelationGenerator.Generate(
// "Microsoft.Automata.Internal.Generated",
// "IgnoreCaseRelation",
// @"C:\GitHub\AutomataDotNet\Automata\src\Automata\Internal\Generated");
//}
static void TestCppCodeGen(Regex[] regexes)
{
Automaton<BDD>[] automata = new Automaton<BDD>[regexes.Length];
Automaton<BDD>[] Cautomata = new Automaton<BDD>[regexes.Length];
var solver = new CharSetSolver();
#region convert the regexes to automata
Console.Write("Converting {0} regexes to automata and minimizing the automata ...", regexes.Length);
int t = System.Environment.TickCount;
Func<Automaton<BDD>, bool> IsFull = (a => a.StateCount == 1 && a.IsFinalState(a.InitialState) && a.IsLoopState(a.InitialState) && a.GetMovesCountFrom(a.InitialState) == 1 && a.GetMoveFrom(a.InitialState).Label.Equals(solver.True));
for (int i = 0; i < regexes.Length; i++)
{
try
{
var aut = CppCodeGenerator.Regex2Automaton(solver, regexes[i]);
automata[i] = aut;
if (IsFull(automata[i]) || automata[i].IsEmpty)
{
Console.WriteLine("\nReplacing trivial regex \"{0}\" with \"^dummy$\"", i, regexes[i]);
regexes[i] = new Regex("^dummy$");
automata[i] = CppCodeGenerator.Regex2Automaton(solver, regexes[i]);
}
}
catch (Exception e)
{
Console.WriteLine("\nCoverting regex {0}: '{1}' failed, reason: {2}, replacing with \"^dummy$\"", i, regexes[i], e.Message);
regexes[i] = new Regex("^dummy$");
automata[i] = CppCodeGenerator.Regex2Automaton(solver, regexes[i]);
}
}
t = System.Environment.TickCount - t;
Console.WriteLine(string.Format(" done ({0}ms)", t));
#endregion
#region complement the automata
t = System.Environment.TickCount;
Console.Write("Creating complements of autmata ...");
for (int i = 0; i < regexes.Length; i++)
{
Cautomata[i] = automata[i].Complement().Minimize();
}
t = System.Environment.TickCount - t;
Console.WriteLine(string.Format(" done ({0}ms)", t));
#endregion
#region generate positive test strings
Console.Write(string.Format("Generating a positive test set for all automata ", NrOfStrings));
t = System.Environment.TickCount;
List<string[]> members = new List<string[]>();
List<string[]> Cmembers = new List<string[]>();
for (int id = 0; id < automata.Length; id++)
{
Console.Write(".");
var M = automata[id].Intersect(solver.Convert("^[\0-\x7F]{0," + CodeGenTests.MaxStringLength + "}$", RegexOptions.Singleline)).Determinize();
var tmp = new string[NrOfStrings];
int time = System.Environment.TickCount;
for (int i = 0; i < NrOfStrings; i++)
{
tmp[i] = solver.GenerateMemberUniformly(M);
//if (i % 10 == 0)
// Console.Write(".");
}
time = System.Environment.TickCount - time;
members.Add(tmp);
}
t = System.Environment.TickCount - t;
Console.WriteLine(string.Format(" done ({0}ms)", t));
#endregion
#region generate negative test strings
t = System.Environment.TickCount;
Console.Write(string.Format("Generating a negative test set for all automata ", NrOfStrings));
for (int id = 0; id < Cautomata.Length; id++)
{
Console.Write(".");
//var M = Cautomata[id].Intersect(solver.Convert("^[^\uD800-\uDFFF]{0,100}$", RegexOptions.Singleline), solver).Determinize(solver);
var M = Cautomata[id].Intersect(solver.Convert("^[\0-\uFFFF]{0,100}$", RegexOptions.Singleline)).Determinize();
var tmp = new string[NrOfStrings];
for (int i = 0; i < NrOfStrings; i++)
{
tmp[i] = solver.GenerateMemberUniformly(M);
//if (i % 10 == 0)
// Console.Write(".");
}
Cmembers.Add(tmp);
}
t = System.Environment.TickCount - t;
Console.WriteLine(string.Format(" done ({0}ms)", t));
#endregion
#region generate c++
int t2 = System.Environment.TickCount;
CppTest.Compile(automata, solver, true);
//.........这里部分代码省略.........