Esempio n. 1
0
        public object?Simplify(IFuzzProfile profile, object?input, Func <object?, bool> isValid)
        {
            if (input is null)
            {
                return(null);
            }

            var simplified = CopyDataObject(input);

            // Assume each property is independent, and simplify separately
            foreach (var property in input.GetType().GetDataProperties())
            {
                var initialValue = property.GetValue(input);
                if (initialValue == null)
                {
                    continue;
                }

                // Use the dynamic type
                var simplifier         = profile.SimplifierFor(initialValue.GetType());
                var simplifiedProperty = simplifier.Simplify(profile, initialValue, candidate =>
                {
                    // Note that mutating `simplified` is okay, because it is completely hidden from the recursive call
                    property.SetValue(simplified, candidate);
                    return(isValid(simplified));
                });

                property.SetValue(simplified, simplifiedProperty);
            }

            return(simplified);
        }
Esempio n. 2
0
        public override T SimplifyInstance(IFuzzProfile profile, T input, Func <object?, bool> isValid)
        {
            if (isValid(DomainMinimum))
            {
                return(DomainMinimum);
            }

            // predicate(high) is always true
            var high = input;

            // predicate(low) is always false
            var low = DomainMinimum;

            while (true)
            {
                var midpoint = Midpoint(low, high);

                if (midpoint.Equals(low))
                {
                    // Convergence has been achieved
                    return(high);
                }
                else if (isValid(midpoint))
                {
                    high = midpoint;
                }
                else
                {
                    low = midpoint;
                }
            }
        }
Esempio n. 3
0
        public override object?Generate(IFuzzProfile profile, Type type, FuzzRandom random)
        {
            var constructor = random.Choice(type.GetConstructors());

            var arguments = constructor.GetParameters()
                            .Select(p => profile.Generate(p.ParameterType, random));

            return(constructor.Invoke(arguments.ToArray()));
        }
Esempio n. 4
0
        public override object?Generate(IFuzzProfile profile, Type type, FuzzRandom random)
        {
            // Only find direct subclasses. Indirect subclasses can be found recursively
            var candidateClasses = DirectSubclassesOf(type)
                                   .ToList();

            var generatedType = random.Choice(candidateClasses);

            return(profile.Generate(generatedType, random));
        }
Esempio n. 5
0
        public static IGenerator GeneratorForOrThrow(this IFuzzProfile profile, Type type)
        {
            var generator = profile.GeneratorFor(type);

            if (generator == null)
            {
                throw new NotImplementedException($"{profile.GetType()} does not gave a generator which can generate a {type.FullName}");
            }

            return(generator);
        }
Esempio n. 6
0
        /// <inheritdoc/>
        public object?Simplify(IFuzzProfile profile, object?input, Func <object?, bool> isValid)
        {
            // If the input is null, always consider it simplified
            if (input == null)
            {
                return(null);
            }

            if (!(input is T inputT))
            {
                throw new ArgumentException();
            }

            return(SimplifyInstance(profile, inputT, isValid));
        }
Esempio n. 7
0
        public override object?Generate(IFuzzProfile profile, Type type, FuzzRandom random)
        {
            var constructor = type.GetConstructor(Array.Empty <Type>()) !;
            var instance    = constructor.Invoke(Array.Empty <object?>());

            foreach (var property in type.GetDataProperties())
            {
                if (!property.CanWrite)
                {
                    continue;
                }

                var value = profile.Generate(property.PropertyType, random);
                property.SetValue(instance, value);
            }

            return(instance);
        }
Esempio n. 8
0
        public override T SimplifyInstance(IFuzzProfile profile, T input, Func <object?, bool> isValid)
        {
            var             simplest   = input;
            IEnumerable <T> candidates = new[] { input };

            while (candidates.Any())
            {
                var next = candidates.First();
                if (isValid(next))
                {
                    simplest   = next;
                    candidates = SimplificationCandidates(simplest);
                }
                else
                {
                    candidates = candidates.Skip(1);
                }
            }

            return(simplest);
        }
Esempio n. 9
0
 public override bool CanGenerate(IFuzzProfile profile, Type type) => type == typeof(int);
Esempio n. 10
0
        public static object?Generate(this IFuzzProfile profile, Type type, FuzzRandom random)
        {
            var generator = profile.GeneratorForOrThrow(type);

            return(generator.Generate(profile, type, random));
        }
Esempio n. 11
0
 public override object?Generate(IFuzzProfile profile, Type type, FuzzRandom random)
 {
     return(random.Choice(_items));
 }
Esempio n. 12
0
 public override object?Generate(IFuzzProfile profile, Type type, FuzzRandom random)
 {
     return(_value);
 }
Esempio n. 13
0
 public override bool CanGenerate(IFuzzProfile profile, Type type)
 {
     return(_value == null || type.IsAssignableFrom(_value.GetType()));
 }
Esempio n. 14
0
 public override bool CanGenerate(IFuzzProfile profile, Type type)
 {
     return(type.IsAbstract && DirectSubclassesOf(type).All(t => profile.GeneratorFor(t) != null));
 }
Esempio n. 15
0
 public abstract object?Generate(IFuzzProfile profile, Type type, FuzzRandom random);
Esempio n. 16
0
 public bool CanGenerate(IFuzzProfile profile, Type type)
 {
     return(true);
 }
Esempio n. 17
0
 public override bool CanGenerate(IFuzzProfile profile, Type type)
 {
     return(ItemType.IsAssignableFrom(type));
 }
Esempio n. 18
0
 public abstract T SimplifyInstance(IFuzzProfile profile, T input, Func <object?, bool> isValid);
Esempio n. 19
0
 public override bool CanGenerate(IFuzzProfile profile, Type type)
 {
     return(type.GetConstructors().Length > 0 &&
            !type.ContainsGenericParameters);
 }
Esempio n. 20
0
 public override bool CanGenerate(IFuzzProfile profile, Type type)
 {
     return(type.IsDataObject() &&
            type.GetDataProperties().All(p => profile.GeneratorFor(p.PropertyType) != null));
 }
Esempio n. 21
0
 public override bool CanGenerate(IFuzzProfile profile, Type type)
 {
     return(type.IsEnum);
 }
Esempio n. 22
0
        public override object?Generate(IFuzzProfile profile, Type type, FuzzRandom random)
        {
            Check.IsTrue(type.IsEnum);

            return(random.Choice(type.GetEnumValues()));
        }
Esempio n. 23
0
 public bool CanSimplify(IFuzzProfile profile, Type type)
 {
     return(typeof(T).IsAssignableFrom(type));
 }
Esempio n. 24
0
 public override object?Generate(IFuzzProfile profile, Type type, FuzzRandom random)
 {
     return(random.Poisson(Mean));
 }
Esempio n. 25
0
 public override object?Generate(IFuzzProfile profile, Type type, FuzzRandom random)
 {
     return(random.Uniform(Min, Max));
 }
Esempio n. 26
0
 public object?Simplify(IFuzzProfile profile, object?input, Func <object?, bool> isValid) => input;
Esempio n. 27
0
 public object?Generate(IFuzzProfile profile, Type type, FuzzRandom random)
 {
     return(null);
 }
Esempio n. 28
0
 public bool CanSimplify(IFuzzProfile profile, Type type) => true;
Esempio n. 29
0
 public abstract bool CanGenerate(IFuzzProfile profile, Type type);
Esempio n. 30
0
 public bool CanSimplify(IFuzzProfile profile, Type type)
 {
     return(type.IsDataObject());
 }