Skip to content

Commit

Permalink
Merge pull request #186 from marklam/Int64
Browse files Browse the repository at this point in the history
Proposed fix for issue #185
  • Loading branch information
moodmosaic authored Jul 8, 2019
2 parents deb2cde + d94e7b8 commit 07805fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
12 changes: 8 additions & 4 deletions src/Hedgehog/Range.fs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module Range =
fromBigInt (z + diff)

/// Scale an integral exponentially with the size parameter.
let inline scaleExponential (sz0 : Size) (z0 : 'a) (n0 : 'a) : 'a =
let inline scaleExponential (lo : 'a) (hi : 'a) (sz0 : Size) (z0 : 'a) (n0 : 'a) : 'a =
let sz =
clamp 0 99 sz0

Expand All @@ -135,7 +135,8 @@ module Range =
let diff =
(((float (abs (n - z) + 1I)) ** (float sz / 99.0)) - 1.0) * float (sign (n - z))

fromBigInt (bigint (round (float z + diff)))
// https://github.com/hedgehogqa/fsharp-hedgehog/issues/185
fromBigInt (clamp (toBigInt lo) (toBigInt hi) (bigint (round (float z + diff))))

/// Construct a range which scales the bounds relative to the size
/// parameter.
Expand Down Expand Up @@ -173,10 +174,13 @@ module Range =
[<CompiledName("ExponentialFrom")>]
let inline exponentialFrom (z : 'a) (x : 'a) (y : 'a) : Range<'a> =
Range (z, fun sz ->
let scale =
// https://github.com/hedgehogqa/fsharp-hedgehog/issues/185
scaleExponential x y sz z
let x_sized =
clamp x y (scaleExponential sz z x)
scale x
let y_sized =
clamp x y (scaleExponential sz z y)
scale y
x_sized, y_sized)

/// Construct a range which scales the second bound exponentially relative
Expand Down
19 changes: 10 additions & 9 deletions tests/Hedgehog.Tests/GenTests.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Hedgehog.Tests.GenTests

open System
open Hedgehog
open Swensen.Unquote
open Xunit
Expand Down Expand Up @@ -66,13 +65,15 @@ let ``dateTime shrinks to correct mid-value`` () =
System.DateTime (2000, 1, 1) =! result

[<Fact>]
let ``uint64 doesn't return any out-of-range value`` () =
let gen = Gen.uint64 <| Range.constant 1UL UInt64.MaxValue
let actual = Gen.sample 0 100 gen
test <@ actual |> List.contains 0UL |> not @>
let ``int64 can create exponentially bounded integer`` () =
Property.check <| property {
let! _ = Gen.int64 (Range.exponentialBounded ())
return true
}

[<Fact>]
let ``uint32 doesn't return any out-of-range value`` () =
let gen = Gen.uint32 <| Range.constant 1ul UInt32.MaxValue
let actual = Gen.sample 0 100 gen
test <@ actual |> List.contains 0ul |> not @>
let ``uint64 can create exponentially bounded integer`` () =
Property.check <| property {
let! _ = Gen.uint64 (Range.exponentialBounded ())
return true
}

0 comments on commit 07805fd

Please sign in to comment.