Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions docs/maintenance/bank-account.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Bank Account: Mutation and Regression Maintenance

## Overview

This document records the mutation and regression study for the `bank-account` practice exercise as part of Task R58-T003. It defines a deliberate, semantic mutant of `BankAccount`, identifies the existing test case that rejects it, reduces the rejecting input to the smallest boundary permitted by the specification, and records the named anchor cases.

---

## Contract Under Examination

The `bank-account` exercise specifies a thread-safe bank account with the following lifecycle and state transitions:
1. **Opening / Closing**: Accounts start closed. Calling `open()` opens the account and initializes its balance to `0`. Re-opening an already open account or closing an already closed/unopened account throws `BankAccountActionInvalidException`.
2. **Deposit**: Money can only be deposited into an open account. The deposited amount must be non-negative (`amount >= 0`). A valid deposit adds the amount to the existing balance (`balance += amount`).
3. **Withdrawal**: Money can only be withdrawn from an open account. Withdrawals must not be negative, must not exceed the current balance, and cannot be made from an empty account.
4. **Balance Query**: `getBalance()` retrieves the current accumulated balance of an open account.

---

## Deliberate Scratch Mutation

**File mutated**: `exercises/practice/bank-account/.meta/src/reference/java/BankAccount.java`
**Location**: `deposit(int amount)` method (line 29)

**Original code**:
```java
synchronized void deposit(int amount) throws BankAccountActionInvalidException {
checkIfClosed();
checkIfValidAmount(amount);

balance += amount;
}
```

**Mutant code** (inverting output balance accumulation):
```java
synchronized void deposit(int amount) throws BankAccountActionInvalidException {
checkIfClosed();
checkIfValidAmount(amount);

balance -= amount; // MUTANT: inverts deposit arithmetic from addition to subtraction
}
```

### Rationale and Nature of Mutation
This is a standard Arithmetic Operator Replacement (AOR) mutation. In real-world software, developers frequently copy-paste code between symmetrical operations (such as `deposit` and `withdraw`) and forget to switch `-=` to `+=`. This is a strict semantic mutation that changes the output state construction; it does not change formatting, comments, or whitespace.

---

## Rejecting Test Case

The existing test suite contains an explicit test case that immediately rejects this mutant:

| Property | Value |
|---|---|
| **Canonical UUID** | `e88d4ec3-c6bf-4752-8e59-5046c44e3ba7` |
| **Description** | `Single deposit` |
| **Test Method** | `singleDeposit()` |
| **Test Class** | `BankAccountTest` |
| **Operations** | `bankAccount.open()`, `bankAccount.deposit(100)` |
| **Named Assertion** | `assertThat(bankAccount.getBalance()).isEqualTo(100);` |
| **Original Reference Result** | `100` (Assertion **PASSED**) |
| **Mutant Result** | `-100` (Assertion **FAILED**: `expected: 100, but was: -100`) |

---

## Input Reduction to Smallest Specification Boundary

The existing test `singleDeposit()` uses an arbitrary deposit amount of `100`. We reduce this rejecting case to the smallest input allowed by the specification:

1. **Input `0`**:
The specification disallows negative amounts (`amount < 0`). While `0` is non-negative and permitted by `checkIfValidAmount`, `0 + 0 = 0` and `0 - 0 = 0`. Because both the correct reference and the mutant produce a balance of `0`, a deposit of `0` cannot distinguish between correct and mutated implementations.
2. **Input `1`**:
The smallest positive integer amount allowed by the specification is `1` (representing 1 unit or 1 cent).

| Input Operation | Expected Balance (Specification) | Mutant Balance | Outcome |
|---|---|---|---|
| `bankAccount.open()` | `0` | `0` | Match |
| `bankAccount.deposit(1)` | `1` (`0 + 1`) | `-1` (`0 - 1`) | **Distinguishes Mutant** |

### Proposed Minimal Test Case (Maintenance Reference)
If an explicit minimal boundary test is desired in future track revisions:
```java
@Test
@DisplayName("Deposit minimum valid amount")
public void depositMinimumValidAmount() throws BankAccountActionInvalidException {
bankAccount.open();
bankAccount.deposit(1);

assertThat(bankAccount.getBalance()).isEqualTo(1);
}
```
*Note: The existing suite already catches this mutant with `singleDeposit(100)`. This proposed minimal case is documented here for maintenance reference; we do not assert that the current maintained implementation is defective.*

---

## Named Cases Anchored for Task R58-T003

The task anchors three sequential test cases from the bank-account test suite:

| UUID | Description | Java Method | Reference Status | Mutant Status |
|---|---|---|---|---|
| `983a1528-4ceb-45e5-8257-8ce01aceb5ed` | Newly opened account has zero balance | `newlyOpenedAccountHasEmptyBalance()` | PASSED | PASSED |
| `e88d4ec3-c6bf-4752-8e59-5046c44e3ba7` | Single deposit | `singleDeposit()` | PASSED | **FAILED** (rejects mutant) |
| `3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d` | Multiple deposits | `multipleDeposits()` | PASSED | FAILED |

---

## Reproduction

Run the automated verification script from the repository root:
```bash
python3 work/R58-T003/check.py
```
Or via the course check harness:
```bash
python3 tools/course_checks/r58-t003.py
```
25 changes: 25 additions & 0 deletions exercises/practice/bank-account/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Hints

## Debugging Account Balance and Deposit Arithmetic

A bank account tracks money by updating an accumulated internal balance. When implementing operations:

- **Account Lifecycle**: Newly opened accounts always start with a balance of `0`. All account operations (`deposit`, `withdraw`, `getBalance`, `close`) require the account to be open first; operating on an unopened or closed account must throw `BankAccountActionInvalidException`.
- **Deposit Arithmetic**: A deposit must add money to the account. When a deposit occurs, the amount is added to the previous balance (`balance = balance + amount`), rather than replacing it or subtracting from it.
- **Input Validation**: Neither deposits nor withdrawals may accept negative amounts. Negative amounts must be rejected by throwing `BankAccountActionInvalidException`.

### Diagnostic Input to Test Your Implementation

You can trace a minimal sequence of operations to verify that your deposit logic correctly accumulates funds:

1. Open a new account: the balance should be `0`.
2. Deposit `1` unit into the account: the balance should now be `1`.

```java
BankAccount account = new BankAccount();
account.open();
account.deposit(1);
int balance = account.getBalance(); // Expected: 1
```

If `balance` returns `-1` instead of `1`, check your arithmetic operator in the deposit method to ensure you are adding the deposited funds rather than subtracting them. If `balance` returns `0` or throws an unexpected exception, verify that the account state was marked as open before the deposit took place.
20 changes: 20 additions & 0 deletions tools/course_checks/r58-t003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python3
"""
Course verification entry point for Task R58-T003 (bank-account).
Delegates to work/R58-T003/check.py.

Usage: python3 tools/course_checks/r58-t003.py
"""

import os
import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[2]
CHECK_SCRIPT = REPO_ROOT / "work" / "R58-T003" / "check.py"

if __name__ == "__main__":
if not CHECK_SCRIPT.is_file():
print(f"Error: check script not found at {CHECK_SCRIPT}", file=sys.stderr)
sys.exit(2)
os.execv(sys.executable, [sys.executable, str(CHECK_SCRIPT)] + sys.argv[1:])
35 changes: 35 additions & 0 deletions work/R58-T003/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Work Directory for Task R58-T003

## Exercise: `bank-account`

This directory contains the verification harness and recorded evidence for Task R58-T003.

### Purpose

The task verifies:
1. Dynamic path resolution from `exercises/practice/bank-account/.meta/config.json`.
2. Clean mapping of canonical test cases between `exercises/practice/bank-account/.meta/tests.toml` and `exercises/practice/bank-account/src/test/java/BankAccountTest.java`.
3. Maintenance of the learner starter stub (`exercises/practice/bank-account/src/main/java/BankAccount.java`), which must remain unsolved (`UnsupportedOperationException`) and unchanged.
4. Correctness of the maintained reference implementation (`exercises/practice/bank-account/.meta/src/reference/java/BankAccount.java`), passing all 17 tests in an isolated temporary environment.
5. Immediate rejection of a deliberate semantic mutant (`balance -= amount;` in `deposit`) by the named test case `singleDeposit` (`assertThat(bankAccount.getBalance()).isEqualTo(100);`).
6. Reduction of the rejecting case to the minimal input permitted by the specification (`deposit(1)` -> expected `1`, mutant `-1`).
7. Guidance and contract explanation provided in `exercises/practice/bank-account/.docs/hints.md`.
8. Complete documentation of the mutation analysis in `docs/maintenance/bank-account.md`.

### Running the Checks

To execute the verification script:
```bash
python3 work/R58-T003/check.py
```

Or via the course check harness:
```bash
python3 tools/course_checks/r58-t003.py
```

### Artifacts

- `check.py`: Automated verification script using subprocess and isolated temp directories.
- `results.json`: JSON output containing tool versions, test counts, named case statuses, and mutant rejection evidence.
- `latest-result.json`: Mirror of the latest check results for automated harness grading.
Loading