| 1 |
package kitty |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"compress/zlib" |
| 6 |
"image" |
| 7 |
"image/color" |
| 8 |
"image/png" |
| 9 |
"reflect" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestDecoder_Decode(t *testing.T) { |
| 14 |
// Helper function to create compressed data |
| 15 |
compress := func(data []byte) []byte { |
| 16 |
var buf bytes.Buffer |
| 17 |
w := zlib.NewWriter(&buf) |
| 18 |
w.Write(data) |
| 19 |
w.Close() |
| 20 |
return buf.Bytes() |
| 21 |
} |
| 22 |
|
| 23 |
tests := []struct { |
| 24 |
name string |
| 25 |
decoder Decoder |
| 26 |
input []byte |
| 27 |
want image.Image |
| 28 |
wantErr bool |
| 29 |
}{ |
| 30 |
{ |
| 31 |
name: "RGBA format 2x2", |
| 32 |
decoder: Decoder{ |
| 33 |
Format: RGBA, |
| 34 |
Width: 2, |
| 35 |
Height: 2, |
| 36 |
}, |
| 37 |
input: []byte{ |
| 38 |
255, 0, 0, 255, // Red pixel |
| 39 |
0, 0, 255, 255, // Blue pixel |
| 40 |
0, 0, 255, 255, // Blue pixel |
| 41 |
255, 0, 0, 255, // Red pixel |
| 42 |
}, |
| 43 |
want: func() image.Image { |
| 44 |
img := image.NewRGBA(image.Rect(0, 0, 2, 2)) |
| 45 |
img.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255}) |
| 46 |
img.Set(1, 0, color.RGBA{R: 0, G: 0, B: 255, A: 255}) |
| 47 |
img.Set(0, 1, color.RGBA{R: 0, G: 0, B: 255, A: 255}) |
| 48 |
img.Set(1, 1, color.RGBA{R: 255, G: 0, B: 0, A: 255}) |
| 49 |
return img |
| 50 |
}(), |
| 51 |
wantErr: false, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "RGB format 2x2", |
| 55 |
decoder: Decoder{ |
| 56 |
Format: RGB, |
| 57 |
Width: 2, |
| 58 |
Height: 2, |
| 59 |
}, |
| 60 |
input: []byte{ |
| 61 |
255, 0, 0, // Red pixel |
| 62 |
0, 0, 255, // Blue pixel |
| 63 |
0, 0, 255, // Blue pixel |
| 64 |
255, 0, 0, // Red pixel |
| 65 |
}, |
| 66 |
want: func() image.Image { |
| 67 |
img := image.NewRGBA(image.Rect(0, 0, 2, 2)) |
| 68 |
img.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255}) |
| 69 |
img.Set(1, 0, color.RGBA{R: 0, G: 0, B: 255, A: 255}) |
| 70 |
img.Set(0, 1, color.RGBA{R: 0, G: 0, B: 255, A: 255}) |
| 71 |
img.Set(1, 1, color.RGBA{R: 255, G: 0, B: 0, A: 255}) |
| 72 |
return img |
| 73 |
}(), |
| 74 |
wantErr: false, |
| 75 |
}, |
| 76 |
{ |
| 77 |
name: "RGBA with compression", |
| 78 |
decoder: Decoder{ |
| 79 |
Format: RGBA, |
| 80 |
Width: 2, |
| 81 |
Height: 2, |
| 82 |
Decompress: true, |
| 83 |
}, |
| 84 |
input: compress([]byte{ |
| 85 |
255, 0, 0, 255, |
| 86 |
0, 0, 255, 255, |
| 87 |
0, 0, 255, 255, |
| 88 |
255, 0, 0, 255, |
| 89 |
}), |
| 90 |
want: func() image.Image { |
| 91 |
img := image.NewRGBA(image.Rect(0, 0, 2, 2)) |
| 92 |
img.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255}) |
| 93 |
img.Set(1, 0, color.RGBA{R: 0, G: 0, B: 255, A: 255}) |
| 94 |
img.Set(0, 1, color.RGBA{R: 0, G: 0, B: 255, A: 255}) |
| 95 |
img.Set(1, 1, color.RGBA{R: 255, G: 0, B: 0, A: 255}) |
| 96 |
return img |
| 97 |
}(), |
| 98 |
wantErr: false, |
| 99 |
}, |
| 100 |
{ |
| 101 |
name: "PNG format", |
| 102 |
decoder: Decoder{ |
| 103 |
Format: PNG, |
| 104 |
// Width and height are embedded and inferred from the PNG data |
| 105 |
}, |
| 106 |
input: func() []byte { |
| 107 |
img := image.NewRGBA(image.Rect(0, 0, 1, 1)) |
| 108 |
img.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255}) |
| 109 |
var buf bytes.Buffer |
| 110 |
png.Encode(&buf, img) |
| 111 |
return buf.Bytes() |
| 112 |
}(), |
| 113 |
want: func() image.Image { |
| 114 |
img := image.NewRGBA(image.Rect(0, 0, 1, 1)) |
| 115 |
img.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255}) |
| 116 |
return img |
| 117 |
}(), |
| 118 |
wantErr: false, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "invalid format", |
| 122 |
decoder: Decoder{ |
| 123 |
Format: 999, |
| 124 |
Width: 2, |
| 125 |
Height: 2, |
| 126 |
}, |
| 127 |
input: []byte{0, 0, 0}, |
| 128 |
want: nil, |
| 129 |
wantErr: true, |
| 130 |
}, |
| 131 |
{ |
| 132 |
name: "incomplete RGBA data", |
| 133 |
decoder: Decoder{ |
| 134 |
Format: RGBA, |
| 135 |
Width: 2, |
| 136 |
Height: 2, |
| 137 |
}, |
| 138 |
input: []byte{255, 0, 0}, // Incomplete pixel data |
| 139 |
want: nil, |
| 140 |
wantErr: true, |
| 141 |
}, |
| 142 |
{ |
| 143 |
name: "invalid compressed data", |
| 144 |
decoder: Decoder{ |
| 145 |
Format: RGBA, |
| 146 |
Width: 2, |
| 147 |
Height: 2, |
| 148 |
Decompress: true, |
| 149 |
}, |
| 150 |
input: []byte{1, 2, 3}, // Invalid zlib data |
| 151 |
want: nil, |
| 152 |
wantErr: true, |
| 153 |
}, |
| 154 |
{ |
| 155 |
name: "default format (RGBA)", |
| 156 |
decoder: Decoder{ |
| 157 |
Width: 1, |
| 158 |
Height: 1, |
| 159 |
}, |
| 160 |
input: []byte{255, 0, 0, 255}, |
| 161 |
want: func() image.Image { |
| 162 |
img := image.NewRGBA(image.Rect(0, 0, 1, 1)) |
| 163 |
img.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255}) |
| 164 |
return img |
| 165 |
}(), |
| 166 |
wantErr: false, |
| 167 |
}, |
| 168 |
} |
| 169 |
|
| 170 |
for _, tt := range tests { |
| 171 |
t.Run(tt.name, func(t *testing.T) { |
| 172 |
got, err := tt.decoder.Decode(bytes.NewReader(tt.input)) |
| 173 |
|
| 174 |
if (err != nil) != tt.wantErr { |
| 175 |
t.Errorf("Decode() error = %v, wantErr %v", err, tt.wantErr) |
| 176 |
return |
| 177 |
} |
| 178 |
|
| 179 |
if tt.wantErr { |
| 180 |
return |
| 181 |
} |
| 182 |
|
| 183 |
if !reflect.DeepEqual(got, tt.want) { |
| 184 |
t.Errorf("Decode() output mismatch") |
| 185 |
if bounds := got.Bounds(); bounds != tt.want.Bounds() { |
| 186 |
t.Errorf("bounds got %v, want %v", bounds, tt.want.Bounds()) |
| 187 |
} |
| 188 |
|
| 189 |
// Compare pixels |
| 190 |
bounds := got.Bounds() |
| 191 |
for y := bounds.Min.Y; y < bounds.Max.Y; y++ { |
| 192 |
for x := bounds.Min.X; x < bounds.Max.X; x++ { |
| 193 |
gotColor := got.At(x, y) |
| 194 |
wantColor := tt.want.At(x, y) |
| 195 |
if !reflect.DeepEqual(gotColor, wantColor) { |
| 196 |
t.Errorf("pixel at (%d,%d) = %v, want %v", x, y, gotColor, wantColor) |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
}) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func TestDecoder_DecodeEdgeCases(t *testing.T) { |
| 206 |
tests := []struct { |
| 207 |
name string |
| 208 |
decoder Decoder |
| 209 |
input []byte |
| 210 |
wantErr bool |
| 211 |
}{ |
| 212 |
{ |
| 213 |
name: "zero dimensions", |
| 214 |
decoder: Decoder{ |
| 215 |
Format: RGBA, |
| 216 |
Width: 0, |
| 217 |
Height: 0, |
| 218 |
}, |
| 219 |
input: []byte{}, |
| 220 |
wantErr: false, |
| 221 |
}, |
| 222 |
{ |
| 223 |
name: "negative width", |
| 224 |
decoder: Decoder{ |
| 225 |
Format: RGBA, |
| 226 |
Width: -1, |
| 227 |
Height: 1, |
| 228 |
}, |
| 229 |
input: []byte{255, 0, 0, 255}, |
| 230 |
wantErr: false, // The image package handles this gracefully |
| 231 |
}, |
| 232 |
{ |
| 233 |
name: "very large dimensions", |
| 234 |
decoder: Decoder{ |
| 235 |
Format: RGBA, |
| 236 |
Width: 1, |
| 237 |
Height: 1000000, // Very large height |
| 238 |
}, |
| 239 |
input: []byte{255, 0, 0, 255}, // Not enough data |
| 240 |
wantErr: true, |
| 241 |
}, |
| 242 |
} |
| 243 |
|
| 244 |
for _, tt := range tests { |
| 245 |
t.Run(tt.name, func(t *testing.T) { |
| 246 |
_, err := tt.decoder.Decode(bytes.NewReader(tt.input)) |
| 247 |
if (err != nil) != tt.wantErr { |
| 248 |
t.Errorf("Decode() error = %v, wantErr %v", err, tt.wantErr) |
| 249 |
} |
| 250 |
}) |
| 251 |
} |
| 252 |
} |