Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
new maxTime option
  • Loading branch information
arcuri82 committed Nov 4, 2019
1 parent ba795af commit 73f1159
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 10 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -129,8 +129,12 @@ Available options can be queried by using:


The most important options are: The most important options are:


* `--maxTimeInSeconds <Int>` * `--maxTime <String>`
Maximum number of seconds allowed for the search. Maximum allowed time for the search, in the form `?h?m?s`, where it can be specified for how many hours (`h`),
minutes (`m`) and seconds (`s`) to run the search.
For example, `1h10m120s` would run the search for `72` minutes.
Each component (i.e., `h`, `m` and `s`) is optional, but at least one must be specified.
In other words, if you need to run the search for just `30` seconds, you can write `30s` instead of `0h0m30s`.
**The more time is allowed, the better results one can expect**. **The more time is allowed, the better results one can expect**.
But then of course the test generation will take longer. But then of course the test generation will take longer.


Expand Down
26 changes: 18 additions & 8 deletions core/src/main/kotlin/org/evomaster/core/EMConfig.kt
Expand Up @@ -9,6 +9,7 @@ import org.evomaster.core.output.OutputFormat
import org.evomaster.core.search.impact.GeneMutationSelectionMethod import org.evomaster.core.search.impact.GeneMutationSelectionMethod
import java.net.MalformedURLException import java.net.MalformedURLException
import java.net.URL import java.net.URL
import java.util.regex.Pattern
import kotlin.reflect.KMutableProperty import kotlin.reflect.KMutableProperty
import kotlin.reflect.jvm.javaType import kotlin.reflect.jvm.javaType


Expand Down Expand Up @@ -261,6 +262,14 @@ class EMConfig {
} }
} }
} }

m.annotations.find { it is Regex }?.also {
it as Regex
if(! parameterValue.matches(kotlin.text.Regex(it.regex))){
throw IllegalArgumentException("Parameter '${m.name}' with value $parameterValue is" +
" not matching the regex: ${it.regex}")
}
}
} }


when(stoppingCriterion){ when(stoppingCriterion){
Expand Down Expand Up @@ -520,8 +529,9 @@ class EMConfig {
@Cfg("Maximum number of seconds allowed for the search." + @Cfg("Maximum number of seconds allowed for the search." +
" The more time is allowed, the better results one can expect." + " The more time is allowed, the better results one can expect." +
" But then of course the test generation will take longer." + " But then of course the test generation will take longer." +
" Only applicable depending on the stopping criterion.") " Only applicable depending on the stopping criterion." +
@Min(1.0) " If this value is 0, the setting 'maxTime' will be used instead.")
@Min(0.0)
var maxTimeInSeconds = defaultMaxTimeInSeconds var maxTimeInSeconds = defaultMaxTimeInSeconds




Expand All @@ -532,9 +542,9 @@ class EMConfig {
" But then of course the test generation will take longer." + " But then of course the test generation will take longer." +
" Only applicable depending on the stopping criterion." + " Only applicable depending on the stopping criterion." +
" The time is expressed with a string where hours(h), minutes(m) and" + " The time is expressed with a string where hours(h), minutes(m) and" +
" seconds(s) can be specified, e.g., '1h 10m 120s' and '72m' are both valid" + " seconds(s) can be specified, e.g., '1h10m120s' and '72m' are both valid" +
" and equivalent.") " and equivalent.")
@Regex("(?=[^ ]+)( *)(\\d+h)?( *)(\\d+m)?( *)(\\d+s)?( *)") @Regex("( *)((?=([^ ]+))(\\d+h)?(\\d+m)?(\\d+s)?)( *)")
var maxTime = defaultMaxTime var maxTime = defaultMaxTime




Expand Down Expand Up @@ -897,18 +907,18 @@ class EMConfig {
val s = maxTime.indexOf('s') val s = maxTime.indexOf('s')


val hours = if(h >= 0){ val hours = if(h >= 0){
maxTime.subSequence(0, h).toString().toInt() maxTime.subSequence(0, h).toString().trim().toInt()
} else 0 } else 0


val minutes = if(m >=0 ){ val minutes = if(m >=0 ){
maxTime.subSequence( if(h>=0) h+1 else 0, m).toString().toInt() maxTime.subSequence( if(h>=0) h+1 else 0, m).toString().trim().toInt()
} else 0 } else 0


val seconds = if(s >=0){ val seconds = if(s >=0){
maxTime.subSequence( if(m>=0) m+1 else (if(h>=0) h+1 else 0), s).toString().toInt() maxTime.subSequence( if(m>=0) m+1 else (if(h>=0) h+1 else 0), s).toString().trim().toInt()
} else 0 } else 0


return (h * 60 * 60) + (m * 60) + s return (hours * 60 * 60) + (minutes * 60) + seconds
} }


} }
71 changes: 71 additions & 0 deletions core/src/test/kotlin/org/evomaster/core/EMConfigTest.kt
Expand Up @@ -164,4 +164,75 @@ internal class EMConfigTest{
options = parser.parse("--$name", noProtocol) options = parser.parse("--$name", noProtocol)
assertThrows(Exception::class.java, {config.updateProperties(options)}) assertThrows(Exception::class.java, {config.updateProperties(options)})
} }


@ParameterizedTest
@ValueSource(strings = [""," ","1","42","-42s","1 42s","42s1m","1m 42s"])
fun testTimeRegexWrong(value: String){

val parser = EMConfig.getOptionParser()
parser.recognizedOptions()["maxTime"] ?: throw Exception("Cannot find option")

val config = EMConfig()
val options = parser.parse("--maxTime", value)
assertThrows(Exception::class.java, {config.updateProperties(options)})
}

@Test
fun testTimeRegexJustSeconds(){

val parser = EMConfig.getOptionParser()
parser.recognizedOptions()["maxTime"] ?: throw Exception("Cannot find option")

val config = EMConfig()
val options = parser.parse("--maxTime", "42s")
config.updateProperties(options)

val seconds = config.timeLimitInSeconds()
assertEquals(42, seconds)
}

@Test
fun testTimeRegexJustMinutes(){

val parser = EMConfig.getOptionParser()
parser.recognizedOptions()["maxTime"] ?: throw Exception("Cannot find option")

val config = EMConfig()
val options = parser.parse("--maxTime", "3m")
config.updateProperties(options)

val seconds = config.timeLimitInSeconds()
assertEquals(180, seconds)
}

@Test
fun testTimeRegexJustHours(){

val parser = EMConfig.getOptionParser()
parser.recognizedOptions()["maxTime"] ?: throw Exception("Cannot find option")

val config = EMConfig()
val options = parser.parse("--maxTime", "2h")
config.updateProperties(options)

val seconds = config.timeLimitInSeconds()
assertEquals(2 * 60 * 60, seconds)
}

@Test
fun testTimeRegex(){

val parser = EMConfig.getOptionParser()
parser.recognizedOptions()["maxTime"] ?: throw Exception("Cannot find option")

val config = EMConfig()
val options = parser.parse("--maxTime", " 1h10m120s ")
config.updateProperties(options)

val seconds = config.timeLimitInSeconds()
assertEquals( (60 * 60) + 600 + 120, seconds)
}


} }

0 comments on commit 73f1159

Please sign in to comment.