Permalink
Browse files

lazy assertions

  • Loading branch information...
arcuri82 committed Dec 3, 2018
1 parent 695967b commit 6c8e0728abe8e897fe52fbe6af06d46c2922ef6b
@@ -0,0 +1,19 @@
package org.evomaster.core
object Lazy {
/**
* As in Java, we want assertions evaluated only if -ea.
* In Kotlin, they are always evaluated.
*
* TODO refactor if/when Kotlin will support lazy asserts
* https://youtrack.jetbrains.com/issue/KT-22292
*/
fun assert(predicate: () -> Boolean) {
if (javaClass.desiredAssertionStatus()) {
assert(predicate.invoke())
}
}
}
@@ -5,7 +5,7 @@ import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.SqlForeignKeyGene
import org.evomaster.core.search.gene.SqlPrimaryKeyGene
import org.evomaster.core.search.service.Randomness
import org.evomaster.core.Lazy
object DbActionUtils {
@@ -51,12 +51,7 @@ object DbActionUtils {
it.randomize(randomness, false, all)
}
if (javaClass.desiredAssertionStatus()) {
//TODO refactor if/when Kotlin will support lazy asserts
//https://youtrack.jetbrains.com/issue/KT-22292
assert(verifyForeignKeys(actions))
}
Lazy.assert{verifyForeignKeys(actions)}
}
private const val DEFAULT_MAX_NUMBER_OF_ATTEMPTS_TO_REPAIR_ACTIONS = 5
@@ -116,7 +111,7 @@ object DbActionUtils {
if (geneToRepair == null) {
return true
} else {
assert(actionIndexToRepair >= 0 && actionIndexToRepair < actions.size)
Lazy.assert{actionIndexToRepair >= 0 && actionIndexToRepair < actions.size}
// truncate list of actions to make them valid
val truncatedListOfActions = actions.subList(0, actionIndexToRepair).toMutableList()
actions.clear()
@@ -1,6 +1,7 @@
package org.evomaster.core.output
import org.apache.commons.lang3.StringEscapeUtils
import org.evomaster.core.Lazy
import org.evomaster.core.database.DbAction
import org.evomaster.core.output.formatter.OutputFormatter
import org.evomaster.core.problem.rest.RestCallAction
@@ -169,7 +170,7 @@ class TestCaseWriter {
* At this point all pk Ids should be valid
* (despite they being NULL or not)
**/
assert(g.hasValidUniqueIdOfPrimaryKey())
Lazy.assert{g.hasValidUniqueIdOfPrimaryKey()}
return if (g.isNull()) {
".d(\"$variableName\", \"NULL\")"
} else {
@@ -1,5 +1,6 @@
package org.evomaster.core.problem.rest
import org.evomaster.core.Lazy
import org.evomaster.core.database.DbAction
import org.evomaster.core.database.DbActionUtils
import org.evomaster.core.search.Action
@@ -72,7 +73,7 @@ class RestIndividual(val actions: MutableList<RestAction>,
*/
if (!verifyInitializationActions()) {
DbActionUtils.repairBrokenDbActionsList(dbInitialization, randomness)
assert(verifyInitializationActions())
Lazy.assert{verifyInitializationActions()}
}
}
@@ -5,6 +5,7 @@ import io.swagger.models.Swagger
import io.swagger.parser.SwaggerParser
import org.evomaster.client.java.controller.api.dto.SutInfoDto
import org.evomaster.core.EMConfig
import org.evomaster.core.Lazy
import org.evomaster.core.database.DbAction
import org.evomaster.core.database.DbActionUtils
import org.evomaster.core.database.SqlInsertBuilder
@@ -293,7 +294,7 @@ class RestSampler : Sampler<RestIndividual>() {
private fun handleSmartPost(post: RestCallAction, test: MutableList<RestAction>): SampleType {
assert(post.verb == HttpVerb.POST)
Lazy.assert{post.verb == HttpVerb.POST}
//as POST is used in all the others, maybe here we do not really need to handle it specially?
test.add(post)
@@ -302,7 +303,7 @@ class RestSampler : Sampler<RestIndividual>() {
private fun handleSmartDelete(delete: RestCallAction, test: MutableList<RestAction>): SampleType {
assert(delete.verb == HttpVerb.DELETE)
Lazy.assert{delete.verb == HttpVerb.DELETE}
createWriteOperationAfterAPost(delete, test)
@@ -311,7 +312,7 @@ class RestSampler : Sampler<RestIndividual>() {
private fun handleSmartPatch(patch: RestCallAction, test: MutableList<RestAction>): SampleType {
assert(patch.verb == HttpVerb.PATCH)
Lazy.assert{patch.verb == HttpVerb.PATCH}
createWriteOperationAfterAPost(patch, test)
@@ -320,7 +321,7 @@ class RestSampler : Sampler<RestIndividual>() {
private fun handleSmartPut(put: RestCallAction, test: MutableList<RestAction>): SampleType {
assert(put.verb == HttpVerb.PUT)
Lazy.assert{put.verb == HttpVerb.PUT}
/*
A PUT might be used to update an existing resource, or to create a new one
@@ -343,7 +344,7 @@ class RestSampler : Sampler<RestIndividual>() {
*/
private fun createWriteOperationAfterAPost(write: RestCallAction, test: MutableList<RestAction>) {
assert(write.verb == HttpVerb.PUT || write.verb == HttpVerb.DELETE || write.verb == HttpVerb.PATCH)
Lazy.assert{write.verb == HttpVerb.PUT || write.verb == HttpVerb.DELETE || write.verb == HttpVerb.PATCH}
test.add(write)
@@ -369,7 +370,7 @@ class RestSampler : Sampler<RestIndividual>() {
private fun handleSmartGet(get: RestCallAction, test: MutableList<RestAction>): SampleType {
assert(get.verb == HttpVerb.GET)
Lazy.assert{get.verb == HttpVerb.GET}
/*
A typical case is something like
@@ -415,7 +416,7 @@ class RestSampler : Sampler<RestIndividual>() {
if (created && !get.path.isLastElementAParameter()) {
val lastPost = test[test.size - 2] as RestCallAction
assert(lastPost.verb == HttpVerb.POST)
Lazy.assert{lastPost.verb == HttpVerb.POST}
val available = config.maxTestSize - test.size
@@ -1,6 +1,7 @@
package org.evomaster.core.problem.rest.service
import com.google.inject.Inject
import org.evomaster.core.Lazy
import org.evomaster.core.database.EmptySelects
import org.evomaster.core.problem.rest.HttpVerb
import org.evomaster.core.problem.rest.RestCallAction
@@ -125,9 +126,9 @@ class RestStructureMutator : StructureMutator() {
(0 until ind.actions.size - 1).forEach {
val a = ind.actions[it]
assert(a !is RestCallAction || a.verb == HttpVerb.POST)
Lazy.assert{a !is RestCallAction || a.verb == HttpVerb.POST}
}
assert({ val a = ind.actions.last(); a is RestCallAction && a.verb == HttpVerb.GET }())
Lazy.assert{ val a = ind.actions.last(); a is RestCallAction && a.verb == HttpVerb.GET }
val indices = ind.actions.indices
.filter { i ->
@@ -161,7 +162,7 @@ class RestStructureMutator : StructureMutator() {
val idx = indices.last()
val postTemplate = ind.actions[idx] as RestCallAction
assert(postTemplate.verb == HttpVerb.POST && !postTemplate.saveLocation)
Lazy.assert{postTemplate.verb == HttpVerb.POST && !postTemplate.saveLocation}
val post = sampler.createActionFor(postTemplate, ind.actions.last() as RestCallAction)
@@ -4,6 +4,7 @@ import com.google.inject.Inject
import org.evomaster.core.EMConfig
import org.evomaster.core.EMConfig.FeedbackDirectedSampling.FOCUSED_QUICKEST
import org.evomaster.core.EMConfig.FeedbackDirectedSampling.LAST
import org.evomaster.core.Lazy
import org.evomaster.core.problem.rest.RestCallResult
import org.evomaster.core.search.EvaluatedIndividual
import org.evomaster.core.search.FitnessValue
@@ -290,7 +291,7 @@ class Archive<T> where T : Individual {
partial, so this check on collateral coverage likely
will not be so effective
*/
assert(current.size == 1) //if covered, should keep only one solution in buffer
Lazy.assert{current.size == 1} //if covered, should keep only one solution in buffer
val shorter = copy.individual.size() < current[0].individual.size()
val sameLengthButBetterScore = (copy.individual.size() == current[0].individual.size())
@@ -1,5 +1,6 @@
package org.evomaster.core.search.service.mutator
import org.evomaster.core.Lazy
import org.evomaster.core.database.DbAction
import org.evomaster.core.database.DbActionUtils
import org.evomaster.core.search.Individual
@@ -63,10 +64,9 @@ class StandardMutator<T> : Mutator<T>() where T : Individual {
}
}
if (javaClass.desiredAssertionStatus()) {
//TODO refactor if/when Kotlin will support lazy asserts
assert(DbActionUtils.verifyForeignKeys(
individual.seeInitializingActions().filterIsInstance<DbAction>()))
Lazy.assert {
DbActionUtils.verifyForeignKeys(
individual.seeInitializingActions().filterIsInstance<DbAction>())
}
return copy
@@ -79,7 +79,7 @@ class StandardMutator<T> : Mutator<T>() where T : Individual {
// Second repair the initialization actions (if needed)
mutatedIndividual.repairInitializationActions(randomness)
// Third check that repair was successful
assert(mutatedIndividual.verifyInitializationActions())
Lazy.assert{mutatedIndividual.verifyInitializationActions()}
return mutatedIndividual
}
@@ -216,7 +216,7 @@ class StandardMutator<T> : Mutator<T>() where T : Individual {
private fun handleIntegerGene(gene: IntegerGene) {
assert(gene.min < gene.max && gene.isMutable())
Lazy.assert{gene.min < gene.max && gene.isMutable()}
//check maximum range. no point in having a delta greater than such range
val range: Long = gene.max.toLong() - gene.min.toLong()
@@ -9,6 +9,7 @@ import org.evomaster.core.BaseModule
import org.evomaster.core.EMConfig
import org.evomaster.core.EMConfig.Algorithm.*
import org.evomaster.core.EMConfig.FeedbackDirectedSampling.NONE
import org.evomaster.core.Lazy
import org.evomaster.core.search.algorithms.MioAlgorithm
import org.evomaster.core.search.algorithms.MosaAlgorithm
import org.evomaster.core.search.algorithms.RandomAlgorithm
@@ -63,7 +64,7 @@ class Main {
optimaY = createOptima(nTargetsPerVar, range, seed.toLong())
}
assert(optimaX.size + optimaY.size == nTargetsPerVar * 2)
Lazy.assert{optimaX.size + optimaY.size == nTargetsPerVar * 2}
listOf(RANDOM, MOSA, WTS).forEach { a ->
runAlg(a, seed.toLong(), budget, range, optimaX, optimaY, inf, NONE, t)

0 comments on commit 6c8e072

Please sign in to comment.