From fae11dce2c873463c0bcfaedfb1006ac5ead591a Mon Sep 17 00:00:00 2001 From: Aniruddha Bongale Date: Sun, 22 Nov 2026 13:22:55 +0530 Subject: [PATCH] test(maths): strengthen armstrongNumber tests against mutation (R50-T050) --- Maths/ArmstrongNumber.js | 16 ++ Maths/test/ArmstrongNumber.test.js | 46 +++++ work/R50-T050/check.py | 279 +++++++++++++++++++++++++++++ work/R50-T050/results.json | 118 ++++++++++++ 4 files changed, 459 insertions(+) create mode 100644 work/R50-T050/check.py create mode 100644 work/R50-T050/results.json diff --git a/Maths/ArmstrongNumber.js b/Maths/ArmstrongNumber.js index 8f861d8c15..30cd053923 100644 --- a/Maths/ArmstrongNumber.js +++ b/Maths/ArmstrongNumber.js @@ -6,6 +6,22 @@ * For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370. * An Armstrong number is often called Narcissistic number. * + * Preconditions and Protected Semantic Invariants: + * - Line 24 (`num < 0`): Boundary check strictly protects non-negative integers including 0. + * The smallest valid input is 0 (0^1 = 0, returning true); mutating to `num <= 0` is an off-by-one + * error that incorrectly rejects 0. + * - Line 27 (`** numStr.length`): Dynamic exponentiation strictly depends on the total number of digits. + * Mutating to a fixed power (e.g. `** 3`) incorrectly evaluates higher-order Armstrong numbers such + * as 1634 (1^4 + 6^4 + 3^4 + 4^4 = 1634). + * + * @param {number} num - Integer candidate to test. + * @returns {boolean} True if num is an Armstrong number, false otherwise. + * + * @example + * armstrongNumber(0) // returns true (smallest valid input, 0^1 = 0) + * armstrongNumber(370) // returns true (3^3 + 7^3 + 0^3 = 370) + * armstrongNumber(1634) // returns true (1^4 + 6^4 + 3^4 + 4^4 = 1634) + * armstrongNumber(300) // returns false */ const armstrongNumber = (num) => { diff --git a/Maths/test/ArmstrongNumber.test.js b/Maths/test/ArmstrongNumber.test.js index 01da1631da..416229cc87 100644 --- a/Maths/test/ArmstrongNumber.test.js +++ b/Maths/test/ArmstrongNumber.test.js @@ -8,7 +8,53 @@ describe('ArmstrongNumber', () => { it('should return false for a non-armstrong number', () => { expect(armstrongNumber(300)).toBeFalsy() }) + it('should return false for negative values', () => { expect(armstrongNumber(-2)).toBeFalsy() }) + + describe('Mutation-Resistant Regression Suite (Protected Conditions)', () => { + it('protects boundary condition against off-by-one mutation on smallest valid input 0 (Line 24: num < 0)', () => { + // Smallest valid input: 0 (0^1 = 0) + // Plausible mistake: mutating Line 24 to `num <= 0` incorrectly rejects 0 + expect(armstrongNumber(0)).toBe(true) + }) + + it('protects single-digit Armstrong numbers (0 through 9)', () => { + // Every single-digit non-negative number d has d^1 = d + expect(armstrongNumber(1)).toBe(true) + expect(armstrongNumber(5)).toBe(true) + expect(armstrongNumber(9)).toBe(true) + }) + + it('protects ordinary 3-digit Armstrong numbers (370, 371, 153, 407)', () => { + // 370: 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370 + expect(armstrongNumber(370)).toBe(true) + // 153: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 + expect(armstrongNumber(153)).toBe(true) + // 407: 4^3 + 0^3 + 7^3 = 64 + 0 + 343 = 407 + expect(armstrongNumber(407)).toBe(true) + }) + + it('protects dynamic exponentiation against hardcoded cube mutation (Line 27: ** numStr.length)', () => { + // 4-digit Armstrong number: 1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634 + // Plausible mistake: hardcoding `** 3` passes 371, 370, 153 but fails 1634 (evaluates to 308) + expect(armstrongNumber(1634)).toBe(true) + // 8208 = 8^4 + 2^4 + 0^4 + 8^4 = 4096 + 16 + 0 + 4096 = 8208 + expect(armstrongNumber(8208)).toBe(true) + }) + + it('correctly rejects ordinary non-Armstrong numbers of varying lengths', () => { + expect(armstrongNumber(10)).toBe(false) + expect(armstrongNumber(100)).toBe(false) + expect(armstrongNumber(1633)).toBe(false) + }) + + it('correctly rejects non-number types and negative numbers', () => { + expect(armstrongNumber('370')).toBe(false) + expect(armstrongNumber(null)).toBe(false) + expect(armstrongNumber(undefined)).toBe(false) + expect(armstrongNumber(-153)).toBe(false) + }) + }) }) diff --git a/work/R50-T050/check.py b/work/R50-T050/check.py new file mode 100644 index 0000000000..d22ff844c4 --- /dev/null +++ b/work/R50-T050/check.py @@ -0,0 +1,279 @@ +#!/usr/bin/env python3 +""" +Automated check runner for Task R50-T050: +Strengthen the tests of armstrongNumber (Maths/ArmstrongNumber.js). + +Executes repository native tests, verifies assertions on fixtures +(smallest valid input, ordinary inputs), proves that deliberate one-line +mutants fail the named assertions for semantic reasons while the original +implementation passes, and writes results to work/R50-T050/results.json. +""" + +import json +import os +import re +import subprocess +import sys +from datetime import datetime, timezone + +WORKSPACE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +RESULTS_DIR = os.path.join(WORKSPACE_DIR, "work", "R50-T050") +RESULTS_PATH = os.path.join(RESULTS_DIR, "results.json") + +def get_tool_versions(): + versions = {} + # Python + versions["python"] = sys.version.split()[0] + + # Node + try: + out = subprocess.run(["node", "--version"], capture_output=True, text=True, check=True) + versions["node"] = out.stdout.strip() + except Exception as e: + versions["node"] = str(e) + + # npm + try: + out = subprocess.run(["npm", "--version"], capture_output=True, text=True, check=True) + versions["npm"] = out.stdout.strip() + except Exception as e: + versions["npm"] = str(e) + + # vitest + try: + out = subprocess.run(["npx", "vitest", "--version"], cwd=WORKSPACE_DIR, capture_output=True, text=True, check=True) + versions["vitest"] = out.stdout.strip() + except Exception as e: + versions["vitest"] = str(e) + + return versions + +def run_native_tests(): + cmd = ["npm", "test", "--", "Maths/test/ArmstrongNumber.test.js"] + env = os.environ.copy() + env["PATH"] = f"/opt/homebrew/bin:{env.get('PATH', '')}" + result = subprocess.run(cmd, cwd=WORKSPACE_DIR, capture_output=True, text=True, env=env) + + output = result.stdout + "\n" + result.stderr + + # Parse test count: e.g. "Tests 9 passed (9)" + tests_passed = 0 + match = re.search(r"Tests\s+(\d+)\s+passed", output) + if match: + tests_passed = int(match.group(1)) + + return { + "command": " ".join(cmd), + "exit_code": result.returncode, + "tests_passed": tests_passed, + "output": output + } + +def run_mutation_evaluation(): + node_script = """ + import { armstrongNumber } from './Maths/ArmstrongNumber.js'; + + // Deliberate Mutant 1: Off-by-one boundary comparison (num <= 0 instead of num < 0) + const mutant1_boundary = (num) => { + if (typeof num !== 'number' || num <= 0) return false; + const numStr = num.toString(); + const sum = [...numStr].reduce( + (acc, digit) => acc + parseInt(digit) ** numStr.length, + 0 + ); + return sum === num; + }; + + // Deliberate Mutant 2: Hardcoded cube accumulation (digit ** 3 instead of digit ** numStr.length) + const mutant2_fixed_power = (num) => { + if (typeof num !== 'number' || num < 0) return false; + const numStr = num.toString(); + const sum = [...numStr].reduce( + (acc, digit) => acc + parseInt(digit) ** 3, + 0 + ); + return sum === num; + }; + + const fixtures = [ + { + name: "smallest_valid_input_0", + precondition_source_line: "Line 24 (if (typeof num !== 'number' || num < 0) return false)", + input: 0, + expected: true, + protected_condition: "Boundary check permits 0 as a valid non-negative integer (0^1 = 0)" + }, + { + name: "single_digit_armstrong_5", + precondition_source_line: "Line 24..29 (5^1 = 5)", + input: 5, + expected: true, + protected_condition: "Single-digit integers evaluate to identity" + }, + { + name: "ordinary_armstrong_370", + precondition_source_line: "Line 24..29 (3^3 + 7^3 + 0^3 = 370)", + input: 370, + expected: true, + protected_condition: "3-digit Armstrong number matches digit sum of cubes" + }, + { + name: "ordinary_armstrong_371", + precondition_source_line: "Line 24..29 (3^3 + 7^3 + 1^3 = 371)", + input: 371, + expected: true, + protected_condition: "3-digit Armstrong number matches digit sum of cubes" + }, + { + name: "multi_digit_armstrong_1634", + precondition_source_line: "Line 27 ((acc, digit) => acc + parseInt(digit) ** numStr.length)", + input: 1634, + expected: true, + protected_condition: "Dynamic exponentiation with numStr.length = 4 (1^4 + 6^4 + 3^4 + 4^4 = 1634)" + }, + { + name: "ordinary_non_armstrong_300", + precondition_source_line: "Line 24..29 (3^3 + 0^3 + 0^3 = 27 != 300)", + input: 300, + expected: false, + protected_condition: "Non-Armstrong number rejected" + }, + { + name: "ordinary_non_armstrong_10", + precondition_source_line: "Line 24..29 (1^2 + 0^2 = 1 != 10)", + input: 10, + expected: false, + protected_condition: "2-digit non-Armstrong number rejected" + }, + { + name: "negative_input_minus_2", + precondition_source_line: "Line 24 (num < 0 guard)", + input: -2, + expected: false, + protected_condition: "Negative numbers rejected at boundary guard" + } + ]; + + const originalResults = fixtures.map(f => { + const actual = armstrongNumber(f.input); + return { + name: f.name, + precondition_source_line: f.precondition_source_line, + input: f.input, + expected: f.expected, + actual: actual, + passed: actual === f.expected, + protected_condition: f.protected_condition + }; + }); + + // Evaluate Mutants against targeted assertions + const mutantResults = [ + { + mutant_name: "mutant1_boundary_off_by_one", + description: "Altered Line 24 boundary from `num < 0` to `num <= 0`", + targeted_assertion: "smallest_valid_input_0", + input: 0, + expected: true, + original_output: armstrongNumber(0), + original_passed: armstrongNumber(0) === true, + mutant_output: mutant1_boundary(0), + mutant_failed_as_expected: mutant1_boundary(0) === false, + semantic_reason: "Mutant erroneously rejected smallest valid input 0 due to off-by-one `<=` check" + }, + { + mutant_name: "mutant2_hardcoded_cube_power", + description: "Altered Line 27 accumulation from `** numStr.length` to `** 3`", + targeted_assertion: "multi_digit_armstrong_1634", + input: 1634, + expected: true, + original_output: armstrongNumber(1634), + original_passed: armstrongNumber(1634) === true, + mutant_output: mutant2_fixed_power(1634), + mutant_failed_as_expected: mutant2_fixed_power(1634) === false, + semantic_reason: "Mutant hardcoded cube power yielding 308 instead of 1634, failing 4-digit Armstrong validation" + } + ]; + + console.log(JSON.stringify({ + original_evaluations: originalResults, + mutant_evaluations: mutantResults + })); + """ + env = os.environ.copy() + env["PATH"] = f"/opt/homebrew/bin:{env.get('PATH', '')}" + res = subprocess.run(["node", "--input-type=module", "-e", node_script], + cwd=WORKSPACE_DIR, capture_output=True, text=True, env=env) + if res.returncode != 0: + raise RuntimeError(f"Node execution failed: {res.stderr}") + return json.loads(res.stdout.strip()) + +def main(): + print(f"Running checks for Task R50-T050...") + tool_versions = get_tool_versions() + native_test_result = run_native_tests() + + if native_test_result["exit_code"] != 0: + print(f"Native tests failed with exit code {native_test_result['exit_code']}") + print(native_test_result["output"]) + sys.exit(1) + + print(f"Native tests passed: {native_test_result['tests_passed']} tests.") + + eval_results = run_mutation_evaluation() + + # Check original evaluations + all_orig_passed = True + for item in eval_results["original_evaluations"]: + if not item["passed"]: + print(f"Original assertion failed for {item['name']}: expected {item['expected']}, got {item['actual']}") + all_orig_passed = False + else: + print(f" ✓ {item['name']}: passed (Expected: {item['expected']})") + + if not all_orig_passed: + print("Some original assertions failed.") + sys.exit(1) + + # Check mutant evaluations + all_mutants_caught = True + for m in eval_results["mutant_evaluations"]: + if not (m["original_passed"] and m["mutant_failed_as_expected"]): + print(f"Mutant detection failed for {m['mutant_name']}!") + all_mutants_caught = False + else: + print(f" ✓ {m['mutant_name']}: Caught! Original={m['original_output']}, Mutant={m['mutant_output']} (Semantic reason: {m['semantic_reason']})") + + if not all_mutants_caught: + print("Mutant detection assertions failed.") + sys.exit(1) + + os.makedirs(RESULTS_DIR, exist_ok=True) + summary = { + "task_id": "R50-T050", + "target": "Maths/ArmstrongNumber.js", + "tested_function": "armstrongNumber", + "timestamp": datetime.now(timezone.utc).isoformat(), + "tool_versions": tool_versions, + "native_test": { + "command": native_test_result["command"], + "exit_code": native_test_result["exit_code"], + "tests_passed": native_test_result["tests_passed"] + }, + "original_assertions": eval_results["original_evaluations"], + "mutant_proofs": eval_results["mutant_evaluations"], + "status": "PASSED" + } + + with open(RESULTS_PATH, "w") as f: + json.dump(summary, f, indent=2) + print(f"Results written to {RESULTS_PATH}") + + # Format with prettier to satisfy pre-commit hook + env = os.environ.copy() + env["PATH"] = f"/opt/homebrew/bin:{env.get('PATH', '')}" + subprocess.run(["npx", "prettier", RESULTS_PATH, "--write"], cwd=WORKSPACE_DIR, env=env, check=False) + print(f"Prettier formatting applied to {RESULTS_PATH}") + +if __name__ == "__main__": + main() diff --git a/work/R50-T050/results.json b/work/R50-T050/results.json new file mode 100644 index 0000000000..7d632f2131 --- /dev/null +++ b/work/R50-T050/results.json @@ -0,0 +1,118 @@ +{ + "task_id": "R50-T050", + "target": "Maths/ArmstrongNumber.js", + "tested_function": "armstrongNumber", + "timestamp": "2026-11-22T07:52:23.087791+00:00", + "tool_versions": { + "python": "3.15.2", + "node": "v26.13.1", + "npm": "11.20.0", + "vitest": "vitest/1.2.1 darwin-arm64 node-v26.13.1" + }, + "native_test": { + "command": "npm test -- Maths/test/ArmstrongNumber.test.js", + "exit_code": 0, + "tests_passed": 9 + }, + "original_assertions": [ + { + "name": "smallest_valid_input_0", + "precondition_source_line": "Line 24 (if (typeof num !== 'number' || num < 0) return false)", + "input": 0, + "expected": true, + "actual": true, + "passed": true, + "protected_condition": "Boundary check permits 0 as a valid non-negative integer (0^1 = 0)" + }, + { + "name": "single_digit_armstrong_5", + "precondition_source_line": "Line 24..29 (5^1 = 5)", + "input": 5, + "expected": true, + "actual": true, + "passed": true, + "protected_condition": "Single-digit integers evaluate to identity" + }, + { + "name": "ordinary_armstrong_370", + "precondition_source_line": "Line 24..29 (3^3 + 7^3 + 0^3 = 370)", + "input": 370, + "expected": true, + "actual": true, + "passed": true, + "protected_condition": "3-digit Armstrong number matches digit sum of cubes" + }, + { + "name": "ordinary_armstrong_371", + "precondition_source_line": "Line 24..29 (3^3 + 7^3 + 1^3 = 371)", + "input": 371, + "expected": true, + "actual": true, + "passed": true, + "protected_condition": "3-digit Armstrong number matches digit sum of cubes" + }, + { + "name": "multi_digit_armstrong_1634", + "precondition_source_line": "Line 27 ((acc, digit) => acc + parseInt(digit) ** numStr.length)", + "input": 1634, + "expected": true, + "actual": true, + "passed": true, + "protected_condition": "Dynamic exponentiation with numStr.length = 4 (1^4 + 6^4 + 3^4 + 4^4 = 1634)" + }, + { + "name": "ordinary_non_armstrong_300", + "precondition_source_line": "Line 24..29 (3^3 + 0^3 + 0^3 = 27 != 300)", + "input": 300, + "expected": false, + "actual": false, + "passed": true, + "protected_condition": "Non-Armstrong number rejected" + }, + { + "name": "ordinary_non_armstrong_10", + "precondition_source_line": "Line 24..29 (1^2 + 0^2 = 1 != 10)", + "input": 10, + "expected": false, + "actual": false, + "passed": true, + "protected_condition": "2-digit non-Armstrong number rejected" + }, + { + "name": "negative_input_minus_2", + "precondition_source_line": "Line 24 (num < 0 guard)", + "input": -2, + "expected": false, + "actual": false, + "passed": true, + "protected_condition": "Negative numbers rejected at boundary guard" + } + ], + "mutant_proofs": [ + { + "mutant_name": "mutant1_boundary_off_by_one", + "description": "Altered Line 24 boundary from `num < 0` to `num <= 0`", + "targeted_assertion": "smallest_valid_input_0", + "input": 0, + "expected": true, + "original_output": true, + "original_passed": true, + "mutant_output": false, + "mutant_failed_as_expected": true, + "semantic_reason": "Mutant erroneously rejected smallest valid input 0 due to off-by-one `<=` check" + }, + { + "mutant_name": "mutant2_hardcoded_cube_power", + "description": "Altered Line 27 accumulation from `** numStr.length` to `** 3`", + "targeted_assertion": "multi_digit_armstrong_1634", + "input": 1634, + "expected": true, + "original_output": true, + "original_passed": true, + "mutant_output": false, + "mutant_failed_as_expected": true, + "semantic_reason": "Mutant hardcoded cube power yielding 308 instead of 1634, failing 4-digit Armstrong validation" + } + ], + "status": "PASSED" +}