canvas_test.gno
2.93 Kb · 116 lines
1package canvas
2
3import (
4 "testing"
5
6 "gno.land/p/nt/uassert/v0"
7)
8
9func TestNewRejectsBadSizes(t *testing.T) {
10 for _, tc := range []struct{ w, h int }{{0, 1}, {1, 0}, {-1, 1}, {MaxSide + 1, 1}, {1, MaxSide + 1}} {
11 func() {
12 defer func() {
13 if r := recover(); r == nil {
14 t.Errorf("New(%d, %d) did not panic", tc.w, tc.h)
15 }
16 }()
17 New(tc.w, tc.h)
18 }()
19 }
20}
21
22func TestSetGet(t *testing.T) {
23 c := New(4, 3)
24 uassert.Equal(t, 4, c.Width())
25 uassert.Equal(t, 3, c.Height())
26 uassert.Equal(t, 0, c.Painted())
27 uassert.Equal(t, Blank, c.Get(3, 2))
28
29 c.Set(Pixel{X: 3, Y: 2, Color: 6})
30 uassert.Equal(t, byte(6), c.Get(3, 2))
31 uassert.Equal(t, 1, c.Painted())
32
33 // Repainting the same pixel does not count twice.
34 c.Set(Pixel{X: 3, Y: 2, Color: 7})
35 uassert.Equal(t, byte(7), c.Get(3, 2))
36 uassert.Equal(t, 1, c.Painted())
37
38 // Other rows stay unallocated and blank.
39 uassert.Equal(t, Blank, c.Get(0, 0))
40 uassert.True(t, c.rows[0] == nil, "row 0 should stay nil")
41}
42
43func TestCheck(t *testing.T) {
44 c := New(2, 2)
45 tests := []struct {
46 name string
47 p Pixel
48 err error
49 }{
50 {"ok", Pixel{0, 0, 1}, nil},
51 {"ok max", Pixel{1, 1, MaxColor}, nil},
52 {"x negative", Pixel{-1, 0, 1}, ErrOutOfRange},
53 {"y negative", Pixel{0, -1, 1}, ErrOutOfRange},
54 {"x too big", Pixel{2, 0, 1}, ErrOutOfRange},
55 {"y too big", Pixel{0, 2, 1}, ErrOutOfRange},
56 {"blank color", Pixel{0, 0, Blank}, ErrInvalidColor},
57 {"color too big", Pixel{0, 0, MaxColor + 1}, ErrInvalidColor},
58 }
59 for _, tc := range tests {
60 got := c.Check(tc.p)
61 if got != tc.err {
62 t.Errorf("%s: got %v, want %v", tc.name, got, tc.err)
63 }
64 }
65}
66
67func TestRows(t *testing.T) {
68 c := New(3, 3)
69 uassert.Equal(t, "000000000", c.Rows(0, 3))
70 c.Set(Pixel{X: 1, Y: 1, Color: 15})
71 c.Set(Pixel{X: 2, Y: 2, Color: 10})
72 uassert.Equal(t, "000", c.Rows(0, 1))
73 uassert.Equal(t, "0f0", c.Rows(1, 2))
74 uassert.Equal(t, "0f000a", c.Rows(1, 3))
75 uassert.Equal(t, "", c.Rows(2, 2))
76
77 for _, r := range [][2]int{{-1, 1}, {0, 4}, {2, 1}} {
78 func() {
79 defer func() {
80 if rec := recover(); rec == nil {
81 t.Errorf("Rows(%d, %d) did not panic", r[0], r[1])
82 }
83 }()
84 c.Rows(r[0], r[1])
85 }()
86 }
87}
88
89func TestParsePixels(t *testing.T) {
90 px, err := ParsePixels("0,0,1;5,7,15;")
91 uassert.NoError(t, err)
92 uassert.Equal(t, 2, len(px))
93 if px[0] != (Pixel{0, 0, 1}) || px[1] != (Pixel{5, 7, 15}) {
94 t.Errorf("unexpected pixels: %v", px)
95 }
96
97 for _, bad := range []string{"", ";", "1,2", "1,2,3,4", "a,2,3", "1,b,3", "1,2,c", "1,2,0", "1,2,16", "1,2,-1"} {
98 if _, err := ParsePixels(bad); err == nil {
99 t.Errorf("ParsePixels(%q) accepted invalid input", bad)
100 }
101 }
102}
103
104func TestEncodeRoundTrip(t *testing.T) {
105 in := []Pixel{{0, 0, 1}, {999, 999, 15}, {3, 4, 8}}
106 s := Encode(in)
107 uassert.Equal(t, "0,0,1;999,999,15;3,4,8", s)
108 out, err := ParsePixels(s)
109 uassert.NoError(t, err)
110 uassert.Equal(t, len(in), len(out))
111 for i := range in {
112 if in[i] != out[i] {
113 t.Errorf("pixel %d: got %v, want %v", i, out[i], in[i])
114 }
115 }
116}