Skip to content

Commit a83b1b3

Browse files
authored
Merge pull request #453 from hedgehogqa/cleanup-round-1
Cleanup round 1
2 parents dacda12 + 2deadfa commit a83b1b3

11 files changed

Lines changed: 67 additions & 39 deletions

File tree

global.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
33
"rollForward": "feature",
4-
"version": "6.0.411"
4+
"version": "8.0.100"
55
}
6-
}
6+
}

src/Hedgehog/Gen.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open System
77
type Gen<'a> =
88
| Gen of Random<Tree<'a>>
99

10+
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
1011
module Gen =
1112

1213
let ofRandom (r : Random<Tree<'a>>) : Gen<'a> =
@@ -608,7 +609,7 @@ module Gen =
608609
let! offsetMinutes = int32 (Range.linearFrom 0 (Operators.int minOffsetMinutes) (Operators.int maxOffsetMinutes))
609610
return DateTimeOffset(ticks, TimeSpan.FromMinutes (Operators.float offsetMinutes))
610611
}
611-
612+
612613
#if !FABLE_COMPILER
613614
/// Generates a random TimeSpan using the specified range.
614615
let timeSpan (range : Range<TimeSpan>) : Gen<TimeSpan> =

src/Hedgehog/Linq/Gen.fs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ type Gen private () =
1212
/// Create a generator that always yields a constant value.
1313
/// </summary>
1414
/// <param name="value">The constant value the generator always returns.</param>
15+
[<Obsolete("Use Gen.Constant instead.")>]
1516
static member FromValue (value : 'T) : Gen<'T> =
1617
Gen.constant value
1718

19+
/// <summary>
20+
/// Create a generator that always yields a constant value.
21+
/// </summary>
22+
/// <param name="value">The constant value the generator always returns.</param>
23+
static member Constant (value : 'T) : Gen<'T> =
24+
25+
Gen.constant value
1826
static member FromRandom (random : Random<Tree<'T>>) : Gen<'T> =
1927
Gen.ofRandom random
2028

@@ -73,18 +81,27 @@ type Gen private () =
7381
/// <i>The input list must be non-empty.</i>
7482
/// </summary>
7583
/// <param name="items">A non-empty IEnumerable of the Gen's possible values</param>
76-
static member Item (items : seq<'T>) : Gen<'T> =
84+
static member Item ([<ParamArray>] items : array<'T>) : Gen<'T> =
7785
Gen.item items
7886

7987
/// Uses a weighted distribution to randomly select one of the gens in the list.
8088
/// This generator shrinks towards the first generator in the list.
8189
/// <i>The input list must be non-empty.</i>
82-
static member Frequency (gens : seq<int * Gen<'T>>) : Gen<'T> =
90+
static member Frequency ([<ParamArray>] gens : array<int * Gen<'T>>) : Gen<'T> =
8391
Gen.frequency gens
8492

93+
/// Uses a weighted distribution to randomly select one of the gens in the list.
94+
/// This generator shrinks towards the first generator in the list.
95+
/// <i>The input list must be non-empty.</i>
96+
static member Frequency ([<ParamArray>] values : array<struct (int * 'T)>) : Gen<'T> =
97+
values
98+
|> Seq.map (fun struct (weight, value) -> (weight, Gen.constant value))
99+
|> Gen.frequency
100+
101+
85102
/// Randomly selects one of the gens in the list.
86103
/// <i>The input list must be non-empty.</i>
87-
static member Choice (gens : seq<Gen<'T>>) : Gen<'T> =
104+
static member Choice ([<ParamArray>] gens : array<Gen<'T>>) : Gen<'T> =
88105
Gen.choice gens
89106

90107
/// Randomly selects from one of the gens in either the non-recursive or the
@@ -157,6 +174,7 @@ type Gen private () =
157174

158175
/// <summary>
159176
/// Generates a random Latin-1 character, i.e. from '\000' to '\255', i.e. any 8 bit character.
177+
/// </summary>
160178
/// <remarks>
161179
/// Non-printable and control characters can be generated, e.g. NULL and BEL.
162180
/// </remarks>
@@ -278,7 +296,6 @@ type Gen private () =
278296
static member DateTimeOffset (range : Range<DateTimeOffset>) : Gen<DateTimeOffset> =
279297
Gen.dateTimeOffset range
280298

281-
[<Extension>]
282299
[<AbstractClass; Sealed>]
283300
type GenExtensions private () =
284301

@@ -289,6 +306,7 @@ type GenExtensions private () =
289306
/// <summary>
290307
/// Generates an array using a 'Range' to determine the length.
291308
/// </summary>
309+
/// <param name="gen">Item generator.</param>
292310
/// <param name="range">Range determining the length of the array.</param>
293311
[<Extension>]
294312
static member Array (gen : Gen<'T>, range : Range<int>) : Gen<'T []> =
@@ -297,6 +315,7 @@ type GenExtensions private () =
297315
/// <summary>
298316
/// Generates an enumerable using a 'Range' to determine the length.
299317
/// </summary>
318+
/// <param name="gen">Item generator.</param>
300319
/// <param name="range">Range determining the length of the enumerable.</param>
301320
[<Extension>]
302321
static member Enumerable (gen : Gen<'T>, range : Range<int>) : Gen<seq<'T>> =
@@ -347,6 +366,7 @@ type GenExtensions private () =
347366
Gen.resize size gen
348367

349368
/// <summary>Returns a List of values produced by the generator.</summary>
369+
/// <param name="gen">Value generator.</param>
350370
/// <param name="size">The size parameter for the generator.</param>
351371
/// <param name="count">The number of samples to produce, i.e. the length of the List.</param>
352372
[<Extension>]
@@ -386,11 +406,11 @@ type GenExtensions private () =
386406
Gen.mapTree binder.Invoke gen
387407

388408
/// <summary>
389-
/// Projects each value of a generator into a new form. Similar to <c>Enumerable.Select<c>.
409+
/// Projects each value of a generator into a new form. Similar to <c>Enumerable.Select</c>.
390410
/// </summary>
391411
/// <example>
392412
/// <code>
393-
/// Gen<Point> pointGen = Gen.Int32(Range.Constant(0,200))
413+
/// Gen&lt;Point&gt; pointGen = Gen.Int32(Range.Constant(0,200))
394414
/// .Tuple2()
395415
/// .Select(tuple => new Point(tuple.Item1, tuple.Item2));
396416
/// </code>
@@ -400,11 +420,11 @@ type GenExtensions private () =
400420
Gen.map mapper.Invoke gen
401421

402422
/// <summary>
403-
/// Projects each value of a generator into a new form. Similar to <c>Enumerable.Select<c>.
423+
/// Projects each value of a generator into a new form. Similar to <c>Enumerable.Select</c>.
404424
/// </summary>
405425
/// <example>
406426
/// <code>
407-
/// Gen<Point> pointGen = Gen.Int32(Range.Constant(0,200))
427+
/// Gen&lt;Point&gt; pointGen = Gen.Int32(Range.Constant(0,200))
408428
/// .Tuple2()
409429
/// .Select(tuple => new Point(tuple.Item1, tuple.Item2));
410430
/// </code>
@@ -416,11 +436,11 @@ type GenExtensions private () =
416436
genB
417437

418438
/// <summary>
419-
/// Projects each value of a generator into a new form. Similar to <c>Enumerable.Select<c>.
439+
/// Projects each value of a generator into a new form. Similar to <c>Enumerable.Select</c>.
420440
/// </summary>
421441
/// <example>
422442
/// <code>
423-
/// Gen<Point> pointGen = Gen.Int32(Range.Constant(0,200))
443+
/// Gen&lt;Point&gt; pointGen = Gen.Int32(Range.Constant(0,200))
424444
/// .Tuple2()
425445
/// .Select(tuple => new Point(tuple.Item1, tuple.Item2));
426446
/// </code>
@@ -433,11 +453,11 @@ type GenExtensions private () =
433453
genC
434454

435455
/// <summary>
436-
/// Projects each value of a generator into a new form. Similar to <c>Enumerable.Select<c>.
456+
/// Projects each value of a generator into a new form. Similar to <c>Enumerable.Select</c>.
437457
/// </summary>
438458
/// <example>
439459
/// <code>
440-
/// Gen<Point> pointGen = Gen.Int32(Range.Constant(0,200))
460+
/// Gen&lt;Point&gt; pointGen = Gen.Int32(Range.Constant(0,200))
441461
/// .Tuple2()
442462
/// .Select(tuple => new Point(tuple.Item1, tuple.Item2));
443463
/// </code>

src/Hedgehog/Linq/Range.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ type Range private () =
1313
//
1414

1515
/// Construct a range which represents a constant single value.
16+
[<Obsolete("Use Range.Singleton instead.", true)>]
1617
static member FromValue (value : 'T) : Range<'T> =
1718
Range.singleton value
1819

20+
/// Construct a range which represents a constant single value.
21+
static member Singleton (value : 'T) : Range<'T> =
22+
Range.singleton value
23+
1924
/// Construct a range which is unaffected by the size parameter with a
2025
/// origin point which may differ from the bounds.
2126
static member Constant (z : 'T, x : 'T, y : 'T) : Range<'T> =

src/Hedgehog/Range.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Size = int
1616
type Range<'a> =
1717
| Range of origin : 'a * (Size -> 'a * 'a)
1818

19+
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
1920
module Range =
2021
let private bimap (f : 'a -> 'b) (g : 'c -> 'd) (a : 'a, b : 'c) : 'b * 'd =
2122
f a, g b

tests/Hedgehog.Benchmarks/GenBenchmarks.fs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ namespace Hedgehog.Benchmarks
22

33
open BenchmarkDotNet.Attributes
44
open BenchmarkDotNet.Jobs
5+
open FsCheck.Fluent
56

67
module HRange = Hedgehog.Range
78
module HGen = Hedgehog.Gen
8-
module FArb = FsCheck.Arb
9-
module FGen = FsCheck.Gen
109

1110
[<SimpleJob(RuntimeMoniker.NetCoreApp31)>]
1211
type GenBenchmarks () =
@@ -23,6 +22,5 @@ type GenBenchmarks () =
2322

2423
[<Benchmark>]
2524
member this.FsCheckGenSampleInt32 () =
26-
FArb.generate<int>
27-
|> FGen.sample 100 this.N
25+
ArbMap.Default.ArbFor<int>().Generator.Sample(this.N)
2826
|> Seq.iter ignore

tests/Hedgehog.Benchmarks/Hedgehog.Benchmarks.fsproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>
@@ -11,8 +11,8 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="BenchmarkDotNet" Version="0.13.6" />
15-
<PackageReference Include="FsCheck" Version="2.16.6" />
14+
<PackageReference Include="BenchmarkDotNet" Version="0.15.6" />
15+
<PackageReference Include="FsCheck" Version="3.3.2" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

tests/Hedgehog.DieHarder/Hedgehog.DieHarder.fsproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>
99
<Compile Include="Program.fs" />
1010
</ItemGroup>
1111

12-
<ItemGroup>
13-
<ProjectReference Include="..\..\src\Hedgehog\Hedgehog.fsproj" />
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\src\Hedgehog\Hedgehog.fsproj" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

tests/Hedgehog.Linq.Tests/Hedgehog.Linq.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
5-
<LangVersion>8.0</LangVersion>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>12</LangVersion>
66
<ApplicationIcon />
77
<OutputType>Library</OutputType>
88
<StartupObject />
99
</PropertyGroup>
1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
12-
<PackageReference Include="xunit" Version="2.5.0" />
13-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
12+
<PackageReference Include="xunit" Version="2.9.3" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
1414
<PrivateAssets>all</PrivateAssets>
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
</PackageReference>

tests/Hedgehog.Linq.Tests/LinqTests.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using Xunit;
54

@@ -75,8 +74,8 @@ public void RecheckIsFasterThanCheck()
7574
var mid = Gen.Int32(Range.Constant(10, 50));
7675
var big = Gen.Int32(Range.Constant(100, 200));
7776
var large = Gen.Int32(Range.Constant(500, 1000));
78-
var choice = Gen.Choice(new List<Gen<int>> { low, mid, big, large }).List(Range.Constant(100, 200));
79-
var prop = ForAll(choice).Select(x => x.Any((x) => x == 990));
77+
var choice = Gen.Choice(low, mid, big, large).List(Range.Constant(100, 200));
78+
var prop = ForAll(choice).Select(x => x.Any(y => y == 990));
8079
var watch = new System.Diagnostics.Stopwatch();
8180

8281
watch.Start();
@@ -174,9 +173,9 @@ from z in ForAll(Gen.Bool)
174173
public void CanUseWhereWithAssertion()
175174
{
176175
var property =
177-
from x in ForAll(Gen.FromValue(true))
176+
from x in ForAll(Gen.Constant(true))
178177
where x == true
179-
from y in ForAll(Gen.FromValue(false))
178+
from y in ForAll(Gen.Constant(false))
180179
where y == false
181180
select Assert.True(x && !y);
182181

@@ -187,9 +186,9 @@ from y in ForAll(Gen.FromValue(false))
187186
public void CanUseWhereWithBool()
188187
{
189188
var property =
190-
from x in ForAll(Gen.FromValue(true))
189+
from x in ForAll(Gen.Constant(true))
191190
where x == true
192-
from y in ForAll(Gen.FromValue(false))
191+
from y in ForAll(Gen.Constant(false))
193192
where y == false
194193
select x && !y;
195194

@@ -238,6 +237,10 @@ where i
238237
[Fact]
239238
public void CanUseSelectManyWithGen()
240239
{
240+
Gen.Frequency(
241+
(1, Gen.Int32(Range.Constant(0, 100))),
242+
(1, Gen.Int32(Range.Constant(0, 100)))
243+
);
241244
Gen<bool> a =
242245
from i in Gen.Bool
243246
from j in Gen.Bool

0 commit comments

Comments
 (0)