-
Notifications
You must be signed in to change notification settings - Fork 2
/
option_test.go
114 lines (103 loc) · 2.78 KB
/
option_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
113
114
package goga
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestOptionWithFitnessFunc(t *testing.T) {
t.Run("success", func(t *testing.T) {
g := &ga{}
err := OptionWithWeightFunc(func(rank int, cost float64) int { return 1 })(g)
require.NoError(t, err)
})
t.Run("failure", func(t *testing.T) {
// nil func
g := &ga{}
err := OptionWithWeightFunc(nil)(g)
require.Error(t, err)
})
}
func TestOptionWithPopulationFunc(t *testing.T) {
t.Run("success", func(t *testing.T) {
g := &ga{}
err := OptionWithPopulationFunc(func(currentPopulation int, step int64, bestCost, worstCost float64) int { return 1 })(g)
require.NoError(t, err)
})
t.Run("failure", func(t *testing.T) {
// nil func
g := &ga{}
err := OptionWithPopulationFunc(nil)(g)
require.Error(t, err)
})
}
func TestOptionWithGeneratorFunc(t *testing.T) {
t.Run("success", func(t *testing.T) {
g := &ga{}
err := OptionWithGeneratorFunc(func() Model { return nil })(g)
require.NoError(t, err)
})
t.Run("failure", func(t *testing.T) {
// nil func
g := &ga{}
err := OptionWithGeneratorFunc(nil)(g)
require.Error(t, err)
})
}
func TestOptionWithSelection(t *testing.T) {
t.Run("success", func(t *testing.T) {
g := &ga{}
err := OptionWithSelection(0.1, 0.2, 0.3)(g)
require.NoError(t, err)
})
t.Run("failure", func(t *testing.T) {
// nil func
g := &ga{}
err := OptionWithSelection(0, 0, 0)(g)
require.Error(t, err)
})
}
func TestOptionWithStepInterval(t *testing.T) {
t.Run("success", func(t *testing.T) {
stepsInterval := 1 * time.Second
g := &ga{}
err := OptionWithStepInterval(stepsInterval)(g)
require.NoError(t, err)
require.Equal(t, stepsInterval, g.config.stepsInterval)
})
}
func TestOptionWithMaximumNumberOfSteps(t *testing.T) {
t.Run("success", func(t *testing.T) {
maxStep := int64(1000)
g := &ga{}
err := OptionWithMaximumNumberOfSteps(maxStep)(g)
require.NoError(t, err)
require.Equal(t, maxStep, g.config.maxNumOfSteps)
})
}
func TestOptionWithTargetCost(t *testing.T) {
t.Run("success", func(t *testing.T) {
targetCost := float64(0.2)
g := &ga{}
err := OptionWithTargetCost(targetCost)(g)
require.NoError(t, err)
require.Equal(t, targetCost, g.config.targetCost)
})
}
func TestOptionWithInitialPopulation(t *testing.T) {
t.Run("success", func(t *testing.T) {
initialPopulation := int(2000)
g := &ga{}
err := OptionWithInitialPopulation(initialPopulation)(g)
require.NoError(t, err)
require.Equal(t, initialPopulation, g.config.initialPopulation)
})
}
func TestOptionWithNumberOfThreads(t *testing.T) {
t.Run("success", func(t *testing.T) {
numberOfThreads := int(20)
g := &ga{}
err := OptionWithNumberOfThreads(numberOfThreads)(g)
require.NoError(t, err)
require.Equal(t, numberOfThreads, g.config.numberOfThreads)
})
}