From 9ab28a208b16d1e7b1851757d8940eb717b5f489 Mon Sep 17 00:00:00 2001 From: Zhihua Lai Date: Tue, 7 Oct 2025 13:38:15 +0100 Subject: [PATCH] Fix test setup and add more tests --- src/functions.js | 7 +++++-- tests/Functions.test.js | 17 +++++++++++++++++ tests/test.setup.js | 10 ++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/functions.js b/src/functions.js index 2684e47..0932634 100755 --- a/src/functions.js +++ b/src/functions.js @@ -1,7 +1,7 @@ // functions.js export function primeFactorization(input) { if (input === null || input === undefined || input === "") { - return "No prime factors"; + return "Please enter a number"; } // Accept number | string | bigint @@ -55,10 +55,13 @@ export function primeFactorization(input) { .join(" * "); } +// Generate all primes up to a given limit using the Sieve of Eratosthenes export function generatePrimes(limit) { // Same as before; limit is a small Number (e.g., 1000) const n = Math.floor(Number(limit)); - if (n < 2 || !Number.isFinite(n)) return []; + if (n < 2 || !Number.isFinite(n)) { + return []; + } const sieve = Array(n + 1).fill(true); sieve[0] = sieve[1] = false; for (let i = 2; i * i <= n; i++) { diff --git a/tests/Functions.test.js b/tests/Functions.test.js index 1e6c202..c251e1d 100755 --- a/tests/Functions.test.js +++ b/tests/Functions.test.js @@ -25,6 +25,18 @@ describe("primeFactorization", () => { expect(primeFactorization(0)).toBe("No prime factors"); expect(primeFactorization(-5)).toBe("No prime factors"); }); + + it("should handle BigInt input", () => { + expect(primeFactorization(12345678901234567890n)).toBe( + "2 * 32 * 5 * 101 * 3541 * 3607 * 3803 * 27961", + ); + }); + + it("should handle string input", () => { + expect(primeFactorization("56")).toBe("23 * 7"); + expect(primeFactorization("97")).toBe("97"); + expect(primeFactorization("not a number")).toBe("No prime factors"); + }); }); describe("generatePrimes", () => { @@ -48,4 +60,9 @@ describe("generatePrimes", () => { expect(generatePrimes(0)).toEqual([]); expect(generatePrimes(-10)).toEqual([]); }); + + it("should handle non-integer limits", () => { + expect(generatePrimes(10.7)).toEqual([2, 3, 5, 7]); + expect(generatePrimes(15.2)).toEqual([2, 3, 5, 7, 11, 13]); + }); }); diff --git a/tests/test.setup.js b/tests/test.setup.js index 47c7ab4..fc9908c 100644 --- a/tests/test.setup.js +++ b/tests/test.setup.js @@ -1,2 +1,8 @@ -import { randomUUID } from "node:crypto"; // Node.js built-in crypto -global.crypto = { randomUUID }; // Mock or polyfill necessary crypto functions +import { randomUUID } from "node:crypto"; + +if (!globalThis.crypto?.randomUUID) { + Object.defineProperty(globalThis, "crypto", { + value: { randomUUID }, + configurable: true, + }); +}