From cb5901fc28b2a89fb041e6c3f6c75d4fa15e6699 Mon Sep 17 00:00:00 2001 From: aniket Date: Fri, 13 Nov 2026 18:43:09 +0530 Subject: [PATCH] fix(maths): add reference comparison regression tests for exponentialFunction --- Maths/ExponentialFunction.js | 13 +++-- Maths/test/ExponentialFunction.test.js | 60 +++++++++++++++++++- work/R50-T033/WORK_LOG.md | 78 ++++++++++++++++++++++++++ work/R50-T033/check.py | 59 +++++++++++++++++++ 4 files changed, 204 insertions(+), 6 deletions(-) create mode 100644 work/R50-T033/WORK_LOG.md create mode 100755 work/R50-T033/check.py diff --git a/Maths/ExponentialFunction.js b/Maths/ExponentialFunction.js index 11da977575..e82f7e9b34 100644 --- a/Maths/ExponentialFunction.js +++ b/Maths/ExponentialFunction.js @@ -1,9 +1,14 @@ /** * @function exponentialFunction - * @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n - * @param {Integer} power - * @param {Integer} order - 1 - * @returns exponentialFunction(2,20) = 7.3890560989301735 + * @description Calculates the n-th partial sum (or n+1 th order Taylor series approximation) + * of the exponential function e^power using Taylor expansion up to degree n-1. + * For example, for representative input power = 2 and n = 2: + * terms are (2^0 / 0!) + (2^1 / 1!) = 1 + 2 = 3. + * + * @param {number} power - The exponent x in e^x. + * @param {number} n - The order / number of terms to sum (n >= 0). + * @returns {number} The calculated approximation of e^power. + * @throws {TypeError} When power or n is NaN or n < 0. * @url https://en.wikipedia.org/wiki/Exponential_function */ function exponentialFunction(power, n) { diff --git a/Maths/test/ExponentialFunction.test.js b/Maths/test/ExponentialFunction.test.js index d1eed4895f..f7a69ccd67 100644 --- a/Maths/test/ExponentialFunction.test.js +++ b/Maths/test/ExponentialFunction.test.js @@ -1,16 +1,72 @@ import { exponentialFunction } from '../ExponentialFunction' describe('Tests for exponential function', () => { + // Preconditions established in Maths/ExponentialFunction.js: + // Line 18: throws TypeError when power or n is NaN or n < 0. + // Line 21: returns 1 when n === 0. + // Lines 24-27: accumulates output += power ** i / fac for i from 0 to n-1. + it('should be a function', () => { expect(typeof exponentialFunction).toEqual('function') }) it('should throw error for invalid input', () => { - expect(() => exponentialFunction(2, -34)).toThrow() + // Precondition: Maths/ExponentialFunction.js line 18 + expect(() => exponentialFunction(2, -34)).toThrow(TypeError) + expect(() => exponentialFunction(NaN, 5)).toThrow(TypeError) + expect(() => exponentialFunction(2, NaN)).toThrow(TypeError) }) - it('should return the exponential function of power of 5 and order of 21', () => { + it('should return the exponential function of power of 5 and order of 20', () => { + // Precondition: Maths/ExponentialFunction.js lines 24-27 const ex = exponentialFunction(5, 20) expect(ex).toBe(148.4131078683383) }) + + describe('Independent Reference Comparison (20+ small valid inputs)', () => { + // Smallest valid input: power=0, n=0 + it('handles smallest valid input (0, 0)', () => { + // Precondition: Maths/ExponentialFunction.js line 21 (n === 0 returns 1) + expect(exponentialFunction(0, 0)).toBe(1) + }) + + // Ordinary input: power=2, n=2 (representative result: 1 + 2 = 3) + it('calculates representative ordinary input (2, 2)', () => { + // Precondition: Maths/ExponentialFunction.js lines 24-27 (2^0/0! + 2^1/1! = 3) + expect(exponentialFunction(2, 2)).toBe(3) + }) + + // Enumerated test table of 22 small valid inputs compared against independent calculation + const testCases = [ + { power: 0, n: 1, expected: 1 }, + { power: 0, n: 2, expected: 1 }, + { power: 0, n: 5, expected: 1 }, + { power: 1, n: 0, expected: 1 }, + { power: 1, n: 1, expected: 1 }, + { power: 1, n: 2, expected: 2 }, + { power: 1, n: 3, expected: 2.5 }, + { power: 1, n: 4, expected: 2.6666666666666665 }, + { power: 1, n: 5, expected: 2.7083333333333335 }, + { power: 2, n: 0, expected: 1 }, + { power: 2, n: 1, expected: 1 }, + { power: 2, n: 3, expected: 5 }, + { power: -1, n: 0, expected: 1 }, + { power: -1, n: 1, expected: 1 }, + { power: -1, n: 2, expected: 0 }, + { power: -1, n: 3, expected: 0.5 }, + { power: -1, n: 4, expected: 0.33333333333333337 }, + { power: 0.5, n: 0, expected: 1 }, + { power: 0.5, n: 1, expected: 1 }, + { power: 0.5, n: 2, expected: 1.5 }, + { power: 0.5, n: 3, expected: 1.625 }, + { power: 2, n: 20, expected: 7.3890560989301735 } + ] + + testCases.forEach(({ power, n, expected }) => { + it(`evaluates exponentialFunction(${power}, ${n}) === ${expected}`, () => { + // Precondition: Maths/ExponentialFunction.js line 21 or lines 24-27 + expect(exponentialFunction(power, n)).toBeCloseTo(expected, 14) + }) + }) + }) }) diff --git a/work/R50-T033/WORK_LOG.md b/work/R50-T033/WORK_LOG.md new file mode 100644 index 0000000000..bc84507ba0 --- /dev/null +++ b/work/R50-T033/WORK_LOG.md @@ -0,0 +1,78 @@ +# WORK_LOG.md: Task R50-T033 (Maths/ExponentialFunction.js) + +## Task Information + +- **Student**: ANIKET KUMAR NISHAD +- **Task**: R50-T033 ยท 3 of 10 +- **Repository**: https://github.com/S-AI-Projects/TheAlgorithms-JavaScript +- **Assigned working branch**: student/e26b07f0789 +- **Target File**: `Maths/ExponentialFunction.js` (`exponentialFunction` near line 9) + +--- + +## 1. Analysis (Step 1) + +- **Public Inputs**: + - `power`: number (exponent $x$ in $e^x$) + - `n`: number (number of terms in Taylor expansion approximation, $n \ge 0$) +- **Results**: + - Returns approximation of $e^{power}$ summing up to $n$ terms. + - Returns $1$ if $n = 0$. + - Throws `TypeError('Invalid Input')` if `power` or `n` is `NaN` or $n < 0$. +- **Mutation Behavior**: + - Pure function; does not mutate any inputs or external state. +- **External Dependencies**: + - JavaScript built-ins: `isNaN`, exponentiation operator `**`. + +--- + +## 2. Independent Reference & Expected Values (Step 2 & 3) + +- **Independent Oracle**: + Calculated mathematically using the Taylor series definition: + $$\sum_{i=0}^{n-1} \frac{\text{power}^i}{i!} \quad \text{with } 0! = 1$$ +- **Smallest Valid Input**: + - `exponentialFunction(0, 0)` -> `1` (Precondition line 21: $n = 0$ returns 1) +- **Ordinary Input (Representative Result)**: + - `exponentialFunction(2, 2)` -> `3` (Precondition lines 24-27: $2^0/0! + 2^1/1! = 1 + 2 = 3$) +- **Enumerated Test Table (22 Small Valid Inputs)**: + - $(0, 0) \to 1$ + - $(0, 1) \to 1$ + - $(0, 2) \to 1$ + - $(0, 5) \to 1$ + - $(1, 0) \to 1$ + - $(1, 1) \to 1$ + - $(1, 2) \to 2$ + - $(1, 3) \to 2.5$ + - $(1, 4) \to 2.6666666666666665$ + - $(1, 5) \to 2.7083333333333335$ + - $(2, 0) \to 1$ + - $(2, 1) \to 1$ + - $(2, 2) \to 3$ + - $(2, 3) \to 5$ + - $(-1, 0) \to 1$ + - $(-1, 1) \to 1$ + - $(-1, 2) \to 0$ + - $(-1, 3) \to 0.5$ + - $(-1, 4) \to 0.33333333333333337$ + - $(0.5, 0) \to 1$ + - $(0.5, 1) \to 1$ + - $(0.5, 2) \to 1.5$ + - $(0.5, 3) \to 1.625$ + - $(2, 20) \to 7.3890560989301735$ + +--- + +## 3. Test Runner & Verification (Step 4 & 5) + +- Script: `work/R50-T033/check.py` +- Command: `python3 work/R50-T033/check.py` +- Non-zero test count: 27 assertions in focused suite (all passed). +- Repository full suite: 347 test files passed. + +--- + +## 4. Documentation & Style (Step 6) + +- Updated JSDoc in `Maths/ExponentialFunction.js` detailing the representative result $(2, 2) \to 3$. +- Formatted with Prettier and verified via `npm run check-style`. diff --git a/work/R50-T033/check.py b/work/R50-T033/check.py new file mode 100755 index 0000000000..e73e4371ee --- /dev/null +++ b/work/R50-T033/check.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +""" +Focused test runner for R50-T033 (Maths/ExponentialFunction.js). +Executes new checks, propagates failures, runs repo test script in non-watch mode. +""" +import os +import subprocess +import sys + +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) +# Support running from either /project/work/R50-T033 or /project/TheAlgorithms-JavaScript/work/R50-T033 +candidates = [ + os.path.abspath(os.path.join(SCRIPT_DIR, "..", "..")), + os.path.abspath(os.path.join(SCRIPT_DIR, "..", "..", "TheAlgorithms-JavaScript")), + "/Users/aniket/project/TheAlgorithms-JavaScript", +] +REPO_DIR = next((c for c in candidates if os.path.exists(os.path.join(c, "package.json"))), candidates[-1]) + +def run_cmd(cmd, cwd=REPO_DIR): + print(f"[RUN] {cmd} (cwd: {cwd})") + res = subprocess.run(cmd, shell=True, cwd=cwd, capture_output=True, text=True) + if res.stdout: + print(res.stdout) + if res.stderr: + print(res.stderr, file=sys.stderr) + return res + +def main(): + print("=== R50-T033 Test Runner ===") + print(f"Repository root: {REPO_DIR}") + + # 1. Verify dependencies from lockfile + if not os.path.exists(os.path.join(REPO_DIR, "node_modules")): + print("Installing dependencies from lockfile...") + res = run_cmd("npm ci") + if res.returncode != 0: + print("npm ci failed") + sys.exit(res.returncode) + + # 2. Run focused check + print("\n--- Running Focused Checks on ExponentialFunction ---") + focused_cmd = "npx vitest run Maths/test/ExponentialFunction.test.js" + res_focused = run_cmd(focused_cmd) + if res_focused.returncode != 0: + print("Focused tests failed!") + sys.exit(res_focused.returncode) + + # 3. Run repository test script in non-watch mode + print("\n--- Running Full Repository Test Suite (Non-Watch Mode) ---") + repo_cmd = "npm test" + res_repo = run_cmd(repo_cmd) + if res_repo.returncode != 0: + print("Repository tests failed!") + sys.exit(res_repo.returncode) + + print("\n=== All Checks Passed Successfully ===") + +if __name__ == "__main__": + main()