Chapter 12 Exercises — Debugging and Testing
These exercises extend Chapter 12. Do the Independent Challenge on a separate Git branch so your guided project stays identical to the reference checkpoint:
First commit and tag the guided work using the workbook's Development Checkpoint. Confirm git status --short prints nothing, then create the branch:
git switch -c challenge-12
Commit your challenge work on that branch as you like. Before you start Chapter 13, return to the guided project:
Uncommitted edits can follow you across branches. Save them before switching; if nothing changed, skip add and commit. The status command should print nothing:
git add -A
git commit -m "Challenge 12: Save practice work"
git status --short
git switch main
Hints get progressively more specific. Try the challenge for a while before you open Hint 1, and try again after each hint. Full solutions and the extra exercise answers are in the Chapter 12 Answer Key.
Challenge 1 — Clamp the modifier as well as the quantity
Goal: make DiceRoller.roll keep the modifier inside DiceRoller.modifierRange, the same way it already keeps the quantity inside DiceRoller.quantityRange.
Requirements:
- Inside
roll, compute a clamped modifier withminandmax, usingSelf.modifierRange. - Pass the clamped modifier, not the raw one, to
RollResult. - Add one new test function to
DiceRollerTests.swiftthat checks the modifier clamp. Give it a name that states the rule. - Do not change the quantity clamp or any existing test.
Acceptance criteria:
- A modifier of 50 produces a
RollResultwhosemodifieris 20. - A modifier of -50 produces a
RollResultwhosemodifieris -20. - A modifier of 7 produces a
RollResultwhosemodifieris 7. - ⌘U passes every test, including the new one.
- The project builds with 0 errors and 0 warnings.
- The test fails if you remove the clamp. Prove this to yourself by removing the clamp line for a moment, running ⌘U, and restoring it.
Hint 1
The quantity clamp is one line near the top of roll. Read it from the inside out, then ask which name the modifier needs in place of quantity and in place of Self.quantityRange.
Hint 2
You need one new let constant, placed next to the quantity clamp. The RollResult initializer on the last line should receive that new constant instead of the parameter.
Hint 3
The new test follows the pattern of quantityIsClampedToValidRange. Create a roller with a fake random source, call roll with three modifiers (one above the range, one below it, and one inside it), and check the modifier property of the result with #expect. Writing the expected values as 20 and -20 is clearer to read; using DiceRoller.modifierRange.upperBound and .lowerBound makes the test follow the rule if the range changes. Either choice is acceptable.
Extra practice
These exercises have no hints. Work each one on paper before you run anything. Check your answers in the Answer Key.
Exercise A — Predict the output (tracing)
Predict the output of the code below, then run it in a test or a #Playground block if you want to check. Write down the value of values, subtotal, and total before you run anything.
let roller = DiceRoller(randomValue: { range in range.upperBound })
let roll = roller.roll(.d20, quantity: 0, modifier: -20)
Then answer: if the quantity had been 4 instead of 0, what would values be, and what would total be?
Exercise B — Spot the test that cannot fail
A teammate, or an AI assistant, wrote the test below. Read it and answer two questions: under what circumstances, if any, would this test fail, and what rule does it actually check?
@Test func rollReturnsValues() {
let roller = DiceRoller()
let roll = roller.roll(.d6, quantity: 2, modifier: 0)
#expect(roll.values.count == roll.values.count)
}
Then rewrite the #expect line so that it checks a rule from the course. Which rule did you choose, and why would your version fail if the roller stopped producing a value for each die?
Exercise C — Boundary check on the die sizes
Write a test function named dieSidesMatchRawValues in DiceRollerTests.swift (or in a new file in the test target) that checks that every case of DieType has sides equal to its raw value. Use @Test(arguments:) with DieType.allCases, the same way realRandomValuesStayInRange does. Then predict: does this test catch a change to the sides property that returns rawValue + 1? Run the test with that change made temporarily, and check your prediction.
Exercise D — Breakpoint investigation
Set a breakpoint on the first line inside HistoryStore.save(_:) in Persistence/HistoryStore.swift. Roll the dice six times in the app, and then check the value of history.count in the Variables view each time the breakpoint is hit. Before you press continue, predict the number that save receives after the sixth roll. Then explain what would have to happen for the array to reach 51 entries, and identify the line in ContentView that prevents that.