backend_transpiled_test.go (1819B)
1 //go:build transpiled 2 3 package webp 4 5 import ( 6 "bytes" 7 "testing" 8 9 transpiled "git.sr.ht/~jackmordaunt/go-libwebp/v2/lib/transpiled/webp" 10 ) 11 12 // TestTranspiledMatchesGolden pins the transpiled backend to the committed 13 // golden bytes; see the note in the wasm equivalent for why the comparison is 14 // against the golden rather than against the selected backend. 15 func TestTranspiledMatchesGolden(t *testing.T) { 16 var got bytes.Buffer 17 if err := transpiled.EncodeImpl(&got, goldenNRGBA(t), 1.0); err != nil { 18 t.Fatalf("encoding webp via transpiled: %v", err) 19 } 20 if !bytes.Equal(got.Bytes(), goldenOut) { 21 t.Errorf("transpiled output differs from golden: %d vs %d bytes", got.Len(), len(goldenOut)) 22 } 23 } 24 25 func TestTranspiledDecodesCurrentGolden(t *testing.T) { 26 got, err := transpiled.DecodeImpl(goldenOut) 27 if err != nil { 28 t.Fatalf("decoding golden: %v", err) 29 } 30 want := goldenNRGBA(t) 31 if got.Bounds() != want.Bounds() { 32 t.Fatalf("bounds: got %v want %v", got.Bounds(), want.Bounds()) 33 } 34 b := want.Bounds() 35 for y := b.Min.Y; y < b.Max.Y; y++ { 36 for x := b.Min.X; x < b.Max.X; x++ { 37 if !colorEqual(got.At(x, y), want.At(x, y)) { 38 t.Fatalf("pixel (%d,%d): got %v want %v", x, y, got.At(x, y), want.At(x, y)) 39 } 40 } 41 } 42 } 43 44 func TestTranspiledDecodesLossy(t *testing.T) { 45 assertLossyDecode(t, transpiled.EncodeImpl, transpiled.DecodeImpl) 46 } 47 48 func FuzzEncodeTranspiled(f *testing.F) { 49 addEncodeSeeds(f) 50 f.Fuzz(func(t *testing.T, x1, y1 uint16, seed int64, quality float32, lossless bool) { 51 fuzzEncode(t, x1, y1, seed, quality, lossless, encodeWith(transpiled.EncodeImpl), 52 decodeReaderWith(transpiled.DecodeImpl)) 53 }) 54 } 55 56 func BenchmarkDecodeTranspiled(b *testing.B) { 57 for ii := 0; ii < b.N; ii++ { 58 if _, err := transpiled.DecodeImpl(goldenOut); err != nil { 59 b.Fatalf("error: %v", err) 60 } 61 } 62 }