Skip to content

Commit

Permalink
Add function Gen.timeSpan
Browse files Browse the repository at this point in the history
  • Loading branch information
njlr committed Jul 25, 2023
1 parent f4a4b00 commit 19c44e8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Hedgehog/Gen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,13 @@ module Gen =
return DateTimeOffset(ticks, TimeSpan.FromMinutes (Operators.float offsetMinutes))
}

/// Generates a random TimeSpan using the specified range.
let timeSpan (range : Range<TimeSpan>) : Gen<TimeSpan> =
range
|> Range.map (fun x -> x.Ticks)
|> int64
|> map TimeSpan.FromTicks

//
// Sampling
//
Expand Down
14 changes: 14 additions & 0 deletions tests/Hedgehog.Tests/GenTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ let genTests = testList "Gen tests" [
|> List.length
=! actual.Length

yield! testCases "timeSpan creates TimeSpan instances"
[ 8; 16; 32; 64; 128; 256; 512 ] <| fun count ->

let actual =
(Range.constant TimeSpan.MinValue TimeSpan.MaxValue)
|> Gen.timeSpan
|> Gen.sample 0 count
|> Seq.toList

actual
|> List.distinct
|> List.length
=! actual.Length

testCase "unicode does not return any surrogate" <| fun _ ->
let actual =
Gen.sample 100 100000 Gen.unicode
Expand Down

0 comments on commit 19c44e8

Please sign in to comment.