Lucene.Net.Util.Automaton.AutomatonTestUtil.RandomAcceptedStrings.GetRandomAcceptedString C# (CSharp) Method

GetRandomAcceptedString() public method

public GetRandomAcceptedString ( Random r ) : int[]
r System.Random
return int[]
            public int[] GetRandomAcceptedString(Random r)
            {
                IList<int?> soFar = new List<int?>();
                if (a.IsSingleton)
                {
                    // accepts only one
                    var s = a.Singleton;

                    int charUpto = 0;
                    while (charUpto < s.Length)
                    {
                        int cp = Character.CodePointAt(s, charUpto);
                        charUpto += Character.CharCount(cp);
                        soFar.Add(cp);
                    }
                }
                else
                {
                    var s = a.InitialState;

                    while (true)
                    {
                        if (s.Accept)
                        {
                            if (s.numTransitions == 0)
                            {
                                // stop now
                                break;
                            }
                            else
                            {
                                if (r.NextBoolean())
                                {
                                    break;
                                }
                            }
                        }

                        if (s.numTransitions == 0)
                        {
                            throw new Exception("this automaton has dead states");
                        }

                        bool cheat = r.NextBoolean();

                        Transition t;
                        if (cheat)
                        {
                            // pick a transition that we know is the fastest
                            // path to an accept state
                            IList<Transition> toAccept = new List<Transition>();
                            for (int i = 0; i < s.numTransitions; i++)
                            {
                                Transition t0 = s.TransitionsArray[i];
                                if (LeadsToAccept.ContainsKey(t0))
                                {
                                    toAccept.Add(t0);
                                }
                            }
                            if (toAccept.Count == 0)
                            {
                                // this is OK -- it means we jumped into a cycle
                                t = s.TransitionsArray[r.Next(s.numTransitions)];
                            }
                            else
                            {
                                t = toAccept[r.Next(toAccept.Count)];
                            }
                        }
                        else
                        {
                            t = s.TransitionsArray[r.Next(s.numTransitions)];
                        }
                        soFar.Add(GetRandomCodePoint(r, t));
                        s = t.Dest;
                    }
                }

                return ArrayUtil.ToIntArray(soFar);
            }
        }

Usage Example

示例#1
0
        private static void AssertAutomaton(Automaton automaton)
        {
            var cra = new CharacterRunAutomaton(automaton);
            var bra = new ByteRunAutomaton(automaton);
            var ras = new AutomatonTestUtil.RandomAcceptedStrings(automaton);

            int num = AtLeast(1000);

            for (int i = 0; i < num; i++)
            {
                string s;
                if (Random().NextBoolean())
                {
                    // likely not accepted
                    s = TestUtil.RandomUnicodeString(Random());
                }
                else
                {
                    // will be accepted
                    int[] codepoints = ras.GetRandomAcceptedString(Random());
                    try
                    {
                        s = UnicodeUtil.NewString(codepoints, 0, codepoints.Length);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(codepoints.Length + " codepoints:");
                        for (int j = 0; j < codepoints.Length; j++)
                        {
                            Console.WriteLine("  " + codepoints[j].ToString("x"));
                        }
                        throw e;
                    }
                }
                var bytes = s.GetBytes(Encoding.UTF8);
                Assert.AreEqual(cra.Run(s), bra.Run(bytes, 0, bytes.Length));
            }
        }
All Usage Examples Of Lucene.Net.Util.Automaton.AutomatonTestUtil.RandomAcceptedStrings::GetRandomAcceptedString
AutomatonTestUtil.RandomAcceptedStrings