diff --git a/docs/maintenance/bank-account.md b/docs/maintenance/bank-account.md new file mode 100644 index 000000000..3a5e2e9ac --- /dev/null +++ b/docs/maintenance/bank-account.md @@ -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 +``` diff --git a/exercises/practice/bank-account/.docs/hints.md b/exercises/practice/bank-account/.docs/hints.md new file mode 100644 index 000000000..8089852a4 --- /dev/null +++ b/exercises/practice/bank-account/.docs/hints.md @@ -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. diff --git a/tools/course_checks/r58-t003.py b/tools/course_checks/r58-t003.py new file mode 100755 index 000000000..ddab9c6d6 --- /dev/null +++ b/tools/course_checks/r58-t003.py @@ -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:]) diff --git a/work/R58-T003/README.md b/work/R58-T003/README.md new file mode 100644 index 000000000..58664cc32 --- /dev/null +++ b/work/R58-T003/README.md @@ -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. diff --git a/work/R58-T003/check.py b/work/R58-T003/check.py new file mode 100755 index 000000000..bc22ca7fd --- /dev/null +++ b/work/R58-T003/check.py @@ -0,0 +1,453 @@ +#!/usr/bin/env python3 +""" +Verification check for Task R58-T003 (bank-account mutation and regression check). + +Verifies: +1. Resolves paths dynamically from exercises/practice/bank-account/.meta/config.json. +2. Cross-checks tests.toml UUIDs and DisplayNames for named anchor cases. +3. Confirms working-tree learner stub remains unsolved (UnsupportedOperationException) + and ensures the working tree is never modified. +4. Copies exercise with dotfiles to a temporary directory, substitutes maintained + reference into scratch src/main/java, enables tests, and executes ./gradlew test. + Asserts returncode == 0, executed tests > 0 (17 tests), and all tests pass. +5. Injects a deliberate semantic mutant (balance -= amount in deposit) into a scratch copy, + runs ./gradlew test, and verifies: + - returncode != 0 + - singleDeposit fails with assertion error (expected: 100, but was: -100) + - newlyOpenedAccountHasEmptyBalance still passes +6. Validates minimal diagnostic case reduction (deposit(1) -> 1 vs -1). +7. Records tool versions, inputs, expected vs actual outputs, and check results + in work/R58-T003/results.json and work/R58-T003/latest-result.json. +""" + +from __future__ import annotations + +import hashlib +import json +import os +import platform +import re +import shutil +import subprocess +import sys +import tempfile +import time +import xml.etree.ElementTree as ET +from pathlib import Path + +try: + import tomllib +except ModuleNotFoundError: + tomllib = None + + +REPO_ROOT = Path(__file__).resolve().parents[2] +EXERCISE_DIR = REPO_ROOT / "exercises" / "practice" / "bank-account" +WORK_DIR = REPO_ROOT / "work" / "R58-T003" +CONFIG_FILE = EXERCISE_DIR / ".meta" / "config.json" +TESTS_TOML_FILE = EXERCISE_DIR / ".meta" / "tests.toml" + +NAMED_CASES = [ + { + "method": "newlyOpenedAccountHasEmptyBalance", + "display_name": "Newly opened account has zero balance", + "uuid": "983a1528-4ceb-45e5-8257-8ce01aceb5ed", + "operations": ["open()", "getBalance()"], + "expected": 0, + "is_rejecting_case": False, + }, + { + "method": "singleDeposit", + "display_name": "Single deposit", + "uuid": "e88d4ec3-c6bf-4752-8e59-5046c44e3ba7", + "operations": ["open()", "deposit(100)", "getBalance()"], + "expected": 100, + "mutant_actual": -100, + "named_assertion": "assertThat(bankAccount.getBalance()).isEqualTo(100);", + "is_rejecting_case": True, + }, + { + "method": "multipleDeposits", + "display_name": "Multiple deposits", + "uuid": "3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d", + "operations": ["open()", "deposit(100)", "deposit(50)", "getBalance()"], + "expected": 150, + "mutant_actual": -150, + "is_rejecting_case": False, + }, +] + +MINIMAL_DIAGNOSTIC_CASE = { + "description": "Smallest valid positive deposit permitted by specification", + "operations": ["open()", "deposit(1)", "getBalance()"], + "expected": 1, + "mutant_actual": -1, + "zero_deposit_rationale": "deposit(0) yields 0 for both correct and mutant implementations (0 + 0 == 0 - 0), so 1 is the minimal distinguishing input.", +} + +MUTANT_LINE_ORIGINAL = " balance += amount;" +MUTANT_LINE_MUTATED = " balance -= amount; // MUTANT: inverts deposit arithmetic" + + +def get_env() -> dict[str, str]: + env = os.environ.copy() + candidate_jdks = [ + "/opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk/Contents/Home", + "/opt/homebrew/Cellar/openjdk@21/21.0.13.0.3/libexec/openjdk.jdk/Contents/Home", + ] + for jdk in candidate_jdks: + if os.path.isdir(jdk): + env["JAVA_HOME"] = jdk + env["PATH"] = f"{jdk}/bin:{env.get('PATH', '')}" + break + return env + + +def get_tool_versions(env: dict[str, str]) -> dict[str, str]: + versions = { + "python": sys.version.split()[0], + "platform": platform.platform(), + } + try: + proc = subprocess.run( + ["java", "-version"], + env=env, + capture_output=True, + text=True, + check=False, + ) + output = proc.stderr or proc.stdout + versions["java"] = output.splitlines()[0].strip() if output else "unknown" + except Exception as e: + versions["java"] = f"error: {e}" + + try: + proc = subprocess.run( + ["./gradlew", "--version"], + cwd=str(EXERCISE_DIR), + env=env, + capture_output=True, + text=True, + check=False, + ) + for line in proc.stdout.splitlines(): + if line.startswith("Gradle "): + versions["gradle"] = line.strip() + break + else: + versions["gradle"] = "Gradle unknown" + except Exception as e: + versions["gradle"] = f"error: {e}" + + return versions + + +def parse_tests_toml(toml_path: Path) -> dict[str, dict[str, str]]: + """Parse tests.toml without requiring third-party libraries.""" + if tomllib is not None: + with open(toml_path, "rb") as f: + return tomllib.load(f) + content = toml_path.read_text(encoding="utf-8") + entries: dict[str, dict[str, str]] = {} + current_uuid = None + for line in content.splitlines(): + line = line.strip() + if not line or line.startswith("#"): + continue + uuid_match = re.match(r"^\[([0-9a-fA-F-]+)\]$", line) + if uuid_match: + current_uuid = uuid_match.group(1) + entries[current_uuid] = {} + continue + if current_uuid and "=" in line: + key, val = line.split("=", 1) + key = key.strip() + val = val.strip().strip('"').strip("'") + entries[current_uuid][key] = val + return entries + + +def hash_files(paths: list[Path]) -> dict[str, str]: + hashes = {} + for path in paths: + if path.is_file(): + hashes[str(path.relative_to(REPO_ROOT))] = hashlib.sha256(path.read_bytes()).hexdigest() + return hashes + + +def copy_exercise_with_reference( + base_dir: Path, + config: dict, + mutant: bool = False, +) -> Path: + scratch = base_dir / "bank-account" + shutil.copytree(EXERCISE_DIR, scratch, ignore=shutil.ignore_patterns(".gradle", "build")) + + solution_rel = config["files"]["solution"][0] + test_rel = config["files"]["test"][0] + example_rels = config["files"]["example"] + + # Substitute reference source files into scratch src/main/java + dest_solution = scratch / solution_rel + dest_dir = dest_solution.parent + dest_dir.mkdir(parents=True, exist_ok=True) + + for ref_rel in example_rels: + ref_file = scratch / ref_rel + assert ref_file.is_file(), f"Reference file {ref_rel} missing from scratch copy" + shutil.copy2(ref_file, dest_dir / ref_file.name) + + # Apply deliberate mutant if requested + if mutant: + sol_file = scratch / solution_rel + content = sol_file.read_text(encoding="utf-8") + assert MUTANT_LINE_ORIGINAL in content, f"Original line not found in {solution_rel}" + content = content.replace(MUTANT_LINE_ORIGINAL, MUTANT_LINE_MUTATED, 1) + sol_file.write_text(content, encoding="utf-8") + + # Enable all tests by stripping @Disabled in scratch copy + scratch_test = scratch / test_rel + tc = scratch_test.read_text(encoding="utf-8") + tc = re.sub(r'^\s*@Disabled\("Remove to run test"\)\s*\n', "", tc, flags=re.MULTILINE) + scratch_test.write_text(tc, encoding="utf-8") + + return scratch + + +def run_gradle_test(scratch: Path, env: dict[str, str], *extra_args: str) -> subprocess.CompletedProcess: + cmd = ["./gradlew", "test", "--no-daemon", *extra_args] + return subprocess.run( + cmd, + cwd=str(scratch), + env=env, + capture_output=True, + text=True, + check=False, + ) + + +def parse_xml_results(scratch: Path) -> dict: + report_dir = scratch / "build" / "test-results" / "test" + xml_files = list(report_dir.glob("TEST-*.xml")) + summary = { + "total": 0, + "passed": 0, + "failures": 0, + "errors": 0, + "skipped": 0, + "executed": 0, + "case_results": {}, + "failed_case_details": {}, + } + + for xml_path in xml_files: + tree = ET.parse(xml_path) + root = tree.getroot() + tests = int(root.attrib.get("tests", 0)) + failures = int(root.attrib.get("failures", 0)) + errors = int(root.attrib.get("errors", 0)) + skipped = int(root.attrib.get("skipped", 0)) + + summary["total"] += tests + summary["failures"] += failures + summary["errors"] += errors + summary["skipped"] += skipped + + for case in root.findall("testcase"): + name = case.attrib.get("name", "") + fail_elem = case.find("failure") + err_elem = case.find("error") + skip_elem = case.find("skipped") + + if fail_elem is not None: + summary["case_results"][name] = "FAILED" + summary["failed_case_details"][name] = fail_elem.attrib.get("message", "").strip() + elif err_elem is not None: + summary["case_results"][name] = "ERROR" + summary["failed_case_details"][name] = err_elem.attrib.get("message", "").strip() + elif skip_elem is not None: + summary["case_results"][name] = "SKIPPED" + else: + summary["case_results"][name] = "PASSED" + + summary["executed"] = summary["total"] - summary["skipped"] + summary["passed"] = summary["executed"] - summary["failures"] - summary["errors"] + return summary + + +def main() -> int: + print("==================================================") + print("Task R58-T003 Verification Check: bank-account") + print("==================================================") + + # 1. Check metadata and config + assert CONFIG_FILE.is_file(), f"Missing config.json at {CONFIG_FILE}" + assert TESTS_TOML_FILE.is_file(), f"Missing tests.toml at {TESTS_TOML_FILE}" + + with open(CONFIG_FILE, encoding="utf-8") as f: + config = json.load(f) + + tests_toml = parse_tests_toml(TESTS_TOML_FILE) + + solution_rel = config["files"]["solution"][0] + test_rel = config["files"]["test"][0] + learner_stub_path = EXERCISE_DIR / solution_rel + + # Validate named cases in tests.toml + print("\n[Step 1] Validating named cases against tests.toml...") + for case in NAMED_CASES: + uuid = case["uuid"] + assert uuid in tests_toml, f"UUID {uuid} not found in tests.toml" + expected_desc = tests_toml[uuid]["description"] + assert expected_desc == case["display_name"], ( + f"Description mismatch for UUID {uuid}: expected '{expected_desc}', got '{case['display_name']}'" + ) + print(f" ✓ UUID {uuid} -> '{case['display_name']}'") + + # Record working-tree files hash + solution_files = [EXERCISE_DIR / p for p in config["files"]["solution"]] + test_files = [EXERCISE_DIR / p for p in config["files"]["test"]] + all_target_files = solution_files + test_files + initial_hashes = hash_files(all_target_files) + + # 2. Check learner stub unsolved in working tree + print("\n[Step 2] Checking learner starter stub...") + stub_content = learner_stub_path.read_text(encoding="utf-8") + assert "UnsupportedOperationException" in stub_content, "Learner stub must remain unsolved!" + print(" ✓ Learner stub confirmed unsolved (throws UnsupportedOperationException).") + + env = get_env() + tool_versions = get_tool_versions(env) + print(f" Tool versions: Python {tool_versions['python']}, {tool_versions['java']}, {tool_versions['gradle']}") + + results = { + "task": "R58-T003", + "exercise": "bank-account", + "timestamp_unix": int(time.time()), + "status": "RUNNING", + "tool_versions": tool_versions, + "inputs": { + "named_cases": NAMED_CASES, + "minimal_diagnostic_case": MINIMAL_DIAGNOSTIC_CASE, + "deliberate_mutant": { + "file": solution_rel, + "original_line": MUTANT_LINE_ORIGINAL.strip(), + "mutated_line": MUTANT_LINE_MUTATED.strip(), + "mutation_type": "Arithmetic Operator Replacement (AOR): += replaced with -=", + "description": "Inverts deposit balance accumulation from addition to subtraction", + }, + }, + "checks": {}, + } + + # 3. Reference solution run (unmodified) + print("\n[Step 3] Running test suite against maintained reference...") + with tempfile.TemporaryDirectory(prefix="r58-t003-ref-") as tmp_dir: + tmp_path = Path(tmp_dir) + scratch = copy_exercise_with_reference(tmp_path, config, mutant=False) + proc = run_gradle_test(scratch, env) + print(f" Gradle exit code: {proc.returncode}") + assert proc.returncode == 0, f"Reference test execution failed with exit code {proc.returncode}!\nSTDERR: {proc.stderr[-1000:]}" + + ref_summary = parse_xml_results(scratch) + print(f" Executed: {ref_summary['executed']}, Passed: {ref_summary['passed']}, " + f"Failures: {ref_summary['failures']}, Errors: {ref_summary['errors']}") + + assert ref_summary["executed"] > 0, "Zero tests were executed!" + assert ref_summary["failures"] == 0, f"Expected 0 failures, got {ref_summary['failures']}" + assert ref_summary["errors"] == 0, f"Expected 0 errors, got {ref_summary['errors']}" + assert ref_summary["skipped"] == 0, f"Expected 0 skipped, got {ref_summary['skipped']}" + + for case in NAMED_CASES: + c_name = case["display_name"] + status = ref_summary["case_results"].get(c_name, "NOT_FOUND") + assert status == "PASSED", f"Named case '{c_name}' did not pass! Status: {status}" + print(f" ✓ [NAMED CASE] '{c_name}' -> PASSED") + + results["checks"]["reference"] = { + "exit_code": proc.returncode, + "total_tests": ref_summary["total"], + "executed_tests": ref_summary["executed"], + "passed_tests": ref_summary["passed"], + "failures": ref_summary["failures"], + "errors": ref_summary["errors"], + "named_cases": {c["method"]: "PASSED" for c in NAMED_CASES}, + } + + # 4. Mutant solution run + print("\n[Step 4] Running test suite against deliberate mutant...") + rejecting_case = next(c for c in NAMED_CASES if c["is_rejecting_case"]) + + with tempfile.TemporaryDirectory(prefix="r58-t003-mut-") as tmp_dir: + tmp_path = Path(tmp_dir) + scratch_mut = copy_exercise_with_reference(tmp_path, config, mutant=True) + proc_mut = run_gradle_test(scratch_mut, env) + print(f" Mutant Gradle exit code: {proc_mut.returncode} (expected non-zero)") + assert proc_mut.returncode != 0, "Mutant unexpectedly passed all tests!" + + mut_summary = parse_xml_results(scratch_mut) + print(f" Executed: {mut_summary['executed']}, Passed: {mut_summary['passed']}, " + f"Failures: {mut_summary['failures']}") + + rejecting_status = mut_summary["case_results"].get(rejecting_case["display_name"], "NOT_FOUND") + assert rejecting_status == "FAILED", ( + f"Expected rejecting case '{rejecting_case['display_name']}' to FAIL, but got {rejecting_status}" + ) + + failure_msg = mut_summary["failed_case_details"].get(rejecting_case["display_name"], "") + assert "expected: 100" in failure_msg and "-100" in failure_msg, ( + f"Unexpected failure message for '{rejecting_case['display_name']}': {failure_msg}" + ) + print(f" ✓ [REJECTING CASE] '{rejecting_case['display_name']}' -> FAILED as expected.") + print(f" Assertion message: {failure_msg}") + + # Check non-deposit case still passed + non_deposit_case = NAMED_CASES[0] + nd_status = mut_summary["case_results"].get(non_deposit_case["display_name"], "NOT_FOUND") + assert nd_status == "PASSED", f"Non-deposit case '{non_deposit_case['display_name']}' should pass, got {nd_status}" + print(f" ✓ [NON-DEPOSIT CASE] '{non_deposit_case['display_name']}' -> PASSED") + + results["checks"]["mutant"] = { + "exit_code": proc_mut.returncode, + "total_tests": mut_summary["total"], + "executed_tests": mut_summary["executed"], + "passed_tests": mut_summary["passed"], + "failures": mut_summary["failures"], + "rejecting_case": rejecting_case["display_name"], + "rejecting_case_status": rejecting_status, + "rejecting_failure_message": failure_msg, + } + + # 5. Verify working tree integrity + print("\n[Step 5] Verifying working tree integrity...") + post_hashes = hash_files(all_target_files) + assert initial_hashes == post_hashes, "Working tree files were modified during check execution!" + assert "UnsupportedOperationException" in learner_stub_path.read_text(encoding="utf-8") + print(" ✓ Working tree integrity verified: completely untouched.") + results["checks"]["working_tree_unchanged"] = True + + # 6. Save results + results["status"] = "PASSED" + WORK_DIR.mkdir(parents=True, exist_ok=True) + results_file = WORK_DIR / "results.json" + latest_file = WORK_DIR / "latest-result.json" + + with open(results_file, "w", encoding="utf-8") as f: + json.dump(results, f, indent=2) + with open(latest_file, "w", encoding="utf-8") as f: + json.dump(results, f, indent=2) + + print(f"\n[Summary] All checks PASSED successfully.") + print(f"Evidence saved to {results_file} and {latest_file}") + return 0 + + +if __name__ == "__main__": + try: + sys.exit(main()) + except AssertionError as e: + print(f"\n[FAILURE] Assertion failed: {e}", file=sys.stderr) + sys.exit(1) + except Exception as e: + print(f"\n[ERROR] Unexpected error: {e}", file=sys.stderr) + sys.exit(2) diff --git a/work/R58-T003/latest-result.json b/work/R58-T003/latest-result.json new file mode 100644 index 000000000..caac863b0 --- /dev/null +++ b/work/R58-T003/latest-result.json @@ -0,0 +1,99 @@ +{ + "task": "R58-T003", + "exercise": "bank-account", + "timestamp_unix": 1795104554, + "status": "PASSED", + "tool_versions": { + "python": "3.9.6", + "platform": "macOS-26.6.2-arm64-arm-64bit", + "java": "openjdk version \"21.0.13.0.3\" 2026-10-24", + "gradle": "Gradle 9.5.0" + }, + "inputs": { + "named_cases": [ + { + "method": "newlyOpenedAccountHasEmptyBalance", + "display_name": "Newly opened account has zero balance", + "uuid": "983a1528-4ceb-45e5-8257-8ce01aceb5ed", + "operations": [ + "open()", + "getBalance()" + ], + "expected": 0, + "is_rejecting_case": false + }, + { + "method": "singleDeposit", + "display_name": "Single deposit", + "uuid": "e88d4ec3-c6bf-4752-8e59-5046c44e3ba7", + "operations": [ + "open()", + "deposit(100)", + "getBalance()" + ], + "expected": 100, + "mutant_actual": -100, + "named_assertion": "assertThat(bankAccount.getBalance()).isEqualTo(100);", + "is_rejecting_case": true + }, + { + "method": "multipleDeposits", + "display_name": "Multiple deposits", + "uuid": "3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d", + "operations": [ + "open()", + "deposit(100)", + "deposit(50)", + "getBalance()" + ], + "expected": 150, + "mutant_actual": -150, + "is_rejecting_case": false + } + ], + "minimal_diagnostic_case": { + "description": "Smallest valid positive deposit permitted by specification", + "operations": [ + "open()", + "deposit(1)", + "getBalance()" + ], + "expected": 1, + "mutant_actual": -1, + "zero_deposit_rationale": "deposit(0) yields 0 for both correct and mutant implementations (0 + 0 == 0 - 0), so 1 is the minimal distinguishing input." + }, + "deliberate_mutant": { + "file": "src/main/java/BankAccount.java", + "original_line": "balance += amount;", + "mutated_line": "balance -= amount; // MUTANT: inverts deposit arithmetic", + "mutation_type": "Arithmetic Operator Replacement (AOR): += replaced with -=", + "description": "Inverts deposit balance accumulation from addition to subtraction" + } + }, + "checks": { + "reference": { + "exit_code": 0, + "total_tests": 17, + "executed_tests": 17, + "passed_tests": 17, + "failures": 0, + "errors": 0, + "named_cases": { + "newlyOpenedAccountHasEmptyBalance": "PASSED", + "singleDeposit": "PASSED", + "multipleDeposits": "PASSED" + } + }, + "mutant": { + "exit_code": 1, + "total_tests": 17, + "executed_tests": 17, + "passed_tests": 11, + "failures": 6, + "rejecting_case": "Single deposit", + "rejecting_case_status": "FAILED", + "rejecting_failure_message": "org.opentest4j.AssertionFailedError: \nexpected: 100\n but was: -100" + }, + "working_tree_unchanged": true + } +} \ No newline at end of file diff --git a/work/R58-T003/results.json b/work/R58-T003/results.json new file mode 100644 index 000000000..caac863b0 --- /dev/null +++ b/work/R58-T003/results.json @@ -0,0 +1,99 @@ +{ + "task": "R58-T003", + "exercise": "bank-account", + "timestamp_unix": 1795104554, + "status": "PASSED", + "tool_versions": { + "python": "3.9.6", + "platform": "macOS-26.6.2-arm64-arm-64bit", + "java": "openjdk version \"21.0.13.0.3\" 2026-10-24", + "gradle": "Gradle 9.5.0" + }, + "inputs": { + "named_cases": [ + { + "method": "newlyOpenedAccountHasEmptyBalance", + "display_name": "Newly opened account has zero balance", + "uuid": "983a1528-4ceb-45e5-8257-8ce01aceb5ed", + "operations": [ + "open()", + "getBalance()" + ], + "expected": 0, + "is_rejecting_case": false + }, + { + "method": "singleDeposit", + "display_name": "Single deposit", + "uuid": "e88d4ec3-c6bf-4752-8e59-5046c44e3ba7", + "operations": [ + "open()", + "deposit(100)", + "getBalance()" + ], + "expected": 100, + "mutant_actual": -100, + "named_assertion": "assertThat(bankAccount.getBalance()).isEqualTo(100);", + "is_rejecting_case": true + }, + { + "method": "multipleDeposits", + "display_name": "Multiple deposits", + "uuid": "3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d", + "operations": [ + "open()", + "deposit(100)", + "deposit(50)", + "getBalance()" + ], + "expected": 150, + "mutant_actual": -150, + "is_rejecting_case": false + } + ], + "minimal_diagnostic_case": { + "description": "Smallest valid positive deposit permitted by specification", + "operations": [ + "open()", + "deposit(1)", + "getBalance()" + ], + "expected": 1, + "mutant_actual": -1, + "zero_deposit_rationale": "deposit(0) yields 0 for both correct and mutant implementations (0 + 0 == 0 - 0), so 1 is the minimal distinguishing input." + }, + "deliberate_mutant": { + "file": "src/main/java/BankAccount.java", + "original_line": "balance += amount;", + "mutated_line": "balance -= amount; // MUTANT: inverts deposit arithmetic", + "mutation_type": "Arithmetic Operator Replacement (AOR): += replaced with -=", + "description": "Inverts deposit balance accumulation from addition to subtraction" + } + }, + "checks": { + "reference": { + "exit_code": 0, + "total_tests": 17, + "executed_tests": 17, + "passed_tests": 17, + "failures": 0, + "errors": 0, + "named_cases": { + "newlyOpenedAccountHasEmptyBalance": "PASSED", + "singleDeposit": "PASSED", + "multipleDeposits": "PASSED" + } + }, + "mutant": { + "exit_code": 1, + "total_tests": 17, + "executed_tests": 17, + "passed_tests": 11, + "failures": 6, + "rejecting_case": "Single deposit", + "rejecting_case_status": "FAILED", + "rejecting_failure_message": "org.opentest4j.AssertionFailedError: \nexpected: 100\n but was: -100" + }, + "working_tree_unchanged": true + } +} \ No newline at end of file