backend_wazero_test.go (1834B)
1 //go:build !transpiled && !nowasm 2 3 // This file is named for wazero rather than for wasm on purpose: wasm is a 4 // valid GOARCH, so a _wasm.go suffix would apply an implicit GOARCH=wasm 5 // build constraint and silently drop the backend from every ordinary build. 6 7 package webp 8 9 import ( 10 "bytes" 11 "testing" 12 13 wasm "git.sr.ht/~jackmordaunt/go-libwebp/v2/lib/wasm/webp" 14 ) 15 16 // TestWasmMatchesGolden pins the wasm backend to the committed golden bytes. 17 // 18 // It deliberately does not compare against whatever Encode selected: the 19 // dynamic backend binds the host's libwebp, which may be a different version 20 // and legitimately encodes differently. The wasm module is built from the 21 // vendored c-lib source, so it must match exactly. 22 func TestWasmMatchesGolden(t *testing.T) { 23 var got bytes.Buffer 24 if err := wasm.EncodeImpl(&got, goldenNRGBA(t), 1.0); err != nil { 25 t.Fatalf("encoding webp via wasm: %v", err) 26 } 27 if !bytes.Equal(got.Bytes(), goldenOut) { 28 t.Errorf("wasm output differs from golden: %d vs %d bytes", got.Len(), len(goldenOut)) 29 } 30 } 31 32 func TestWasmRoundTrip(t *testing.T) { 33 m := goldenNRGBA(t) 34 var buf bytes.Buffer 35 if err := wasm.EncodeImpl(&buf, m, 1.0); err != nil { 36 t.Fatalf("encoding webp via wasm: %v", err) 37 } 38 assertOutput(t, m, &buf, decodeReaderWith(wasm.DecodeImpl)) 39 } 40 41 func TestWasmDecodesLossy(t *testing.T) { 42 assertLossyDecode(t, wasm.EncodeImpl, wasm.DecodeImpl) 43 } 44 45 func FuzzEncodeWasm(f *testing.F) { 46 addEncodeSeeds(f) 47 f.Fuzz(func(t *testing.T, x1, y1 uint16, seed int64, quality float32, lossless bool) { 48 fuzzEncode(t, x1, y1, seed, quality, lossless, encodeWith(wasm.EncodeImpl), 49 decodeReaderWith(wasm.DecodeImpl)) 50 }) 51 } 52 53 func BenchmarkDecodeWasm(b *testing.B) { 54 for ii := 0; ii < b.N; ii++ { 55 if _, err := wasm.DecodeImpl(goldenOut); err != nil { 56 b.Fatalf("error: %v", err) 57 } 58 } 59 }