|
| 1 | +namespace Hedgehog.Linq |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Runtime.CompilerServices |
| 5 | +open Hedgehog |
| 6 | +open Hedgehog.FSharp |
| 7 | + |
| 8 | +[<AbstractClass; Sealed>] |
| 9 | +type GenFunctionExtensions private () = |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Generates a list together with a function that maps each of the distinct |
| 13 | + /// elements in the list to values generated by outGen. |
| 14 | + /// </summary> |
| 15 | + /// <remarks> |
| 16 | + /// Distinct elements in the input list may map to the same output values. |
| 17 | + /// For example, [2; 3; 2] may map to ['A'; 'B'; 'A'] or ['A'; 'A'; 'A'], |
| 18 | + /// but never ['A'; 'B'; 'C']. The generated function throws if called with |
| 19 | + /// values not present in the input list. |
| 20 | + /// </remarks> |
| 21 | + /// <param name="inpGen">Generator for the input list.</param> |
| 22 | + /// <param name="outGen">Generator for the output values.</param> |
| 23 | + /// <returns>A generator that produces a tuple of the input list and a mapping function.</returns> |
| 24 | + [<Extension>] |
| 25 | + static member WithMapTo(inpGen : Gen<ResizeArray<'T>>, outGen : Gen<'TResult>) : Gen<struct (ResizeArray<'T> * Func<'T, 'TResult>)> = |
| 26 | + inpGen |
| 27 | + |> Gen.map List.ofSeq |
| 28 | + |> Gen.withMapTo outGen |
| 29 | + |> Gen.map (fun (inputs, f) -> struct (ResizeArray inputs, Func<'T, 'TResult>(f))) |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Generates a list together with a function that maps each of the distinct |
| 33 | + /// elements in the list to distinct values generated by outGen. |
| 34 | + /// </summary> |
| 35 | + /// <remarks> |
| 36 | + /// Distinct elements in the input list are guaranteed to map to distinct |
| 37 | + /// output values. For example, [2; 3; 2] may map to ['A'; 'B'; 'A'], but |
| 38 | + /// never ['A'; 'A'; 'A'] or ['A'; 'B'; 'C']. Only use this if the output |
| 39 | + /// space is large enough that the required number of distinct output values |
| 40 | + /// are likely to be generated. The generated function throws if called with |
| 41 | + /// values not present in the input list. |
| 42 | + /// </remarks> |
| 43 | + /// <param name="inpGen">Generator for the input list.</param> |
| 44 | + /// <param name="outGen">Generator for the output values.</param> |
| 45 | + /// <returns>A generator that produces a tuple of the input list and a mapping function.</returns> |
| 46 | + [<Extension>] |
| 47 | + static member WithDistinctMapTo(inpGen : Gen<ResizeArray<'T>>, outGen : Gen<'TResult>) : Gen<struct(ResizeArray<'T> * Func<'T, 'TResult>)> = |
| 48 | + inpGen |
| 49 | + |> Gen.map List.ofSeq |
| 50 | + |> Gen.withDistinctMapTo outGen |
| 51 | + |> Gen.map (fun (inputs, f) -> struct (ResizeArray inputs, Func<'T, 'TResult>(f))) |
0 commit comments