forked from makiuchi-d/gozxing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luminance_source_test.go
101 lines (85 loc) · 2.28 KB
/
luminance_source_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
package gozxing
import (
"fmt"
"testing"
)
type testLuminanceSource struct {
LuminanceSourceBase
get func(x, y int) byte
}
func newTestLuminanceSource(size int) LuminanceSource {
return &testLuminanceSource{
LuminanceSourceBase{size, size},
func(x, y int) byte { return byte(255 * x / (size - 1)) },
}
}
func (this *testLuminanceSource) GetRow(y int, row []byte) ([]byte, error) {
if y >= this.GetHeight() {
return row, fmt.Errorf("y(%d) >= height(%d)", y, this.GetHeight())
}
width := this.GetWidth()
for i := 0; i < width; i++ {
row[i] = this.get(i, y)
}
return row, nil
}
func (this *testLuminanceSource) GetMatrix() []byte {
width := this.GetWidth()
height := this.GetHeight()
matrix := make([]byte, width*height)
for y := 0; y < height; y++ {
this.GetRow(y, matrix[width*y:])
}
return matrix
}
func (this *testLuminanceSource) Invert() LuminanceSource {
return LuminanceSourceInvert(this)
}
func (this *testLuminanceSource) String() string {
return LuminanceSourceString(this)
}
func TestLuminanceSource(t *testing.T) {
s := newTestLuminanceSource(16)
if w, h := s.GetWidth(), s.GetHeight(); w != 16 || h != 16 {
t.Fatalf("TestLuminanceSource size = %v,%v, expect (16,16)", w, h)
}
if s.IsCropSupported() {
t.Fatalf("IsCropped is not false")
}
if _, e := s.Crop(1, 1, 10, 10); e == nil {
t.Fatalf("Crop must be error")
}
if s.IsRotateSupported() {
t.Fatalf("IsRotateSupported is not false")
}
if _, e := s.RotateCounterClockwise(); e == nil {
t.Fatalf("RotateCounterClockwise must be error")
}
if _, e := s.RotateCounterClockwise45(); e == nil {
t.Fatalf("RotateCounterClockwise45 must be error")
}
inv := s.Invert()
if _, ok := inv.(*InvertedLuminanceSource); !ok {
t.Fatalf("Invert returns %T, expect *InvertedLuminanceSource", inv)
}
expect := "" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n" +
"####++++.... \n"
if str := s.String(); str != expect {
t.Fatalf("s.String:\n%v\nexpect:\n%v", str, expect)
}
}