Skip to content

Commit eab1b5e

Browse files
committed
shiny/iconvg: add a decode + encode round-trip test
Given we promise deterministic encoding, and decoding is also (trivially) deterministic, it's viable to add another test to ensure that decoding and encoding an IconVG file gives us the same bytes back. Since the Encoder.HighResolutionCoordinates option is not a part of the image metadata but needed for this test to work, obtain it from the filename. Also remove some comparisons of adj that can never be false because no value of type uint8 is less than 0. Change-Id: I356ec01c8dd582aaeff14b39bb0272ca93eb1d2a Reviewed-on: https://go-review.googlesource.com/c/exp/+/279295 Run-TryBot: Dmitri Shuralyov <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Nigel Tao <[email protected]> Trust: Nigel Tao <[email protected]> Trust: Dmitri Shuralyov <[email protected]>
1 parent e89b829 commit eab1b5e

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

shiny/iconvg/decode_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"io/ioutil"
1515
"os"
1616
"path/filepath"
17+
"runtime"
1718
"strings"
1819
"testing"
1920
)
@@ -173,6 +174,54 @@ func TestDisassembly(t *testing.T) {
173174
}
174175
}
175176

177+
// The IconVG decoder and encoder are expected to be completely deterministic,
178+
// so check that we get the original bytes after a decode + encode round-trip.
179+
func TestDecodeEncodeRoundTrip(t *testing.T) {
180+
for _, tc := range testdataTestCases {
181+
ivgData, err := ioutil.ReadFile(filepath.FromSlash(tc.filename) + ".ivg")
182+
if err != nil {
183+
t.Errorf("%s: ReadFile: %v", tc.filename, err)
184+
continue
185+
}
186+
var e resolutionPreservingEncoder
187+
e.HighResolutionCoordinates = strings.HasSuffix(tc.filename, ".hires")
188+
if err := Decode(&e, ivgData, nil); err != nil {
189+
t.Errorf("%s: Decode: %v", tc.filename, err)
190+
continue
191+
}
192+
got, err := e.Bytes()
193+
if err != nil {
194+
t.Errorf("%s: Encoder.Bytes: %v", tc.filename, err)
195+
continue
196+
}
197+
if want := ivgData; !bytes.Equal(got, want) {
198+
t.Errorf("%s:\ngot %d bytes (on GOOS=%s GOARCH=%s, using compiler %q):\n% x\nwant %d bytes:\n% x",
199+
tc.filename, len(got), runtime.GOOS, runtime.GOARCH, runtime.Compiler, got, len(want), want)
200+
gotDisasm, err1 := disassemble(got)
201+
wantDisasm, err2 := disassemble(want)
202+
if err1 == nil && err2 == nil {
203+
diffLines(t, string(gotDisasm), string(wantDisasm))
204+
}
205+
}
206+
}
207+
}
208+
209+
// resolutionPreservingEncoder is an Encoder
210+
// whose Reset method keeps prior resolution.
211+
type resolutionPreservingEncoder struct {
212+
Encoder
213+
}
214+
215+
// Reset resets the Encoder for the given Metadata.
216+
//
217+
// Unlike Encoder.Reset, it leaves the value
218+
// of e.HighResolutionCoordinates unmodified.
219+
func (e *resolutionPreservingEncoder) Reset(m Metadata) {
220+
orig := e.HighResolutionCoordinates
221+
e.Encoder.Reset(m)
222+
e.HighResolutionCoordinates = orig
223+
}
224+
176225
func TestDecodeAndRasterize(t *testing.T) {
177226
for _, tc := range testdataTestCases {
178227
ivgData, err := ioutil.ReadFile(filepath.FromSlash(tc.filename) + ".ivg")

shiny/iconvg/encode.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (e *Encoder) SetCReg(adj uint8, incr bool, c Color) {
233233
if e.err != nil {
234234
return
235235
}
236-
if adj < 0 || 6 < adj {
236+
if adj > 6 {
237237
e.err = errInvalidSelectorAdjustment
238238
return
239239
}
@@ -272,7 +272,7 @@ func (e *Encoder) SetNReg(adj uint8, incr bool, f float32) {
272272
if e.err != nil {
273273
return
274274
}
275-
if adj < 0 || 6 < adj {
275+
if adj > 6 {
276276
e.err = errInvalidSelectorAdjustment
277277
return
278278
}
@@ -440,7 +440,7 @@ func (e *Encoder) StartPath(adj uint8, x, y float32) {
440440
if e.err != nil {
441441
return
442442
}
443-
if adj < 0 || 6 < adj {
443+
if adj > 6 {
444444
e.err = errInvalidSelectorAdjustment
445445
return
446446
}

shiny/iconvg/encode_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ func testEncode(t *testing.T, e *Encoder, wantFilename string) {
4646
// to non-determinism in floating-point math, the encoder needs to be fixed.
4747
//
4848
// See golang.org/issue/43219#issuecomment-748531069.
49-
t.Fatalf("\ngot %d bytes (on GOOS=%s GOARCH=%s, using compiler %q):\n% x\nwant %d bytes:\n% x",
49+
t.Errorf("\ngot %d bytes (on GOOS=%s GOARCH=%s, using compiler %q):\n% x\nwant %d bytes:\n% x",
5050
len(got), runtime.GOOS, runtime.GOARCH, runtime.Compiler, got, len(want), want)
51+
gotDisasm, err1 := disassemble(got)
52+
wantDisasm, err2 := disassemble(want)
53+
if err1 == nil && err2 == nil {
54+
diffLines(t, string(gotDisasm), string(wantDisasm))
55+
}
5156
}
5257
}
5358

0 commit comments

Comments
 (0)