CsCheck/Check.cs
/// <summary>Generate an example that satisfies the predicate.</summary>
/// <param name="gen">The data generator.</param>
/// <param name="predicate">The predicate the data has to satisfy.</param>
/// <param name="seed">The initial seed to use to pin the example once found.</param>
Generate an example that satisfies the predicate.
Generate an example that satisfies the predicate .
public static T Example<T>(this Gen<T> gen, Func<T, bool> predicate, string seed = null)
        {
            if (seed is null)
            {
                var mre = new ManualResetEventSlim();
                T ret = default;
                string message = null;
                var threads = Environment.ProcessorCount;
                while (threads-- > 0)
                    ThreadPool.UnsafeQueueUserWorkItem(__ =>
                    {
                        var pcg = PCG.ThreadPCG;
                        while (true)
                        {
                            if (mre.IsSet) return;
                            var state = pcg.State;
                            var t = gen.Generate(pcg, null, out _);
                            if (predicate(t))
                            {
                                lock (mre)
                                {
                                    if (message is null)
                                    {
                                        message = "Example " + typeof(T).Name + " seed = \"" + pcg.ToString(state) + "\"";
                                        ret = t;
                                        mre.Set();
                                    }
                                }
                            }
                        }
                    }, null);
                mre.Wait();
                throw new CsCheckException(message);
            }
            else
            {
                var pcg = PCG.Parse(seed);
                var t = gen.Generate(pcg, null, out _);
                if (!predicate(t)) throw new CsCheckException("where clause no longer satisfied");
                return t;
            }
        }
public static T Example < T > ( this Gen < T > gen , Func < T , bool > predicate , string seed = null ) { if ( seed is null ) { var mre = new ManualResetEventSlim ( ) ; T ret = default ; string message = null ; var threads = Environment . ProcessorCount ; while ( threads -- > 0 ) ThreadPool . UnsafeQueueUserWorkItem ( __ => { var pcg = PCG . ThreadPCG ; while ( true ) { if ( mre . IsSet ) return ; var state = pcg . State ; var t = gen . Generate ( pcg , null , out _ ) ; if ( predicate ( t ) ) { lock ( mre ) { if ( message is null ) { message = " " + typeof ( T ) . Name + " \" " + pcg . ToString ( state ) + " \" " ; ret = t ; mre . Set ( ) ; } } } } } , null ) ; mre . Wait ( ) ; throw new CsCheckException ( message ) ; } else { var pcg = PCG . Parse ( seed ) ; var t = gen . Generate ( pcg , null , out _ ) ; if ( ! predicate ( t ) ) throw new CsCheckException ( " " ) ; return t ; } }
Generate an example that satisfies the predicate.
Generate an example that satisfies the predicate .
Gen<T>
this
predicate
Func<T, bool>
The predicate the data has to satisfy.
The predicate the data has to satisfy .
seed
string
The initial seed to use to pin the example once found.
The initial seed to use to pin the example once found .
gen
The data generator.
The data generator .