-
Notifications
You must be signed in to change notification settings - Fork 39
/
shortencolor_test.go
66 lines (63 loc) · 1.89 KB
/
shortencolor_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
package png2svg
import (
"reflect"
"testing"
)
func TestShortenColor(t *testing.T) {
tests := []struct {
name string
hexColorBytes []byte
colorOptimize bool
expectedOutput []byte
}{
{
name: "No shorten #0c0000 with colorOptimize false",
hexColorBytes: []byte("#0c0000"),
colorOptimize: false,
expectedOutput: []byte("#0c0000"),
},
{
name: "Lossy shorten #0c0000 with colorOptimize true",
hexColorBytes: []byte("#0c0000"),
colorOptimize: true,
expectedOutput: []byte("#000"), // Rounded to nearest single hex digit equivalent
},
{
name: "Lossless shorten #ffffff with colorOptimize true",
hexColorBytes: []byte("#ffffff"),
colorOptimize: true,
expectedOutput: []byte("#fff"), // Lossless compression as each pair is identical
},
{
name: "No shorten #ffffff with colorOptimize false",
hexColorBytes: []byte("#ffffff"),
colorOptimize: false,
expectedOutput: []byte("#fff"), // Should still shorten losslessly
},
{
name: "Lossy shorten #112233 with colorOptimize true",
hexColorBytes: []byte("#112233"),
colorOptimize: true,
expectedOutput: []byte("#123"), // Each pair different, so simplified to nearest single hex equivalent
},
{
name: "No shorten #123456 with colorOptimize false",
hexColorBytes: []byte("#123456"),
colorOptimize: false,
expectedOutput: []byte("#123456"), // No simplification as no pairs are identical
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result []byte
if tt.colorOptimize {
result = shortenColorLossy(tt.hexColorBytes)
} else {
result = shortenColorLossless(tt.hexColorBytes)
}
if !reflect.DeepEqual(result, tt.expectedOutput) {
t.Errorf("shortenColor(%s, %v) = %s, want %s", tt.hexColorBytes, tt.colorOptimize, result, tt.expectedOutput)
}
})
}
}