Round-Trip Tests for Encoders and Decoders

A round-trip test encodes a value and decodes the result to verify that the intended information survives.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Name the preserved value
  2. Add an independently known example
  3. Test the direction that actually defines the guarantee
  4. Add examples the two halves cannot agree on by accident

Name the preserved value

For a reversible UTF-8 round trip on valid text, decoding its encoded bytes recovers the string. A lossy display formatter can intentionally discard precision. State what equality means across the representation boundary.

Add an independently known example

An encoder and decoder could agree on an incorrect format. Include a known byte spelling where external compatibility matters. For example, NFC é encodes to C3 A9 in UTF-8. Strings, bytes, and text encoding explains the underlying distinction.[1]

Test the direction that actually defines the guarantee

A serializer can round-trip an internal object through encode and decode even if it canonicalizes whitespace. Decoding a hand-written file and then writing it may produce changed bytes. State whether preservation concerns parsed values or the original representation. See lossless record round trips.

Internal value {n:2} → serialize → parse → same value
Source bytes with extra spaces → parse → serialize → possibly different bytes
A format fixture can verify interoperability independently

Add examples the two halves cannot agree on by accident

A writer and reader can share an incorrect field order and pass their own round trip. Check an independently known payload or reader. Include empty text, delimiters, and the largest allowed numeric value.

References

  1. Hypothesis Contributors, Introduction to Hypothesis ↩