CsCheck/Check.cs
/// <summary>Sample metamorphic (two path) operations on a random initial state checking that both paths are equal.
/// If not the failing initial state and sequence will be shrunk down to the shortest and simplest.</summary>
/// <param name="initial">The initial state generator.</param>
/// <param name="operations">A metamorphic operation generator that can act on the state.</param>
/// <param name="equal">A function to check if the actual and model are the same (default Check.ModelEqual).</param>
/// <param name="seed">The initial seed to use for the first iteration.</param>
/// <param name="iter">The number of iterations to run in the sample (default 100).</param>
/// <param name="time">The number of seconds to run the sample.</param>
/// <param name="threads">The number of threads to run the sample on (default number logical CPUs).</param>
/// <param name="print">A function to convert the state to a string for error reporting (default Check.Print).</param>
Sample metamorphic (two path) operations on a random initial state checking that both paths are equal.
If not the failing initial state and sequence will be shrunk down to the shortest and simplest.
Sample metamorphic ( two path ) operations on a random initial state checking that both paths are equal . If not the failing initial state and sequence will be shrunk down to the shortest and simplest .
public static void SampleMetamorphic<T>(this Gen<T> initial, GenMetamorphic<T> operations,
            Func<T, T, bool> equal = null, string seed = null, long iter = -1, int time = -1, int threads = -1,
            Func<T, string> print = null)
        {
            if (equal is null) equal = ModelEqual;
            if (seed is null) seed = Seed;
            if (iter == -1) iter = Iter;
            if (time == -1) time = Time;
            if (threads == -1) threads = Threads;
            if (print is null) print = Print;
            Gen.Create((PCG pcg, Size min, out Size size) =>
            {
                var stream = pcg.Stream;
                var seed = pcg.Seed;
                var i1 = initial.Generate(pcg, null, out size);
                var i2 = initial.Generate(new PCG(stream, seed), null, out size);
                return new MetamorphicData<T> { State1 = i1, State2 = i2, Stream = stream, Seed = seed };
            })
            .Select(operations)
            .Sample(d =>
            {
                try
                {
                    d.V1.Item2(d.V0.State1);
                    d.V1.Item3(d.V0.State2);
                    return equal(d.V0.State1, d.V0.State2);
                }
                catch (Exception e)
                {
                    d.V0.Exception = e;
                    return false;
                }
            }, seed, iter, time, threads,
            p =>
            {
                if (p.V0 == null) return "";
                var sb = new StringBuilder();
                var initialState = initial.Generate(new PCG(p.V0.Stream, p.V0.Seed), null, out _);
                sb.Append("\nInitial State: ").Append(print(initialState));
                sb.Append("\n   Operations: ").Append(p.V1.Item1);
                if (p.V0.Exception is null)
                {
                    sb.Append("\nFinal State 1: ").Append(print(p.V0.State1));
                    sb.Append("\nFinal State 2: ").Append(print(p.V0.State2));
                }
                else
                {
                    sb.Append("\n    Exception: ").Append(p.V0.Exception.ToString());
                }
                return sb.ToString();
            });
        }
public static void SampleMetamorphic < T > ( this Gen < T > initial , GenMetamorphic < T > operations , Func < T , T , bool > equal = null , string seed = null , long iter = - 1 , int time = - 1 , int threads = - 1 , Func < T , string > print = null ) { if ( equal is null ) equal = ModelEqual ; if ( seed is null ) seed = Seed ; if ( iter == - 1 ) iter = Iter ; if ( time == - 1 ) time = Time ; if ( threads == - 1 ) threads = Threads ; if ( print is null ) print = Print ; Gen . Create ( ( PCG pcg , Size min , out Size size ) => { var stream = pcg . Stream ; var seed = pcg . Seed ; var i1 = initial . Generate ( pcg , null , out size ) ; var i2 = initial . Generate ( new PCG ( stream , seed ) , null , out size ) ; return new MetamorphicData < T > { State1 = i1 , State2 = i2 , Stream = stream , Seed = seed } ; } ) . Select ( operations ) . Sample ( d => { try { d . V1 . Item2 ( d . V0 . State1 ) ; d . V1 . Item3 ( d . V0 . State2 ) ; return equal ( d . V0 . State1 , d . V0 . State2 ) ; } catch ( Exception e ) { d . V0 . Exception = e ; return false ; } } , seed , iter , time , threads , p => { if ( p . V0 == null ) return " " ; var sb = new StringBuilder ( ) ; var initialState = initial . Generate ( new PCG ( p . V0 . Stream , p . V0 . Seed ) , null , out _ ) ; sb . Append ( " \n " ) . Append ( print ( initialState ) ) ; sb . Append ( " \n " ) . Append ( p . V1 . Item1 ) ; if ( p . V0 . Exception is null ) { sb . Append ( " \n " ) . Append ( print ( p . V0 . State1 ) ) ; sb . Append ( " \n " ) . Append ( print ( p . V0 . State2 ) ) ; } else { sb . Append ( " \n " ) . Append ( p . V0 . Exception . ToString ( ) ) ; } return sb . ToString ( ) ; } ) ; }
Sample metamorphic (two path) operations on a random initial state checking that both paths are equal.
Sample metamorphic ( two path ) operations on a random initial state checking that both paths are equal .
Gen<T>
this
operations
GenMetamorphic<T>
A metamorphic operation generator that can act on the state.
A metamorphic operation generator that can act on the state .
equal
Func<T, T, bool>
A function to check if the actual and model are the same (default Check.ModelEqual).
A function to check if the actual and model are the same ( default Check . ModelEqual ) .
seed
string
The initial seed to use for the first iteration.
The initial seed to use for the first iteration .
iter
long
The number of iterations to run in the sample (default 100).
The number of iterations to run in the sample ( default 100 ) .
time
int
The number of seconds to run the sample.
The number of seconds to run the sample .
threads
int
The number of threads to run the sample on (default number logical CPUs).
The number of threads to run the sample on ( default number logical CPUs ) .
print
Func<T, string>
A function to convert the state to a string for error reporting (default Check.Print).
A function to convert the state to a string for error reporting ( default Check . Print ) .
initial
The initial state generator.
The initial state generator .