From 73ddcc1065c2ae163a92844f3b1d8612749e9cf2 Mon Sep 17 00:00:00 2001 From: Naresh Jain Date: Wed, 19 Jun 2024 15:00:27 +0200 Subject: [PATCH] Fixed negative test for range for Integers --- .../in/specmatic/core/pattern/NumberPattern.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/main/kotlin/in/specmatic/core/pattern/NumberPattern.kt b/core/src/main/kotlin/in/specmatic/core/pattern/NumberPattern.kt index c600be43e..f86877d41 100644 --- a/core/src/main/kotlin/in/specmatic/core/pattern/NumberPattern.kt +++ b/core/src/main/kotlin/in/specmatic/core/pattern/NumberPattern.kt @@ -33,8 +33,8 @@ data class NumberPattern( require(minimum <= maximum) { "Inappropriate minimum and maximum values set" } } - private val smallestValue: BigDecimal - get() = if (isDoubleFormat) BigDecimal(Double.MIN_VALUE) else Int.MIN_VALUE.toBigDecimal() + private val smallestIncValue: BigDecimal + get() = if (isDoubleFormat) BigDecimal(Double.MIN_VALUE) else BigDecimal(1) private val largestValue: BigDecimal get() = if (isDoubleFormat) BigDecimal(Double.MAX_VALUE) else Int.MAX_VALUE.toBigDecimal() @@ -76,10 +76,10 @@ data class NumberPattern( return resolver.resolveExample(example, this) ?: NumberValue(randomNumber(minLength)) val min = if (minimum == MIN) { - if (maximum < smallestValue) + if (maximum < smallestIncValue) maximum - BigDecimal(1) else - smallestValue + smallestIncValue } else minimum val max = if (maximum == MAX) largestValue else maximum @@ -102,13 +102,13 @@ data class NumberPattern( values.add(this) if (minValueIsNotSet()) { if(exclusiveMinimum) - values.add(ExactValuePattern(NumberValue(minimum + smallestValue))) + values.add(ExactValuePattern(NumberValue(minimum + smallestIncValue))) else values.add(ExactValuePattern(NumberValue(minimum))) } if (maxValueIsNotSet()) { if(exclusiveMaximum) - values.add(ExactValuePattern(NumberValue(maximum - smallestValue))) + values.add(ExactValuePattern(NumberValue(maximum - smallestIncValue))) else values.add(ExactValuePattern(NumberValue(maximum))) } @@ -124,10 +124,10 @@ data class NumberPattern( override fun negativeBasedOn(row: Row, resolver: Resolver): Sequence> { val dataTypeNegatives: Sequence = sequenceOf(NullPattern, BooleanPattern(), StringPattern()) val negativeForMinimumValue: Sequence> = - negativeRangeValues(minValueIsNotSet(), minimum - smallestValue, "value less than minimum of $minimum") + negativeRangeValues(minValueIsNotSet(), minimum - smallestIncValue, "value less than minimum of $minimum") val negativeForMaximumValue: Sequence> = - negativeRangeValues(maxValueIsNotSet(), maximum + smallestValue, "value greater than maximum of $maximum") + negativeRangeValues(maxValueIsNotSet(), maximum + smallestIncValue, "value greater than maximum of $maximum") return scalarAnnotation(this, dataTypeNegatives) + negativeForMinimumValue + negativeForMaximumValue }