Skip to content

Commit bbafa87

Browse files
committed
fixed all remaining issues in GeneRandomizedTest
1 parent 701d105 commit bbafa87

10 files changed

Lines changed: 63 additions & 19 deletions

File tree

core/src/main/kotlin/org/evomaster/core/search/gene/Gene.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,12 +876,16 @@ abstract class Gene(
876876
/**
877877
* Genes might contain a value that is also stored
878878
* in another gene of the same type.
879+
*
880+
* If the type of [other] is different, this method might throw an [IllegalArgumentException]
881+
*
882+
* TODO refactor, in which type-check is done here
879883
*/
880884
abstract fun containsSameValueAs(other: Gene): Boolean
881885

882886

883887
/**
884-
* evaluate whether [this] and [gene] belong to one evolution during search
888+
* evaluate whether `this` and [gene] belong to one evolution during search
885889
*/
886890
open fun possiblySame(gene: Gene): Boolean = gene.name == name && gene::class == this::class
887891

core/src/main/kotlin/org/evomaster/core/search/gene/SeededGene.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,14 @@ class SeededGene<T>(
114114
if (other !is SeededGene<*>)
115115
return false
116116

117+
this.employSeeded = other.employSeeded
118+
this.isEmploySeededMutable = other.isEmploySeededMutable
119+
117120
val ok = if (employSeeded)
118121
this.seeded.unsafeCopyValueFrom(other.seeded)
119122
else
120123
this.gene.unsafeCopyValueFrom(other.gene as Gene)
121124

122-
if (ok){
123-
this.employSeeded = other.employSeeded
124-
this.isEmploySeededMutable = other.isEmploySeededMutable
125-
}
126125
return ok
127126
}
128127

core/src/main/kotlin/org/evomaster/core/search/gene/collection/MapGene.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,14 @@ abstract class MapGene<K, V>(
243243
TODO support other types if needed
244244
*/
245245
if (isElementApplicableToUniqueCheck(geneValue)){
246-
return elements.filter { ParamUtil.getValueGene(it.first).containsSameValueAs(ParamUtil.getValueGene(geneValue)) }
246+
return elements.filter {
247+
try {
248+
//TODO need refactoring/cleanup... should avoid try/catch
249+
ParamUtil.getValueGene(it.first).containsSameValueAs(ParamUtil.getValueGene(geneValue))
250+
}catch (e: Exception){
251+
false
252+
}
253+
}
247254
}
248255
return listOf()
249256
}

core/src/main/kotlin/org/evomaster/core/search/gene/regex/PatternCharacterBlockGene.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class PatternCharacterBlockGene(
5050

5151
override fun containsSameValueAs(other: Gene): Boolean {
5252
if (other !is PatternCharacterBlockGene) {
53-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
53+
return false
5454
}
5555
return this.stringBlock == other.stringBlock
5656
}

core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlAutoIncrementGene.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ class SqlAutoIncrementGene(name: String) : SimpleGene(name) {
5858
if (other !is SqlAutoIncrementGene) {
5959
return false
6060
}
61-
return this === other
61+
62+
/*
63+
auto-increment are always unique... but need to satisfy invariants on copyFrom...
64+
TODO might check for side-effects, and see if need more complex handling
65+
*/
66+
//return this === other
67+
return true
6268
}
6369

6470
override fun isMutable() = false

core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlForeignKeyGene.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ class SqlForeignKeyGene(
142142
return true
143143
}
144144

145+
override fun isPrintable(): Boolean {
146+
//TODO what about pk? anyway, likely PK/FK will need a major overhaul...
147+
return isBound() || nullable
148+
}
149+
145150
override fun getValueAsPrintableString(previousGenes: List<Gene>, mode: GeneUtils.EscapeMode?, targetFormat: OutputFormat?, extraCheck: Boolean): String {
146151

147152
if (!isBound()) {

core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlRangeGene.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ class SqlRangeGene<T>(
9292
if (other !is SqlRangeGene<*>) {
9393
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
9494
}
95+
96+
if(this.template.javaClass != other.template.javaClass) {
97+
return false
98+
}
99+
95100
return isLeftClosed.containsSameValueAs(other.isLeftClosed)
96101
&& left.containsSameValueAs(other.left as Gene)
97102
&& right.containsSameValueAs(other.right as Gene)

core/src/main/kotlin/org/evomaster/core/search/gene/wrapper/ChoiceGene.kt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ class ChoiceGene<T>(
199199
TODO this is bit limited... what about if there are wrapper genes involved?
200200
*/
201201

202+
//first check if can find gene of same type
203+
for(i in geneChoices.indices){
204+
val g = geneChoices[i]
205+
206+
if(g.javaClass == x.javaClass) {
207+
val updated = g.copyValueFrom(x)
208+
if (updated) {
209+
this.activeGeneIndex = i
210+
return true
211+
}
212+
}
213+
}
214+
215+
//if none found, check at any other possibly compatible types
202216
for(i in geneChoices.indices){
203217
val g = geneChoices[i]
204218

@@ -208,11 +222,12 @@ class ChoiceGene<T>(
208222
of them would be fine. also if one works, we should not break validity of the other
209223
options even if not selected
210224
*/
211-
val updated = g.copyValueFrom(x)
212-
g.forceNewTaints()
213-
if(updated){
214-
this.activeGeneIndex = i
215-
return true
225+
if(g.javaClass != x.javaClass) {
226+
val updated = g.copyValueFrom(x)
227+
if (updated) {
228+
this.activeGeneIndex = i
229+
return true
230+
}
216231
}
217232
}
218233

@@ -240,12 +255,9 @@ class ChoiceGene<T>(
240255
if (other !is ChoiceGene<*>) {
241256
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
242257
}
243-
if (this.activeGeneIndex != other.activeGeneIndex) {
244-
return false
245-
}
246258

247-
return this.geneChoices[activeGeneIndex]
248-
.containsSameValueAs(other.geneChoices[activeGeneIndex])
259+
return this.activeGene().javaClass == other.activeGene().javaClass
260+
&& this.activeGene().containsSameValueAs(other.activeGene())
249261
}
250262

251263
/**

core/src/test/kotlin/org/evomaster/core/search/gene/GeneRandomizedTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class GeneRandomizedTest : AbstractGeneTest(){
6464
val sx = x.getValueAsRawString()
6565

6666
val y = x.copy().apply { randomize(rand, true) }
67+
if(!y.isPrintable()){
68+
return@forEach
69+
}
6770
val sy = y.getValueAsRawString()
6871

6972
if (sx == sy) {

core/src/test/kotlin/org/evomaster/core/search/gene/GeneSamplerForTests.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,13 @@ object GeneSamplerForTests {
481481

482482
//TODO update after refactoring SeededGene with ChoiceGene (to implement)
483483

484+
val seeds = EnumGene<StringGene>("rand EnumGene ${rand.nextInt()}",
485+
listOf(StringGene("A"), StringGene("B"), StringGene("C")))
486+
484487
return SeededGene(
485488
name = "rand SeededGene",
486489
gene = sampleStringGene(rand),
487-
seeded = sampleEnumGene(rand) as EnumGene<StringGene>,
490+
seeded = seeds,
488491
employSeeded = rand.nextBoolean()
489492
)
490493
}

0 commit comments

Comments
 (0)