-
Notifications
You must be signed in to change notification settings - Fork 96
/
individual_test.go
112 lines (105 loc) · 2.43 KB
/
individual_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package eaopt
import (
"fmt"
"testing"
)
func TestIndividualString(t *testing.T) {
var testCases = []struct {
indi Individual
str string
}{
{
indi: Individual{
Genome: Vector{0, 1, 2},
Fitness: 42,
Evaluated: true,
ID: "bob",
},
str: "bob - 42.000 - [0 1 2]",
},
{
indi: Individual{
Genome: Vector{0, 1, 2},
Fitness: 42,
Evaluated: false,
ID: "ALICE",
},
str: "ALICE - ??? - [0 1 2]",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("TC %d", i), func(t *testing.T) {
if tc.indi.String() != tc.str {
t.Errorf("Expected %s, got %s", tc.str, tc.indi.String())
}
})
}
}
func TestCloneIndividual(t *testing.T) {
var (
rng = newRand()
genome = NewVector(rng)
indi1 = NewIndividual(genome, rng)
indi2 = indi1.Clone(rng)
)
if &indi1 == &indi2 || &indi1.Genome == &indi2.Genome {
t.Error("Individual was not deep copied")
}
}
func TestEvaluateIndividual(t *testing.T) {
var (
rng = newRand()
genome = NewVector(rng)
indi = NewIndividual(genome, rng)
)
if indi.Evaluated {
t.Error("Individual shouldn't have Evaluated set to True")
}
if indi.Evaluate() != nil {
t.Error("No error should have been raised")
}
if !indi.Evaluated {
t.Error("Individual should have Evaluated set to True")
}
}
func TestEvaluateIndividualWithError(t *testing.T) {
var (
rng = newRand()
genome = NewRuntimeErrorGenome(rng)
indi = NewIndividual(genome, rng)
)
if indi.Evaluate() == nil {
t.Error("An error should have been raised")
}
if indi.Evaluated {
t.Error("The individual shouldn't have Evaluated set to true")
}
}
func TestMutateIndividual(t *testing.T) {
var (
rng = newRand()
genome = NewVector(rng)
indi = NewIndividual(genome, rng)
)
indi.Evaluate()
indi.Mutate(rng)
if indi.Evaluated {
t.Error("Individual shouldn't have Evaluated set to True")
}
}
func TestCrossoverIndividual(t *testing.T) {
var (
rng = newRand()
indi1 = NewIndividual(NewVector(rng), rng)
indi2 = NewIndividual(NewVector(rng), rng)
offspring1 = indi1.Clone(rng)
offspring2 = indi2.Clone(rng)
)
indi1.Crossover(indi2, rng)
if offspring1.Evaluated || offspring2.Evaluated {
t.Error("Offsprings shouldn't have Evaluated set to True")
}
if &offspring1 == &indi1 || &offspring1 == &indi2 || &offspring2 == &indi1 || &offspring2 == &indi2 {
t.Error("Offsprings shouldn't share pointers with parents")
}
}