/
AlgoTest.scala
42 lines (34 loc) · 1.02 KB
/
AlgoTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import org.scalatest.FreeSpec
import org.scalameter._
/**
* Created by Kotobotov.ru on 13.09.2018.
*/
class AlgoTest extends FreeSpec {
"Correctnes" - {
val target = "How many monkeys does it take to produce Shakespeare?"
val geneSize = target.length
val mutationRate = 5
def fitness = (dna: DNA) => {
dna.gene
.zip(target)
.filter(item => item._1 == item._2)
.size
.toDouble / geneSize
}
"should have good solution" in {
val geneBase = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !?"
assert(
GeneticAlgorithm(geneBase, geneSize, fitness, mutationRate).maxScore >= 1)
}
"should have at least some solution" in {
val geneBase = "abcdefghijk"
assert(
GeneticAlgorithm(geneBase, geneSize, fitness, mutationRate).maxScore > 0)
}
"should don't have any solution" in {
val geneBase = "-1232342"
assertThrows[Exception](
GeneticAlgorithm(geneBase, geneSize, fitness, mutationRate))
}
}
}