
A telemetry record begins with a one-byte version followed by a two-byte big-endian counter. A desktop implementation that relies on native alignment can disagree with a phone decoder. The format string fixes byte order and field sizes. Values like 0x0100 expose a byte-order mistake much more clearly than zero.
A fixture for the behaviour
The script runs struct.unpack against three independent inputs. Each call starts from the value shown, so changing one example leaves the other expectations available for comparison. The table records real return values from the example. Quotation marks around text in the table make its boundaries visible.
import struct
def evaluate(value):
return struct.unpack('>BH',bytes.fromhex(value))
for sample in ['01002a', '020100', '00ffff']:
print(evaluate(sample))Inspect the boundary cases
| Input to evaluate | Observed result |
|---|---|
| '01002a' | [1, 42] |
| '020100' | [2, 256] |
| '00ffff' | [0, 65535] |
For '01002a', the observed output is [1, 42]. With '020100', it becomes [2, 256]. If a second implementation produces a different answer, begin with these small inputs. They allow a reviewer to trace the expression without reconstructing the entire application or replaying a production workload.
For '00ffff', the operation returns 2 top-level items. An assertion on that count would miss a change in their contents. Capture the expected sequence or compare membership explicitly if order has no meaning in the product.
Check the calling code
Validate header length and version before parsing the following body. A well-formed three-byte prefix says nothing about a truncated payload.
A practical test can begin with assert evaluate('01002a') == (1, 42). Add the other rows as separate expectations so one failure identifies the input that changed. For a service fed by stored data, repeat the check on one anonymized record from an earlier version of its file or message format.
One more fixture before integrating
Store the binary header fixture as a fixed hex string provided by an independent implementation. An encoder and decoder that share the same mistaken format can otherwise pass a round-trip check while disagreeing with the actual device.
The same Python function was also run with '' / '01002a ' / 'a20010'. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| '' | raises error |
| '01002a ' | [1, 42] |
| 'a20010' | [162, 16] |
Tuples are printed with list-style brackets in the comparison table. The runnable Python assertion preserves the actual tuple return type.
Python’s struct reference · Python standard library
Sources and further reading
Cover photograph: Late 2016 MacBook Pro by Sonicdrewdriver. CC BY-SA 4.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
