This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialization_test.go
156 lines (148 loc) · 3.44 KB
/
initialization_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package gago
import (
"fmt"
"strings"
"testing"
)
func TestInitUnifFloat64(t *testing.T) {
var (
testCases = []struct {
n int
bounds struct{ lower, upper float64 }
}{
{
n: 0,
bounds: struct{ lower, upper float64 }{-1, 0},
},
{
n: 1,
bounds: struct{ lower, upper float64 }{-1, 0},
},
{
n: 2,
bounds: struct{ lower, upper float64 }{-1, 0},
},
{
n: 42,
bounds: struct{ lower, upper float64 }{-1, 0},
},
{
n: 3,
bounds: struct{ lower, upper float64 }{0, 1},
},
{
n: 3,
bounds: struct{ lower, upper float64 }{-1, 1},
},
}
rng = newRand()
)
for i, tc := range testCases {
t.Run(fmt.Sprintf("TC %d", i), func(t *testing.T) {
var vector = InitUnifFloat64(tc.n, tc.bounds.lower, tc.bounds.upper, rng)
// Check length
if len(vector) != tc.n {
t.Error("InitUnifFloat64 didn't produce the right number of values")
}
// Check values are bounded
for _, v := range vector {
if v <= tc.bounds.lower || v >= tc.bounds.upper {
t.Error("InitUnifFloat64 produced out of bound values")
}
}
})
}
}
func TestInitJaggFloat64(t *testing.T) {
var (
N = []int{0, 1, 2, 42}
rng = newRand()
)
for _, n := range N {
var (
lower = make([]float64, n)
upper = make([]float64, n)
)
for i := 0; i < n; i++ {
lower[i] = 0.0 + rng.Float64()*100.0
upper[i] = lower[i] + rng.Float64()*100.0
}
var vector = InitJaggFloat64(n, lower, upper, rng)
// Check length
if len(vector) != n {
t.Error("InitJaggFloat64 didn't produce the right number of values")
}
// Check values are bounded
for i, v := range vector {
if v <= lower[i] || v >= upper[i] {
t.Error("InitJaggFloat64 produced out of bound values")
}
}
}
}
func TestInitNormFloat64(t *testing.T) {
var rng = newRand()
for _, n := range []int{0, 1, 2, 42} {
if len(InitNormFloat64(n, 0, 1, rng)) != n {
t.Error("InitNormFloat64 didn't produce the right number of values")
}
}
}
func TestInitUnifString(t *testing.T) {
var (
rng = newRand()
corpus = strings.Split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
)
for _, n := range []int{0, 1, 2, 42} {
var genome = InitUnifString(n, corpus, rng)
if len(genome) != n {
t.Error("InitUnifString didn't produce the right number of values")
}
// Check the values are part of the corpus
for _, v := range genome {
var partOfCorpus = false
for _, c := range corpus {
if v == c {
partOfCorpus = true
break
}
}
if !partOfCorpus {
t.Error("InitUnifString produced a value out of the corpus")
}
}
}
}
func TestInitUniqueString(t *testing.T) {
var (
rng = newRand()
corpus = strings.Split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
)
for _, n := range []int{0, 1, 2, 42} {
var genome = InitUniqueString(n, corpus, rng)
if len(genome) != n {
t.Error("InitUniqueString didn't produce the right number of values")
}
// Check the values are part of the corpus
for _, v := range genome {
var partOfCorpus = false
for _, c := range corpus {
if v == c {
partOfCorpus = true
break
}
}
if !partOfCorpus {
t.Error("InitUniqueString produced a value out of the corpus")
}
}
// Check the values are unique
for i, v1 := range genome {
for j, v2 := range genome {
if i != j && v1 == v2 {
t.Error("InitUniqueString didn't produce unique values")
}
}
}
}
}