Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ class BigDecimalGene(
private fun getRoundingMode() = DEFAULT_ROUNDING_MODE

private fun setValueWithDecimal(bd: BigDecimal, precision: Int?, scale: Int?){
val nextValue = if (precision == null){

val ensureRoundedValueIsInRange = (getMinimum () < bd && bd < getMaximum())

val rounded = if (precision == null){
if (scale == null) bd
else bd.setScale(scale, getRoundingMode())
} else{
Expand All @@ -294,7 +297,17 @@ class BigDecimalGene(
else this
}
}
value = nextValue

value = rounded

if (ensureRoundedValueIsInRange) {
// ensure the rounded value is also within range
if (value > getMaximum()) {
value = getMaximum()
} else if (value < getMinimum()) {
value = getMinimum()
}
}
}

private fun getMaxUsedInSearchAsLong() : Long{
Expand Down Expand Up @@ -386,9 +399,9 @@ class BigDecimalGene(
override fun checkForLocallyValidIgnoringChildren(): Boolean {
if (!super.checkForLocallyValidIgnoringChildren())
return false
if (value > getMaximum())
if (max != null && value > getMaximum())
return false
if (value < getMinimum())
if (min != null && value < getMinimum())
return false
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import org.evomaster.core.EMConfig
import org.evomaster.core.search.gene.numeric.BigDecimalGene
import org.evomaster.core.search.service.AdaptiveParameterControl
import org.evomaster.core.search.service.Randomness
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.math.BigDecimal
import java.math.MathContext
import java.math.RoundingMode

class BigDecimalGeneTest {

Expand Down Expand Up @@ -48,4 +47,51 @@ class BigDecimalGeneTest {
}
}

@Test
fun testSetValueWithLong() {
val minValue = 0L
val midValue = 1005L
val maxValue = 1006L

val gene = BigDecimalGene("gene",
min = BigDecimal(minValue),
max = BigDecimal(maxValue),
precision = 3)

val rand = Randomness()
rand.updateSeed(42)
gene.doInitialize(rand)
gene.setValueWithLong(midValue)

// As precision is 3, 1005L is rounded up to 1010L.
// Therefore, the comparison 1010L < 1006L fails.
// After fixing BigDecimalGene, it is clamped to 1006L.
assertTrue(gene.value <= gene.getMaximum())
}


@Test
fun testSetValueWithLongOutsideRange() {
val minValue = 0L
val maxValue = 10L

val gene = BigDecimalGene("gene",
min = BigDecimal(minValue),
max = BigDecimal(maxValue))

val rand = Randomness()
rand.updateSeed(42)
gene.doInitialize(rand)

/*
* This should not throw an exception. There
* might be scenarios where the client wants to
* set a value outside the range.
*/
gene.setValueWithLong(1000L)

assertEquals(BigDecimal(1000L) , gene.value)

}

}
Loading