public static IExampleSpace <TResult> MergeInternal <T, TResult>(
                List <IExampleSpace <T> > exampleSpaces,
                Func <List <T>, TResult> mergeValues,
                ShrinkFunc <List <IExampleSpace <T> > > shrinkExampleSpaces,
                MeasureFunc <List <IExampleSpace <T> > > measureMerge,
                ImmutableHashSet <ExampleId> encounteredIds,
                bool enableSmallestExampleSpacesOptimization,
                bool isRoot)
            {
                IExampleSpace <TResult> GenerateNextExampleSpace(IEnumerable <IExampleSpace <T> > exampleSpaces, ImmutableHashSet <ExampleId> encounteredIds)
                {
                    return(MergeInternal(
                               exampleSpaces.ToList(),
                               mergeValues,
                               shrinkExampleSpaces,
                               measureMerge,
                               encounteredIds,
                               enableSmallestExampleSpacesOptimization,
                               false));
                }

                var mergedId = exampleSpaces.Aggregate(
                    ExampleId.Primitive(exampleSpaces.Count()),
                    (acc, curr) => ExampleId.Combine(acc, curr.Current.Id));

                var mergedValue = mergeValues(exampleSpaces.Select(es => es.Current.Value).ToList());

                var mergedDistance = measureMerge(exampleSpaces);

                var current = new Example <TResult>(
                    mergedId,
                    mergedValue,
                    mergedDistance);

                var smallestExampleSpacesShrink = enableSmallestExampleSpacesOptimization && isRoot && exampleSpaces.Any(exampleSpace => exampleSpace.Subspace.Any())
                    ? exampleSpaces
                                                  .Select(exampleSpace =>
                {
                    var smallestExampleSpace = exampleSpace.Subspace.FirstOrDefault() ?? exampleSpace;
                    return(smallestExampleSpace);
                })
                                                  .ToList()
                    : null;

                var exampleSpaceCullingShrinks = shrinkExampleSpaces(exampleSpaces);

                var subspaceMergingShrinks = exampleSpaces
                                             .Select((exampleSpace, index) => LiftAndInsertSubspace(exampleSpaces, exampleSpace.Subspace, index))
                                             .SelectMany(exampleSpaces => exampleSpaces);

                var shrinks = TraverseUnencountered(
                    Enumerable.Concat(
                        smallestExampleSpacesShrink == null ? Enumerable.Empty <List <IExampleSpace <T> > >() : new[] { smallestExampleSpacesShrink }.AsEnumerable(),
                        Enumerable.Concat(exampleSpaceCullingShrinks, subspaceMergingShrinks)),
                    encounteredIds,
                    GenerateNextExampleSpace);

                return(new ExampleSpace <TResult>(current, shrinks));
            }
Exemple #2
0
 /// <summary>
 /// Creates an example space by recursively applying a shrinking function to the root value.
 /// </summary>
 /// <typeparam name="T">The type of the example's value.</typeparam>
 /// <param name="rootValue"></param>
 /// <param name="shrink"></param>
 /// <param name="measure"></param>
 /// <param name="identify"></param>
 /// <returns></returns>
 public static IExampleSpace <T> Unfold <T>(
     T rootValue,
     ShrinkFunc <T> shrink,
     MeasureFunc <T> measure,
     IdentifyFunc <T> identify)
 {
     return(Unfold(
                rootValue,
                ShrinkFunc.WithoutContext(shrink),
                measure,
                identify));
 }
 public static IExampleSpace <TResult> Merge <T, TResult>(
     List <IExampleSpace <T> > exampleSpaces,
     Func <List <T>, TResult> mergeValues,
     ShrinkFunc <List <IExampleSpace <T> > > shrinkExampleSpaces,
     MeasureFunc <List <IExampleSpace <T> > > measureMerge,
     bool enableSmallestExampleSpacesOptimization) =>
 MergeHelpers.MergeInternal(
     exampleSpaces,
     mergeValues,
     shrinkExampleSpaces,
     measureMerge,
     ImmutableHashSet.Create <ExampleId>(),
     enableSmallestExampleSpacesOptimization,
     true);
Exemple #4
0
        public static IExampleSpace <T> Unfold <T, TContext>(
            T rootValue,
            ContextualShrinker <T, TContext> contextualShrinker,
            MeasureFunc <T> measure,
            IdentifyFunc <T> identify)
        {
            var functions = new ContextualizedUnfoldFunctions <T, TContext, T>(
                contextualShrinker.ContextualShrink, measure, identify, x => x, contextualShrinker.ContextualTraverse);

            var contextualizedExampleSpace = UnfoldHelpers.UnfoldInternal(
                rootValue,
                contextualShrinker.RootContext,
                functions);

            return(contextualizedExampleSpace.Map(x => x.Value));
        }
Exemple #5
0
 private record ContextualizedUnfoldFunctions <TAccumulator, TContext, TProjection>(
     ContextualShrinkFunc <TAccumulator, TContext> Shrink,
     MeasureFunc <TAccumulator> Measure,
     IdentifyFunc <TAccumulator> Identify,
     Func <TAccumulator, TProjection> Projection,
     NextContextFunc <TProjection, TContext> NextContext);