-
Notifications
You must be signed in to change notification settings - Fork 22
/
obs_test.go
65 lines (58 loc) · 1.29 KB
/
obs_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
package muniverse
import (
"bytes"
"image"
"image/png"
"math/rand"
"reflect"
"testing"
)
func TestObsRGB(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 100, 200))
expected := make([]uint8, 0, 100*200*3)
for i := 0; i < 100*200; i++ {
idx := i * 4
for j := 0; j < 3; j++ {
val := uint8(rand.Intn(0x100))
img.Pix[idx+j] = val
expected = append(expected, val)
}
img.Pix[idx+3] = 0xff
}
var pngData bytes.Buffer
if err := png.Encode(&pngData, img); err != nil {
t.Fatal(err)
}
actual, width, height, err := RGB(pngObs(pngData.Bytes()))
if err != nil {
t.Fatal(err)
}
if width != 100 || height != 200 {
t.Errorf("expected size 100x200 but got %dx%d", width, height)
}
if !reflect.DeepEqual(actual, expected) {
t.Error("got different pixel buffer")
}
}
func BenchmarkObsRGB(b *testing.B) {
img := image.NewRGBA(image.Rect(0, 0, 100, 200))
expected := make([]uint8, 0, 100*200*3)
for i := 0; i < 100*200; i++ {
idx := i * 4
for j := 0; j < 3; j++ {
val := uint8(rand.Intn(0x100))
img.Pix[idx+j] = val
expected = append(expected, val)
}
img.Pix[idx+3] = 0xff
}
var pngData bytes.Buffer
if err := png.Encode(&pngData, img); err != nil {
b.Fatal(err)
}
obs := pngObs(pngData.Bytes())
b.ResetTimer()
for i := 0; i < b.N; i++ {
RGB(obs)
}
}