Skip to content

Commit

Permalink
#1158 Handling precision loss due to conversion to Double by instead …
Browse files Browse the repository at this point in the history
…using BigDecimal
  • Loading branch information
harikrishnan83 authored and nashjain committed Jun 30, 2024
1 parent aab6bf3 commit 1e017bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ data class NumberPattern(
if (sampleData.toStringLiteral().length > maxLength)
return mismatchResult("number with maxLength $maxLength", sampleData, resolver.mismatchMessages)

val sampleNumber = BigDecimal(sampleData.number.toDouble())
val sampleNumber = BigDecimal(sampleData.number.toString())

val minOp = if (exclusiveMinimum) ">" else ">="
if (!eval(sampleNumber, minOp, minimum))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ internal class NumberPatternTest {
assertThat(generated).isEqualTo(NumberValue(10))
}

@Test
fun `test`() {
val pointZeroOne = ".01"
val minimumPointZeroOne = NumberPattern(minimum = BigDecimal(pointZeroOne))
val result = minimumPointZeroOne.matches(NumberValue(convertToNumber(pointZeroOne)), Resolver())
assertThat(result.isSuccess()).withFailMessage(result.reportString()).isTrue()
}

@Test
@Tag(GENERATION)
fun `negative values should be generated`() {
Expand Down

0 comments on commit 1e017bd

Please sign in to comment.