internal static void Register()
        {
            Arbitrary.Register(new Arbitrary <char> (
                                   Gen.Elements(CharCandidates().ToArray()),
                                   ShrinkChar));

            Arbitrary.Register(new Arbitrary <int> (
                                   Gen.ChooseInt(),
                                   x => ShrinkInteger(x).Distinct()));

            Arbitrary.Register(new Arbitrary <long> (
                                   Gen.ChooseInt().ToLong(),
                                   x => ShrinkInteger((int)x).Distinct().Select(i => (long)i)));

            Arbitrary.Register(new Arbitrary <float> (
                                   Gen.ChooseDouble().ToFloat(),
                                   x => ShrinkDouble(x).Select(d => (float)d)));

            Arbitrary.Register(new Arbitrary <double> (
                                   Gen.ChooseDouble(),
                                   ShrinkDouble));

            Arbitrary.Register(new Arbitrary <string> (
                                   from a in Arbitrary.Gen <char> ().ArrayOf()
                                   select new string (a),
                                   x => ShrinkEnumerable(x).Select(cs => new string (cs.ToArray()))));

            Arbitrary.Register(typeof(Enumerable <>));
            Arbitrary.Register(typeof(Array <>));
            Arbitrary.Register(typeof(AList <>));
        }
        /*
        ## Registering Default Arbitrary Types
        ## All of the default arbitrary types are registered automatically in the
        ## static constructor. Most of these types rely on the helper methods,
        ## which are introduced below. We will discuss the details of each type
        ## in context of the helper methods.
        */
        internal static void Register()
        {
            /*
             ### Character Type
             ###Characters are randomly selected from a predefined set. This
             ###produces simpler and more readable characters than randomly
             ###generating ASCII codes.
             */
            Arbitrary.Register(new Arbitrary <char> (
                                   Gen.ChooseFrom(CharCandidates().ToArray()),
                                   ShrinkChar));

            /*
             ### Integral Types
             ###Integral types `int` and `long` use the same helpers to generate
             ###and shrink the values. The maximum number generated depends on the
             ###size parameter used by the generators.
             */
            Arbitrary.Register(new Arbitrary <int> (
                                   Gen.ChooseInt(),
                                   x => ShrinkInteger(x).Distinct()));

            Arbitrary.Register(new Arbitrary <long> (
                                   Gen.ChooseInt().ToLong(),
                                   x => ShrinkInteger((int)x).Distinct().Select(i => (long)i)));

            /*
             ### Floating Point Types
             ###`float` and `double` types are generated and shrunk with the same
             ###methods. The results are casted to lower precision when necessary.
             */
            Arbitrary.Register(new Arbitrary <float> (
                                   Gen.ChooseDouble().ToFloat(),
                                   x => ShrinkDouble(x).Select(d => (float)d)));

            Arbitrary.Register(new Arbitrary <double> (
                                   Gen.ChooseDouble(),
                                   ShrinkDouble));

            /*
             ### Strings
             ###Strings are arrays of characters, so they can be composed from
             ###arbitrary characters with appropriate combinators.
             */
            Arbitrary.Register(new Arbitrary <string> (
                                   from a in Arbitrary.Gen <char> ().ArrayOf()
                                   select new string (a),
                                   x => ShrinkEnumerable(x).Select(cs => new string (cs.ToArray()))));

            /*
             ### Collection Types
             ###The arbitrary implementations for collections are generic.
             ###Therefore, they work with any item type. Their implementation is
             ###discussed below.
             */
            Arbitrary.Register(typeof(Enumerable <>));
            Arbitrary.Register(typeof(Array <>));
            Arbitrary.Register(typeof(AList <>));
        }
        private static IEnumerable <IEnumerable <T> > ShrinkOne <T> (IEnumerable <T> e)
        {
            if (e.None())
            {
                return(new IEnumerable <T> [0]);
            }
            var first = e.First();
            var rest  = e.Skip(1);

            return((from x in Arbitrary.Get <T> ().Shrink(first)
                    select rest.Append(x)).Concat(
                       from xs in ShrinkOne(e.Skip(1))
                       select xs.Append(first)));
        }
Exemple #4
0
 public static Property <T> Choose <T> ()
 {
     return(ForAll(Arbitrary.Get <T> ()));
 }
Exemple #5
0
 /*
  * The other version of the ForAll method can be called without specifying
  * the IArbitrary instance. The method uses the default arbitrary
  * implementation that is registered for the given type.
  */
 public static Prop <T> ForAll <T> ()
 {
     return(ForAll(Arbitrary.Get <T> ()));
 }