Reading Sample Inputs and Outputs
A sample reveals how a program represents data, chooses its boundaries, and formats its result. Interpret it before generalizing.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 2 min
In this article4 chapters
Locate the input boundary
For an input consisting of a header 3 followed by three temperatures, decide whether the header is data or a count. A mean computed over all four numbers answers a different problem. Writing the parsed list in a hand trace lets you compare the actual records with the intended ones before touching the calculation.
Vary a single assumption
If the sample temperatures are 10, 20, and 30 and the displayed answer is 20, that sample cannot tell you how noninteger means are rounded. A second example using 10 and 11 distinguishes a result of 10.5 from several integer conventions. Treat the expected formatting as part of the function contract or command's output specification.[1][2]
Read the sample as a small specification
Suppose a fixture shows a first line containing 3 and the next three lines containing 2, 4, and 8. The correct output is 14. The heading or surrounding specification must say whether the first number tells the reader how many records follow. Summing every numeric line would produce seventeen. A staged parsing trace can keep the count separate from the parsed data.
Also inspect the printed format. If the sample's output is 14.00, the task may require two decimal places even for whole amounts. If it shows two values on one line, their order is part of the representation. When using a sample as a test, compare its logical values or exact bytes according to what a consumer relies on. A sample may also omit final newline information when presented visually.
Add a sample that tells the two rules apart
The first example cannot distinguish all totals that happen to agree. Suppose a program must compute the mean of its data. On values 2, 4, and 6, an integer implementation and a fractional implementation can both display four. Add a run with 2 and 3, whose mean is 2.5, and decide its spelling. Use a well-defined output comparison that preserves any meaningful whitespace and decimal places.
When the whole input cannot be accepted, an example can document where the error appears. Give too few records for the declared count and check that the result identifies the incomplete input. Review empty data independently. An absent count and an explicit count of zero describe different inputs.