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

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