Testing your code · Coding Class
This topic is not available in the selected language or version. Showing the start of this section.
Apply the chapter in three steps: an exercise, a program project, and a debugging task. Implement first, then consult the hints and completion criteria. Use a separate file or working directory for each task.
1. Exercise · Run tests and filters
Section titled “1. Exercise · Run tests and filters”Run the chapter’s testing.xen, rerun with the doubles filter, then use an unmatched filter and inspect the exit status.
Completion: Two tests pass without a filter, one with doubles; an unmatched filter fails.
Hint: Use xen test testing.xen --filter doubles.
2. Build a program · Build word-count regression tests
Section titled “2. Build a program · Build word-count regression tests”Extract count_words(text: String) -> Int from file reading and add separate tests for an empty string, whitespace only, and one two three.
Completion: Expected counts are 0, 0, 3; all three tests pass without external files.
Hint: Separating the calculation lets tests pass input directly.
3. Debug · Catch an endpoint bug
Section titled “3. Debug · Catch an endpoint bug”Run the failing test below. Keep the expectation 10 and fix the range bug in the function. Add tests with limits 0 and 1.
fn sum_to(limit: Int) -> Int { let mut total = 0; for n in 1..limit { total = total + n; } return total;}test fn includes_limit() { assert(sum_to(4) == 10); }xen test sum_to.xenCompletion: All three tests pass, summing from 1 through limit inclusive.
Hint: A range excludes its endpoint.
Verify and record
Section titled “Verify and record”Use xen check file.xen followed by xen run file.xen for ordinary programs; use xen test file.xen for test-only files. Record the original error, its cause, and the corrected result, then rerun every input in the completion criteria.