Skip to content

Commit

Permalink
Generates negative decimal values (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
sasmithjr committed Oct 8, 2023
1 parent b2e116b commit f1bd17a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/FsCheck/Internals.DefaultArbs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ type internal Default private() =
|> Gen.map Decimal
|> Gen.map (fun d -> d / (Decimal tenPow9))
let generator = Gen.sized (fun size ->
decimalGen
|> Gen.map (fun d -> d * Decimal(size)))
Gen.map2
(fun d isNegative ->
let value = d * Decimal(size)
if isNegative then -value else value)
decimalGen
Default.Bool.Generator
)
let shrinker d =
let (|<|) x y = abs x < abs y
seq {
Expand Down
9 changes: 9 additions & 0 deletions tests/FsCheck.Test/Arbitrary.fs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,15 @@ module Arbitrary =
|> Seq.forall (fun shrunkv -> shrunkv = 0m || shrunkv <= abs value)
|> assertTrue

[<Property>]
let ``Negative decimal values are generated`` (size : PositiveInt) =
let predicate = fun (d: decimal) -> d < 0m
generate<decimal>
|> Gen.filter predicate
|> Gen.sampleWithSize size.Get 10
|> Array.forall predicate
|> assertTrue

[<Fact>]
let DoNotSizeDecimal() =
generate<DoNotSize<decimal>> |> sample 10 |> ignore
Expand Down

0 comments on commit f1bd17a

Please sign in to comment.