-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvgpath_test.go
More file actions
152 lines (138 loc) · 3.77 KB
/
svgpath_test.go
File metadata and controls
152 lines (138 loc) · 3.77 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package svgpath
import (
"bytes"
"encoding/base64"
"maps"
"slices"
"strings"
"testing"
"golang.org/x/tools/txtar"
)
type compressTest struct {
name string
path string
data string
}
var compressTests = []compressTest{
{"M", "M 100 100", "\x02\xc0\x9a\f\xc0\x9a\f"},
{"m", "m 100 100", "\x01\xc0\x9a\f\xc0\x9a\f"},
{"Z", "Z", "\x04"},
{"z", "z", "\x03"},
{"L", "L 100 100", "\x06\xc0\x9a\f\xc0\x9a\f"},
{"l", "l 100 100", "\x05\xc0\x9a\f\xc0\x9a\f"},
{"H", "H 100", "\b\xc0\x9a\f"},
{"h", "h 100", "\a\xc0\x9a\f"},
{"V", "V 100", "\n\xc0\x9a\f"},
{"v", "v 100", "\t\xc0\x9a\f"},
{"C", "C 100 100 250 100 250 200", "\f\xc0\x9a\f\xc0\x9a\f\xa0\xc2\x1e\xc0\x9a\f\xa0\xc2\x1e\x80\xb5\x18"},
{"c", "c 100 100 250 100 250 200", "\v\xc0\x9a\f\xc0\x9a\f\xa0\xc2\x1e\xc0\x9a\f\xa0\xc2\x1e\x80\xb5\x18"},
{"S", "S 400 300 400 200", "\x0e\x80\xea0\xc0\xcf$\x80\xea0\x80\xb5\x18"},
{"s", "s 400 300 400 200", "\x0d\x80\xea0\xc0\xcf$\x80\xea0\x80\xb5\x18"},
{"Q", "Q 400 50 600 300", "\x10\x80\xea0\xa0\x8d\x06\x80\x9fI\xc0\xcf$"},
{"q", "q 400 50 600 300", "\x0f\x80\xea0\xa0\x8d\x06\x80\x9fI\xc0\xcf$"},
{"T", "T 1000 300", "\x12\x80\x89z\xc0\xcf$"},
{"t", "t 1000 300", "\x11\x80\x89z\xc0\xcf$"},
{"A", "A 25 100 -30 0 1 50 -25", "\x14\u0406\x03\xc0\x9a\f\xdf\xd4\x03\x00\xd0\x0f\xa0\x8d\x06\u03c6\x03"},
{"a", "a 25 100 -30 0 1 50 -25", "\x13\u0406\x03\xc0\x9a\f\xdf\xd4\x03\x00\xd0\x0f\xa0\x8d\x06\u03c6\x03"},
{"M/Overflow", "M 100 100 100 100", "\x02\xc0\x9a\f\xc0\x9a\f\x06\xc0\x9a\f\xc0\x9a\f"},
{"m/Overflow", "m 100 100 100 100", "\x01\xc0\x9a\f\xc0\x9a\f\x05\xc0\x9a\f\xc0\x9a\f"},
}
func TestCompress(t *testing.T) {
for _, c := range compressTests {
t.Run(c.name, func(t *testing.T) {
got, err := Compress(c.path)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, []byte(c.data)) {
t.Errorf("got %q, want: %q", got, c.data)
}
})
}
}
func TestDecompress(t *testing.T) {
for _, c := range compressTests {
t.Run(c.name, func(t *testing.T) {
got, err := Decompress(string(c.data))
if err != nil {
t.Fatal(err)
}
if got != c.path {
t.Errorf("got %q, want: %q", got, c.path)
}
})
}
}
var compressBenchmarks []compressTest
func init() {
ar, err := txtar.ParseFile("testdata/benchmark.txtar")
if err != nil {
return
}
cases := make(map[string]compressTest, len(ar.Files))
for _, f := range ar.Files {
name, kind, found := strings.Cut(f.Name, "/")
if !found {
continue
}
c := cases[name]
c.name = name
switch kind {
case "path":
c.path = string(f.Data)
case "data":
data := bytes.TrimSpace(f.Data)
buf := make([]byte, base64.RawStdEncoding.DecodedLen(len(data)))
_, err := base64.RawStdEncoding.Decode(buf, data)
if err == nil {
c.data = string(buf)
}
}
cases[name] = c
}
for _, name := range slices.Sorted(maps.Keys(cases)) {
compressBenchmarks = append(compressBenchmarks, cases[name])
}
}
func BenchmarkCompress(b *testing.B) {
var cases []compressTest
cases = append(cases, compressTests...)
cases = append(cases, compressBenchmarks...)
for _, c := range cases {
b.Run(c.name, func(b *testing.B) {
b.SetBytes(int64(len(c.path)))
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
got, err := Compress(c.path)
if err != nil {
b.Fatal(err)
}
if got == nil {
b.Fatal("expected output")
}
}
})
}
}
func BenchmarkDecompress(b *testing.B) {
var cases []compressTest
cases = append(cases, compressTests...)
cases = append(cases, compressBenchmarks...)
for _, c := range cases {
b.Run(c.name, func(b *testing.B) {
b.SetBytes(int64(len(c.data)))
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
got, err := Decompress(c.data)
if err != nil {
b.Fatal(err)
}
if got == "" {
b.Fatal("expected output")
}
}
})
}
}