-
Notifications
You must be signed in to change notification settings - Fork 0
/
genome_test.go
46 lines (39 loc) · 1.24 KB
/
genome_test.go
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
43
44
45
46
package ga
import (
"testing"
)
func TestGenome_ToString(t *testing.T) {
t.Parallel()
var genA = NewGeneticAlgorithm()
genA.SetSeed(3)
genA.SetOutputFunc(func(a ...interface{}) { t.Log(a...) })
genA.SetFitnessFunc(func(gene Genome) int {
count := 0
for _, i := range gene.Sequence {
if i == "1" {
count++
}
}
return count
})
outputString := Genome{Bitstring{"1", "1", "1", "1"}}.String()
expected := "{[1 1 1 1 ]}"
if outputString != expected {
t.Error("Incorrect string:", outputString, "Expected:", expected)
}
outputString = Genome{Bitstring{"1", "0", "1", "0", "1", "0", "1", "0", "1", "0"}}.String()
expected = "{[1 0 1 0 1 0 1 0 1 0 ]}"
if outputString != expected {
t.Error("Incorrect string:", outputString, "Expected:", expected)
}
outputString = Genome{Bitstring{"1", "1", "1", "1", "1", "1", "1", "1", "1", "1"}}.String()
expected = "{[1 1 1 1 1 1 1 1 1 1 ]}"
if outputString != expected {
t.Error("Incorrect string:", outputString, "Expected:", expected)
}
outputString = Genome{Bitstring{"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"}}.String()
expected = "{[1 1 1 1 1 1 1 1 1 1 1 1 ]}"
if outputString != expected {
t.Error("Incorrect string:", outputString, "Expected:", expected)
}
}