-
Notifications
You must be signed in to change notification settings - Fork 22
/
env_test.go
149 lines (142 loc) · 3.03 KB
/
env_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
package muniverse
import (
"math"
"testing"
"time"
)
var BenchmarkEnvs = []string{"PopUp-v0", "FlappyBird-v0"}
func TestEnvs(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
for _, spec := range EnvSpecs {
t.Run(spec.Name, func(t *testing.T) {
env, err := NewEnv(spec)
if err != nil {
t.Error(err)
return
}
defer env.Close()
for i := 0; i < 2; i++ {
if err := env.Reset(); err != nil {
t.Error(err)
t.Log(env.Log())
break
}
if _, _, err := env.Step(time.Millisecond); err != nil {
t.Error(err)
t.Log(env.Log())
break
}
}
})
}
}
func TestEnvObserve(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
for _, spec := range EnvSpecs {
if !spec.AllCanvas {
continue
}
t.Run(spec.Name, func(t *testing.T) {
env, err := NewEnv(spec)
if err != nil {
t.Error(err)
return
}
defer env.Close()
if err := env.Reset(); err != nil {
t.Log(env.Log())
t.Fatal(err)
}
if _, _, err := env.Step(time.Millisecond * 30); err != nil {
t.Log(env.Log())
t.Fatal(err)
}
actualObs, err := env.Observe()
if err != nil {
t.Fatal(err)
}
env.(*rawEnv).spec.AllCanvas = false
expectedObs, err := env.Observe()
if err != nil {
t.Fatal(err)
}
actual, actualWidth, actualHeight, err := RGB(actualObs)
if err != nil {
t.Fatal(err)
}
expected, expectedWidth, expectedHeight, err := RGB(expectedObs)
if err != nil {
t.Fatal(err)
}
if actualWidth != expectedWidth || actualHeight != expectedHeight {
t.Fatalf("dimensions should be %dx%d but got %dx%d",
expectedWidth, expectedHeight, actualWidth, actualHeight)
}
for i, a := range actual {
x := expected[i]
diff := math.Abs(float64(x) - float64(a))
if math.Abs(diff) > 30 {
t.Fatalf("pixel %d: got %d, expected %d", i, a, x)
}
}
})
}
}
func BenchmarkEnvObserve(b *testing.B) {
for _, envName := range BenchmarkEnvs {
for _, compression := range []bool{false, true} {
name := envName
if compression {
name += "-Compressed"
}
b.Run(name, func(b *testing.B) {
opts := &Options{}
if compression {
opts.Compression = true
opts.CompressionQuality = 100
}
env, err := NewEnvOptions(SpecForName(envName), opts)
if err != nil {
b.Fatal(err)
}
defer env.Close()
defer b.StopTimer()
if err := env.Reset(); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := env.Observe(); err != nil {
b.Fatal(err)
}
}
})
}
}
}
func BenchmarkEnvReset(b *testing.B) {
for _, envName := range BenchmarkEnvs {
name := envName
b.Run(name, func(b *testing.B) {
env, err := NewEnv(SpecForName(envName))
if err != nil {
b.Fatal(err)
}
defer env.Close()
defer b.StopTimer()
if err := env.Reset(); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := env.Reset(); err != nil {
b.Fatal(err)
}
}
})
}
}